|
@@ -1,13 +1,14 @@
|
|
|
-import snap7
|
|
|
+from snap7.client import Client
|
|
|
import time
|
|
|
from datetime import datetime
|
|
|
import struct
|
|
|
|
|
|
-from structures.measurement import Measurement
|
|
|
+from structures.measurement import CurrentMeasurement
|
|
|
+from inputs.common import Input
|
|
|
|
|
|
localtz = datetime.now().astimezone().tzinfo
|
|
|
|
|
|
-class SiemensCPU(snap7.client.Client):
|
|
|
+class SiemensCPU(Input):
|
|
|
|
|
|
cpu_start_time = None
|
|
|
cpu_last_time = None
|
|
@@ -17,10 +18,28 @@ class SiemensCPU(snap7.client.Client):
|
|
|
|
|
|
cpu_db_value_count = 50
|
|
|
|
|
|
- def get_ints(self, start, count):
|
|
|
- raw = self.db_read(self.db, start*4, count*4)
|
|
|
- data = struct.unpack(">" + "i" * count, raw)
|
|
|
- return data
|
|
|
+ def __init__(self) -> None:
|
|
|
+ super().__init__(self.read_handler)
|
|
|
+ self.cpu = Client()
|
|
|
+
|
|
|
+ def start(self):
|
|
|
+ self.cpu.connect("10.0.10.1", 0, 0)
|
|
|
+ super().start()
|
|
|
+
|
|
|
+ def read_handler(self):
|
|
|
+ raw = self.cpu.db_read(self.db, 0, (self.cpu_db_value_count + 1)*4)
|
|
|
+ data = struct.unpack(">" + "i" * (self.cpu_db_value_count + 1), raw)
|
|
|
+
|
|
|
+ cpu_time = data[0] / 1000
|
|
|
+ if not self.cpu_last_time:
|
|
|
+ self.cpu_last_time = cpu_time - self.interval
|
|
|
+ if cpu_time == self.cpu_last_time:
|
|
|
+ return
|
|
|
+ inc_time = (cpu_time - self.cpu_last_time) / self.cpu_db_value_count
|
|
|
+ for i, val in enumerate(data[1:]):
|
|
|
+ timestamp = self.get_timestamp(self.cpu_last_time + inc_time * (i+1))
|
|
|
+ self._q.put(CurrentMeasurement(timestamp, "S7", 0, val / 10))
|
|
|
+ self.cpu_last_time = cpu_time
|
|
|
|
|
|
def get_timestamp(self, cpu_time):
|
|
|
if not self.cpu_start_time:
|
|
@@ -46,24 +65,4 @@ class SiemensCPU(snap7.client.Client):
|
|
|
self.interval = sum(intervals) / len(intervals)
|
|
|
return self.interval
|
|
|
|
|
|
- def read(self):
|
|
|
- data = self.get_ints(0, self.cpu_db_value_count + 1)
|
|
|
- cpu_time = data[0] / 1000
|
|
|
- if not self.cpu_last_time:
|
|
|
- self.cpu_last_time = cpu_time - self.interval
|
|
|
- if cpu_time == self.cpu_last_time:
|
|
|
- return []
|
|
|
- inc_time = (cpu_time - self.cpu_last_time) / self.cpu_db_value_count
|
|
|
- points = []
|
|
|
- for i, val in enumerate(data[1:]):
|
|
|
- timestamp = self.get_timestamp(self.cpu_last_time + inc_time * (i+1))
|
|
|
- points.append(Measurement(timestamp, "siemens_cpu", 0, val / 10))
|
|
|
- self.cpu_last_time = cpu_time
|
|
|
- return points
|
|
|
-
|
|
|
- def read_continous(self):
|
|
|
- while True:
|
|
|
- points = self.read()
|
|
|
- for point in points:
|
|
|
- yield point
|
|
|
|