Blame view

simulators/alert/alertSimulator.py 1.95 KB
3c179769   Jeremy   userSimulator / a...
1
import os
86dd6e43   Jeremy   Simulator script ...
2
3
import sys
import time
3c179769   Jeremy   userSimulator / a...
4
5
import json
import shutil
86dd6e43   Jeremy   Simulator script ...
6

3224f14a   Jeremy   Fixed some simula...
7
8
DEBUG_FILE = False

86dd6e43   Jeremy   Simulator script ...
9
class AlertSimulator():
3c179769   Jeremy   userSimulator / a...
10
11
    alert_dir = "../resources/"
    alert_dest = "../../src/alert_manager/events_received/"
605b65e5   Jeremy   Update simulators
12
13
    conf_file = "../config/conf.json"
    conf_path = "../config/"
3c179769   Jeremy   userSimulator / a...
14
15
16
17
    request_path = "../resources/"
    sims = []
    ended = 0

b6b0fd47   Jeremy   Added parameters ...
18
    def alertPrint(self, string: str):
3224f14a   Jeremy   Fixed some simula...
19
20
        if DEBUG_FILE:
            print ("AlertSimulator : " + string)
3c179769   Jeremy   userSimulator / a...
21

65149de7   Jeremy   Update
22
    def __init__(self, argv):
86dd6e43   Jeremy   Simulator script ...
23
        if (len(argv) > 1):
3c179769   Jeremy   userSimulator / a...
24
25
26
27
28
29
30
31
32
33
34
            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())
678838ed   Jeremy   Weather ans insid...
35
        for dic in json_content[1:]:
3c179769   Jeremy   userSimulator / a...
36
37
38
39
40
41
            for key, value in dic.items():
                if (key == "alertSimulator"):
                    self.sims.append(dic)
                    self.ended = max(dic["time"], self.ended)
        return (0)

b6b0fd47   Jeremy   Added parameters ...
42
    def sendAlert(self, file_name: str):
3c179769   Jeremy   userSimulator / a...
43
44
45
46
47
48
49
50
        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)
86dd6e43   Jeremy   Simulator script ...
51
52

    def run(self):
3c179769   Jeremy   userSimulator / a...
53
54
55
56
57
58
59
60
61
62
        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)
1aed430d   jeremy   Alert handled + k...
63
        self.clean_dir()
3c179769   Jeremy   userSimulator / a...
64
        return (0)
86dd6e43   Jeremy   Simulator script ...
65
66
67
68

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