Skip to content
This repository has been archived by the owner on May 4, 2023. It is now read-only.

Commit

Permalink
add timeseries storage
Browse files Browse the repository at this point in the history
  • Loading branch information
vvzvlad committed Dec 4, 2018
1 parent bf26c3a commit 10ed490
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 19 deletions.
53 changes: 39 additions & 14 deletions iot_scada.lua
@@ -1,6 +1,7 @@
#!/usr/bin/env tarantool
local mqtt = require 'mqtt'
local fiber = require 'fiber'
local clock = require 'clock'

local config = {}
config.MQTT_WIRENBOARD_HOST = "192.168.1.111"
Expand All @@ -10,7 +11,6 @@ config.HTTP_PORT = 8080

io.stdout:setvbuf("no")

local sensor_values = {}
local box = box -- Fake define for disabling IDE warnings

local storage
Expand Down Expand Up @@ -51,39 +51,62 @@ local function http_server_action_handler(req)
end
end

local function get_values()
local function get_values_for_table(serial)
local temperature_data_object, i = {}, 0
for _, tuple in storage:pairs() do
for _, tuple in storage.index.serial:pairs(serial) do
i = i + 1
local absolute_time_text = os.date("%Y-%m-%d, %H:%M:%S", tuple["timestamp"])
local relative_time_text = (os.time() - tuple["timestamp"]).."s ago"
local time_in_sec = math.ceil(tuple["timestamp"]/10000)
local absolute_time_text = os.date("%Y-%m-%d, %H:%M:%S", time_in_sec)

temperature_data_object[i] = {}
temperature_data_object[i].sensor = tuple["serial"]
temperature_data_object[i].serial = tuple["serial"]
temperature_data_object[i].temperature = tuple["value"]
temperature_data_object[i].update_time_epoch = tuple["timestamp"]
temperature_data_object[i].update_time_text = absolute_time_text.." ("..relative_time_text..")"
temperature_data_object[i].time_epoch = tuple["timestamp"]
temperature_data_object[i].time_text = absolute_time_text
end
return temperature_data_object
end

local function get_values_for_graph(serial)
local temperature_data_object, i = {}, 1
temperature_data_object[1] = {"Time", "Value"}
for _, tuple in storage.index.serial:pairs(serial) do
i = i + 1
local time_in_sec = math.ceil(tuple["timestamp"]/10000)
temperature_data_object[i] = {os.date("%H:%M", time_in_sec), tuple["value"]}
end
return temperature_data_object
end

local function gen_id()
local new_id = clock.realtime()*10000
while storage.index.timestamp:get(new_id) do
new_id = new_id + 1
end
return new_id
end

local function save_value(serial, value)
local timestamp = os.time()
value = tonumber(value)
if (value ~= nil and serial ~= nil) then
storage:upsert({serial, timestamp, value}, {{"=", 2, timestamp} , {"=", 3, value}})
storage:insert({serial, gen_id(), value})
return true
end
return false
end

local function http_server_data_handler(req)
local type_param = req:param("type")
local params = req:param()

if (type_param == "temperature") then
local values = get_values()
if (params["data"] == "table") then
local values = get_values_for_table(params["serial"])
return req:render{ json = { values } }
elseif (params["data"] == "graph") then
local values = get_values_for_graph(params["serial"])
return req:render{ json = { values } }
end


return req:render{ json = { none_data = "true" } }
end

Expand Down Expand Up @@ -124,9 +147,11 @@ local function database_init()
{name='value', type='number'}, --3
}
storage = box.schema.space.create('storage', {if_not_exists = true, format = format})
storage:create_index('serial', {parts = {'serial'}, if_not_exists = true})
storage:create_index('timestamp', {parts = {'timestamp'}, if_not_exists = true})
storage:create_index('serial', {parts = {'serial'}, unique = false, if_not_exists = true})
end


--//-----------------------------------------------------------------------//--

database_init()
Expand Down
42 changes: 37 additions & 5 deletions templates/dashboard.html
Expand Up @@ -47,7 +47,8 @@
</form>

<br>
<h3>Sensors:</h3>
<h3>Sensor 28-000008e538e6:</h3>
<div id="chart_div" style="width: 100%; height: 300px;"></div>
<div class="table-responsive">
<table class="table table-striped table-sm" id="table_values_temp"></table>
</div>
Expand Down Expand Up @@ -144,20 +145,51 @@ <h3>Sensors:</h3>
var json_data = JSON.parse(xhr_tmr.responseText);
if (json_data.none_data != "true") {
clear_table("table_values_temp")
add_row_table("table_values_temp", "head", ["Sensor serial", "Temperature", "Update time"])
add_row_table("table_values_temp", "head", ["Time", "Temperature"])
for (let index = 0; index < json_data[0].length; index++) {
add_row_table("table_values_temp", "body", [json_data[0][index].sensor, json_data[0][index].temperature, json_data[0][index].update_time_text])
add_row_table("table_values_temp", "body", [json_data[0][index].time_text, json_data[0][index].temperature])
}
}
}
}

function timer_update_field() {
xhr_tmr.onreadystatechange = update_table_callback
xhr_tmr.open('POST', 'data?type=temperature', true);
xhr_tmr.open('POST', 'data?data=table&serial=28-000008e538e6', true);
xhr_tmr.send()
}
setInterval(timer_update_field, 1000);
setInterval(timer_update_field, 3000);

</script>


<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', { 'packages': ['corechart'] });
google.charts.setOnLoadCallback(timer_update_graph);


var xhr_graph = new XMLHttpRequest();

function update_graph_callback() {
let data_b = JSON.parse(xhr_graph.responseText);
var data = google.visualization.arrayToDataTable(data_b[0]);

var options = {
title: 'Temperatype',
hAxis: { title: 'Time', titleTextStyle: { color: '#333' } },
};

var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
chart.draw(data, options);
}

function timer_update_graph() {
xhr_graph.onreadystatechange = update_graph_callback
xhr_graph.open('POST', 'data?data=graph&serial=28-000008e538e6', true);
xhr_graph.send()
setTimeout(timer_update_graph, 3000);
}

</script>

Expand Down

0 comments on commit 10ed490

Please sign in to comment.