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 # There is no point in enabling this if you disabled IRC
# Still has one bug - python-irc won't retrieve properly topic # Still has one bug - python-irc won't retrieve properly topic
# enabled - True/False # enabled - True/False
# status_file - file in sysfs that contains 1 or 0 defining the state of button # 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, 1 character # 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 enabled = False
status_file = /sys/class/gpio/gpio11/value status_file = /sys/class/gpio/gpio11/value
open_value = 1 open_value = 1

View file

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