1 | #!/usr/bin/python3 |
||
2 | |||
3 | """ |
||
4 | Mainprogram for scooter simulation |
||
5 | """ |
||
6 | |||
7 | |||
8 | import time |
||
9 | from src.scooter import Scooter |
||
10 | from src.api import ApiData |
||
11 | from main import Handler |
||
12 | |||
13 | |||
14 | |||
15 | class Simulation(): |
||
16 | """ Simulation class """ |
||
17 | |||
18 | # saves scooter id in an array |
||
19 | _scooter_id = [] |
||
20 | |||
21 | # saves log id in an array |
||
22 | _log_id = [] |
||
23 | |||
24 | # saves station id in an array |
||
25 | _station_id = [] |
||
26 | |||
27 | # saves city info in an array |
||
28 | _city_data = [] |
||
29 | |||
30 | def __init__(self): |
||
31 | """ Initialize class """ |
||
32 | self.scooter = Scooter() |
||
33 | self.handler = Handler() |
||
34 | self.api = ApiData(1) |
||
35 | |||
36 | |||
37 | View Code Duplication | def move(self) -> None: |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
38 | """ Move scooters around the cities. Simulation time is 2 minutes. """ |
||
39 | end_time = time.time() + 120 # 2 minutes |
||
40 | |||
41 | while time.time() <= end_time: |
||
42 | count = 0 |
||
43 | |||
44 | while count < len(self._scooter_id): |
||
45 | id = self._scooter_id[count] |
||
46 | data = self.api.get_scooter_data(scooter_id = id) |
||
47 | |||
48 | self.scooter.add_scooter_to_dict(data) |
||
49 | self.scooter.add_city_to_dict(self._city_data[count]) |
||
50 | self.scooter.set_station_id(self._station_id[count]) |
||
51 | |||
52 | |||
53 | self.run(count, id) |
||
54 | count += 1 |
||
55 | time.sleep(0.001) |
||
56 | |||
57 | |||
58 | def run(self, count: int, id: int): |
||
59 | """ |
||
60 | Check if the scooter is inside the city and check battery level, if TRUE |
||
61 | the scooter will be returned otherwise the scooter will continue moving. |
||
62 | """ |
||
63 | if self.scooter.check_scooter_in_city() is False: |
||
64 | print("\nScooter {} is outside of the city\n".format(id)) |
||
65 | print("Scooter {} will be returned.".format(id)) |
||
66 | |||
67 | self.end(count) |
||
68 | |||
69 | del self._scooter_id[count] |
||
70 | del self._log_id[count] |
||
71 | del self._station_id[count] |
||
72 | del self._city_data[count] |
||
73 | |||
74 | count -= 1 |
||
75 | elif self.scooter.check_battery(): |
||
76 | print("\n\033[1;31m*\033[1;0m Low battery!! the scooter {} needs to be charged.".format(id)) |
||
77 | print("Scooter {} will be returned.".format(id)) |
||
78 | |||
79 | self.end(count) |
||
80 | |||
81 | del self._scooter_id[count] |
||
82 | del self._log_id[count] |
||
83 | del self._station_id[count] |
||
84 | del self._city_data[count] |
||
85 | |||
86 | count -= 1 |
||
87 | else: |
||
88 | self.scooter.change_location() |
||
89 | self.scooter.move_scooter() |
||
90 | self.api.update_scooter() |
||
91 | |||
92 | |||
93 | def end(self, count): |
||
94 | """ |
||
95 | Scooter will be checked if it's outside the city/has low battery level or need |
||
96 | meantience. The scooter will be returned and the log will be uppdated. |
||
97 | """ |
||
98 | self.handler.end_rental() |
||
99 | self.api.update_scooter() |
||
100 | |||
101 | self.api.log_id = self._log_id[count] |
||
102 | self.api.update_log() |
||
103 | |||
104 | |||
105 | View Code Duplication | def return_scooters(self): |
|
0 ignored issues
–
show
|
|||
106 | """ All scooters will be returned when simulation time ends. """ |
||
107 | count = 0 |
||
108 | |||
109 | while count < len(self._scooter_id): |
||
110 | id = self._scooter_id[count] |
||
111 | data = self.api.get_scooter_data(scooter_id = id) |
||
112 | |||
113 | self.scooter.add_scooter_to_dict(data) |
||
114 | self.scooter.add_city_to_dict(self._city_data[count]) |
||
115 | self.scooter.set_station_id(self._station_id[count]) |
||
116 | |||
117 | self.end(count) |
||
118 | time.sleep(0.001) |
||
119 | count += 1 |
||
120 | |||
121 | |||
122 | def main(self, total:int): |
||
123 | """ Start simulation program. """ |
||
124 | print("\n************ Welcome to Scooter simulation program **************\n") |
||
125 | print("\nSimulation time is 2 minutes, do not try to break/stop the program.\n") |
||
126 | |||
127 | all_customers = self.api.get_all_customers() |
||
128 | |||
129 | count = 1 |
||
130 | customer = 0 |
||
131 | |||
132 | while count <= total: |
||
133 | data = self.api.get_scooter_data(scooter_id = count) |
||
134 | |||
135 | if self.scooter.check_scooter_status(data): |
||
136 | city = self.api.get_city_data() |
||
137 | self.scooter.add_city_to_dict(city) |
||
138 | self._city_data.append(city) |
||
139 | self._scooter_id.append(count) |
||
140 | |||
141 | # create log |
||
142 | self.api.user_id = str(all_customers[customer]["id"]) |
||
143 | self.api.create_log() |
||
144 | self._log_id.append(self.api.log_id) |
||
145 | |||
146 | ## get station |
||
147 | zone = self.scooter.get_zone_id() |
||
148 | station = self.api.get_station(zone) |
||
149 | self._station_id.append(station) |
||
150 | |||
151 | customer += 1 |
||
152 | else: |
||
153 | print("\n\033[1;31m*\033[1;0m Scooter {} is not available.\n".format(count)) |
||
154 | |||
155 | count += 1 |
||
156 | |||
157 | self.move() |
||
158 | self.return_scooters() |
||
159 | |||
160 | |||
161 | |||
162 | if __name__ == "__main__": |
||
163 | Simulation().main(497) |