StatusManager.py
1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import json
from . import settings
DEBUG_FILE = True
class StatusManager():
status = {}
config_file = "../config/conf.json"
sims = []
status_name = ""
ended = 0
def __init__(self):
super().__init__()
def setStatusManager(self, name, argv):
'''
:param name: for ex, plcSimulator
:param argv: config file (json) to read for execution of events
'''
self.status_name = name
if (len(argv) > 1):
self.config_file = "../config/" + argv[1]
else:
self.statusPrint("Using the default configuration file : conf.json")
def parse(self):
try:
json_data = open(self.config_file, 'r')
except IOError:
self.statusPrint("Configuration file not found")
return False
json_content = json.loads(json_data.read())
self.ended = json_content[0]
for dic in json_content[1:]:
for key, value in dic.items():
if (key == self.status_name):
self.sims.append(dic)
self.statusPrint("The simulator will end in " + str(self.ended))
return True
def create_status(self):
status = json.dumps(self.status)
return (status)
def updateStatus(self, i: int):
for dic in self.sims:
if (int(dic["time"]) == i):
for key, value in dic[self.status_name].items():
self.status[key] = value
self.statusPrint("Element added to status : %s with value : "%(key) + str(value))
return (0)
def statusPrint(self, message):
if DEBUG_FILE:
print("From : " + self.status_name + " -> " + message)