forked from Tank/braga
67 lines
1.4 KiB
Lua
67 lines
1.4 KiB
Lua
local json = require('cjson.safe')
|
|
local base64 = require('base64')
|
|
local flatdb = require('database.flatdb')
|
|
local db = flatdb('/home/braga/database')
|
|
|
|
local limits = {
|
|
weather = {
|
|
temp = {
|
|
optimal = {0,10},
|
|
critical = {-25,35}
|
|
},
|
|
warm = {
|
|
optimal = {40,60},
|
|
critical = {10,90}
|
|
}
|
|
},
|
|
cellar = {
|
|
temp = {
|
|
optimal = {10,12},
|
|
critical = {8,14}
|
|
},
|
|
warm = {
|
|
optimal = {65,80},
|
|
critical = {50,90}
|
|
}
|
|
},
|
|
room = {
|
|
temp = {
|
|
optimal = {18,24},
|
|
critical = {16,28}
|
|
},
|
|
warm = {
|
|
optimal = {60,75},
|
|
critical = {50,80}
|
|
}
|
|
}
|
|
}
|
|
|
|
-- Расшифровка сообщения от устройства
|
|
|
|
local function decode(rec)
|
|
rec = base64.decode(rec)
|
|
ngx.log(ngx.INFO, rec)
|
|
return json.decode(rec)
|
|
end
|
|
|
|
local sock = assert(ngx.req.socket(true),'no socket')
|
|
sock:settimeout(10000) -- one second timeout
|
|
local rec = sock:receive() -- read a line from downstream
|
|
|
|
|
|
if rec == nil then
|
|
ngx.log(ngx.NOTICE, 'Empty data')
|
|
return ngx.exit(444)
|
|
else
|
|
rec = decode(rec)
|
|
local res = db.add({['guid']=rec.id,['type']='bme',['alt']=rec.bme.alt,['temp']=rec.bme.temp,['press']=rec.bme.press})
|
|
ngx.log(ngx.NOTICE, res)
|
|
res = db.add({['guid']=rec.id,['type']='dht',['humi']=rec.dht.humi,['temp']=rec.dht.temp})
|
|
ngx.log(ngx.NOTICE, res)
|
|
res = db.add({['guid']=rec.id,['type']='solar',['uf']=rec.uf})
|
|
ngx.log(ngx.NOTICE, res)
|
|
sock:send(base64.encode(json.encode(rec)))
|
|
db:save()
|
|
end
|
|
|