StatusManager.py 1.51 KB
import json

class StatusManager():
    status = {}
    config_file = "../config/conf.json"
    sims = []
    status_name = ""
    ended = 0

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

    def setStatusManager(self, name, argv):
        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 (1)
        json_content = json.loads(json_data.read())
        for dic in json_content:
            for key, value in dic.items():
                if (key == self.status_name):
                    self.sims.append(dic)
                    self.ended = max(dic["time"], self.ended)
        self.statusPrint("The simulator will end in " + str(self.ended))
        return (0)

    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):
        print("From : " + self.status_name + " -> " + message)