Johannes Müller 3 år sedan
förälder
incheckning
521640cc66

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

@@ -0,0 +1,6 @@
+<?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/SearchIndex/segments.gen


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


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


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


+ 2 - 1
box-pc/application/database/csvFile.py

@@ -3,6 +3,7 @@ import os
 from datetime import datetime
 import dataclasses
 import zipfile
+import logging
 
 from structures.measurement import Measurement24v
 
@@ -45,7 +46,7 @@ class CSVFile:
         self.new_file()
         self.row_count = 0
     except Exception as ex:
-      print("CSV write failed", ex)
+      logging.exception("CSV write failed")
 
 def dataclass_to_dict(dc):
   ret = {}

+ 3 - 1
box-pc/application/database/influxdb.py

@@ -1,3 +1,5 @@
+import logging
+
 from influxdb_client import InfluxDBClient, Point
 from influxdb_client.client.write_api import SYNCHRONOUS
 import dataclasses
@@ -27,4 +29,4 @@ class InfluxDB:
     try:
       self.write_api.write(bucket=self.bucket, record=points)
     except Exception as ex:
-      print("Influx DB write failed", ex)
+      logging.exception("Influx DB write failed")

+ 7 - 1
box-pc/application/inputs/common.py

@@ -2,6 +2,7 @@ from threading import Thread
 from queue import Queue
 import time
 import struct
+import logging
 
 from structures.measurement import *
 
@@ -36,7 +37,12 @@ class Input:
   def _main(self):
     start_time = time.monotonic()
     while not self._stop:
-      self._read_cb()
+      try:
+        self._read_cb()
+      except Exception as e:
+        logging.exception(F"An exception occured while reading from {type(self)}!")
+        time.sleep(1)
+
       end_time = time.monotonic()
       remaining = self.interval + start_time - end_time
       start_time = end_time

+ 3 - 2
box-pc/application/inputs/snap7_connect.py

@@ -19,15 +19,16 @@ class SiemensCPU(Input):
 
   def start(self):
     self.cpu.connect(self.address, rack=0, slot=0)
+    self.cpu.get_connected()
     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=2, start=0, size=4)
+    status = self.cpu.db_read(db_number=3, start=0, size=4)
 
-    hydraulics_powered = status & 1 > 0
+    hydraulics_powered = status[0] & 1 > 0
 
     data = struct.unpack(">BBBB", status)
 

+ 3 - 3
box-pc/application/inputs/snap7_server.py

@@ -1,6 +1,6 @@
 from psutil import cpu_times
 import snap7
-import time
+import logging
 import struct
 import re
 from datetime import datetime, tzinfo
@@ -30,11 +30,11 @@ class SiemensServer(Input):
       text = self.server.event_text(event)
       match = re.match("^(?P<datetime>\d+-\d+-\d+ \d+:\d+:\d+) \[(?P<host>[\w\.:]+)\] (?P<type>[\w ]+), Area : (?P<area>.+), Start : (?P<start>\d+), Size : (?P<size>\d+) --> (?P<response>.+)$", text)
       if not match:
-        print(text)
+        logging.info(text)
         continue
       
       if match.group("type") != "Write request":
-        print(text)
+        logging.info(text)
         continue
       
       if int(match.group("start")) + int(match.group("size")) <= 4:

+ 10 - 2
box-pc/application/main.py

@@ -3,10 +3,12 @@ from inputs.snap7_connect import SiemensCPU
 from inputs.balluff_html import Balluff
 from inputs.allen_bradley_connect import AllenBradleyCPU
 from database import *
-from datetime import datetime
+import logging
 import time
 
-print("starting")
+logging.basicConfig(level=logging.WARNING)
+
+logging.info("starting")
 
 sources = [
   SiemensCPU("192.168.10.6"),
@@ -15,14 +17,19 @@ 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()
 
+logging.info("started sources")
 
 startTime = 0
 
@@ -56,4 +63,5 @@ while True:
   for sink in sinks:
     sink.write(values)
 
+  printStats(values)
   time.sleep(1)