-
Notifications
You must be signed in to change notification settings - Fork 1
/
tb_config.py
executable file
·38 lines (31 loc) · 1.26 KB
/
tb_config.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
import threading, asyncio, logging
from concurrent.futures import ThreadPoolExecutor
logger = logging.getLogger('ThermoBeaconSrv')
#logger.setLevel(logging.DEBUG)
class ConfigProtocol:
def __init__(self, command_handler):
self.command_handler = command_handler
def connection_made(self, transport):
self.transport = transport
def datagram_received(self, data, addr):
message = data.decode()
logger.debug('Received %r from %s' % (message, addr))
result = self.command_handler.processCommand(message)
logger.debug('Send %s to %s' % (result, addr))
self.transport.sendto(bytes(result, 'utf-8'), addr)
'''
UDP server, listen for commands on localhost, port 9999
'''
class UDPSrvThread(threading.Thread):
def __init__(self, bridge):
threading.Thread.__init__(self)
self.bridge = bridge
def run(self):
self.loop = asyncio.new_event_loop()
self.executor = ThreadPoolExecutor()
self.loop.set_default_executor(self.executor)
connect = self.loop.create_datagram_endpoint(
lambda: ConfigProtocol(self.bridge),
local_addr=('127.0.0.1', 9999))
transport, protocol = self.loop.run_until_complete(connect)
self.loop.run_forever()