blakeblackshear.frigate/frigate/zeroconf.py

63 lines
1.6 KiB
Python
Raw Permalink Normal View History

2020-12-03 15:00:23 +01:00
import logging
import socket
from zeroconf import (
ServiceInfo,
NonUniqueNameException,
InterfaceChoice,
IPVersion,
Zeroconf,
)
logger = logging.getLogger(__name__)
ZEROCONF_TYPE = "_frigate._tcp.local."
# Taken from: http://stackoverflow.com/a/11735897
def get_local_ip() -> bytes:
2020-12-03 15:00:23 +01:00
"""Try to determine the local IP address of the machine."""
host_ip_str = ""
2020-12-03 15:00:23 +01:00
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Use Google Public DNS server to determine own IP
sock.connect(("8.8.8.8", 80))
host_ip_str = sock.getsockname()[0]
2020-12-03 15:00:23 +01:00
except OSError:
try:
host_ip_str = socket.gethostbyname(socket.gethostname())
2020-12-03 15:00:23 +01:00
except socket.gaierror:
host_ip_str = "127.0.0.1"
2020-12-03 15:00:23 +01:00
finally:
sock.close()
try:
host_ip_pton = socket.inet_pton(socket.AF_INET, host_ip_str)
except OSError:
host_ip_pton = socket.inet_pton(socket.AF_INET6, host_ip_str)
return host_ip_pton
2021-02-17 14:23:32 +01:00
def broadcast_zeroconf(frigate_id: str) -> Zeroconf:
2020-12-03 15:00:23 +01:00
zeroconf = Zeroconf(interfaces=InterfaceChoice.Default, ip_version=IPVersion.V4Only)
host_ip = get_local_ip()
info = ServiceInfo(
ZEROCONF_TYPE,
name=f"{frigate_id}.{ZEROCONF_TYPE}",
addresses=[host_ip],
2020-12-03 15:00:23 +01:00
port=5000,
)
logger.info("Starting Zeroconf broadcast")
try:
zeroconf.register_service(info)
except NonUniqueNameException:
logger.error(
"Frigate instance with identical name present in the local network"
)
return zeroconf