local json = require('cjson.safe')
local base64 = require('base64')
local flatdb = require('database.flatdb')
local db = flatdb('/home/braga/database')

if not db.cellar then 
	db.cellar = {{temp=0, date = os.date('%Y-%m-%d %H:%M:%S'), warm=0, warm_status='is-danger',temp_status='is-warning'}} 
	db:save()
end

if not db.weather then 
	db.weather = {{temp=0, date = os.date('%Y-%m-%d %H:%M:%S'), warm=0, warm_status='is-success',temp_status='is-success'}} 
	db:save()
end

if not db.room then 
	db.room = {{temp=0, date = os.date('%Y-%m-%d %H:%M:%S'), warm=0, warm_status='is-danger',temp_status='is-warning'}} 
	db:save()
end

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 = ngx.req.socket(true)
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)
	if rec.sensors then
		for id,temp in pairs(rec.sensors) do
			if not db[id] then db[id] = {} end
			table.insert(db[id],{temp=temp, date=os.date('%Y-%m-%d %H:%M:%S')})
		end
	else
		ngx.log(ngx.NOTICE, 'не получены данные с сенсоров')
	end
	if rec.cellar and rec.cellar.temp then 
		rec.cellar.warm = rec.cellar.warm or 0
		if rec.cellar.temp >= limits.cellar.temp.optimal[1] and rec.cellar.temp <= limits.cellar.temp.optimal[2] then rec.cellar.temp_status = 'is-success' 
		elseif rec.cellar.temp >= limits.cellar.temp.critical[1] and rec.cellar.temp <= limits.cellar.temp.critical[2] then rec.cellar.temp_status = 'is-warning' 
		else  rec.cellar.temp_status = 'is-danger' end
		if rec.cellar.warm >= limits.cellar.warm.optimal[1] and rec.cellar.warm <= limits.cellar.warm.optimal[2] then rec.cellar.warm_status = 'is-success' 
		elseif rec.cellar.warm >= limits.cellar.warm.critical[1] and rec.cellar.warm <= limits.cellar.warm.critical[2] then rec.cellar.warm_status = 'is-warning' 
		else  rec.cellar.warm_status = 'is-danger' end
		rec.cellar.date = os.date('%Y-%m-%d %H:%M:%S')
 		table.insert(db.cellar,rec.cellar)
	end
	if rec.room and rec.room.temp then 
		rec.room.warm = rec.room.warm or 0
		if rec.room.temp >= limits.room.temp.optimal[1] and rec.room.temp <= limits.room.temp.optimal[2] then rec.room.temp_status = 'is-success' 
		elseif rec.room.temp >= limits.room.temp.critical[1] and rec.room.temp <= limits.room.temp.critical[2] then rec.room.temp_status = 'is-warning' 
		else  rec.room.temp_status = 'is-danger' end
		if rec.room.warm >= limits.room.warm.optimal[1] and rec.room.warm <= limits.room.warm.optimal[2] then rec.room.warm_status = 'is-success' 
		elseif rec.room.warm >= limits.room.warm.critical[1] and rec.room.warm <= limits.room.warm.critical[2] then rec.room.warm_status = 'is-warning' 
		else  rec.room.warm_status = 'is-danger' end
		rec.room.date = os.date('%Y-%m-%d %H:%M:%S')
 		table.insert(db.room,rec.room)
	end
	if rec.weather and rec.weather.temp then 
		rec.weather.warm = rec.weather.warm or 0
		if rec.weather.temp >= limits.weather.temp.optimal[1] and rec.weather.temp <= limits.weather.temp.optimal[2] then rec.weather.temp_status = 'is-success' 
		elseif rec.weather.temp >= limits.weather.temp.critical[1] and rec.weather.temp <= limits.weather.temp.critical[2] then rec.weather.temp_status = 'is-warning' 
		else  rec.weather.temp_status = 'is-danger' end
		if rec.weather.warm >= limits.weather.warm.optimal[1] and rec.weather.warm <= limits.weather.warm.optimal[2] then rec.weather.warm_status = 'is-success' 
		elseif rec.weather.warm >= limits.weather.warm.critical[1] and rec.weather.warm <= limits.weather.warm.critical[2] then rec.weather.warm_status = 'is-warning' 
		else  rec.weather.warm_status = 'is-danger' end
		rec.weather.date = os.date('%Y-%m-%d %H:%M:%S')
 		table.insert(db.weather,rec.weather)
	end
	db:save()
	ngx.log(ngx.NOTICE, json.encode(rec)) 
	sock:send('OK')
end

