[SOLVED] Thread safety in a Thing handler

Hello! I want to develop a binding and currently i am studying how existing bindings are made. I took Daikin as an example.
So i see that the whole thing revolves around BaseThingHandler class. It has handleCommand method to be called by OH and updateStatus method to post changed values. Pretty simple.
The question is: Can i call updateStatus from within different thread? My design has a separate thread, reading data from a socket, and the device sends out states itself, no need for polling.

1 Like

Take a look at DSMR and NibeHeatPump bindings. There’s good examples howto handle updates from another threads using event listeners.

1 Like

Thank you for the adivice!

2 Likes

Have had a look at Nibe code, apparently the receiver thread simply calls listener.msgReceived() on every listener object, then the listener calls updateStatus(). There no async message passing mechanism, just calls. So the answer is: yes, it is thread-safe

Got another question. Initialize and dispose methods need to be fast, but they may schedule a background task to do blocking things via scheduler, like host resolution and connect. But what if dispose() is called before my background task is complete? How do i cancel a scheduler, which is perhaps in the middle? I can schedule another cleanup task, but are they guaranteed to be executed in order?

Put your tasks in Future<?> or ScheduledFuture<?>. They can be cancelled.