-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrealtime-sim.py
executable file
·132 lines (103 loc) · 4.38 KB
/
realtime-sim.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3
import argparse
import csv
from datetime import datetime
from time import sleep
import sys
import requests
def get_payload_from_row(row: list) -> dict:
id, date, time, latitude, longitude = row
payload = {
'id': id,
'date': date,
'time': time,
'latitude': latitude,
'longitude': longitude,
}
return payload
def get_payload_from_rowSP(row: list) -> dict:
#print(row)
#id, date, time, latitude, longitude = row
Timestamp, Longitude, Latitude, _Speed, _Operatorname, _Operator, _CGI, _Cellname, _Node, _CellID, _LAC, NetworkTech, _NetworkMode, *_ = row
#id = NetworkTech
id = 'celular 1 - SM-G991B'
latitude = Latitude
longitude = Longitude
payload = {
'id': id,
'timestamp': Timestamp,
'latitude': latitude,
'longitude': longitude,
}
return payload
def send_request(endpoint, payload: dict):
print(payload)
response = requests.post(endpoint, json=payload)
return response.json()
def get_milliseconds_from_datetime(date: str , time: str) -> int:
data = f'{date}T{time}'
format_data = '%d-%m-%YT%H:%M:%S'
current = datetime.strptime(data, format_data)
return current
def get_milliseconds_from_datetimeSP(timestamp: str) -> int:
print(timestamp)
format_data = '%Y.%m.%d_%H.%M.%S'
current = datetime.strptime(timestamp, format_data)
return current
def mainSP(city, objects_quantity, simulation_speed, endpoint):
coordinates_list = []
for object_index in range(1, objects_quantity+1):
path = f'{city.lower()}/object_{object_index}.csv'
with open(path, 'r', encoding='utf-8') as csv_file:
data = csv.reader(csv_file, dialect='excel-tab')
for row in data:
coordinates_list.append(
get_payload_from_rowSP(row)
)
coordinates_list = coordinates_list[1:]
#coordinates_list.sort(key=lambda data: (data['date'], data['time']))
first_record = coordinates_list[0]
current_ms = get_milliseconds_from_datetimeSP(first_record['timestamp'])
for payload in coordinates_list:
next_ms = get_milliseconds_from_datetimeSP(payload['timestamp'])
delta = next_ms - current_ms
date = next_ms.strftime('%Y-%m-%d')
time = next_ms.strftime('%H:%M:%S')
del payload['timestamp']
response = send_request(endpoint, { **payload, 'date': date, 'time': time })
print(response)
sleep((1.0/simulation_speed) * (delta.total_seconds()))
current_ms = next_ms
def main(city, objects_quantity, simulation_speed, endpoint):
coordinates_list = []
for object_index in range(1, objects_quantity+1):
path = f'{city.lower()}/object_{object_index}.csv'
with open(path, 'r', encoding='utf-8') as csv_file:
data = csv.reader(csv_file)
for row in data:
coordinates_list.append(
get_payload_from_row(row)
)
coordinates_list.sort(key=lambda data: (data['date'], data['time']))
first_record = coordinates_list[0]
current_ms = get_milliseconds_from_datetime(first_record['date'], first_record['time'])
for payload in coordinates_list:
next_ms = get_milliseconds_from_datetime(payload['date'], payload['time'])
delta = next_ms - current_ms
response = send_request(endpoint, payload)
print(response)
sleep((1.0/simulation_speed) * (delta.total_seconds()))
current_ms = next_ms
if __name__ == '__main__':
MAX_OBJECTS = 3
city_choices = ['BELO_HORIZONTE', 'RIO_DE_JANEIRO', 'ROME', 'SAN_FRANCISCO', 'SAO_PAULO']
parser = argparse.ArgumentParser()
parser.add_argument('--city', '-c', choices=city_choices, default='RIO_DE_JANEIRO', help='Which city dataset to use')
parser.add_argument('--objects-quantity', '-q', type=int, choices=range(1, MAX_OBJECTS + 1), default=1, help='Amount of objects to use')
parser.add_argument('--endpoint', '-e', required=True, help='Endpoint to send the requests')
parser.add_argument('--simulation-speed', '-s', type=int, default=120, help='Simulation speed')
args = parser.parse_args()
if args.city == 'SAO_PAULO':
mainSP(args.city, args.objects_quantity, args.simulation_speed, args.endpoint)
else:
main(args.city, args.objects_quantity, args.simulation_speed, args.endpoint)