This repository was archived by the owner on Jul 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
68 lines (51 loc) · 1.83 KB
/
main.py
File metadata and controls
68 lines (51 loc) · 1.83 KB
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
import logging
import os
import requests
import sys
import time
from argparse import ArgumentParser
from random import random
from gps import GpsCoordinates, GpsSensor
from hardware import generate_uuid
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
logger = logging.getLogger(__name__)
if os.getenv("USE_STUBS", False):
class GpsSensor:
# Russia, Moscow
__lat = 55.751244
__lon = 37.618423
@staticmethod
def get_coordinates():
time.sleep(0.9 + random() * 0.1)
GpsSensor.__lat += (random() - 0.5) / 10000
GpsSensor.__lon += (random() - 0.5) / 10000
logging.debug("GPS generated: %s,%s", GpsSensor.__lat, GpsSensor.__lon)
return GpsCoordinates(lat=GpsSensor.__lat, lon=GpsSensor.__lon)
def init_sensor(uri, sensor_uuid):
while not (200 <= requests.post(
'{uri}/private/sensor/{sensor_uuid}/register'.format(uri=uri, sensor_uuid=sensor_uuid),
json={'sensor_type': 6, 'status': 1},
).status_code < 300):
time.sleep(1)
def send_coordinates(uri, sensor_uuid, coordinates):
requests.post(
'{uri}/private/sensor/{sensor_uuid}/data'.format(uri=uri, sensor_uuid=sensor_uuid),
json={'value': {'lat': coordinates.lat, 'lon': coordinates.lon}},
)
def main():
parser = ArgumentParser()
parser.add_argument("--uri", required=True)
args = parser.parse_args()
sensor_uuid = generate_uuid()
init_sensor(args.uri, sensor_uuid)
gps = GpsSensor()
logger.info("GPS sensor initialized")
while True:
try:
coordinates = gps.get_coordinates()
if coordinates.is_valid():
send_coordinates(args.uri, sensor_uuid, coordinates)
except Exception as e:
logging.error(e)
if __name__ == "__main__":
main()