Page 1 of 1

Asynchronous shell command execution: get result back

Posted: Tuesday 05 January 2021 8:25
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.

Re: Asynchronous shell command execution: get result back

Posted: Tuesday 05 January 2021 8:47
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.

Re: Asynchronous shell command execution: get result back

Posted: Tuesday 05 January 2021 9:59
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.

Re: Asynchronous shell command execution: get result back

Posted: Tuesday 05 January 2021 10:31
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.

Re: Asynchronous shell command execution: get result back

Posted: Tuesday 05 January 2021 11:14
by Number8
Ok fine, thank you

Re: Asynchronous shell command execution: get result back

Posted: Tuesday 05 January 2021 15:02
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"

Re: Asynchronous shell command execution: get result back

Posted: Tuesday 05 January 2021 15:59
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
}

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

Posted: Tuesday 05 January 2021 16:32
by Number8
Thanks a lot to help me to find the error. I had hard time to find it.

Re: Asynchronous shell command execution: get result back

Posted: Friday 08 January 2021 11:11
by javalin
Good morning, with this new feature it possible to restart a python script with a shell command, right?

Re: Asynchronous shell command execution: get result back

Posted: Friday 08 January 2021 15:44
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.

Re: Asynchronous shell command execution: get result back

Posted: Monday 11 January 2021 16:33
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

Re: Asynchronous shell command execution: get result back

Posted: Monday 11 January 2021 17:26
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.

Re: Asynchronous shell command execution: get result back

Posted: Monday 11 January 2021 17:59
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

Re: Asynchronous shell command execution: get result back

Posted: Monday 11 January 2021 19:51
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

Re: Asynchronous shell command execution: get result back

Posted: Tuesday 12 January 2021 16:13
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

Re: Asynchronous shell command execution: get result back

Posted: Tuesday 12 January 2021 16:17
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.