forked from Tank/braga
98 lines
3.8 KiB
Lua
98 lines
3.8 KiB
Lua
local array = require("database.array")
|
||
local flatdb = require('database.flatdb')
|
||
local date = require('date')
|
||
local db = flatdb(ngx.var.document_root..'/database')
|
||
|
||
local _M = {}
|
||
|
||
local fields = {
|
||
{sensor_id = 'ID сенсора'},
|
||
{name = 'Название емкости'},
|
||
{volume = 'Объем емкости'},
|
||
{date_start = 'Дата постановки на брожение'},
|
||
{raw_volume = 'Объем сока'},
|
||
{honey_weight = 'Начальная масса добавленного меда'},
|
||
{fructose_weight = 'Начальная масса добавленной фруктозы'},
|
||
{dextrose_weight = 'Начальная масса добавленной декстрозы'},
|
||
{polisugar_weight = 'Начальная масса добавленных полисахаров'},
|
||
{ph = 'PH'},
|
||
{sugar = 'Сахаристость'},
|
||
{date_end = 'Дата снятия с осадка'},
|
||
{date_restart = 'Дата постановки на повторное брожение'},
|
||
{date_reend = 'Дата повторного снятия с осадка'},
|
||
{date_blending = 'Дата купажа'},
|
||
{date_fructose_add = 'Дата добавления фруктозы'},
|
||
{add_fructose_weight = 'Масса добавляемой фруктозы'},
|
||
{date_dextrose_add = 'Дата добавления декстрозы'},
|
||
{add_dextrose_weight = 'Масса добавляемой декстрозы'},
|
||
{date_polisugar_add = 'Дата добавления полисахаров'},
|
||
{add_polisugar_weight = 'Масса добавляемых полисахаров'},
|
||
{comment = 'Комментарии'},
|
||
{archive = 'Архивная емкость'}
|
||
}
|
||
|
||
local count = 0
|
||
|
||
local results = {}
|
||
|
||
for line in io.lines(ngx.var.document_root..'/tanks.csv') do
|
||
if count>0 then
|
||
local data = array.explode(line,',')
|
||
results[count] = {notification = {typ = 'is-success', text = 'Брожение протекает нормально'}, status = 'is-primary'}
|
||
for n,value in pairs(data) do
|
||
local key,text = array.first(fields[n])
|
||
if key:find('date_') and value ~='' and pcall(date(),value) and date(value) < date() then
|
||
results[count].notification = {typ = 'is-danger', text = text}
|
||
end
|
||
results[count][key] = {value = value, text = text, order = n}
|
||
end
|
||
if not db[results[count].sensor_id.value] then
|
||
db[results[count].sensor_id.value] = {
|
||
{temp = 0, date = date():fmt('%Y-%d-%m')},
|
||
{temp = 0, date = date():fmt('%Y-%d-%m')}
|
||
}
|
||
db:save()
|
||
end
|
||
results[count].stat = db[results[count].sensor_id.value]
|
||
if (results[count]['date_blending'].value ~= '' and pcall(date(),results[count]['date_blending'].value) and date(results[count]['date_blending'].value) < date()) or (results[count]['date_end'].value ~= '' and date(results[count]['date_end'].value) < date()) then
|
||
results[count].status = 'is-success'
|
||
end
|
||
end
|
||
count = count + 1
|
||
end
|
||
|
||
function _M.all()
|
||
return results
|
||
end
|
||
|
||
function _M.active(start,ends)
|
||
if not start then start = date(start) else start = date():addhours(-24) end
|
||
if not ends then ends = date(ends) else ends = date() end
|
||
local res = {}
|
||
for key,result in pairs(results) do
|
||
if result.archive and result.archive.value ~= '' and tonumber(result.archive.value) ~= 1 then
|
||
local stat = {}
|
||
for i,val in pairs(result.stat) do
|
||
if date(val.date) >= start and date(val.date) <= ends then
|
||
table.insert(stat,val)
|
||
end
|
||
end
|
||
result.stat = stat
|
||
table.insert(res,result)
|
||
end
|
||
end
|
||
return res
|
||
end
|
||
|
||
function _M.archive()
|
||
local res = {}
|
||
for key,result in pairs(results) do
|
||
if (result.archive and result.archive.value ~= '' and tonumber(result.archive.value) == 1) or (result['date_blending'].value ~= '' and pcall(date(),result['date_blending'].value) and date(result['date_blending'].value) < date()) then
|
||
table.insert(res,result)
|
||
end
|
||
end
|
||
return res
|
||
end
|
||
|
||
return _M
|