Skip to content

Commit

Permalink
Fix postback (#2)
Browse files Browse the repository at this point in the history
* Fixes for mqtt posback and signal handling

* Remove unused signalhandler function
  • Loading branch information
bojleros authored Oct 25, 2018
1 parent 122048c commit 2ad00da
Showing 1 changed file with 37 additions and 29 deletions.
66 changes: 37 additions & 29 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
import paho.mqtt.publish as publish
import paho.mqtt.client as mqtt


def msg(text):
print("%s : %s" % (datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"),text))
print("%s : %s" % (datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"), text))


def getenv():
conf = {
Expand Down Expand Up @@ -54,7 +56,7 @@ def getenv():

class Ec133:

def __init__(self, serconf, ecconf , callback=False):
def __init__(self, serconf, ecconf, callback=False):
self.serconf = serconf
self.ecconf = ecconf
self.ser = False
Expand All @@ -64,15 +66,15 @@ def __init__(self, serconf, ecconf , callback=False):
self.brightness = [255, 255, 255]
self.register = [255, 255, 255]
self.lock = threading.Lock()

def __del__(self):
msg("Closing serial device")
if bool(self.rtu):
del self.rtu
if bool(self.ser):
del self.ser

def set_callback(self,callback):
def set_callback(self, callback):
self.callback = callback

def connect(self):
Expand Down Expand Up @@ -102,28 +104,29 @@ def connect(self):
raise e

def set_channel(self, client, userdata, message):

ch = int(userdata['channel'])

payload_str = str(message.payload.decode("utf-8"))

try:
payload = json.loads(payload_str)
payload = json.loads(message.payload.decode("utf-8"))
except Exception as e:
msg("Channel%s : Malformed json message : %s" % (ch,e))
msg("Channel%s : Malformed json message : %s" % (ch, e))
return

if type(payload) is not dict:
msg("Channel%s : mqtt_json format expected , got %s!" % (ch,type(payload)))
msg("Channel%s : mqtt_json format expected , got %s!" % (ch, type(payload)))
return

self.lock.acquire(blocking=True, timeout=-1)

msg("Channel%s: %s" % (ch,payload))
if payload.get("brightness",False):
msg("Channel%s command: %s" % (ch, payload))

if payload.get("brightness", False):
self.brightness[ch] = int(payload['brightness'])
else:
payload['brightness'] = int(self.brightness[ch])

if payload.get('state','ON') == 'ON':
if payload.get('state', 'ON') == 'ON':
self.register[ch] = int(self.brightness[ch])
else:
self.register[ch] = int(0)
Expand All @@ -143,7 +146,8 @@ def set_channel(self, client, userdata, message):
else:
time.sleep(0.02)
if bool(self.callback):
self.callback(ch,payload_str)

self.callback(ch, json.dumps(payload))
self.lock.release()


Expand Down Expand Up @@ -177,33 +181,37 @@ def consume_all(self):
for ch, topic in self.ctopics.items():
self._consume_topic(ch)

def postback(self,ch,payload):
def postback(self, ch, payload):
auth = None
if self.mqconf['username'] != None:
auth = { 'username': self.mqconf['username'],
auth = {'username': self.mqconf['username'],
'password': self.mqconf['password']
}
}

try:
publish.single(self.stopics[str(ch)],
hostname = self.mqconf['address'],
port=self.mqconf['port'],
auth=auth,
payload=payload,
qos=self.mqconf['qos'],
keepalive=15,
retain=True)
hostname=self.mqconf['address'],
port=self.mqconf['port'],
auth=auth,
payload=payload,
qos=self.mqconf['qos'],
keepalive=15,
retain=True)
except Exception as e:
msg("Unable to send channel%s state update : %s" % (ch,e))
msg("Unable to send channel%s state update : %s" % (ch, e))
else:
msg("Channel%s state update sent" % ch)
msg("Channel%s state: %s" % (ch, payload))


def main():
"""
Main routine
"""


# signals are only used to break out of signal.pause()
signal.signal(signal.SIGINT, (lambda signum, frame: None))
signal.signal(signal.SIGTERM, (lambda signum, frame: None))

msg("Start ...")
conf = getenv()

Expand All @@ -212,7 +220,8 @@ def main():
ec.connect()

msg("Consume mqtt topics")
mq = Mqtt(conf['mqtt'], conf['ec133']['command_topics'], conf['ec133']['state_topics'], ec.set_channel)
mq = Mqtt(conf['mqtt'], conf['ec133']['command_topics'],
conf['ec133']['state_topics'], ec.set_channel)
mq.consume_all()
ec.set_callback(mq.postback)

Expand All @@ -225,4 +234,3 @@ def main():

if __name__ == "__main__":
main()

0 comments on commit 2ad00da

Please sign in to comment.