Web Socket Connection using OpenHab rules

Hi All,

I am trying to determine the best way to send and receive data via a Socket. I have a commercial Samsung TV which is not supported by the Samsung binding since it uses an MDC protocol. The protocol itself is quite simple and just requires a few bytes to be written to a socket. What I cannot work out is how to implement the following example Python code within OpenHab using a javascript rule, binding, or something else:

        data = [0xAA, 0x11, 0x00, 0x01, 0x01, 0x13]
        print(bytes(data))

        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((TCP_IP, TCP_PORT))
        s.send(bytes(data))
        ack = s.recv(BUFFER_SIZE)
        s.close()

Perhaps someone has already encountered something like this and worked out a solution? Thanks in advance!

Is it a websocket or a tcp socket?
Since you have to implement this yourself I would suggest you implement it in a language you are familiar with, either python or java or something else. And the add enpoints using REST or MQTT to invoke.

For java you can take a look at the OpenHab UniFi Protect binding, it listens to events on a websocket and also sends messages.

I’m not sure this is suited to be implemented in a rule, since you might have to consider reopening the socket if it is disconnected etc.

I agree with @Seaside, this is probably not suitable to be implemented in a rule, regardless of the language. Rules are intended to be invoked upon an event to do something and then exit. They are not intended and in fact problems can occur if rules are long running or never exit.

And here you have a websocket and sockets in general are intended to be opened and left open and connected, listening for new messages and publishing as needed. You’d want a rule to trigger once and never stop.

So you will either want to write a new binding, add support to the existing binding (see Developer Guide | openHAB), or code this externally to openHAB and interface it with OH using OIH’s REST API or MQTT or the like. In all likelihood someone has already implemented this on Github as a stand alone service which you can modify to pub/sub MQTT if it isn’t already supported.

@rlkoshak , @Seaside thank you very much for your comments.

My intention was, exactly as you suggest, to open the socket within a rule, send the command and then close it. This way there would be no need to keep it open or any issues with a long running rule. There is also no expectation here that data will arrive via the socket at a later point in time which would need to be interpreted.

For example, a power switch item is changed in OpenHab, the rule then sends a command over a tcp socket by opening it first and closing it after the command is sent.

I can, naturally, just trigger a python script via something like an Exec binding, but just seems like a very complicated approach for such a (presumably) simple requirement.

In that case, if you think that’s going to be complicated you’re really not going to like what will be involved if you want to do this from a rule. You’ve mentioned JavaScript but not which version. If the JS that comes with OH (ECMAScript 5.1) you are probably out of luck doing this using only JS in your rule. You’ll have to implement it by bringing in the core Java library classes and implement your socket connection stuff using those. Java Socket Programming (Java Networking Tutorial) - javatpoint

If you are using the JS Scripting add-on, I think that most of the core Node.js libraries are available to you. See Net | Node.js v18.9.0 Documentation for how to connect to sockets using that. If my assumption is incorrect, you’ll have to use Java here too.

Since you already have working Python code, I guarantee it will be less work to use Exec than it will be to try to implement and test it in a rule. But hey, that’s how we learn stuff.

Note, are you aware of GitHub - smarthomej/addons: SmartHome/J addons for openHAB which has a TCP/UDP addon. Maybe you don’t need a rule for this.

@rlkoshak thank you! Let me have a look through all of the links over the weekend to see what I can do. I did in the meantime find a Python library on GitHub for the MDC protocol, so I suspect I will follow your suggestion and simply call it via an Exec binding :slight_smile:

This may give you a head start