-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.lua
157 lines (145 loc) · 4.76 KB
/
app.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
function findFunctionMeta(meta)
local match = 1
for i, fun in ipairs(functions) do
match = 1;
for k, v in pairs(meta) do
if fun.meta[k] ~= v then
match = 0
end
end
if match == 1 then
return functions[i]
end
end
return nil;
end
function findDeviceMeta(meta)
devices = lynx.getDevices()
local match = 1
for i, dev in ipairs(devices) do
match = 1;
for k, v in pairs(meta) do
if dev.meta[k] ~= v then
match = 0
end
end
if match == 1 then
return devices[i]
end
end
return nil;
end
function create_device_if_needed(payload)
local dev = findDeviceMeta({
station_key = payload.station.key
})
if dev == nil then
local _dev = {
type = "SMHI weatherstation",
installation_id = app.installation_id,
meta = {
name = "SMHI station: " .. payload.station.name,
station_key = tostring(payload.station.key),
station_name = payload.station.name,
station_owner = payload.station.owner,
latitude = tostring(payload.position[1].latitude),
longitude = tostring(payload.position[1].longitude)
}
}
lynx.createDevice(_dev)
dev = findDeviceMeta({
station_key = payload.station.key
})
end
return dev.id
end
function fetchAndPublishData(station,parameter)
local http_request = require "http.request"
local url = "https://opendata-download-metobs.smhi.se/api/version/1.0/parameter/" .. parameter .. "/station/" .. station .. "/period/latest-hour/data.json"
local headers, stream = assert(http_request.new_from_uri(url):go())
local body = assert(stream:get_body_as_string())
if headers:get ":status" ~= "200" then
-- error(body)
print("Could not fetch parameter: " .. parameter .. " from: " .. station)
return nil
end
local payload = json:decode(body)
local func = findFunctionMeta({
smhi_station_key = payload.station.key,
smhi_parameter = payload.parameter.key
})
if func == nil then
device = create_device_if_needed(payload)
local fn = {
type = "SMHI weatherstation data",
installation_id = app.installation_id,
meta = {
name = payload.station.name .. " - " .. payload.parameter.name .. " (" .. payload.parameter.summary .. ")",
device_id = tostring(device),
smhi_unit = tostring(payload.parameter.unit),
smhi_station_key = tostring(payload.station.key),
smhi_station_name = tostring(payload.station.name),
smhi_parameter = tostring(payload.parameter.key),
smhi_paramater_summary = tostring(payload.parameter.summary),
smhi_paramater_name = tostring(payload.parameter.name),
latitude = tostring(payload.position[1].latitude),
longitude = tostring(payload.position[1].longitude),
height = tostring(payload.position[1].height),
data_url = url,
topic_read = "obj/smhi/".. payload.station.key .."/" .. payload.parameter.key,
}
}
lynx.createFunction(fn)
end
if payload.value ~= nil and next(payload.value) ~= nil then
print(json:encode(payload.value))
local mqtt_payload = json:encode({value = payload.value[1].value, timestamp = payload.value[1].date / 1000, msg = "quality="..payload.value[1].quality})
mq:pub("obj/smhi/".. payload.station.key .."/" .. payload.parameter.key, mqtt_payload, false, 0)
end
end
function sendData()
fetchAndPublishData(cfg.station, 1)
fetchAndPublishData(cfg.station, 21)
fetchAndPublishData(cfg.station, 39)
fetchAndPublishData(cfg.station, 11)
fetchAndPublishData(cfg.station, 22)
fetchAndPublishData(cfg.station, 26)
fetchAndPublishData(cfg.station, 27)
fetchAndPublishData(cfg.station, 19)
fetchAndPublishData(cfg.station, 2)
fetchAndPublishData(cfg.station, 20)
fetchAndPublishData(cfg.station, 9)
fetchAndPublishData(cfg.station, 24)
fetchAndPublishData(cfg.station, 40)
fetchAndPublishData(cfg.station, 25)
fetchAndPublishData(cfg.station, 28)
fetchAndPublishData(cfg.station, 30)
fetchAndPublishData(cfg.station, 32)
fetchAndPublishData(cfg.station, 34)
fetchAndPublishData(cfg.station, 36)
fetchAndPublishData(cfg.station, 37)
fetchAndPublishData(cfg.station, 29)
fetchAndPublishData(cfg.station, 31)
fetchAndPublishData(cfg.station, 33)
fetchAndPublishData(cfg.station, 35)
fetchAndPublishData(cfg.station, 17)
fetchAndPublishData(cfg.station, 18)
fetchAndPublishData(cfg.station, 15)
fetchAndPublishData(cfg.station, 38)
fetchAndPublishData(cfg.station, 23)
fetchAndPublishData(cfg.station, 14)
fetchAndPublishData(cfg.station, 5)
fetchAndPublishData(cfg.station, 7)
fetchAndPublishData(cfg.station, 6)
fetchAndPublishData(cfg.station, 13)
fetchAndPublishData(cfg.station, 12)
fetchAndPublishData(cfg.station, 8)
fetchAndPublishData(cfg.station, 10)
fetchAndPublishData(cfg.station, 16)
fetchAndPublishData(cfg.station, 4)
fetchAndPublishData(cfg.station, 3)
end
function onStart()
sendData()
local t = timer:interval(cfg.interval * 3600, sendData)
end