-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice_broadcaster.py
More file actions
49 lines (39 loc) · 1.53 KB
/
device_broadcaster.py
File metadata and controls
49 lines (39 loc) · 1.53 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
48
49
from typing import List
import struct
import socket
from threading import Timer
from light_state_manager import LightStateManager
device_ident_opcode = 0
class DeviceBroadcaster(object):
def __init__(self, uuid: str, strips: List[LightStateManager], socket: socket.socket):
self.socket = socket
self.init_socket()
self.exit_flag = False
self.broadcast_message = struct.pack(
"!16sIII",
uuid.encode("utf-8"),
device_ident_opcode,
0,
len(strips)*3
)
for i in strips:
self.broadcast_message += struct.pack("!BH", i.ident, i.length)
#start the broadasting event loop
self.bcast_timer = Timer(5, self.broadcast)
self.bcast_timer.start()
def init_socket(self):
if self.socket is None:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
self.socket.bind(("0.0.0.0", 0))
def broadcast(self):
if not self.exit_flag:
# broadcast the message
try:
self.socket.sendto(self.broadcast_message, ("<broadcast>", 2000))
# restart the timer
self.bcast_timer = Timer(5, self.broadcast)
self.bcast_timer.start()
except Exception as err:
print("Failed to broadcast device data", err)