ctucx.git: smartied

[nimlang] smarthome server

commit 5e443b37c3f245d200eff6abf08ebf15ba3597de
parent 2f540e4a04b9ae36bd14c52d181f6e1e1055d090
Author: Milan Pässler <me@pbb.lc>
Date: Fri, 27 Sep 2019 21:06:58 +0200

retry
1 file changed, 20 insertions(+), 8 deletions(-)
M
src/modbus.nim
|
28
++++++++++++++++++++--------
diff --git a/src/modbus.nim b/src/modbus.nim
@@ -18,17 +18,18 @@ proc readPacket_mbTcp(): Future[(uint16, string)] {.async.} =
   var res = ""
 
   res = await sock.recv(8)
-  if res == "":
-    raise newException(OsError, "verbindung putt")
+  while res == "":
+	  echo "verbindung putt, ich fix das mal"
+    sock = await asyncnet.dial(server.config.modbusAddr, Port(server.config.modbusPort))
 
   let transaction_id = fromHex[uint16](toHex(res[0..1]))
   let length = fromHex[uint16](toHex(res[4..5]))
   let function_code = cast[uint8](res[7])
 
   res = await sock.recv(int(length) - 2)
-  if res == "":
-    raise newException(OsError, "verbindung putt")
-
+  while res == "":
+	  echo "verbindung putt, ich fix das mal"
+    sock = await asyncnet.dial(server.config.modbusAddr, Port(server.config.modbusPort))
 
   echo toHex(res)
   if function_code >= 128u8:

@@ -51,10 +52,21 @@ proc doRequest[T](req: string, parse_proc: proc(foo: string): T): Future[T] =
   let tcp_packet = mkPacket_mbTcp(req)
   asyncCheck sock.send(tcp_packet)
   transactions[transaction_id] = proc(answer: string) =
+    transactions.del(transaction_id)
     fut.complete(parse_proc(answer))
 
   return fut
 
+proc retry[T](req: string, parse_proc: proc(foo: string): T): Future[T] {.async.} =
+  var retries = 5
+  var result: T
+
+  while retries > 0:
+    retries = retries - 1
+    result = await doRequest(req, parse_proc)
+
+  return result
+
 ### readInputRegisters ###
 
 proc mkPacket_readInputRegisters(unit_id: uint8, address: uint16, count: uint16): string =

@@ -70,7 +82,7 @@ proc parsePacket_readInputRegisters(packet: string): seq[uint16] =
   return res
 
 proc readInputRegisters*(unit_id: uint8, address: uint16, count: uint16): Future[seq[uint16]] {.async.} =
-  return await doRequest(mkPacket_readInputRegisters(unit_id, address, count), parsePacket_readInputRegisters)
+  return await retry(mkPacket_readInputRegisters(unit_id, address, count), parsePacket_readInputRegisters)
 
 ### readRegisters ###
 

@@ -87,7 +99,7 @@ proc parsePacket_readRegisters(packet: string): seq[uint16] =
   return res
 
 proc readRegisters*(unit_id: uint8, address: uint16, count: uint16): Future[seq[uint16]] {.async.} =
-  return await doRequest(mkPacket_readRegisters(unit_id, address, count), parsePacket_readRegisters)
+  return await retry(mkPacket_readRegisters(unit_id, address, count), parsePacket_readRegisters)
 
 ### writeRegister ###
 

@@ -98,7 +110,7 @@ proc parsePacket_writeRegister(packet: string): bool =
   return true
 
 proc writeRegister*(unit_id: uint8, address: uint16, value: uint16): Future[bool] {.async.} =
-  return await doRequest(mkPacket_writeRegister(unit_id, address, value), parsePacket_writeRegister)
+  return await retry(mkPacket_writeRegister(unit_id, address, value), parsePacket_writeRegister)
 
 ### conversion ###