ctucx.git: smartied

[nimlang] smarthome server

commit daaedb68ee030901e1d51e8f20062fa4829b005a
parent 4bf8045fab79743f3d3313204762db84f2600492
Author: Milan Pässler <me@pbb.lc>
Date: Sat, 20 Jul 2019 08:12:10 +0200

foo
8 files changed, 18 insertions(+), 44 deletions(-)
A
src/backend_powermeter
|
0
M
src/backend_powermeter.nim
|
19
++++++++++++++-----
D
src/foo
|
0
D
src/foo.nim
|
12
------------
M
src/modbus.nim
|
4
++--
D
src/serial.nim
|
22
----------------------
D
src/serial.nim.cfg
|
2
--
M
src/types.nim
|
3
++-
diff --git a/src/backend_powermeter b/src/backend_powermeter  Binary files differ.
diff --git a/src/backend_powermeter.nim b/src/backend_powermeter.nim
@@ -5,13 +5,22 @@ import tables
 import util
 import json
 import vars
+import times
 
 proc updatePowermeter(key: string, device: DeviceConfig) {.async.} =
-  server.state[key].voltage = await mb.asyncReadFloat(device.address, 0)
-  server.state[key].frequency = await mb.asyncReadFloat(device.address, 70)
-  server.state[key].`import` = await mb.asyncReadFloat(device.address, 72)
-  server.state[key].cosphi = await mb.asyncReadFloat(device.address, 30)
-  server.state[key].power = await mb.asyncReadFloat(device.address, 12)
+  let voltage = await mb.asyncReadFloat(device.address, 0)
+  let frequency = await mb.asyncReadFloat(device.address, 70)
+  let `import` = await mb.asyncReadFloat(device.address, 72)
+  let cosphi = await mb.asyncReadFloat(device.address, 30)
+  let power = await mb.asyncReadFloat(device.address, 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 = toUnix(getTime())
 
   broadcast($(%*server.state))
 
diff --git a/src/foo b/src/foo  Binary files differ.
diff --git a/src/foo.nim b/src/foo.nim
@@ -1,12 +0,0 @@
-import asyncdispatch
-
-proc p1() {.async.} =
-  GC_fullCollect()
-  echo "foo"
-
-proc timerFunc(fd: AsyncFD): bool =
-  let fut = p1()
-
-addTimer(1, true, timerFunc)
-
-runForever()
diff --git a/src/modbus.nim b/src/modbus.nim
@@ -47,7 +47,7 @@ proc asyncReadFloat*(mb: modbus, ad: uint8, reg: uint8): Future[float32] {.async
   return await mb.retry(proc (): (cint, float32) =
       var data = [0u32]
       mb.modbus_set_slave(cint(ad))
-      let status = cint(0) #mb.modbus_read_input_registers(cint(reg), 2, data.addr)
+      let status = mb.modbus_read_input_registers(cint(reg), 2, data.addr)
       let res = modbus_get_float_dcba(data.addr)
       return (status, float32(res))
     )

@@ -63,7 +63,7 @@ proc asyncReadBits*(mb: modbus, ad: uint8, reg: uint8, nb: uint8): Future[seq[bo
   var data: array[256, bool]
   discard await mb.retry(proc (): (cint, bool) =
       mb.modbus_set_slave(cint(ad))
-      let status = cint(0) #mb.modbus_read_bits(cint(reg), cint(nb), data.addr)
+      let status = mb.modbus_read_bits(cint(reg), cint(nb), data.addr)
       return (status, false)
     )
   return @data[0..nb-1]
diff --git a/src/serial.nim b/src/serial.nim
@@ -1,21 +0,0 @@
-import asyncdispatch, asyncnet, os, threadpool
-
-proc asyncReadLine*(file: File): Future[string] =
-  var fut = newFuture[string]()
-  var flowVar = spawn file.readLine()
-
-  addTimer(50, false, proc(fd: AsyncFD): bool =
-      if flowVar.isReady():
-        fut.complete(^flowVar)
-        return true
-    )
-
-  return fut
-
-proc main() {.async.} =
-  let file = open("/dev/ttyS2", fmReadWrite)
-  while true:
-    let line = await file.asyncReadLine()
-    echo line
-
-waitFor main()-
\ No newline at end of file
diff --git a/src/serial.nim.cfg b/src/serial.nim.cfg
@@ -1 +0,0 @@
---threads:on-
\ No newline at end of file
diff --git a/src/types.nim b/src/types.nim
@@ -34,6 +34,7 @@ type DeviceState* = object
     voltage*: float32
     `import`*: float32
     frequency*: float32
+    lastUpdated*: int64
   of RelayBoard:
     relays*: seq[bool]
 

@@ -67,5 +68,5 @@ type Response* = object
   status*: ResponseStatus
   data*: JsonNode
 
-type modbus* = ref object
+type modbus* = pointer