Commit d18e437efb2dde22f47a86c089765fcf9e9ab89f
1 parent
c6f1b364
Exists in
dev
New json in
- plcSimulator feed fro logfile - handled in monotoring.tasks
Showing
4 changed files
with
47 additions
and
81 deletions
Show diff stats
simulators/plc/meteo_worker.py
... | ... | @@ -28,7 +28,7 @@ class WorkerThread(threading.Thread): |
28 | 28 | self.rain = False |
29 | 29 | self.log_file = "meteo_20180917.log" |
30 | 30 | self.log_input = open(self.log_file, 'r') |
31 | - self.time_step = 0.1 | |
31 | + self.time_step = 1 | |
32 | 32 | |
33 | 33 | # call by thread.start() |
34 | 34 | def run(self): |
... | ... | @@ -100,6 +100,11 @@ class WorkerThread(threading.Thread): |
100 | 100 | # update message structure |
101 | 101 | print(str(moment)+ ' '+ line[2]+ ' '+ line[4]+ ' '+ line[6]+ ' '+ line[7]+ ' '+ line[8]) |
102 | 102 | # resultat = get_device_attribute_value("WXT520","14656423"","OutsideTemp") |
103 | - self.msg_struct['device'][0]['values'][0]['value'] = line[2] | |
103 | + self.msg_struct['devices'][0]['device_values'][0]['value'] = line[2] #OutsideTemp | |
104 | + self.msg_struct['devices'][0]['device_values'][1]['value'] = line[4] #OutsideHumidity | |
105 | + self.msg_struct['devices'][0]['device_values'][2]['value'] = line[6] #Pressure | |
106 | + self.msg_struct['devices'][0]['device_values'][3]['value'] = line[7] #RainRate | |
107 | + self.msg_struct['devices'][0]['device_values'][4]['value'] = line[8] #WindSpeed | |
108 | + self.msg_struct['devices'][0]['device_values'][5]['value'] = line[9] #WindDir | |
104 | 109 | if line[7] != "0.0": |
105 | 110 | self.rain = True | ... | ... |
simulators/plc/plcSimulator.py
... | ... | @@ -3,6 +3,7 @@ |
3 | 3 | import os |
4 | 4 | import sys |
5 | 5 | import json |
6 | +import time | |
6 | 7 | |
7 | 8 | DEBUG_FILE = True |
8 | 9 | PACKAGE_PARENT = '..' |
... | ... | @@ -12,7 +13,7 @@ sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT))) |
12 | 13 | #(EP renamed) from utils.Device import Device |
13 | 14 | from utils.DeviceSim import DeviceSim |
14 | 15 | from utils.StatusManager import StatusManager |
15 | - | |
16 | +from simulators.plc.meteo_worker import WorkerState, WorkerThread | |
16 | 17 | |
17 | 18 | #(EP renamed) class PLCSimulator(Device, StatusManager): |
18 | 19 | class PLCSimulator(DeviceSim, StatusManager): |
... | ... | @@ -33,7 +34,17 @@ class PLCSimulator(DeviceSim, StatusManager): |
33 | 34 | if (len(argv) > 1): |
34 | 35 | self.setStatusManager("plcSimulator", argv) |
35 | 36 | self.EVENT_GENERATOR = True |
36 | - self.set_status() | |
37 | + #self.set_status() | |
38 | + # Load PLC status in colibri-new-fixed-2.json | |
39 | + colibri_json = open("colibri-new-fixed-2.json") | |
40 | + colibri_struct = json.load(colibri_json) | |
41 | + self.status = colibri_struct["statuses"][1]["entities"][0] | |
42 | + # load meteo_worker | |
43 | + self.my_state = WorkerState() | |
44 | + self.my_worker = WorkerThread(self.my_state, self.status) | |
45 | + self.my_worker.start() | |
46 | + #self.my_state.status = 'init' | |
47 | + | |
37 | 48 | |
38 | 49 | def plcPrint(self, string: str): |
39 | 50 | if DEBUG_FILE: |
... | ... | @@ -46,10 +57,10 @@ class PLCSimulator(DeviceSim, StatusManager): |
46 | 57 | print(dic) |
47 | 58 | change = dic[self.status_name] # ex: {"device_name":"VantagePro", "value_name":"RainRate", "value":12.0} |
48 | 59 | device_name = change["device_name"] # ex: VantagePro |
49 | - devices = self.status["device"] | |
60 | + devices = self.status["devices"] | |
50 | 61 | for device in devices: |
51 | - if device["name"] == device_name: | |
52 | - values = device["values"] | |
62 | + if device["device_name"] == device_name: | |
63 | + values = device["device_values"] | |
53 | 64 | # ex: for VantagePro |
54 | 65 | # values = |
55 | 66 | # {"name": "OutsideTemp", "value": 12.45, "unit": "Celcius", "comment": ""}, |
... | ... | @@ -102,7 +113,16 @@ class PLCSimulator(DeviceSim, StatusManager): |
102 | 113 | self.sendMessage(answer, conn) |
103 | 114 | return (0) |
104 | 115 | |
105 | - def loop(self): | |
116 | + def loop_1(self): | |
117 | + self.my_state.status = 'init' | |
118 | + while (self.my_state.worker == 'start'): | |
119 | + if (self.isConnected()): | |
120 | + self.handleConnections() | |
121 | + | |
122 | + time.sleep(0.5) | |
123 | + return True | |
124 | + | |
125 | + def loop_2(self): | |
106 | 126 | i = 0 |
107 | 127 | if (self.EVENT_GENERATOR and self.ended==0): |
108 | 128 | self.plcPrint("No entry for PLC found in config file : " + self.config_file) |
... | ... | @@ -121,7 +141,7 @@ class PLCSimulator(DeviceSim, StatusManager): |
121 | 141 | print("PLC simulator running...") |
122 | 142 | if self.EVENT_GENERATOR and not self.parse(): return False |
123 | 143 | self.configSocket() |
124 | - return self.loop() | |
144 | + return self.loop_1() | |
125 | 145 | #return (0) |
126 | 146 | |
127 | 147 | def set_status(self): | ... | ... |
simulators/plc/run_meteo.py
1 | 1 | |
2 | 2 | |
3 | - | |
4 | - | |
3 | +import json, os | |
5 | 4 | from simulators.plc.meteo_worker import WorkerState, WorkerThread |
6 | 5 | |
7 | -status = { | |
8 | - "name": "STATUS", "from": "Beckhoff", "version_firmware": "20170809", "site": "OSM-Mexico", | |
9 | - "date": "2017-03-03T13:45:00", | |
10 | - "devices": [{"name": "WXT520", "type": "meteo", "serial_number": "14656423", "valid": "yes", | |
11 | - "values": [ | |
12 | - {"name": "OutsideTemp", "value": 12.12, "unit": "Celcius", "comment": ""}, | |
13 | - {"name": "OutsideHumidity", "value": 64.1, "unit": "percent", "comment": ""}, | |
14 | - {"name": "Pressure", "value": 769.2, "unit": "mbar", "comment": "At site elevation"}, | |
15 | - {"name": "RainRate", "value": 0.0, "unit": "mm/h", "comment": ""}, | |
16 | - {"name": "WindSpeed", "value": 3.1, "unit": "m/s", "comment": ""}, | |
17 | - {"name": "WindDir", "value": 202.3, "unit": "deg", "comment": "(N=0, E=90)"}, | |
18 | - {"name": "DewPoint", "value": 8.3, "unit": "deg", "comment": ""} | |
19 | - ] | |
20 | - }, | |
21 | - {"name": "DRD11", "type": "meteo", "serial_number": "RET6789", "valid": "yes", | |
22 | - "values": [ | |
23 | - {"name": "analog", "value": 2.345, "unit": "V", "comment": "3V=Dry <2.5V=Rain"}, | |
24 | - {"name": "digital", "value": 1, "unit": "bool", "comment": "1=Dry 0=Rain"} | |
25 | - ] | |
26 | - }, | |
27 | - {"name": "VantagePro", "type": "meteo", "serial_number": "ERTRY2344324", "valid": "no", | |
28 | - "values": [ | |
29 | - {"name": "OutsideTemp", "value": 12.45, "unit": "Celcius", "comment": ""}, | |
30 | - {"name": "InsideTemp", "value": 20.28, "unit": "Celcius", "comment": ""}, | |
31 | - {"name": "OutsideHumidity", "value": 65.3, "unit": "percent", "comment": ""}, | |
32 | - {"name": "InsideHumidity", "value": 45.6, "unit": "percent", "comment": ""}, | |
33 | - {"name": "Pressure", "value": 768.7, "unit": "mbar", "comment": "At site elevation"}, | |
34 | - {"name": "RainRate", "value": 0.0, "unit": "mm/h", "comment": ""}, | |
35 | - {"name": "WindSpeed", "value": 2.8, "unit": "m/s", "comment": ""}, | |
36 | - {"name": "WindDir", "value": 207.0, "unit": "deg", "comment": "(N=0, E=90)"}, | |
37 | - {"name": "WindDirCardinal", "value": "SW", "unit": "string", "comment": ""}, | |
38 | - {"name": "DewPoint", "value": 8.5, "unit": "deg", "comment": ""} | |
39 | - ] | |
40 | - }, | |
41 | - {"name": "MLX90614-1", "type": "meteo", "serial_number": "Unknown", "valid": "yes", | |
42 | - "values": [ | |
43 | - {"name": "SensorTemperature", "value": 23.56, "unit": "Celcius", "comment": ""}, | |
44 | - {"name": "SkyTemperature", "value": -15.67, "unit": "Celcius", | |
45 | - "comment": "Clear<-10 VeryCloudy>4"} | |
46 | - ] | |
47 | - }, | |
48 | - {"name": "LAMP_FLAT_CAGIRE", "type": "calib", "serial_number": "REF_3434", "valid": "yes", | |
49 | - "values": [ | |
50 | - {"name": "status", "value": "on", "unit": "string", "comment": "on or off"}, | |
51 | - {"name": "current", "value": 0.234, "unit": "Ampere", "comment": ""} | |
52 | - ] | |
53 | - }, | |
54 | - {"name": "LAMP FLAT CAGIRE", "type": "calib", "serial_number": "REF_3434", "valid": "yes", | |
55 | - "values": [ | |
56 | - {"name": "status", "value": "on", "unit": "string", "comment": "on or off"}, | |
57 | - {"name": "current", "value": 0.234, "unit": "Ampere", "comment": ""} | |
58 | - ] | |
59 | - }, | |
60 | - {"name": "GLOBAL", | |
61 | - "values": [ | |
62 | - {"name": "mode", "value": "AUTO", "unit": "String", "comment": "AUTO OFF MANU"}, | |
63 | - {"name": "is_safe", "value": True, "unit": "bool", "comment": "True or False"}, | |
64 | - {"name": "LIGHTS", "value": "", "unit": "String", "comment": "ON or OFF"}, | |
65 | - {"name": "SHUTTERS", "value": "", "unit": "String", "comment": "OPEN OR CLOSE"}, | |
66 | - ] | |
67 | - } | |
68 | - ], | |
69 | - } | |
6 | +print(os.path.abspath(".")) | |
7 | +# Load PLC status in colibri-new-fixed-2.json | |
8 | +colibri_json = open("colibri-new-fixed-2.json") | |
9 | +colibri_struct = json.load(colibri_json) | |
10 | +plc_status = colibri_struct["statuses"][1]["entities"][0] | |
70 | 11 | |
71 | 12 | |
72 | 13 | my_state = WorkerState() |
73 | -my_worker = WorkerThread(my_state, status) | |
14 | +my_worker = WorkerThread(my_state, plc_status) | |
74 | 15 | |
75 | 16 | my_worker.start() |
76 | 17 | my_state.status = 'init' | ... | ... |
src/monitoring/tasks.py
... | ... | @@ -159,29 +159,29 @@ class Monitoring(Task): |
159 | 159 | ''' |
160 | 160 | def extractFromDict(self, status): |
161 | 161 | synthesis = {} |
162 | - devices = status["device"] | |
162 | + devices = status["devices"] | |
163 | 163 | #print(devices) |
164 | 164 | #is_safe_str = status["is_safe"] |
165 | 165 | #mode = status["mode"] |
166 | 166 | for device in devices: |
167 | - for value in device["values"]: | |
167 | + for value in device["device_values"]: | |
168 | 168 | synthesis[value["name"]] = value["value"] |
169 | 169 | synthesis[value["name"] + "_unit"] = value["unit"] |
170 | 170 | return synthesis |
171 | 171 | |
172 | 172 | # TODO ATTENTION SI DEUX DEVICES ONT LE MEME NOM |
173 | 173 | def saveContent(self, content): |
174 | - devices = content[0]["device"] | |
174 | + devices = content[0]["devices"] | |
175 | 175 | |
176 | 176 | for device in devices: |
177 | 177 | status = PlcDeviceStatus() |
178 | 178 | try: |
179 | - database_device = PlcDevice.objects.get(name=device["name"]) | |
179 | + database_device = PlcDevice.objects.get(name=device["device_name"]) | |
180 | 180 | except Exception as e: |
181 | 181 | # plc = Plc.objects.first() |
182 | 182 | database_device = PlcDevice.objects.create(name=device["name"]) |
183 | 183 | status.device = database_device |
184 | - for value in device["values"]: | |
184 | + for value in device["device_values"]: | |
185 | 185 | status.setValue(value["name"], value["value"], value["unit"]) |
186 | 186 | |
187 | 187 | #status.setValue("mode", mode) |
... | ... | @@ -195,7 +195,7 @@ class Monitoring(Task): |
195 | 195 | try: |
196 | 196 | status = {} |
197 | 197 | dict = json.loads(status_plc) |
198 | - if dict[0]["name"] == "STATUS": | |
198 | + if dict[0]["entity_name"] == "PLC_STATUS": | |
199 | 199 | if self.isStatusValid(): |
200 | 200 | status = self.extractFromDict(dict[0]) |
201 | 201 | self.saveContent(dict) | ... | ... |