Forráskód Böngészése

fix tcp connection handling

Johannes Müller 3 éve
szülő
commit
1626783cf2

+ 0 - 6
AI-energy-meter-Project/AI-energy-meter-Project.info

@@ -1,6 +0,0 @@
-<?xml version="1.0"?>
-<InfoFile xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-  <User>Jomueller</User>
-  <Computer>C2404</Computer>
-  <Time>2022-04-20T13:01:06.368863Z</Time>
-</InfoFile>

BIN
AI-energy-meter-Project/IM/SPL/options


BIN
AI-energy-meter-Project/IM/SearchIndex/_tj_1.del


BIN
AI-energy-meter-Project/IM/SearchIndex/_tk.cfs


BIN
AI-energy-meter-Project/IM/SearchIndex/_tk_1.del


BIN
AI-energy-meter-Project/IM/SearchIndex/_tl.cfs


BIN
AI-energy-meter-Project/IM/SearchIndex/_tl_1.del


BIN
AI-energy-meter-Project/IM/SearchIndex/_tm.cfs


BIN
AI-energy-meter-Project/IM/SearchIndex/segments.gen


BIN
AI-energy-meter-Project/IM/SearchIndex/segments_l9


BIN
AI-energy-meter-Project/IM/SearchIndex/segments_lc


+ 0 - 0
AI-energy-meter-Project/IM/SearchIndex/write.lock


BIN
AI-energy-meter-Project/System/PEData.idx


BIN
AI-energy-meter-Project/System/PEData.plf


BIN
AI-energy-meter-Project/System/~PEData.1


+ 23 - 4
box-pc/application/inputs/snap7_connect.py

@@ -1,8 +1,11 @@
-from snap7.client import Client
 import time
 from datetime import datetime
 import struct
 
+from snap7.client import Client
+from snap7.exceptions import Snap7Exception
+from snap7.types import Areas
+
 from structures.plant import *
 from inputs.common import Input
 
@@ -18,19 +21,35 @@ class SiemensCPU(Input):
     self.cpu = Client()
 
   def start(self):
-    self.cpu.connect(self.address, rack=0, slot=0)
-    self.cpu.get_connected()
+    try:
+      self.cpu.connect(self.address, rack=0, slot=0)
+    except Snap7Exception:
+      pass
     super().start()
 
   def read_handler(self):
     timestamp = datetime.now(localtz)
     cpu_state = self.cpu.get_cpu_state() == "S7CpuStatusRun"
 
-    status = self.cpu.db_read(db_number=3, start=0, size=4)
+    if self.cpu.get_connected():
+      try:
+        status = self.cpu.read_area(area=Areas.DB, dbnumber=3, start=0, size=4)
+        #inputs = self.cpu.read_area(area=Areas.PE, dbnumber=0, start=32, size=112-32)
+      except Snap7Exception as ex:
+        if "TCP" in str(ex):
+          self.cpu.disconnect()
+          return
+        else:
+          raise ex
+    else:
+      self.cpu.disconnect()
+      self.cpu.connect(self.address, rack=0, slot=0)
+      return
 
     hydraulics_powered = status[0] & 1 > 0
 
     data = struct.unpack(">BBBB", status)
+    #print(''.join(["{:02X}".format(x) for x in inputs]))
 
     cooling_enabled = tuple([data[1] & (1 << i) > 0 for i in range(8)])
     heating_enabled = tuple([data[2] & (1 << i) > 0 for i in range(8)])

+ 3 - 7
box-pc/application/main.py

@@ -17,15 +17,11 @@ sources = [
   AllenBradleyCPU("192.168.10.5"),
 ]
 
-logging.info("initialized sources")
-
 sinks = [
   InfluxDB("http://localhost:8086"),
   CSVFile("logs"),
 ]
 
-logging.info("initialized sinks")
-
 for source in sources:
   source.start()
 
@@ -49,9 +45,9 @@ def printStats(values):
     ids = list(counts.keys())
     ids.sort()
     for id in ids:
-      text += "{}: {:4d} Messungen in {:.03f} s, {:.3f} pro Sekunde    ".format(id, counts[id], dt, counts[id] / dt)
+      text += "{}: {:4d} in {:.03f}s, {:.1f}/s    ".format(id, counts[id], dt, counts[id] / dt)
   else:
-    text = "0 Messungen in {:.03f} s                          ".format(dt)
+    text = "0 Messungen in {:.03f}s               ".format(dt)
 
   print(text, end='\r')
 
@@ -64,4 +60,4 @@ while True:
     sink.write(values)
 
   printStats(values)
-  time.sleep(1)
+  time.sleep(1.9)