Asynchronous shell command execution: get result back  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

Asynchronous shell command execution: get result back

Post by Number8 »

Hello,
Thank you for the very nice addition of domoticz.executeShellCommand to dzVents. In case one is using Bash or Python as a script langage for instance, I'm wondering how to generate the various data for the response object (data, error, statusCode, callback, etc.). There is an example that shows how to call the script, some generic examples to show how to send the response back would be a very nice addition to the documentation.
There is a typo error in the documentation
callback: String. Optional. A custom string that will be used by dzVents to find a the callback handler script.
Debian buster on NUC and three RPi with buster.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Asynchronous shell command execution: get result back

Post by waaren »

Number8 wrote: Tuesday 05 January 2021 8:25 I'm wondering how to generate the various data for the response object (data, error, statusCode, callback, etc.). There is an example that shows how to call the script, some generic examples to show how to send the response back would be a very nice addition to the documentation.
Thx for your reaction and pointing to the typo.
There are 3 generic examples in the wiki how to use the attributes response object (which is automatically generated and send back) so I have a hard time to understand what is missing. Would be nice if you could describe a real life case that is not covered by the wiki now so that it can be added.

I have corrected the typo in the wiki.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

Re: Asynchronous shell command execution: get result back

Post by Number8 »

Oh maybe I was not very clear in my question, sorry for that. I'm wondering what is the generic code in a bash/python script that will generate the response object that dzVents is expecting in order to be processed in the callback. In other words, this is not really a dzVents question, but more a Bash / Python question related to this new function.
Debian buster on NUC and three RPi with buster.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Asynchronous shell command execution: get result back

Post by waaren »

Number8 wrote: Tuesday 05 January 2021 9:59 I'm wondering what is the generic code in a bash/python script that will generate the response object that dzVents is expecting in order to be processed in the callback.
There is no generic code required to generate a response object. Domoticz will execute the command and pick up the returncode and the output that would normally go to stdout.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

Re: Asynchronous shell command execution: get result back

Post by Number8 »

Ok fine, thank you
Debian buster on NUC and three RPi with buster.
Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

Re: Asynchronous shell command execution: get result back

Post by Number8 »

I'm having problem getting the callback trigger actually triggered. I'm working on a non production RaspPI, brain new Domoticz install.
The bash code is executed (ie switch light turns on) but there is no callback. Local networks is defined to 127.0.0.1 as per documentation
Thanks for your help

dzVents code

Code: Select all

return {
	active = true,
	on = { devices = { 7 } },
	shellCommandResponses = { "myTrigger" },
	logging = {
		level = domoticz.LOG_INFO,
		marker = "test script"
	},
	execute = function(dz, item)
		if item.idx == 7 then
			local cde = "/home/pi/domoticz/scripts/bash/test.sh"
			dz.executeShellCommand({
	      			command = cde,
	      			callback = "myTrigger",
				timeout = 2,
	  		})

		elseif item.isShellCommandResponse then
			dz.log ("!!: " .. item.statusCode, dz.LOG_INFO)
     		end	
	end
}
bash script

Code: Select all

[#!/bin/bash
#echo 1
curl --silent "192.168.21.110:8080/json.htm?type=command&param=switchlight&idx=8&switchcmd=On"
Debian buster on NUC and three RPi with buster.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Asynchronous shell command execution: get result back

Post by waaren »

Number8 wrote: Tuesday 05 January 2021 15:02 I'm having problem getting the callback trigger actually triggered. I'm working on a non production RaspPI, brain new Domoticz install.
The bash code is executed (ie switch light turns on) but there is no callback. Local networks is defined to 127.0.0.1 as per documentation
Thanks for your help
You misplaced a closing curly bracket and I changed the check for a device to ensure the right flow through your code.

Code: Select all

return {
	active = true,
	on = 
	{ 
	    devices = 
	    { 
	        7, 
	    },
	 
	    shellCommandResponses = 
	    { 
	        "myTrigger" 
	    },
	},
	
	logging = 
	{
		level = domoticz.LOG_INFO,
		marker = "test script"
	},
	
	execute = function(dz, item)
		if item.isDevice then
			local cde = "/home/pi/domoticz/scripts/bash/test.sh"
			dz.executeShellCommand({
	      			command = cde,
	      			callback = "myTrigger",
				timeout = 2,
	  		})

		elseif item.isShellCommandResponse then
			dz.log ("!!: " .. item.statusCode, dz.LOG_INFO)
     	       end	
	end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

Re: Asynchronous shell command execution: get result back  [Solved]

Post by Number8 »

Thanks a lot to help me to find the error. I had hard time to find it.
Debian buster on NUC and three RPi with buster.
javalin
Posts: 71
Joined: Tuesday 30 April 2019 16:06
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: Portugal
Contact:

Re: Asynchronous shell command execution: get result back

Post by javalin »

Good morning, with this new feature it possible to restart a python script with a shell command, right?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Asynchronous shell command execution: get result back

Post by waaren »

javalin wrote: Friday 08 January 2021 11:11 Good morning, with this new feature it possible to restart a python script with a shell command, right?
Yes. Same as with os.execute() or io.popen() with the advantage of an async callback So not blocking the event system.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
PSUtec
Posts: 9
Joined: Sunday 11 November 2018 12:25
Target OS: Linux
Domoticz version: 12896
Location: Ferrières (BE)
Contact:

Re: Asynchronous shell command execution: get result back

Post by PSUtec »

Hi,

I suspect a typo in the example of the script that issues the request and handles the response.
...
domoticz.executeShellCommand({
command = '...',
callback = 'trigger'
timeout = 10,
})
---
A comma is missing after 'trigger'.
It is not very serious, but confusing when you copy the code and try to proceed, especially for a newbee.

Jacques
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Asynchronous shell command execution: get result back

Post by waaren »

PSUtec wrote: Monday 11 January 2021 16:33 A comma is missing after 'trigger'.
It is not very serious, but confusing when you copy the code and try to proceed, especially for a newbee.
Thx for reporting. Will add the comma and yes I can see it could cause confusion.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
PSUtec
Posts: 9
Joined: Sunday 11 November 2018 12:25
Target OS: Linux
Domoticz version: 12896
Location: Ferrières (BE)
Contact:

Re: Asynchronous shell command execution: get result back

Post by PSUtec »

To prove that I'm a newbee, I've a problem with this very simple script:

Code: Select all

return {
    on = {
        devices = {'VS test Lua'}
        },
    execute = function(dz)
			dz.executeShellCommand({
	      		    command ="ls -l",
	      		    callback = "trigger",
			    timeout = 2,
	  		})
    end
}
In the log, I've the following message when I activate the switch:

Code: Select all

2021-01-11 17:56:55.593 Error: dzVents: Error: (3.0.1) An error occurred when calling event handler Script #1
2021-01-11 17:56:55.593 Error: dzVents: Error: (3.0.1) ...domoticz/scripts/dzVents/generated_scripts/Script #1.lua:6: attempt to call a nil value (field 'executeShellCommand')
Where is my stupid error?

Thanks in advance
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Asynchronous shell command execution: get result back

Post by waaren »

PSUtec wrote: Monday 11 January 2021 17:59 To prove that I'm a newbee, I've a problem with this very simple script:
Where is my stupid error?
Not stupid for a newbie but you overlooked the fact that the executeShellCommand() function is available in dzVents >= 3.1.0 (domoticz 2020.2 build 12771)

btw.
In most cases It helps to state your OS and domoticz version when reporting a perceived bug or even when asking a question like 'is it possible to ...'
I could now tell that you are not at the required dzVents version for the executeShellCommand because of the error message but that is not always true.

btw2.
You can check the dzVents history on the wiki to see which commands, attributes, etc are introduced when and the combination of dzVents versions and domoticz build numbers can be found here
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
PSUtec
Posts: 9
Joined: Sunday 11 November 2018 12:25
Target OS: Linux
Domoticz version: 12896
Location: Ferrières (BE)
Contact:

Re: Asynchronous shell command execution: get result back

Post by PSUtec »

Thanks for the lesson!

However, how to update dzVents; I don't find the information neither in the wiki, nor on the forum.
When I go to github, I see the files, but not the way to transfer them automatically.
It could be good to include this point in the wiki. I suspect that many people have abandoned dzVents, facing this kind of problem.

For your information, I've already developed several successful dzVents scripts and I really appreciate the usefulness of the tool.
When I say I'm a newbee, it is mainly in access to external information (MQTT, JSON and family).

Regards,

Jacques
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Asynchronous shell command execution: get result back

Post by waaren »

PSUtec wrote: Tuesday 12 January 2021 16:13 How to update dzVents; I don't find the information neither in the wiki, nor on the forum.
dzVents is full integrated in domoticz and cannot upgraded separately. So if you need the latest dzVents you will have to update to the latest domoticz Beta.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 0 guests