Reporting switch change from status file in OpenSwitchThread working.

This commit is contained in:
Ondrej Mikle 2018-04-22 22:20:09 +02:00
parent 5a565caeba
commit 4dc09f0e16
2 changed files with 24 additions and 20 deletions

View file

@ -48,8 +48,19 @@ reconnect_delay = 300
# There is no point in enabling this if you disabled IRC
# Still has one bug - python-irc won't retrieve properly topic
# enabled - True/False
# status_file - file in sysfs that contains 1 or 0 defining the state of button
# open_value - which value in status_file respresents the switch being in "OPEN" position, 1 character
# status_file - file that contains value of the button, may end with newline (you probably want something in /sys fs)
# open_value - which value in status_file respresents the switch being in "OPEN" position
#
# Note: for use with Raspberry Pi, to read from GPIO in sysfs, you need to enable the PIN in input mode before starting
# this daemon, e.g. with GPIO 11 as an example (note that there are 2 numbering PIN schemes):
# #!/bin/bash
# export PIN=11
#
# if [ '!' -d /sys/class/gpio/gpio$PIN ]; then
# echo $PIN > /sys/class/gpio/export
# echo in > /sys/class/gpio/gpio$PIN/direction
# fi
enabled = False
status_file = /sys/class/gpio/gpio11/value
open_value = 1

View file

@ -9,7 +9,6 @@ import threading
import irc.client
import ssl
import Queue
import re
from binascii import hexlify
from functools import partial
@ -138,9 +137,6 @@ class NFCScanner(object):
sys.exit(2)
except Exception:
logging.exception("Exception in main unlock thread")
logging.info("Request topic")
channelPrefixMap[self.ircThread.channels[0]] = "OPEN |"
self.ircThread.getTopic(self.ircThread.channels[0])
def sendIrcMessage(self, msg):
"""
@ -349,9 +345,8 @@ class OpenSwitchThread(threading.Thread):
lastStatus = None #Some random value so that first time it will be registered as change
while True:
try:
switchFile = open(self.statusFile)
status = switchFile.read(1)
switchFile.close()
with open(self.statusFile) as switchFile:
status = switchFile.read().rstrip()
if status != lastStatus:
logging.info("Open switch status changed, new status: %s", status)
lastStatus = status
@ -361,18 +356,16 @@ class OpenSwitchThread(threading.Thread):
strStatus = "CLOSED |"
if self.ircThread.connected:
with self.ircThread.threadLock:
for channel in self.ircThread.channels:
#TODO: getTopic always returns None, the problem is in implementenation
topic = self.ircThread.getTopic(channel)
if not topic or not re.match(r"^\s*(OPEN|CLOSED) \|", topic):
newTopic = strStatus
else:
newTopic = re.sub(r"^\s*(OPEN|CLOSED) \|", strStatus, topic)
self.ircThread.setTopic(channel, newTopic)
for channel in self.ircThread.channels:
logging.info("Request topic for channel %s with intention to change it, prefix %s",
channel, strStatus)
channelPrefixMap[channel] = strStatus
self.ircThread.getTopic(channel)
except (IOError, OSError):
logging.exception("Could not read switch status")
pass #silently ignore non-existent file and other errors, otherwise it'd spam log
logging.debug("Could not read switch status file %s", self.statusFile)
e = threading.Event()
e.wait(timeout=5)
pass #just log the error, but don't spam IRC
except Exception:
logging.exception("Exception in open switch thread")
e = threading.Event()