From fb0aab674e56ee11185f58a6ebc43078d9131e6a Mon Sep 17 00:00:00 2001 From: mvoelkel Date: Thu, 24 Sep 2015 16:20:44 +0200 Subject: [PATCH] Temperature monitor up to 4 sensors display if there is no sensor available fixed a race condition --- octoserve/var/monitor/fancontrol.lua | 10 +++- octoserve/var/www/monitor.html | 79 +++++++++++++++++++--------- octoserve/var/www/monitor.lua | 4 +- 3 files changed, 65 insertions(+), 28 deletions(-) diff --git a/octoserve/var/monitor/fancontrol.lua b/octoserve/var/monitor/fancontrol.lua index 52c473d..785dde0 100755 --- a/octoserve/var/monitor/fancontrol.lua +++ b/octoserve/var/monitor/fancontrol.lua @@ -71,6 +71,13 @@ for i = 0,4,1 do NumSensors = NumSensors + 1 end end + +local fh = io.open(LogFile,"w") +if fh then + fh:write(NumSensors..","..interval..","..fanstate.."\n") + fh:close() +end + if NumSensors == 0 then return end @@ -116,7 +123,7 @@ while true do TmpLogFile = os.tmpname() - local fh = io.open(TmpLogFile,"w") + fh = io.open(TmpLogFile,"w") if fh then fh:write(NumSensors..","..interval..","..fanstate.."\n") @@ -135,4 +142,3 @@ while true do end collectgarbage() end - diff --git a/octoserve/var/www/monitor.html b/octoserve/var/www/monitor.html index d90e9d9..a9b31f7 100644 --- a/octoserve/var/www/monitor.html +++ b/octoserve/var/www/monitor.html @@ -13,9 +13,14 @@ var xmlhttp = new XMLHttpRequest(); var url = "/monitor.lua"; -xmlhttp.onreadystatechange=function() { - if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { - myFunction(xmlhttp.responseText); +xmlhttp.onreadystatechange=function() +{ + if (xmlhttp.readyState == 4 ) + { + if( xmlhttp.status == 200 ) + myFunction(xmlhttp.responseText); + else if( xmlhttp.status == 204 ) + window.setTimeout(Reload,1000); } } @@ -27,31 +32,42 @@ function Reload() function myFunction(response) { var Sensor = JSON.parse(response); + var j; if( Sensor.NumSensors > 0 ) { - var pts = document.getElementById("pts").points; - var svgRoot = document.getElementById("svg"); - - var i; - var v = Sensor.SensorData[0]; - var n = v.length; - var i0 = 0; - if( n > 120 ) + if( Sensor.NumSensors > 4 ) Sensor.NumSensors = 4; + for( j = 0; j < Sensor.NumSensors; j += 1 ) { - i0 = n - 120; - n = 120; - } - pts.clear(); - for(i = 0; i < n; i += 1) - { - var pt = svgRoot.createSVGPoint(); - pt.x = (i+1) * 20; - pt.y = v[i+i0] * 10; - pts.appendItem(pt); + var id = "pts"+j + var pts = document.getElementById(id).points; + var svgRoot = document.getElementById("svg"); + + var i; + var v = Sensor.SensorData[j]; + var n = v.length; + var i0 = 0; + if( n > 120 ) + { + i0 = n - 120; + n = 120; + } + pts.clear(); + for(i = 0; i < n; i += 1) + { + var pt = svgRoot.createSVGPoint(); + pt.x = (i+1) * 20; + pt.y = v[i+i0] * 10; + pts.appendItem(pt); + } } window.setTimeout(Reload,10000); } + else + { + document.getElementById("TGraph").style.display = "none"; + document.getElementById("NoSensor").style.display = "block"; + } } @@ -91,9 +107,13 @@ function OnLoad() -->

Frontend Temperature

+ + - -
+
Temperature @@ -147,9 +167,18 @@ function OnLoad() - + style="stroke:#C00000; stroke-width: 3; fill : none;"/> + + + diff --git a/octoserve/var/www/monitor.lua b/octoserve/var/www/monitor.lua index b990402..c0af731 100644 --- a/octoserve/var/www/monitor.lua +++ b/octoserve/var/www/monitor.lua @@ -43,6 +43,7 @@ end if method == "GET" then data = "" + status = " 204" local file = io.open("/tmp/Temperatur.log") if file then @@ -84,10 +85,11 @@ if method == "GET" then end data = data .. "]\n" data = data .. "}\n" + status = "200" end - http_print(proto.." 200" ) + http_print(proto.." "..status ) http_print("Pragma: no-cache") http_print("Content-Type: application/json; charset=UTF-8") http_print(string.format("Content-Length: %d",#data))