|
@@ -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)])
|