-
Notifications
You must be signed in to change notification settings - Fork 3
/
vigicrue2.lua
76 lines (71 loc) · 2.99 KB
/
vigicrue2.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
-- Parameters to change according to you needs
debug=false
-- Station ID, from one to many
Station = {'W141001001', 'L800001020'}
-- IDX of sensor for height value | STATION_ID='IDX_SENSOR'
SIDXHeight = {W141001001='215', L800001020='218'}
-- IDX of sensor for speed value | STATION_ID='IDX_SENSOR'
SIDXSpeed = {W141001001='216', L800001020='217'}
-- JSON.lua path
json = (loadfile "/opt/domoticz/scripts/lua/JSON.lua")()
-- Curl path
curl = '/usr/bin/curl'
-- End of parameters
time = os.date("*t")
-- Function to update sensor
local function update(idx, value1)
local cmd = idx..'|0|'..value1
--print(cmd)
table.insert (commandArray, { ['UpdateDevice'] = cmd } )
end
-- function to get data from url
local function getdata(urlh,urls)
local DataHeight = assert(io.popen(curl..' -s "'..urlh..'"'))
local BlocJsonHeight = DataHeight:read('*all')
DataHeight:close()
local JsonHeight = json:decode(BlocJsonHeight)
local Height = JsonHeight.Serie.ObssHydro
local DataSpeed = assert(io.popen(curl..' -s "'..urls..'"'))
local BlocJsonSpeed = DataSpeed:read('*all')
DataSpeed:close()
local JsonSpeed = json:decode(BlocJsonSpeed)
local Speed = JsonSpeed.Serie.ObssHydro
return Height, Speed
end
local function test_error(url)
local DataTest = assert(io.popen(curl..' -s --max-time 8 "'..url..'"'))
local BlocJsonTest = DataTest:read('*all')
DataTest:close()
JsonTest = json:decode(BlocJsonTest)
Test = JsonTest[1].error_msg
end
commandArray = {}
if (time.min == 0 or time.min == 30) then
for k,v in pairs(Station) do
local IDStation = v
local IDXHeight = SIDXHeight[v]
local IDXSpeed = SIDXSpeed[v]
local urlHeight = 'https://www.vigicrues.gouv.fr/services/observations.json/index.php?CdStationHydro='..IDStation..'&GrdSerie=H&FormatSortie=simple'
local urlSpeed = 'https://www.vigicrues.gouv.fr/services/observations.json/index.php?CdStationHydro='..IDStation..'&GrdSerie=Q&FormatSortie=simple'
local status, retval = pcall(test_error,urlHeight);
if (status) then
print("Error, station inconnu")
commandArray['SendNotification']='subject#Erreur, station '..IDStation..' inconnu ou problème temporaire du site Vigicrue, merci de vérifier : '..urlHeight..'#0#sound#extradata#telegram'
else
ResultHeight,ResultSpeed=getdata(urlHeight,urlSpeed)
if (#ResultHeight ~= 0) then
if (debug) then print("ResultHeight: "..ResultHeight[#ResultHeight][2]) end
update(IDXHeight, ResultHeight[#ResultHeight][2])
else
if (debug) then print('Height level is empty.') end
end
if (#ResultSpeed ~= 0) then
if (debug) then print("ResultSpeed: "..ResultSpeed[#ResultSpeed][2]) end
update(IDXSpeed, ResultSpeed[#ResultSpeed][2])
else
if (debug) then print('Speed is empty.') end
end
end
end
end
return commandArray