Python plugin: set a timer to get a callback after N ms

Python and python framework

Moderator: leecollings

Post Reply
User avatar
psubiaco
Posts: 245
Joined: Monday 20 August 2018 9:38
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Italy
Contact:

Python plugin: set a timer to get a callback after N ms

Post by psubiaco »

Hi,
I'm developing Creasol DomBus plugin, in python, and I have a question expecially for @Dnpwwo and who is involved with Domoticz python framework.

Let me explain...
Dombus plugin manages some modules (inputs, outputs, sensors) connected by RS485 serial bus.
Now the plugin send ACK as soon as it receives a command from the bus, and this is ok.

Suppose to have a pushbutton connected to a input device, that must toggle 2 lights connected to 2 relay outputs

1.PUSHBUTTON is pushed => INPUT module send command to Dombus plugin
2.Dombus plugin informs Domoticz that the input device has changed, and immediately transmits back an ACK to the INPUT module
3.Domoticz or a script manages that input, and after a certain time (usually 50-80ms with a Raspberry) sends a onCommand callback to Dombus plugin to activate OUTPUT1 => Dombus transmits a frame to activate OUTPUT1 module by RS485
4.and immediately after, another onCommand callback to notify that OUTPU2 has been activated => Dombus transmits anoter frame to activate OUTPUT2

more schematically we have:
RX command from pushbutton input
TX ACK to pushbutton input module
Domoticz informs DomBus that OUTPUT1 has changed =>TX CMD to OUTPUT1
Domoticz informs DomBus that also OUTPUT2 has changed => TX CMD to both OUTPUT1 and OUTPUT2

The result now is that Dombus plugin transmits a frame within ACK to the input module, and 2 frames to the output module.
This is not a real problem, I'm using 115200bps and frames are only 1-2 ms long, but as it's possible to transmits more commands/ack in the same frame, limiting the overhead, it will be better if I can send a single frame with both ACK and CMDs together.

To get a single transmission frame with both ACK and CMD, it will be perfect to delay transmission, for example, 100ms: is there a way to program a timer to get a callback (HeartBeat, for example) after a specified number of ms ?? If so, I can put the ACK/CMD into a TX queue, program a delay, and if in the meanwhile other commands are received from Domoticz, they will be added to the TX queue, and after that delay they will be transmitted together in a single frame.

I've tried to explain the reason of my request. If it's not clear, I can explain in a different way.
Thanks a lot.
Paolo
Paolo
--
I use DomBus modules to charge EV car, get a full alarm system, control heat pump, fire alarm detection, lights and much more. Video
Facebook page - Youtube channel
pipiche
Posts: 2035
Joined: Monday 02 April 2018 20:33
Target OS: Linux
Domoticz version:
Contact:

Re: Python plugin: set a timer to get a callback after N ms

Post by pipiche »

You can probably do the call back yourself by using a multithreaded env.
When sending your message you wan wait for as ms you want and send the ack


Envoyé de mon iPhone en utilisant Tapatalk
User avatar
psubiaco
Posts: 245
Joined: Monday 20 August 2018 9:38
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Italy
Contact:

Re: Python plugin: set a timer to get a callback after N ms

Post by psubiaco »

pipiche wrote: Thursday 25 March 2021 8:17 You can probably do the call back yourself by using a multithreaded env.
When sending your message you wan wait for as ms you want and send the ack
Hi @pipiche,
that's nice. If I start a new thread for my plugin, will it works (communicate with bus modules) even while Domoticz is doing backup, updating plugins by PP-Manager, or doing other stuff? Actually, I've seen long latency (10s or more) when Domoticz is doing backup (everything is freezed), and long latency (few seconds) when PP-Manager is checking for updates.
Thanks a lot.
Paolo
Paolo
--
I use DomBus modules to charge EV car, get a full alarm system, control heat pump, fire alarm detection, lights and much more. Video
Facebook page - Youtube channel
pipiche
Posts: 2035
Joined: Monday 02 April 2018 20:33
Target OS: Linux
Domoticz version:
Contact:

Re: Python plugin: set a timer to get a callback after N ms

Post by pipiche »

psubiaco wrote: Thursday 25 March 2021 16:01
pipiche wrote: Thursday 25 March 2021 8:17 You can probably do the call back yourself by using a multithreaded env.
When sending your message you wan wait for as ms you want and send the ack
Hi @pipiche,
that's nice. If I start a new thread for my plugin, will it works (communicate with bus modules) even while Domoticz is doing backup, updating plugins by PP-Manager, or doing other stuff? Actually, I've seen long latency (10s or more) when Domoticz is doing backup (everything is freezed), and long latency (few seconds) when PP-Manager is checking for updates.
Thanks a lot.
Paolo
I'm coming from the same latence issue with the plugin I maintain when there is a backup starting.
So I fully decouple the serial or tcp communication and all is handle via several thread. The only issue is, to use a queing system when sending update to Domoticz, so if there is a lock of the Domoticz DB, then your reader/writer are not stuck.
User avatar
psubiaco
Posts: 245
Joined: Monday 20 August 2018 9:38
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Italy
Contact:

Re: Python plugin: set a timer to get a callback after N ms

Post by psubiaco »

Wow, I'm really happy to know that with multithread, I/O can be managed without any issue and latency. Of course, if Domoticz or db is busy, command exchange with Domoticz will be delayed.
Thanks a lot for your nice info.
Paolo
Paolo
--
I use DomBus modules to charge EV car, get a full alarm system, control heat pump, fire alarm detection, lights and much more. Video
Facebook page - Youtube channel
Post Reply