alertSimulator.py 1.95 KB
import os
import sys
import time
import json
import shutil

DEBUG_FILE = False

class AlertSimulator():
    alert_dir = "../resources/"
    alert_dest = "../../src/alert_manager/events_received/"
    conf_file = "../config/conf.json"
    conf_path = "../config/"
    request_path = "../resources/"
    sims = []
    ended = 0

    def alertPrint(self, string: str):
        if DEBUG_FILE:
            print ("AlertSimulator : " + string)

    def __init__(self, argv):
        if (len(argv) > 1):
            self.conf_file = self.conf_path + argv[1]
        else:
            self.alertPrint("Using the default configuration file : conf.json")

    def parse(self):
        try:
            json_data = open(self.conf_file, 'r')
        except IOError:
            self.alertPrint("Configuration file not found")
            sys.exit(0)
        json_content = json.loads(json_data.read())
        for dic in json_content[1:]:
            for key, value in dic.items():
                if (key == "alertSimulator"):
                    self.sims.append(dic)
                    self.ended = max(dic["time"], self.ended)
        return (0)

    def sendAlert(self, file_name: str):
        shutil.copyfile(self.alert_dir + file_name, self.alert_dest + file_name)
        return (0)

    def clean_dir(self):
        for dic in self.sims:
            if (os.path.isfile(self.alert_dest + dic["alertSimulator"])):
                os.remove(self.alert_dest + dic["alertSimulator"])
        return (0)

    def run(self):
        i = 0
        self.parse()
        self.clean_dir()
        self.alertPrint("The simulator will end in %d seconds"%(int(self.ended)))
        while (i < self.ended):
            i += 1
            for dic in self.sims:
                if (int(dic["time"]) == i):
                    self.sendAlert(dic["alertSimulator"])
            time.sleep(1)
        self.clean_dir()
        return (0)

if __name__ == "__main__":
    sim = AlertSimulator(sys.argv)
    sim.run()