|
| 1 | +import asyncio |
| 2 | +import functools |
| 3 | +import json |
| 4 | +import os |
| 5 | +import socket |
| 6 | +from datetime import datetime |
| 7 | + |
| 8 | +import enapter |
| 9 | + |
| 10 | +def parse_json(bytes): |
| 11 | + return json.loads(bytes.decode()) |
| 12 | + |
| 13 | + |
| 14 | +async def main(): |
| 15 | + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: |
| 16 | + device_factory = functools.partial( |
| 17 | + NIDAQ, |
| 18 | + socket=sock, |
| 19 | + tcp_port=os.environ["LISTEN_TCP_PORT"], |
| 20 | + ) |
| 21 | + await enapter.vucm.run(device_factory) |
| 22 | + |
| 23 | + |
| 24 | +class NIDAQ(enapter.vucm.Device): |
| 25 | + def __init__(self, socket, tcp_port, **kwargs): |
| 26 | + super().__init__(**kwargs) |
| 27 | + self.socket = socket |
| 28 | + self.tcp_port = tcp_port |
| 29 | + |
| 30 | + async def task_properties_sender(self): |
| 31 | + while True: |
| 32 | + await self.send_properties( |
| 33 | + { |
| 34 | + "model": "NI cDAQ 9178", |
| 35 | + } |
| 36 | + ) |
| 37 | + await asyncio.sleep(10) |
| 38 | + |
| 39 | + async def task_telemetry_sender(self): |
| 40 | + server_address = ('localhost', int(self.tcp_port)) |
| 41 | + self.socket.bind(server_address) |
| 42 | + self.socket.setblocking(False) |
| 43 | + self.socket.listen(1) |
| 44 | + |
| 45 | + while True: |
| 46 | + try: |
| 47 | + await self.log.info('waiting for a connection') |
| 48 | + connection, client_address = await asyncio.get_event_loop().sock_accept(self.socket) |
| 49 | + |
| 50 | + await self.log.info(f'connection from {client_address}') |
| 51 | + data = bytearray() |
| 52 | + |
| 53 | + while True: |
| 54 | + try: |
| 55 | + received = await asyncio.get_event_loop().sock_recv(connection, 1024) |
| 56 | + if not received: |
| 57 | + await self.log.info(f'no more data from {client_address}') |
| 58 | + break |
| 59 | + data.extend(received) |
| 60 | + # await self.log.info(f'got data: {received}') |
| 61 | + except Exception as e: |
| 62 | + await self.log.error(f"Error receiving data: {e}") |
| 63 | + break |
| 64 | + |
| 65 | + try: |
| 66 | + telemetry = parse_json(data) |
| 67 | + if telemetry['Date'] and telemetry['Time']: |
| 68 | + tt = telemetry['Date'] + ' ' + telemetry['Time'] |
| 69 | + date = datetime.strptime(tt,'%d/%m/%Y %H:%M:%S') |
| 70 | + telemetry["timestamp"] = date.timestamp() |
| 71 | + telemetry["status"] = "ok" # TODO: define status |
| 72 | + await self.log.info(f'data to send: {telemetry}') |
| 73 | + await self.send_telemetry(telemetry) |
| 74 | + self.alerts.clear() |
| 75 | + except Exception as e: |
| 76 | + self.alerts.add("parse_error") |
| 77 | + await self.log.error(f"failed to process data: {e}") |
| 78 | + |
| 79 | + except Exception as e: |
| 80 | + await self.log.error(f"Connection error: {e}") |
| 81 | + finally: |
| 82 | + connection.close() |
| 83 | + await asyncio.sleep(1) |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + asyncio.run(main()) |
| 88 | + |
0 commit comments