-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_gps_translation.py
63 lines (52 loc) · 2.82 KB
/
main_gps_translation.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
# -*- coding: utf-8 -*-
import usocket as socket
import utime as time
import config
import machine
from lib import logging
from nce.translator.message_service import fill_bytes, ValueType
from nce.network.network_connector import NetworkConnector
logging.basic_config(level=logging.INFO)
logger = logging.get_logger("__main__")
def write_udp_gps_message(input_values):
if input_values[0] == "T":
message_bytes = bytearray(98)
message_bytes = fill_bytes(message_bytes, 0, 1, input_values[0], ValueType.CHAR)
message_bytes = fill_bytes(message_bytes, 1, 20, input_values[1], ValueType.STRING)
message_bytes = fill_bytes(message_bytes, 20, 28, input_values[2], ValueType.DOUBLE)
message_bytes = fill_bytes(message_bytes, 28, 36, input_values[3], ValueType.DOUBLE)
message_bytes = fill_bytes(message_bytes, 36, 38, input_values[4], ValueType.SHORT)
message_bytes = fill_bytes(message_bytes, 38, 42, input_values[5], ValueType.FLOAT)
message_bytes = fill_bytes(message_bytes, 42, 46, input_values[6], ValueType.FLOAT)
message_bytes = fill_bytes(message_bytes, 46, 50, input_values[7], ValueType.FLOAT)
message_bytes = fill_bytes(message_bytes, 50, 52, input_values[8], ValueType.SHORT)
message_bytes = fill_bytes(message_bytes, 52, 54, input_values[9], ValueType.UINT)
message_bytes = fill_bytes(message_bytes, 54, 56, input_values[10], ValueType.SHORT)
message_bytes = fill_bytes(message_bytes, 56, 71, input_values[11], ValueType.STRING)
message_bytes = fill_bytes(message_bytes, 71, 98, input_values[12], ValueType.STRING)
return message_bytes
elif input_values[0] == "S":
message_bytes = bytearray(36)
message_bytes = fill_bytes(message_bytes, 0, 1, input_values[0], ValueType.CHAR)
message_bytes = fill_bytes(message_bytes, 1, 36, input_values[1], ValueType.STRING)
return message_bytes
if __name__ == '__main__':
logger.info("Opening UDP Socket")
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
addr = socket.getaddrinfo(config.UDP_ENDPOINT_ADDRESS, config.UDP_ENDPOINT_PORT)[0][-1]
logger.info("Resolved Address [{}]".format(addr))
with open(config.GPS_TRANSLATION_DATA_PATH, "r") as file:
for line in file.readlines():
values = line.split(",")
message = write_udp_gps_message(values)
logger.info(
"Sending UDP message to {}:{} with body {}".format(
config.UDP_ENDPOINT_ADDRESS,
config.UDP_ENDPOINT_PORT,
message))
s.sendto(message, addr)
logger.info("Sent UDP Message to the UDP Broker")
time.sleep(config.MESSAGE_INTERVAL_SECONDS)
logger.info("Closing the network connection")
NetworkConnector().disconnect()
machine.idle()