ctucx.git: smartied

[nimlang] smarthome server

commit 62ff18726a229422f99e4a19d3a054e4ab7da15d
parent 5dcb4e1245ceae7df282cadcb10c65116ca57af0
Author: Leah (ctucx) <leah@ctu.cx>
Date: Thu, 18 Feb 2021 19:14:43 +0100

fix timestamps for influx
2 files changed, 25 insertions(+), 21 deletions(-)
M
src/devices/lacrosseSensors.nim
|
2
+-
M
src/devices/modbusPowermeter.nim
|
44
++++++++++++++++++++++++--------------------
diff --git a/src/devices/lacrosseSensors.nim b/src/devices/lacrosseSensors.nim
@@ -43,7 +43,7 @@ proc lacrosseHandleLoop (sock: AsyncSocket) {.async.} =
 
             fields["temperature"] = $msg.temp.isaRound(2)
 
-            discard await config.insertDatabase(config.lacrosseDatabase.get, key, tags, fields, currentTimestamp)
+            discard await config.insertDatabase(config.lacrosseDatabase.get, key, tags, fields, currentTimestamp * 1000000000)
 
 
         broadcastServerState()
diff --git a/src/devices/modbusPowermeter.nim b/src/devices/modbusPowermeter.nim
@@ -2,37 +2,41 @@ import asyncdispatch, tables, times, options, tables
 import ../types, ../vars, ../modbus, ../influx, ../utils
 
 proc updatePowermeter (key: string, device: DeviceConfig) {.async.} =
-  let deviceAddress = device.address.get
+  let deviceAddress    = device.address.get
+  let currentTimestamp = toUnix(getTime())
 
-  let voltage = await mb.asyncReadFloat(deviceAddress, 0)
-  let frequency = await mb.asyncReadFloat(deviceAddress, 70)
-  let `import` = await mb.asyncReadFloat(deviceAddress, 72)
-  let cosphi = await mb.asyncReadFloat(deviceAddress, 30)
-  let power = await mb.asyncReadFloat(deviceAddress, 12)
-
-  server.state[key].voltage = voltage
-  server.state[key].frequency = frequency
-  server.state[key].`import` = `import`
-  server.state[key].cosphi = cosphi
-  server.state[key].power = power
-
-  server.state[key].lastUpdated = some(toUnix(getTime()))
+  let voltage   = (await mb.asyncReadFloat(deviceAddress, 0)).isaRound(2)
+  let frequency = (await mb.asyncReadFloat(deviceAddress, 70)).isaRound(2)
+  let `import`  = (await mb.asyncReadFloat(deviceAddress, 72)).isaRound(3)
+  let cosphi    = (await mb.asyncReadFloat(deviceAddress, 30)).isaRound(3)
+  let power     = (await mb.asyncReadFloat(deviceAddress, 12)).isaRound(2)
 
   if server.config.serverConfig.influx.isSome:
     let config = server.config.serverConfig.influx.get
 
     if config.powermeterDatabase.isSome:
+      if server.state[key].lastUpdated.isSome:
+        if (currentTimestamp - server.state[key].lastUpdated.get) < 30:
+          break
+
       var tags, fields = initTable[string, string]()
 
       tags["device"] = key
 
-      fields["voltage"]   = $voltage.isaRound(2)
-      fields["frequency"] = $frequency.isaRound(2)
-      fields["import"]    = $`import`.isaRound(3)
-      fields["cosphi"]    = $cosphi.isaRound(3)
-      fields["power"]     = $power.isaRound(2)
+      fields["voltage"]   = $voltage
+      fields["frequency"] = $frequency
+      fields["import"]    = $`import`
+      fields["cosphi"]    = $cosphi
+      fields["power"]     = $power
+
+      discard await config.insertDatabase(config.powermeterDatabase.get, key, tags, fields, currentTimestamp * 1000000000)
 
-      discard await config.insertDatabase(config.powermeterDatabase.get, key, tags, fields, server.state[key].lastUpdated.get)
+  server.state[key].voltage     = voltage
+  server.state[key].frequency   = frequency
+  server.state[key].`import`    = `import`
+  server.state[key].cosphi      = cosphi
+  server.state[key].power       = power
+  server.state[key].lastUpdated = some(currentTimestamp)
 
   broadcastServerState()