Running Windows.bat file from Vera initiated http call

Wondering if any kind devs might be able to help shed some light on an error i am getting when attempting to run a windows .bat file from a http call using an open source service named SleepOnLan. The service is from here:

GitHub

SR-G/sleep-on-lan

Multi-platform process allowing to sleep on LAN a linux or windows computer, through wake-on-lan (reversed) magic packets or through HTTP REST requests. - SR-G/sleep-on-lan

and i have it running on my PC and can successfully sleep my pc from calling http://i.paddress:8009/sleep
I have been attempting to alter this service to run a local .bat file from http://i.paddress:8009/dson
call and by adding the following to to a sol.json file in the same directory as the sol.exe
{
“Listeners” : [“UDP:9”, “HTTP:8009” ],
“LogLevel” : “INFO”,
“BroadcastIP” : “255.255.255.255”,
“Commands” : [
{
“Operation” : “dson”,
“Command” : “C:\Scripts\DreamScreenCommander-master\DsOn.bat”
]}
but when the sol.exe tries to read the sol.json file i get the following error:
INFO: 2019/10/12 17:20:16 configuration.go:59: Configuration file found under [C:\Tools\SleepOnLan\sol.json], now reading content
ERROR: 2019/10/12 17:20:16 configuration.go:64: error while loading configuration : invalid character ‘S’ in string escape code

I probably have some incorrect syntax or character…
Appreciate any assistance

The error is exactly what it says… an invalid escape string, due to the use of a reverse solidus (backslash.)

Replace ALL your occurrences of \ with \\ in your Lua command string and that should fix it.

i have done as you said and now i have the following in the sol.json file

{
  "Listeners" : ["UDP:9", "HTTP:8009" ],
  "LogLevel" : "INFO",
  "BroadcastIP" : "255.255.255.255",
  "Commands" : [
   {
		"Operation" : "dson",
        "Command" : "C:\\Scripts\\DreamScreenCommander-master\\DsOn.bat"
		]}

and i get the following error

INFO: 2019/10/12 20:22:07 configuration.go:59: Configuration file found under [C:\Tools\SleepOnLan\sol.json], now reading content
ERROR: 2019/10/12 20:22:07 configuration.go:64: error while loading configuration : invalid character ']' after object key:value pair

Yes, that’s right, you now have a syntax error in the JSON.

Try changing the last line to:

    }]}

(I reformatted your response since the system was only showing single backslashes without being explicitly quoted.)

Nice,
That worked, thanks heaps. I now have a method to call a .bat file which executes a python script with arguments to turn on and off my dreamscreen when a movie is played and stopped (using Chefs Vera plugin for emby and a http call in a scene from vera )
This is the working sol.json

{
  "Listeners" : ["UDP:9", "HTTP:8009" ],
  "LogLevel" : "INFO",
  "BroadcastIP" : "255.255.255.255",
  "Commands" : [
   {
		"Operation" : "dson",
        "Command" : "C:\\Scripts\\DreamScreenCommander-master\\DsOn.bat"
		}]}

Thanks again Akbooer!

Edit just one more thing
how would i add a second operation

"Operation" : "dsoff"
"Command" : "C:\\Scripts\\DreamScreenCommander-master\\DsOff.bat"

You’re welcome.

(Once again, I reformatted your code in the post to show \\ rather than \ for the benefit of any interested readers. It’s best to use the correct forum formatting with the buttons on the top of the post – makes things so much more readable.)

I am not sure why is not showing double slash as this is what i have typed,
I may of accidentally over written your formating

Yes, you did. I just changed it again.

To answer your question, I’m not sure, because I don’t know the syntax required by your target application. However, I would guess from the JSON structure:

{
  "Listeners" : ["UDP:9", "HTTP:8009" ],
  "LogLevel" : "INFO",
  "BroadcastIP" : "255.255.255.255",
  "Commands" : [
     {
        "Operation" : "dson",
        "Command" : "C:\\Scripts\\DreamScreenCommander-master\\DsOn.bat"
      },{
        "Operation" : "dsoff",
        "Command" : "C:\\Scripts\\DreamScreenCommander-master\\DsOnff.bat"
}]}

Nice, working on and off now with http://127.0.0.1:8009/dsoff (replace 127.0.0.1 with i.p) and http://127.0.0.1:8009/dson

{
  "Listeners" : ["UDP:9", "HTTP:8009" ],
  "LogLevel" : "INFO",
  "BroadcastIP" : "255.255.255.255",
  "Commands" : [
   {
		"Operation" : "dson",
        "Command" : "C:\\Scripts\\DreamScreenCommander-master\\DsOn.bat"
		},{
        "Operation" : "dsoff",
        "Command" : "C:\\Scripts\\DreamScreenCommander-master\\DsOff.bat"
}]}

Thanks again!

If your still around Akbooer, i have 1 final hurdle
I have a scene that i already use to call when a movie is started with the following luup code:
local dID = 595 – Device ID of your VirtualSwitch
local allow = true – true runs scene if switch is on, false blocks it
local status = luup.variable_get(“urn:upnp-org:serviceId:VSwitch1”,“Status”,dID)
return ((status == “1”) == allow)

To this scene and luup i would like to add the following http call:
luup.inet.wget(‘http://i.p address:8010/dson’)

If i just add a new line to the existing luup i get an error, might you please be able to assist with the correct way to make both call in a single vera scene ?

You can’t add a line after a return statement.

Your code is, presumably, borrowed from elsewhere, and is horrible. See Post by @rigpapa :

Might it be possible to call another scene after the initial original code?

Yes, you can, but it’s not really necessary.

Just put your additional code before the return.

Still just throws an error

If you share the code you’re trying, then perhaps we can see what’s wrong.

luup.inet.wget(‘http://i.p.address:8010/dson’)
local dID = 595 – Device ID of your VirtualSwitch
local allow = true – true runs scene if switch is on, false blocks it
local status = luup.variable_get(“urn:upnp-org:serviceId:VSwitch1”,“Status”,dID)
return ((status == “1”) == allow)

Suggest you just try this:

luup.inet.wget "http://i.p.address:8010/dson"
local dID = 595
local status = luup.variable_get("urn:upnp-org:serviceId:VSwitch1", "Status", dID)
return status == “1”

I don’t know whether it’s a function of the way that you’ve cut and pasted the code, or the forum software, but I am concerned that the double quotes have been turned into inverted commas and the double minus sign, for comments, converted to an em-dash. These will certainly give syntax errors. I’ve tried to cut out all the gratuitous stuff (including the comments) in the code to make it simpler.

It’s also possible that you should use:

local dID = 595
local status = luup.variable_get("urn:upnp-org:serviceId:VSwitch1", "Status", dID)
if status ~= “1” then return false end
luup.inet.wget "http://i.p.address:8010/dson"
return true

…depending on the effect you are actually trying to achieve.

Thanks a lot

© 2019 Vera Control Ltd., All Rights Reserved. Terms of Use | Privacy Policy