--[[
%% properties
%% events
%% globals
--]]
--[[
%% autostart
%% properties
%% globals
--]]
if (fibaro:countScenes() > 1) then
fibaro:abort()
end
--------------------------------------------------------------------
--------------------------------------------------------------------
-- Name: HC2 Waze Calculator
-- Type: Virtual Device & Scene
-- Description: Calcul de temps pour un trajet entre 2 coordonnées.
-- Service: API Waze https://www.waze.com/fr/
-- Authors: Jean-Christophe Vermandé & Icon by Moicphil from (domotique-fibaro.fr)
-- Ref: http://www.domo-blog.fr/trajets-voiture-waze-eedomus/
-- Release date: 07 Nov 2015
-- Version 1.0.0
--------------------------------------------------------------------
--------------------------------------------------------------------
--------------------------------------------------------------------
-- USER DATA -------------------------------------------------------
--------------------------------------------------------------------
local params = {
{
name = "Trajet 1",
virtualDeviceId = 1449,
from = {
name = "Home Gomez",
x = 2.5075405000000046, -- longitude
y = 49.0960441 -- latitude
},
to = {
name = "Parking Citadine",
x = 2.331380999999965, -- longitude
y = 48.885163 -- latitude
},
pollingMs = 60*1000
},
{
name = "Trajet 2",
virtualDeviceId = 1452,
from = {
name = "Home Gomez",
x = 2.5075405000000046, -- longitude
y = 49.0960441 -- latitude
},
to = {
name = "Parking Citadine",
x = 2.331380999999965, -- longitude
y = 48.885163 -- latitude
},
pollingMs = 5*60*1000
}
}
--------------------------------------------------------------------
-- SCRIPT ----------------------------------------------------------
--------------------------------------------------------------------
local serviceUri = "https://www.waze.com/row-RoutingManager/routingRequest"
local dataObject = {
mainRouteName = "n.c",
resultsCache = nil
}
function Add(key, value)
if (value) then
dataObject[key] = value
end
end
function ComputeTime()
--calcul du temps de trajet
local ts = 0
for k,v in pairs(dataObject.resultsCache) do
ts = ts + v['crossTime']
end
return ts
end
function SetUI(id, target, value)
fibaro:call(id, "setProperty", "ui."..target..".value", value);
end
function ResetUI(id, value)
SetUI(id, "lblVia", value)
SetUI(id, "lblFrom", value)
SetUI(id, "lblTo", value)
SetUI(id, "lblCrossTime", value)
SetUI(id, "lblDepTime", value)
SetUI(id, "lblArrTime", value)
end
function Start()
for i=1, #params do
GetWazeData(i)
end
end
function GetWazeData(idx)
local query = serviceUri .. "?from=x:"..(params[idx].from.x).."+y:"..(params[idx].from.y).."&to=x:"..(params[idx].to.x).."+y:"..(params[idx].to.y).."&returnJSON=true&timeout=6000&nPaths=1&options=AVOID_TRAILS:t,ALLOW_UTURNS"
local http = net.HTTPClient({ timeout = 2000 })
http:request(query, {
options = {
method = 'GET',
headers = {
["Content-Type"] = "application/json",
["User-Agent"] = "User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:43.0) Gecko/20100101 Firefox/43.0\nreferer: https://www.waze.com"
},
data = body
},
success = function(p)
if p.status == 200 then
local status, data = pcall(json.decode, p.data)
if (status and data) then
Add('mainRouteName', data.response.routeName)
Add('resultsCache', data.response.results)
if (dataObject.mainRouteName) then
print("Via : " .. data.response.routeName)
SetUI(params[idx].virtualDeviceId, "lblVia", dataObject.mainRouteName)
end
print("De " .. params[idx].from.name)
print("À " .. params[idx].to.name)
SetUI(params[idx].virtualDeviceId, "lblFrom", params[idx].from.name)
SetUI(params[idx].virtualDeviceId, "lblTo", params[idx].to.name)
-- calcul du temps total de trajet
local ts = ComputeTime()
local tm = ts/60-((ts%60)/60)
print("Temps de trajet approximatif en minutes: " .. tm)
SetUI(params[idx].virtualDeviceId, "lblCrossTime", tm .. " m")
-- Heure d'arrivée estimée
local now = os.time();
local depTime = os.date("%X",now)
local arrTime = os.date("%X",now+ts)
print("Heure de départ: " .. depTime)
print("Heure d'arrivée estimée: " .. arrTime)
SetUI(params[idx].virtualDeviceId, "lblDepTime", depTime)
SetUI(params[idx].virtualDeviceId, "lblArrTime", arrTime)
else
print("no data to parse")
ResetUI(params[idx].virtualDeviceId, "-- no data --")
end
else
print("parsing error")
ResetUI(params[idx].virtualDeviceId, "-- error --")
end
-- Polling
setTimeout(function()
GetWazeData(idx)
end, params[idx].pollingMs)
end,
error = function(err)
print('Get data error = ' .. err)
-- retry after 10 seconds
setTimeout(function()
GetWazeData(idx)
end, 10000)
end
})
end
local modetrace = true
local modedebud = true
Start()