From f47ec7270dec86233086b8c61b30bd40b98700e8 Mon Sep 17 00:00:00 2001 From: Quentin Durand Date: Thu, 19 Jul 2018 17:12:25 +0200 Subject: [PATCH] hotfix monitoring --- simulators/plc/plcSimulator.py | 1 - src/devices/Device.py | 5 +++-- src/devices/PLC.py | 8 +++++--- src/monitoring/tasks.py | 21 +++++++++++++-------- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/simulators/plc/plcSimulator.py b/simulators/plc/plcSimulator.py index 366b7f0..d5285d3 100755 --- a/simulators/plc/plcSimulator.py +++ b/simulators/plc/plcSimulator.py @@ -169,7 +169,6 @@ class PLCSimulator(DeviceSim, StatusManager): def loop(self): i = 0 - if (self.EVENT_GENERATOR and self.ended==0): self.plcPrint("No entry for PLC found in config file : " + self.config_file) return False diff --git a/src/devices/Device.py b/src/devices/Device.py index 5d250da..aecd3f7 100644 --- a/src/devices/Device.py +++ b/src/devices/Device.py @@ -103,8 +103,9 @@ class DeviceController(): try: readable, writable, exceptional = select.select([self.sock], [], [self.sock], 0) if not (readable or exceptional): - ret = self.sock.recv(size).decode() - raise (Exception("KO: " + ret)) + #ret = self.sock.recv(size).decode() + raise (Exception("KO: socket error")) + ret = self.sock.recv(size).decode() if (not ret): if (settings.DEBUG): diff --git a/src/devices/PLC.py b/src/devices/PLC.py index 7b89341..aad117e 100644 --- a/src/devices/PLC.py +++ b/src/devices/PLC.py @@ -21,8 +21,10 @@ class PLCController(DeviceController): def sendCommandWithAnswer(self, dict_list): # Send commmand TO plc status = self.sendCommand(dict_list) + #EP added - if not status: return "NOT_SET1" + if not status: + return "NOT_SET1" # Read result FROM plc return (self.blockAndReadBytes(4096)) @@ -30,8 +32,8 @@ class PLCController(DeviceController): return self.sendCommandWithAnswer([{"name":"LIST"}]) def getStatus(self): - return self.sendCommandWithAnswer([{"name":"STATUS"}]) - + s = self.sendCommandWithAnswer([{"name":"STATUS"}]) + return s ''' Value is a string ("on" or "off"), current is a decimal value ("0.231") ''' diff --git a/src/monitoring/tasks.py b/src/monitoring/tasks.py index ebdec59..7ed46e6 100644 --- a/src/monitoring/tasks.py +++ b/src/monitoring/tasks.py @@ -160,6 +160,7 @@ class Monitoring(Task): def extractFromDict(self, status): synthesis = {} devices = status["device"] + #print(devices) #is_safe_str = status["is_safe"] #mode = status["mode"] for device in devices: @@ -170,7 +171,8 @@ class Monitoring(Task): # TODO ATTENTION SI DEUX DEVICES ONT LE MEME NOM def saveContent(self, content): - devices = content["device"] + devices = content[0]["device"] + for device in devices: status = PlcDeviceStatus() try: @@ -192,15 +194,16 @@ class Monitoring(Task): def parseStatus(self, status_plc): try: status = {} - dict = json.loads(status_plc)[0] - if dict["name"] == "STATUS": + dict = json.loads(status_plc) + if dict[0]["name"] == "STATUS": if self.isStatusValid(): - status = self.extractFromDict(dict) + status = self.extractFromDict(dict[0]) self.saveContent(dict) else: # TODO HANDLE ERROR HERE pass except Exception as e: + if DEBUG_FILE and settings.DEBUG: log.info(str(e)) self.status_plc = {} @@ -254,15 +257,18 @@ class Monitoring(Task): # Reset timer total duration self.timers["timer_status"] = self.timer_status status_plc = self.plcController.getStatus() - + #print("status ->" + status_plc) + # Error while READING status ? if (self.plcController.isError(status_plc)): + if (settings.DEBUG and DEBUG_FILE): log.info("Invalid PLC status returned (while reading) : " + str(status_plc)) # EP Non car 1 = true #return (1) + return False - + # (EP) if parseStatus() = THERE WAS AN ERROR !!! # Error while PARSING status ? if self.parseStatus(status_plc): @@ -271,7 +277,6 @@ class Monitoring(Task): # EP Non car 1 = true #return (1) return False - print("Status received from PLC (read and parsed ok):") print(status_plc) #return self.saveStatus() @@ -284,7 +289,7 @@ class Monitoring(Task): Main loop ''' def loop(self): - i=0 + i = 0 while (self.state != "SHUTDOWN"): print("-") print("-") -- libgit2 0.21.2