fcwilt
April 15, 2023, 10:15pm
1
I have managed to a code a simple driver that establishes a Telnet connection and sends and receives.
I found the Telnet documentation rather sparse but it seemed to be correct.
What was surprising was there was no command to send data.
Eventually I located a device in the HPM which used a Telnet connection and I found this bit of code which worked to send data:
def sendMsg(String msg) {
def hubCmd = sendHubCommand(new hubitat.device.HubAction("${msg}", Hubitat.device.Protocol.TELNET))
pauseExecution(500)
return hubCmd
}
But I have not found any documentation to explain the why and what of it all.
I know what "pauseExecution" does but I don't know why it is there.
And I basically know what the return does but, again, why is it there.
Thanks, Frederick
Take everything below with a grain of salt.
The actual telnetInterface is used to open the telnet connection. You then need a function (like you posted) to actually send data across the connection. Your return is to bring back the telnet response that will get parsed by the required parse function. The pause is to allow for the telnet connection to receive that response before moving on.
All of this goes in a driver for a device that will connect to telnet. You open the connection this way:
telnetConnect(String ip, int port, String username, String password)
or
telnetConnect(Map options, String ip, int port, String username, String password)
Only option is "termChars" which changes what the termination character is.
Then, to send/receive messages:
def sendMsg(String msg) {
return new hubitat.device.HubAction(msg, hubitat.device.Protocol.TELNET)
}
and
def parse(String …
fcwilt
April 16, 2023, 12:06am
3
Thanks much.
At least I know what I found in the other users code is basically correct.
I still wonder why there isn't a simple built-in procedure for sending, something along the lines of "telnetSend(String msg)".
Not terribly important, it's just my insatiable curiosity working overtime.
Thanks again, Frederick