37 lines
1.2 KiB
Python
37 lines
1.2 KiB
Python
from ssdpy import SSDPClient
|
|
import re
|
|
import json
|
|
from flickerstrip_py.flickerstrip import Flickerstrip
|
|
|
|
|
|
class FlickerstripDiscoveryClient:
|
|
def __init__(self, max_attempts=3):
|
|
self.max_attempts = max_attempts
|
|
self.client = SSDPClient()
|
|
|
|
def __discovered(self, device):
|
|
loc = device["location"]
|
|
result = re.search("http://(.*):80/description.xml", loc)
|
|
ip_address = result.group(1)
|
|
return Flickerstrip(ip_address)
|
|
|
|
def discover(self):
|
|
for i in range(self.max_attempts):
|
|
print(f"Discovering devices... (Attempt {i + 1})")
|
|
devices = self.client.m_search("ssdp:all")
|
|
print(f"Discovered {len(devices)} device(s).")
|
|
filtered = [d for d in devices if "Flickerstrip" in d["server"]]
|
|
print(f"Discovered {len(filtered)} flickerstrip(s).")
|
|
if len(filtered) > 0:
|
|
mapped = [self.__discovered(d) for d in filtered]
|
|
return mapped
|
|
return []
|
|
|
|
|
|
if __name__ == "__main__":
|
|
client = FlickerstripDiscoveryClient()
|
|
flickerstrips = client.discover()
|
|
if len(flickerstrips) > 0:
|
|
flickerstrips[0].force_update()
|
|
print(json.dumps(flickerstrips[0].status.to_json()))
|