From 0cc6f28db98b33d1aa185262690b391b0bf45788 Mon Sep 17 00:00:00 2001 From: scott francis Date: Fri, 14 Aug 2020 18:55:35 -0700 Subject: [PATCH] add delta message handler --- GreengrassAwareConnection.py | 11 +++++++ PentairProtocol.py | 59 +++++++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/GreengrassAwareConnection.py b/GreengrassAwareConnection.py index 3b92376..c372a3e 100644 --- a/GreengrassAwareConnection.py +++ b/GreengrassAwareConnection.py @@ -147,6 +147,15 @@ def publishMessageOnTopic(self, message, topic, qos=0): def isShadowConnected(self): return self.shadowConnected + def memberDeltaHandler(self, payload, responseStatus, token): + print("\nReceived a Delta Message") + + payloadDict = json.loads(payload) + deltaMessage = json.dumps(payloadDict["state"]) + print(deltaMessage + "\n") + + + def connectShadow(self): if not self.isConnected(): self.logger.warn("connect regula client first to get host and port") @@ -168,6 +177,8 @@ def connectShadow(self): # Create a deviceShadow with persistent subscription self.deviceShadowHandler = self.shadowClient.createShadowHandlerWithName(self.thingName, True) + self.deviceShadowHandler.shadowRegisterDeltaCallback(self.memberDeltaHandler) + self.shadowConnected = True diff --git a/PentairProtocol.py b/PentairProtocol.py index 18915b2..563d324 100644 --- a/PentairProtocol.py +++ b/PentairProtocol.py @@ -222,6 +222,63 @@ def __init__(self, body): except Exception as err: pass +# +# A Command +# +class Command: + def __init__(self, dst, cmd, type=0x24, src=0x21): + pass + +# 0x86 - Turn Circuits On and Off +# +# example: +# 24 10 20 86 2 06 01 +# +# will be followed with a ACK from the dest +# +class CircuitChangeCommand(Command): + SPA = 1 + AUX1 = 2 + AUX2 = 3 + AUX3 = 4 + FEATURE1 = 5 + POOL = 6 + FEATURE2 = 7 + FEATURE3 = 8 + FEATURE4 = 9 + HEAT_BOOST = 0x85 + + def __init__(self, ckt, onOff, dst=0x10): + super().__init__(dst, 0x86) + + state = b'\x01' if onOff else b'\x00' + + self.payload = ckt.to_bytes(1, byteorder='big') + state + pass + +# 0x88 - Change heating parameters -- set points, enable +# +# example: +# 24 10 20 88 4 52 64 05 00 +# +class HeatChangeCommand(Command): + # modes + OFF = 0x00 + HEATER = 0x01 + SOLAR = 0x02 + SOLAR_PREF = 0x03 + + def __init__(self, poolSet, spaSet, spaMode, poolMode, dst=0x10): + super().__init__(dst, 0x88) + + mode = (spaMode << 2) | poolMode + + self.payload = poolSet.to_bytes(1, byteorder='big') + \ + spaSet.to_bytes(1, byteorder='big') + \ + mode.to_bytes(1, byteorder='big') + \ + b'\x00' + pass + class PentairProtocol: RECORD_SEPARATOR = b'\xFF\x00\xFF' @@ -230,7 +287,7 @@ class PentairProtocol: def __init__(self): self.payloads = { - 0x00: { #0x01: CommandPayload, + 0x00: { #0x01: CommandPayload, # this is really an ACKnowledgement of the cmd in the payload #0x04: PingPayload, #0x06: PumpStatus, 0x07: PumpPayload },