Page 1 of 1

Help with Waze ETA script

Posted: Wednesday 09 March 2016 8:44
by RobertH
Hi,

I have tried to modifie the script under to work under Windows I changed the include path of the JSON parser file but I still gives me an error, the file is there and the name is exactly the same. Could this be the curl part and how would I go about changing it?

From log:
2016-03-09 08:43:00.070 Error: EventSystem: in C:\Program Files (x86)\Domoticz\scripts\lua\script_time_eta.lua: ...ram Files (x86)\Domoticz\scripts\lua\script_time_eta.lua:50: attempt to index local 'jsonTrajet' (a nil value)


The code:

Code: Select all

--C:\Program Files (x86)\Domoticz\scripts\lua\script_time_eta.lua
------------- Begin time functions -------------
 
function timedifference_lastupdate_homework (s)
	if s == nil then 
		print('Error: Could not get lastupdate from device')
		difference = 1000 --value have to be larger than timeout. new virtual devices don't get corrent lastupdate value, skips the first check
		return difference
	end
	year = string.sub(s, 1, 4)
	month = string.sub(s, 6, 7)
	day = string.sub(s, 9, 10)
	hour = string.sub(s, 12, 13)
	minutes = string.sub(s, 15, 16)
	seconds = string.sub(s, 18, 19)
	t1 = os.time()
	t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
	difference = os.difftime (t1, t2)
	return difference
end
 
------------- End time functions -------------
 
 
------------- Begin local variables -------------
 
-- Debug variables:
 
timeout                = 300 
difference_homework    = timedifference_lastupdate_homework(otherdevices_lastupdate['ETA']) --change to the name of the virtual device
 
------------- End local variables -------------
 
------------- Begin code -------------
 
commandArray = {}
 
if (difference_homework > timeout)
then
 
function traveltime(departx,departy,arrivex,arrivey)
   --get route from WAZE--
   local waze=assert(io.popen('curl "https://www.waze.com/row-RoutingManager/routingRequest?from=x%3A'..departx..'+y%3A'..departy..'&to=x%3A'..arrivex..'+y%3A'..arrivey..'&returnJSON=true&returnGeometries=true&returnInstructions=true&timeout=60000&nPaths=1&clientVersion=4.0.0&options=AVOID_TRAILS%3At%2CALLOW_UTURNS"'))
   local trajet = waze:read('*all')
   waze:close()
   
   local jsonTrajet = json:decode(trajet)
 
   --Get major road from JSON
   routeName = jsonTrajet['response']['routeName']
   --Get route steps from JSON info
   route = jsonTrajet['response']['results']
 
   --Calculate the total time by adding the time of all sections
   routeTotalTimeSec = 0
   for response,results in pairs(route) do
      routeTotalTimeSec = routeTotalTimeSec + results['crossTime']
   end
 
   --translate the number of seconds to minutes
   routeTotalTimeMin = routeTotalTimeSec/60-((routeTotalTimeSec%60)/60)
 
   return routeTotalTimeMin
end
 
--import JSON addin (download from http://regex.info/code/JSON.lua)
json = (loadfile "C:\\Program Files (x86)\\Domoticz\\scripts\\lua\\JSON.lua")()
 
-- Update these variables ---------------
--idx of virtual device for capturing the travel minutes
idxhomework = '68'
 
departy="59.684534" --lat
departx="9.627667" --long
 
arrivey="59.675331" --lat
arrivex="9.641017" --long
 
-- get travel time
homework=traveltime(departx,departy,arrivex,arrivey)
 
 
 
 commandArray[1] = {['UpdateDevice'] = idxhomework .. '|0|' .. homework}
 
 
end
 
------------- End code -------------
 
return commandArray

Robert

Re: Help with Waze ETA script

Posted: Wednesday 09 March 2016 10:07
by simonrg
If JSON.lua is in the same directory as the script, then you should be able to just use:

Code: Select all

json = (loadfile "JSON.lua")()

Re: Help with Waze ETA script

Posted: Wednesday 09 March 2016 10:17
by Rydin
I see that you use my version of the script. I did not try it in windows, but after some testing now i see that all you need is to install curl.
I downloaded curl from here http://winampplugins.co.uk/curl/ and copied it to c:\windows
simonrg wrote:If JSON.lua is in the same directory as the script, then you should be able to just use:

Code: Select all

json = (loadfile "JSON.lua")()
I tried to skip the path to JSON.lua on my raspberry, but for some reason it couldn't find the file.

Edit:
Same problem on windows. Can't find JSON.lua without the full path to the file.

Re: Help with Waze ETA script

Posted: Wednesday 09 March 2016 13:04
by jvdz
That is indeed the default behavior I have seen in the past. The search path for the LUA loadfile command doesn't include the calling script directory.

Jos

Re: Help with Waze ETA script

Posted: Wednesday 09 March 2016 13:09
by simonrg
Rydin wrote:Same problem on windows. Can't find JSON.lua without the full path to the file.
Strange loadfile works without using package.path, so if Lua is running in the directory where the scripts are then JSON.lua should be found.

Whereas require uses package.path which could explicitly include the current path or anyother directory.

What does print(package.path) return?

Does require(JSON) find JSON.lua?

For Windows specifically then you may need to escape the brackets in the path name, so you could try moving JSON to a very simple directory - c:\test\JSON.lua.

Re: Help with Waze ETA script

Posted: Wednesday 09 March 2016 13:20
by Rydin
print(package.path)
2016-03-09 13:15:00.148 LUA: C:\Program Files (x86)\Domoticz\lua\?.lua;C:\Program Files (x86)\Domoticz\lua\?\init.lua;C:\Program Files (x86)\Domoticz\?.lua;C:\Program Files (x86)\Domoticz\?\init.lua;.\?.lua

edit:
test = require("JSON")
print(test)

2016-03-09 13:31:00.242 Error: EventSystem: in C:\Program Files (x86)\Domoticz\scripts\lua\script_time_waze.lua: ...am Files (x86)\Domoticz\scripts\lua\script_time_waze.lua:69: module 'JSON' not found:
no field package.preload['JSON']
no file 'C:\Program Files (x86)\Domoticz\lua\JSON.lua'
no file 'C:\Program Files (x86)\Domoticz\lua\JSON\init.lua'
no file 'C:\Program Files (x86)\Domoticz\JSON.lua'
no file 'C:\Program Files (x86)\Domoticz\JSON\init.lua'
no file '.\JSON.lua'
no file 'C:\Program Files (x86)\Domoticz\JSON.dll'
no file 'C:\Program Files (x86)\Domoticz\loadall.dll'
no file '.\JSON.dll'

Re: RE: Re: Help with Waze ETA script

Posted: Wednesday 09 March 2016 13:26
by RobertH
Rydin wrote:I see that you use my version of the script. I did not try it in windows, but after some testing now i see that all you need is to install curl.
I downloaded curl from here http://winampplugins.co.uk/curl/ and copied it to c:\windows
simonrg wrote:If JSON.lua is in the same directory as the script, then you should be able to just use:

Code: Select all

json = (loadfile "JSON.lua")()
I tried to skip the path to JSON.lua on my raspberry, but for some reason it couldn't find the file.

Edit:
Same problem on windows. Can't find JSON.lua without the full path to the file.
Thank you very much. That worked perfectly! Image Are you the one who made the ST1 script also? Curl solved my problem on that one also..

Robert

Sent from my SM-G925K using Tapatalk

Re: Help with Waze ETA script

Posted: Wednesday 09 March 2016 13:31
by Rydin
Yes, I made the ST1 script. Happy that it works for you :)

Re: Help with Waze ETA script

Posted: Wednesday 09 March 2016 16:42
by simonrg
Rydin wrote:print(package.path)
2016-03-09 13:15:00.148 LUA: C:\Program Files (x86)\Domoticz\lua\?.lua;C:\Program Files (x86)\Domoticz\lua\?\init.lua;C:\Program Files (x86)\Domoticz\?.lua;C:\Program Files (x86)\Domoticz\?\init.lua;.\?.lua

test = require("JSON")
print(test)

2016-03-09 13:31:00.242 Error: EventSystem: in C:\Program Files (x86)\Domoticz\scripts\lua\script_time_waze.lua: ...am Files (x86)\Domoticz\scripts\lua\script_time_waze.lua:69: module 'JSON' not found:
no field package.preload['JSON']
no file 'C:\Program Files (x86)\Domoticz\lua\JSON.lua'
no file 'C:\Program Files (x86)\Domoticz\lua\JSON\init.lua'
no file 'C:\Program Files (x86)\Domoticz\JSON.lua'
no file 'C:\Program Files (x86)\Domoticz\JSON\init.lua'
no file '.\JSON.lua'
no file 'C:\Program Files (x86)\Domoticz\JSON.dll'
no file 'C:\Program Files (x86)\Domoticz\loadall.dll'
no file '.\JSON.dll'
Not sure you haven't solved this already.
Just intersting that if you look at package.path it does not include Domoticz\scripts\lua, but Domoticz\lua - so placing JSON.lua in Domoticz\lua would allow require to find it.
Equally json = (loadfile "JSON.lua")() may well work then if Lua is really running is Domoticz\lua.