import os import sys import json DEBUG_FILE = False PACKAGE_PARENT = '..' SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__)))) sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT))) from utils.Device import Device from utils.StatusManager import StatusManager class PLCSimulator(Device, StatusManager): status = { "name": "STATUS", "from": "Beckhoff", "version_firmware": "20170809", "site": "OSM-Mexico", "date": "2017-03-03T13:45:00", "device": [{"name": "WXT520", "type": "meteo", "serial_number": "14656423", "valid": "yes", "values": [ {"name": "OutsideTemp", "value": 12.12, "unit": "Celcius", "comment": ""}, {"name": "OutsideHumidity", "value": 64.1, "unit": "percent", "comment": ""}, {"name": "Pressure", "value": 769.2, "unit": "mbar", "comment": "At site elevation"}, {"name": "RainRate", "value": 0.0, "unit": "mm/h", "comment": ""}, {"name": "WindSpeed", "value": 3.1, "unit": "m/s", "comment": ""}, {"name": "WindDir", "value": 202.3, "unit": "deg", "comment": "(N=0, E=90)"}, {"name": "DewPoint", "value": 8.3, "unit": "deg", "comment": ""} ] }, {"name": "DRD11", "type": "meteo", "serial_number": "RET6789", "valid": "yes", "values": [ {"name": "analog", "value": 2.345, "unit": "V", "comment": "3V=Dry <2.5V=Rain"}, {"name": "digital", "value": 1, "unit": "bool", "comment": "1=Dry 0=Rain"} ] }, {"name": "VantagePro", "type": "meteo", "serial_number": "ERTRY2344324", "valid": "no", "values": [ {"name": "OutsideTemp", "value": 12.45, "unit": "Celcius", "comment": ""}, {"name": "InsideTemp", "value": 20.28, "unit": "Celcius", "comment": ""}, {"name": "OutsideHumidity", "value": 65.3, "unit": "percent", "comment": ""}, {"name": "InsideHumidity", "value": 45.6, "unit": "percent", "comment": ""}, {"name": "Pressure", "value": 768.7, "unit": "mbar", "comment": "At site elevation"}, {"name": "RainRate", "value": 0.0, "unit": "mm/h", "comment": ""}, {"name": "WindSpeed", "value": 2.8, "unit": "m/s", "comment": ""}, {"name": "WindDir", "value": 207.0, "unit": "deg", "comment": "(N=0, E=90)"}, {"name": "WindDirCardinal", "value": "SW", "unit": "string", "comment": ""}, {"name": "DewPoint", "value": 8.5, "unit": "deg", "comment": ""} ] }, {"name": "MLX90614-1", "type": "meteo", "serial_number": "Unknown", "valid": "yes", "values": [ {"name": "SensorTemperature", "value": 23.56, "unit": "Celcius", "comment": ""}, {"name": "SkyTemperature", "value": -15.67, "unit": "Celcius", "comment": "Clear<-10 VeryCloudy>4"} ] }, {"name": "LAMP_FLAT_CAGIRE", "type": "calib", "serial_number": "REF_3434", "valid": "yes", "values": [ {"name": "status", "value": "on", "unit": "string", "comment": "on or off"}, {"name": "current", "value": 0.234, "unit": "Ampere", "comment": ""} ] }, {"name": "LAMP FLAT CAGIRE", "type": "calib", "serial_number": "REF_3434", "valid": "yes", "values": [ {"name": "status", "value": "on", "unit": "string", "comment": "on or off"}, {"name": "current", "value": 0.234, "unit": "Ampere", "comment": ""} ] } ] } list = {"name": "LIST", "from": "Beckhoff", "version_firmware": "20170809", "site": "OSM-Mexico", "date": "2017-03-03T13:42:46", "command": [ {"name": "LIST", "type": "", "parameters": "" }, {"name": "SATUS", "type": "", "parameters": "" }, {"name": "LAMP_FLAT_CAGIRE", "type": "calib", "parameters": [ {"name": "value", "unit": "string", "comment": "on or off", "default": ""}, {"name": "current", "unit": "Ampere", "comment": "", "default": "0.345"} ] } ] } def __init__(self, argv): super().__init__(argv) self.setDevice("PLC") self.setStatusManager("plcSimulator", argv) def plcPrint(self, string: str): if DEBUG_FILE: print("PLCSimulator : " + string) def updateStatus(self, i: int): for dic in self.sims: if (int(dic["time"]) == i): change = dic[self.status_name] device_name = change["device_name"] devices = self.status["device"] for device in devices: if device["name"] == device_name: values = device["values"] for value in values: if value["name"] == change["value_name"]: value["value"] = change["value"] return 0 def handleConnections(self): for co in self.connections: data = self.readBytes(1024, co) answer = "[" count = False json_array = json.loads(data) command_list = json_array["command"] for command in command_list: if command["name"] == "STATUS": if count: answer += ',' answer += json.dumps(self.status) count = True elif command["name"] == "LIST": if count: answer += ',' answer += json.dumps(self.status) count = True else: print("command received by PLC simulator : [>>> "+ str(command["name"]) + " <<<] unknown") answer += ']' if len(answer) > 0 and answer != "[]": self.sendMessage(answer, co) return (0) def loop(self): i = 0 if (self.ended == 0): self.plcPrint("Not entry for telescope found in config file : " + self.config_file) return (0) while (True): self.updateStatus(i) if (self.isConnected()): self.handleConnections() i += 1 if (i > self.ended): return (0) return (1) def run(self): if DEBUG_FILE: print("PLC simulator running") self.parse() self.configSocket() self.loop() return (0) if __name__ == "__main__": sim = PLCSimulator(sys.argv) sim.run()