-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathudp.py
More file actions
47 lines (33 loc) · 1.19 KB
/
Copy pathudp.py
File metadata and controls
47 lines (33 loc) · 1.19 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
import logging
import socket
import threading
from configuration import Config
class SensorConnection:
def __init__(self, config, callback):
self.config = config
self.t = None
self.callback = callback
self.buffer_size = config.BUFFER_SIZE
self.sock = socket.socket(socket.AF_INET, # Internet
# socket.SOCK_DGRAM) # UDP
socket.SOCK_STREAM) # UDP
self.sockFile = self.sock.makefile()
logging.info(f"Connecting to '{config.SENSOR_IP}':{config.SENSOR_PORT} with buffer size: {self.buffer_size}")
self.sock.connect((config.SENSOR_IP, config.SENSOR_PORT))
self.stop = False
self.t = threading.Thread(target=self.handle, daemon=True)
self.t.start()
def __del__(self):
self.stop = True
if self.t is not None:
self.t.join()
def handle(self):
while not self.stop:
data = self.sockFile.readline()
data = data.strip()
self.callback(data)
if __name__ == '__main__':
config = Config()
sql = SensorConnection(config, lambda x: print(x))
while True:
pass