Skip to content

Commit

Permalink
Fix sonoff camera random port
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed May 26, 2020
1 parent 8386d02 commit 5da3196
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions custom_components/sonoff/sonoff_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,6 @@ class EWeLinkCameras(Thread):
def __init__(self):
super().__init__(name="Sonoff_CAM", daemon=True)

self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# TODO: random port
self.sock.bind(('', 50000))
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)

def datagram_received(self, data: bytes, addr: tuple):
# _LOGGER.debug(f"<= {addr[0]:15} {data[:80].hex()}")

Expand Down Expand Up @@ -130,13 +124,21 @@ def sendto(self, data: Union[bytes, str], device: Camera):
# _LOGGER.debug(f"=> {device.addr[0]:15} {data[:60].hex()}")
self.sock.sendto(data, device.addr)

def start(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
self.sock.bind(('', 0))

super().start()

async def send(self, deviceid: str, command: str):
device = self.devices.get(deviceid)

if not device or time.time() - device.last_time > 9:
# start Thread if first time
if not self.is_alive():
super().start()
self.start()

if not device:
# create new device, we want wait for it
Expand Down

0 comments on commit 5da3196

Please sign in to comment.