The 6 outlet strip returns a JSON payload that is too big for a single packet. The existing plugin code makes a TCP connection that closes too soon, resulting in incomplete JSON. I can overcome that and just strip it as a string and get the DeviceID I need (since that is at the top of the file) but getting status updates on all six. plugs requires working JSON.
I've overcome this with some UDP code (that I found on GitHub for another non-Indigo solution to this) that works 99.5% of the time; I have three physical devices representing 14 Indigo devices (2 strips and a dual outlet) and the other .5% it throws a timeout error. Code is below. Any suggestions on this?
2nd question:
Thinking of actually re-doing this to have a "parent" device and child devices for the power strips. Where the user would input and create the powerstrip itself as the parent, and the 6 child devices would be automatically created. And for status updates (outlet on/off and energy usage) there'd just be one query issued by the parent device and it would update the child devices within Indigo. (The JSON file for on/off status shows every device).
Any good examples on how to have a parent device create the child devices and update state on them? On/off would still be sent from the child devices.
And last question, this is a power strip, but in my case is mostly used for ceiling and counter lights. Is it possible to reclassify a device via a checkbox from a SimpleRelay to a LightSwitch? Is it as simple as setting supportsAllLightsOnOff to True? And perhaps changing the icon?
Here is the code for sending via TCP from the existing plugin; this works for all but getting info on the 6 outlet powerstripas:
- Code: Select all
if debug:
print ("send cmd=%s" % (cmd, ))
try:
sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock_tcp.connect((self.ip, self.port))
sock_tcp.send(encrypt(cmd))
data = sock_tcp.recv(2048)
sock_tcp.close()
# don't know what the first 3 decrypted bytes are. skip them
# Byte 4 is a '?' but for valid json replace it with '{'
result = decrypt(data)
return '{' + result[5:]
except socket.error:
quit("ERROR: Cound not connect to host " + self.ip + ":" + str(self.port))
And here is what I call for UDP. Timeout is set to 2.
- Code: Select all
if debug:
print ("send cmd=%s" % (cmd, ))
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client_socket.settimeout(timeout)
addr = (self.ip, self.port)
client_socket.sendto(_encrypt_udp(cmd, prepend_length=False), addr)
data, server = client_socket.recvfrom(1024)
result = _decrypt_udp(data)
return result