ctucx.git: smartied

[nimlang] smarthome server

commit fdb7eb38005b3e0a20b1c33bc92e329e8de966fe
parent 2f41e31293896d9310b721b6ba5d7088c0094f5a
Author: ctucx <ctucx@git.ctu.cx>
Date: Sun, 19 Jan 2020 19:06:37 +0000

frontend_prometheus: fixes
1 file changed, 17 insertions(+), 12 deletions(-)
M
src/frontend_prometheus.nim
|
29
+++++++++++++++++------------
diff --git a/src/frontend_prometheus.nim b/src/frontend_prometheus.nim
@@ -7,13 +7,14 @@ import json
 import vars
 import tables
 
-proc addVal(resp: var string, name: string, key: string, val: string, lastUpdated: string) =
+proc addVal(resp: var string, name: string, key: string, val: string, lastUpdated: int64) =
   resp = resp & name & "{"
   resp = resp & "device=\"" & key & "\""
   resp = resp & "} "
   resp = resp & val
-  resp = resp & " "
-  resp = resp & lastUpdated
+  if lastUpdated != 0:
+    resp = resp & " "
+    resp = resp & $(lastUpdated * 1000)
   resp = resp & "\n"
 
 proc fmtBool(b: bool): string =

@@ -28,20 +29,24 @@ proc processPrometheusClient(req: Request) {.async,gcsafe.} =
       var resp = ""
       for key, device in server.config.devices.pairs():
         if device.type == PowerMeter:
-          resp.addVal("powermeter_import", key, $(server.state[key].import), $(server.state[key].lastUpdated * 1000))
-          resp.addVal("powermeter_cosphi", key, $(server.state[key].cosphi), $(server.state[key].lastUpdated * 1000))
-          resp.addVal("powermeter_power", key, $(server.state[key].power), $(server.state[key].lastUpdated * 1000))
-          resp.addVal("powermeter_frequency", key, $(server.state[key].frequency), $(server.state[key].lastUpdated * 1000))
-          resp.addVal("powermeter_voltage", key, $(server.state[key].voltage), $(server.state[key].lastUpdated * 1000))
+          if server.state[key].frequency == 0:
+            continue
+          resp.addVal("powermeter_import", key, $(server.state[key].import), server.state[key].lastUpdated)
+          resp.addVal("powermeter_cosphi", key, $(server.state[key].cosphi), server.state[key].lastUpdated)
+          resp.addVal("powermeter_power", key, $(server.state[key].power), server.state[key].lastUpdated)
+          resp.addVal("powermeter_frequency", key, $(server.state[key].frequency), server.state[key].lastUpdated)
+          resp.addVal("powermeter_voltage", key, $(server.state[key].voltage), server.state[key].lastUpdated)
 
         if device.type == RelayBoard:
           for i, val in server.state[key].relays:
-            resp.addVal("relayboard_relay", key & "\",relay=\"" & $(i), fmtBool(val), "")
+            resp.addVal("relayboard_relay", key & "\",relay=\"" & $(i), fmtBool(val), 0)
 
         if device.type == LacrosseTempSensor:
-          resp.addVal("lacrossetempsensor_temperature", key, $(server.state[key].temperature), $(server.state[key].lastUpdated2 * 1000))
-          resp.addVal("lacrossetempsensor_humidity", key, $(server.state[key].humidity), $(server.state[key].lastUpdated2 * 1000))
-          resp.addVal("lacrossetempsensor_weakbattery", key, fmtBool(server.state[key].weakBattery), $(server.state[key].lastUpdated2 * 1000))
+          resp.addVal("lacrossetempsensor_temperature", key, $(server.state[key].temperature), server.state[key].lastUpdated2)
+          resp.addVal("lacrossetempsensor_weakbattery", key, fmtBool(server.state[key].weakBattery), server.state[key].lastUpdated2)
+          if server.state[key].humidity < 0:
+            continue
+          resp.addVal("lacrossetempsensor_humidity", key, $(server.state[key].humidity), server.state[key].lastUpdated2)
 
       await req.respond(Http200, resp)
     else: