HCDevice: return a parsed list of values
This commit is contained in:
62
HCDevice.py
62
HCDevice.py
@@ -58,6 +58,7 @@ class HCDevice:
|
|||||||
self.tx_msg_id = None
|
self.tx_msg_id = None
|
||||||
self.device_name = "hcpy"
|
self.device_name = "hcpy"
|
||||||
self.device_id = "0badcafe"
|
self.device_id = "0badcafe"
|
||||||
|
self.debug = False
|
||||||
|
|
||||||
def load_description(self, device_type):
|
def load_description(self, device_type):
|
||||||
json_file = "xml/" + device_type + ".json"
|
json_file = "xml/" + device_type + ".json"
|
||||||
@@ -82,15 +83,7 @@ class HCDevice:
|
|||||||
name = uid
|
name = uid
|
||||||
status = None
|
status = None
|
||||||
|
|
||||||
if uid in self.machine["status"]:
|
if uid in self.machine["features"]:
|
||||||
status = self.machine["status"][uid]
|
|
||||||
elif uid in self.machine["options"]:
|
|
||||||
status = self.machine["options"][uid]
|
|
||||||
elif uid in self.machine["commands"]:
|
|
||||||
status = self.machine["commands"][uid]
|
|
||||||
elif uid in self.machine["events"]:
|
|
||||||
status = self.machine["events"][uid]
|
|
||||||
elif uid in self.machine["features"]:
|
|
||||||
status = self.machine["features"][uid]
|
status = self.machine["features"][uid]
|
||||||
|
|
||||||
if status:
|
if status:
|
||||||
@@ -108,8 +101,14 @@ class HCDevice:
|
|||||||
def recv(self):
|
def recv(self):
|
||||||
try:
|
try:
|
||||||
buf = self.ws.recv()
|
buf = self.ws.recv()
|
||||||
self.handle_message(buf)
|
if buf is None:
|
||||||
return buf
|
return None
|
||||||
|
except Exception as e:
|
||||||
|
print("receive error", e, traceback.format_exc())
|
||||||
|
return None
|
||||||
|
|
||||||
|
try:
|
||||||
|
return self.handle_message(buf)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("error handling msg", e, buf, traceback.format_exc())
|
print("error handling msg", e, buf, traceback.format_exc())
|
||||||
return None
|
return None
|
||||||
@@ -143,14 +142,22 @@ class HCDevice:
|
|||||||
|
|
||||||
def handle_message(self, buf):
|
def handle_message(self, buf):
|
||||||
msg = json.loads(buf)
|
msg = json.loads(buf)
|
||||||
|
if self.debug:
|
||||||
print(now(), "RX:", msg)
|
print(now(), "RX:", msg)
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
|
|
||||||
|
|
||||||
resource = msg["resource"]
|
resource = msg["resource"]
|
||||||
action = msg["action"]
|
action = msg["action"]
|
||||||
|
|
||||||
|
values = {}
|
||||||
|
|
||||||
if "code" in msg:
|
if "code" in msg:
|
||||||
print(now(), "ERROR", msg["code"])
|
#print(now(), "ERROR", msg["code"])
|
||||||
|
values = {
|
||||||
|
"error": msg["code"],
|
||||||
|
"resource": msg.get("resource", ''),
|
||||||
|
}
|
||||||
elif action == "POST":
|
elif action == "POST":
|
||||||
if resource == "/ei/initialValues":
|
if resource == "/ei/initialValues":
|
||||||
# this is the first message they send to us and
|
# this is the first message they send to us and
|
||||||
@@ -183,14 +190,15 @@ class HCDevice:
|
|||||||
elif action == "RESPONSE" or action == "NOTIFY":
|
elif action == "RESPONSE" or action == "NOTIFY":
|
||||||
if resource == "/iz/info" or resource == "/ci/info":
|
if resource == "/iz/info" or resource == "/ci/info":
|
||||||
# see if we have a device file for this model
|
# see if we have a device file for this model
|
||||||
if "data" in msg:
|
if not "data" in msg:
|
||||||
device = msg["data"][0]["vib"]
|
return values
|
||||||
self.load_description(device)
|
values = msg["data"][0]
|
||||||
|
self.load_description(values["vib"])
|
||||||
|
|
||||||
elif resource == "/ro/allMandatoryValues" \
|
elif resource == "/ro/allMandatoryValues" \
|
||||||
or resource == "/ro/values":
|
or resource == "/ro/values":
|
||||||
values = self.parse_values(msg["data"])
|
values = self.parse_values(msg["data"])
|
||||||
print(now(), values)
|
#print(now(), values)
|
||||||
elif resource == "/ci/registeredDevices":
|
elif resource == "/ci/registeredDevices":
|
||||||
# we don't care
|
# we don't care
|
||||||
pass
|
pass
|
||||||
@@ -201,7 +209,7 @@ class HCDevice:
|
|||||||
self.services[service["service"]] = {
|
self.services[service["service"]] = {
|
||||||
"version": service["version"],
|
"version": service["version"],
|
||||||
}
|
}
|
||||||
print(now(), "services", self.services)
|
#print(now(), "services", self.services)
|
||||||
|
|
||||||
# we should figure out which ones to query now
|
# we should figure out which ones to query now
|
||||||
# if "iz" in self.services:
|
# if "iz" in self.services:
|
||||||
@@ -217,21 +225,5 @@ class HCDevice:
|
|||||||
#print(now(), "Unknown reponse", resource)
|
#print(now(), "Unknown reponse", resource)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# we should wait till we know this worked...
|
# return whatever we've parsed out of it
|
||||||
#self.get("/ei/deviceReady", version=2, action="NOTIFY")
|
return values
|
||||||
# if machine and "data" in msg:
|
|
||||||
# for el in msg["data"]:
|
|
||||||
# if "uid" not in el:
|
|
||||||
# continue
|
|
||||||
# uid = str(el["uid"])
|
|
||||||
# if not(uid in machine["status"]):
|
|
||||||
# continue
|
|
||||||
#
|
|
||||||
# status = machine["status"][uid]
|
|
||||||
# value = str(el["value"])
|
|
||||||
#
|
|
||||||
# if "values" in status and value in status["values"]:
|
|
||||||
# value = status["values"][value]
|
|
||||||
#
|
|
||||||
# print(status["name"] + "=" + value)
|
|
||||||
|
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ class HCSocket:
|
|||||||
def __init__(self, host, psk64, iv64=None):
|
def __init__(self, host, psk64, iv64=None):
|
||||||
self.host = host
|
self.host = host
|
||||||
self.psk = base64url(psk64 + '===')
|
self.psk = base64url(psk64 + '===')
|
||||||
|
self.debug = False
|
||||||
|
|
||||||
if iv64:
|
if iv64:
|
||||||
# an HTTP self-encrypted socket
|
# an HTTP self-encrypted socket
|
||||||
@@ -146,6 +147,7 @@ class HCSocket:
|
|||||||
buf = json.dumps(msg, separators=(',', ':') )
|
buf = json.dumps(msg, separators=(',', ':') )
|
||||||
# swap " for '
|
# swap " for '
|
||||||
buf = re.sub("'", '"', buf)
|
buf = re.sub("'", '"', buf)
|
||||||
|
if self.debug:
|
||||||
print(now(), "TX:", buf)
|
print(now(), "TX:", buf)
|
||||||
if self.http:
|
if self.http:
|
||||||
self.ws.send_binary(self.encrypt(buf))
|
self.ws.send_binary(self.encrypt(buf))
|
||||||
|
|||||||
20
hcpy
20
hcpy
@@ -9,7 +9,7 @@ import io
|
|||||||
from HCSocket import HCSocket, now
|
from HCSocket import HCSocket, now
|
||||||
from HCDevice import HCDevice
|
from HCDevice import HCDevice
|
||||||
|
|
||||||
if True:
|
if False:
|
||||||
# clothes dryer
|
# clothes dryer
|
||||||
# RX:{'service': 'ro', 'version': 1}
|
# RX:{'service': 'ro', 'version': 1}
|
||||||
# {'service': 'ei', 'version': 2}
|
# {'service': 'ei', 'version': 2}
|
||||||
@@ -29,13 +29,19 @@ else:
|
|||||||
psk64 = "Dsgf2MZJ-ti85_00M1QT1HP5LgH82CaASYlMGdcuzcs="
|
psk64 = "Dsgf2MZJ-ti85_00M1QT1HP5LgH82CaASYlMGdcuzcs="
|
||||||
iv64 = None # no iv == https
|
iv64 = None # no iv == https
|
||||||
|
|
||||||
ws = HCSocket(host, psk64, iv64)
|
|
||||||
dev = HCDevice(ws)
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
buf = dev.recv()
|
ws = HCSocket(host, psk64, iv64)
|
||||||
|
dev = HCDevice(ws)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
msg = dev.recv()
|
||||||
|
if msg is None:
|
||||||
|
break
|
||||||
|
if len(msg) > 0:
|
||||||
|
print(now(), msg)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("error handling msg", e, buf)
|
print("ERROR", host, e, file=sys.stderr)
|
||||||
|
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
ws.reconnect()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user