StatusManager.py 1.69 KB
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)