-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_env_translation.py
49 lines (38 loc) · 1.8 KB
/
main_env_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
# -*- 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_env_data_coll_message(input_values):
message_bytes = bytearray(17)
message_bytes = fill_bytes(message_bytes, 0, 4, input_values[0], ValueType.FLOAT)
message_bytes = fill_bytes(message_bytes, 4, 8, input_values[1], ValueType.FLOAT)
message_bytes = fill_bytes(message_bytes, 8, 12, input_values[2], ValueType.FLOAT)
message_bytes = fill_bytes(message_bytes, 12, 16, input_values[3], ValueType.FLOAT)
message_bytes = fill_bytes(message_bytes, 16, 17, input_values[4], ValueType.CHAR)
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.ENV_DATA_COLLECTOR_TRANSLATION_DATA_PATH, "r") as file:
for line in file.readlines():
values = line.split(",")
message = write_udp_env_data_coll_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()