We are trying to integrate Hubitat with a TCP Device.
We have the API for the Device, and it's pretty straight forward.
Expects Hexadecimal commands. Basic Example:
"23 31 11 A9" = Turn on
"23 39 55 E5" = Turn off
We are using the "interfaces.rawSocket.connect(ip, port)" method in HE.
The method only accepts strings, so I believe that the message is getting corrupted when trying to send it as an Hex.
If I want to send the following 4 bytes to a device over TCP:
0x23 0x31 0x11 0xA9
I construct a byte[] from the hex string:
byte[] bytes = hexStringToByteArray("233111A9")
Then I try to convert it into a String that preserves the binary values:
String raw = bytes.collect { (char)(it & 0xFF) }.join()
interfaces.rawSocket.sendMessage(raw)
However, this causes the byte 0xA9 to be UTF-8 encoded into two bytes:
0xC2 0xA9
So the device receives:
0x23 0x31 0x11 0xC2 0xA9 ← incorrect
We tried many different approaches to convert the string before sending, but nothing worked.
Looks like the best thing would add support for sending byte[] directly over rawSocket
interfaces.rawSocket.sendBytes(byte[] bytes)
Or have allow sendMessage(String message, boolean isBase64)
interfaces.rawSocket.sendMessage(base64EncodedString, true)
Where the true flag tells Hubitat to Base64-decode before transmission.
All approaches involving non-ASCII bytes are UTF-8 encoded and corrupted on transmission.
Any ideas? @bobbyD
