Blame view

simulators/utils/StatusManager.py 1.69 KB
605b65e5   Jeremy   Update simulators
1
import json
f7dd3df1   Jeremy   Update simulators...
2
from . import settings
605b65e5   Jeremy   Update simulators
3

c7583f6e   Etienne Pallier   Cleaner isolation...
4
DEBUG_FILE = True
3224f14a   Jeremy   Fixed some simula...
5

605b65e5   Jeremy   Update simulators
6
7
8
9
10
11
12
13
14
15
16
class StatusManager():
    status = {}
    config_file = "../config/conf.json"
    sims = []
    status_name = ""
    ended = 0

    def __init__(self):
        super().__init__()

    def setStatusManager(self, name, argv):
c7583f6e   Etienne Pallier   Cleaner isolation...
17
18
19
20
        '''
        :param name: for ex, plcSimulator
        :param argv: config file (json) to read for execution of events
        '''
605b65e5   Jeremy   Update simulators
21
22
23
24
25
26
27
28
29
30
31
        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")
c7583f6e   Etienne Pallier   Cleaner isolation...
32
            return False
605b65e5   Jeremy   Update simulators
33
        json_content = json.loads(json_data.read())
678838ed   Jeremy   Weather ans insid...
34
35
        self.ended = json_content[0]
        for dic in json_content[1:]:
605b65e5   Jeremy   Update simulators
36
37
38
            for key, value in dic.items():
                if (key == self.status_name):
                    self.sims.append(dic)
605b65e5   Jeremy   Update simulators
39
        self.statusPrint("The simulator will end in " + str(self.ended))
c7583f6e   Etienne Pallier   Cleaner isolation...
40
        return True
605b65e5   Jeremy   Update simulators
41
42
43
44
45
46
47
48
49
50
51
52
53
54

    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):
3224f14a   Jeremy   Fixed some simula...
55
        if DEBUG_FILE:
c7583f6e   Etienne Pallier   Cleaner isolation...
56
            print("From : " + self.status_name + " -> " + message)