diff --git a/database/16 b/database/16 index c04f60f..9ef2b53 100644 Binary files a/database/16 and b/database/16 differ diff --git a/database/cellar b/database/cellar index 03ec032..cbeb451 100644 Binary files a/database/cellar and b/database/cellar differ diff --git a/database/sensors.lua b/database/sensors.lua index ef6f634..9e225aa 100644 --- a/database/sensors.lua +++ b/database/sensors.lua @@ -7,6 +7,8 @@ local _M = {} _M.cellar = {} +_M.weather = {} + _M.room = {} if not db.cellar then @@ -14,6 +16,11 @@ if not db.cellar then 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() @@ -35,6 +42,22 @@ function _M.cellar.data(start,ends) return res end +function _M.weather.now() + return db.weather[#db.weather] +end + +function _M.weather.data(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,val in pairs(db.weather) do + if date(val.date) >= start and date(val.date) <= ends then + table.insert(res,val) + end + end + return res +end + function _M.room.now() return db.room[#db.room] end diff --git a/database/weather b/database/weather new file mode 100644 index 0000000..7b91ade Binary files /dev/null and b/database/weather differ diff --git a/front/archive.lua b/front/archive.lua index b4322fb..6225d38 100644 --- a/front/archive.lua +++ b/front/archive.lua @@ -5,8 +5,9 @@ local tanks = require('database.tanks') local function resp(start,ends) local view = template.new("archive.html", "layout.html") view.title = "Мои архивные чаны" - view.cellar = {temp=12, warm=45, warm_status='is-danger',temp_status='is-warning'} - view.room = {temp=18, warm=45,warm_status='is-success',temp_status='is-warning'} + view.cellar = sensors.cellar.now() + view.room = sensors.room.now() + view.weather = sensors.weather.now() view.tanks = tanks.archive() view:render() end diff --git a/front/cellar.lua b/front/cellar.lua index 6d75757..2e79a0a 100644 --- a/front/cellar.lua +++ b/front/cellar.lua @@ -10,6 +10,7 @@ local function resp(start,ends) view.data = sensors.cellar.data(start,ends) view.cellar = sensors.cellar.now() view.room = sensors.room.now() + view.weather = sensors.weather.now() view:render() end diff --git a/front/index.lua b/front/index.lua index 6b002d5..39189cd 100644 --- a/front/index.lua +++ b/front/index.lua @@ -11,6 +11,7 @@ local function resp(start,ends) view.cellar = sensors.cellar.now() view.room = sensors.room.now() view.tanks = tanks.active(start,ends) + view.weather = sensors.weather.now() view:render() end diff --git a/front/room.lua b/front/room.lua index 39199fb..4b6a5f4 100644 --- a/front/room.lua +++ b/front/room.lua @@ -10,6 +10,7 @@ local function resp(start,ends) view.data = sensors.room.data(start,ends) view.cellar = sensors.cellar.now() view.room = sensors.room.now() + view.weather = sensors.weather.now() view:render() end diff --git a/front/weather.lua b/front/weather.lua new file mode 100644 index 0000000..c4955bb --- /dev/null +++ b/front/weather.lua @@ -0,0 +1,30 @@ +local template = require "resty.template" +local handle = require('framework.handle') +local sensors = require('database.sensors') + +local function resp(start,ends) + local view = template.new("climate.html", "layout.html") + view.start = start + view.ends = ends + view.title = "Погода на улице" + view.data = sensors.weather.data(start,ends) + view.cellar = sensors.cellar.now() + view.room = sensors.room.now() + view.weather = sensors.weather.now() + view:render() +end + +local request = { + POST = function() + ngx.req.read_body() + local args, err = ngx.req.get_post_args() + resp(string.match(args['datetimes'], "([%d-%s:]+)%s>%s([%d-%s:]+)")) + end, + GET = function() + local ends = os.date('%Y-%m-%d %H:%M:%S',os.time()+14400) + local start = os.date('%Y-%m-%d %H:%M:%S',os.time()-86400) + resp(start,ends) + end +} + +ngx.say(handle(request)) diff --git a/templates/layout.html b/templates/layout.html index 144a7ae..be7f221 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -58,13 +58,25 @@ {{room.temp}} {{room.warm}} + -
  • - {{cellar.temp}} - {{cellar.warm}} -
  • + + +