Skip to content

Commit

Permalink
Add option to specify host for server
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonacox committed Jul 8, 2024
1 parent 1f37510 commit a1e35a2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
5 changes: 4 additions & 1 deletion server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ Starting threads...
-p 8888:8888 \
-p 6666:6666/udp \
-p 6667:6667/udp \
-e DEBUG='no' \
-p 7000:7000/udp \
--network host \
-e DEBUGMODE='no' \
-e HOST='192.168.0.100' \
-v $PWD/devices.json:/app/devices.json \
-v $PWD/tinytuya.json:/app/tinytuya.json \
--name tinytuya \
Expand Down
60 changes: 53 additions & 7 deletions server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,10 @@
from tinytuya import scanner
import os

BUILD = "p12"
BUILD = "p13"

# Defaults from Environment
APIPORT = int(os.getenv("APIPORT", "8888"))
DEBUGMODE = os.getenv("DEBUGMODE", "False").lower() == "true"
DEVICEFILE = os.getenv("DEVICEFILE", tinytuya.DEVICEFILE)
SNAPSHOTFILE = os.getenv("SNAPSHOTFILE", tinytuya.SNAPSHOTFILE)
CONFIGFILE = os.getenv("CONFIGFILE", tinytuya.CONFIGFILE)
Expand All @@ -81,6 +80,14 @@
RETRYCOUNT = int(os.getenv("RETRYCOUNT", "5"))
SAVEDEVICEFILE = os.getenv("SAVEDEVICEFILE", "True").lower() == "true"
DEBUGMODE = os.getenv("DEBUGMODE", "no").lower() == "yes"
HOST = os.getenv("HOST", None)
BROADCAST = os.getenv("BROADCAST", None)

# Set up broadcast address
if HOST and not BROADCAST:
BROADCAST = HOST.split('.')
BROADCAST[3] = '255'
BROADCAST = '.'.join(BROADCAST)

# Logging
log = logging.getLogger(__name__)
Expand Down Expand Up @@ -275,6 +282,7 @@ def tuyalisten(port):
"""
Thread to listen for Tuya devices UDP broadcast on port
"""
global BROADCAST
log.debug("Started tuyalisten thread on %d", port)
print(" - tuyalisten %d Running" % port)
last_broadcast = 0
Expand All @@ -290,11 +298,20 @@ def tuyalisten(port):
client.bind(("", port))
client.settimeout(5)

iface_list = None
if HOST:
iface_list = {}
iface_list[HOST] = { 'broadcast': BROADCAST }
log.debug("Using iface_list: %r", iface_list)

while(running):
if port == UDPPORTAPP and time.time() - last_broadcast > scanner.BROADCASTTIME:
log.debug("Sending discovery request to all 3.5 devices on the network")
log.debug("Sending discovery request to all 3.5 devices on the network")
if HOST:
scanner.send_discovery_request(iface_list)
else:
scanner.send_discovery_request()
last_broadcast = time.time()
last_broadcast = time.time()
try:
data, addr = client.recvfrom(4048)
except (KeyboardInterrupt, SystemExit) as err:
Expand Down Expand Up @@ -653,6 +670,28 @@ def api(port):
"\n%sTinyTuya %s(Server)%s [%s%s]\n"
% (bold, normal, dim, tinytuya.__version__, BUILD)
)

# IP Address
print("%sConfiguration Settings:" % dim)
if HOST:
print(" Using Host IP: %s%s%s" % (cyan, HOST, dim))
if BROADCAST:
print(" Using Broadcast IP: %s%s%s" % (cyan, BROADCAST, dim))
print(" UDP Ports: %s%d%s, %s%d%s, %s%d%s" % (cyan, UDPPORT, dim, cyan, UDPPORTS, dim, cyan, UDPPORTAPP, dim))
print(" TCP Port: %s%d%s" % (cyan, TCPPORT, dim))
print(" API Port: %s%d%s" % (cyan, APIPORT, dim))
print(" Device File: %s%s%s" % (cyan, DEVICEFILE, dim))
print(" Snapshot File: %s%s%s" % (cyan, SNAPSHOTFILE, dim))
print(" Config File: %s%s%s" % (cyan, CONFIGFILE, dim))
print(" TCP Timeout: %s%s%s" % (cyan, TCPTIMEOUT, dim))
print(" UDP Timeout: %s%s%s" % (cyan, TIMEOUT, dim))
print(" Max Devices: %s%s%s" % (cyan, MAXCOUNT, dim))
print(" Retry Time: %s%s%s" % (cyan, RETRYTIME, dim))
print(" Retry Count: %s%s%s" % (cyan, RETRYCOUNT, dim))
print(" Save Device File: %s%s%s" % (cyan, SAVEDEVICEFILE, dim))
print(" Debug Mode: %s%s%s" % (cyan, DEBUGMODE, dim))
print("")

if len(tuyadevices) > 0:
print("%s[Loaded devices.json - %d devices]%s\n" % (dim, len(tuyadevices), normal))
else:
Expand Down Expand Up @@ -681,9 +720,10 @@ def api(port):
# maxdevices=0)
try:
found = scanner.devices(forcescan=True, verbose=False, discover=False, assume_yes=True, tuyadevices=tuyadevices)
except:
log.error("Error during scanner.devices()")
except Exception as err:
log.error(f"Error during scanner.devices() {err}")
found = []
forcescandone = True
print(f" - ForceScan: Found {len(found)} devices")
for f in found:
log.debug(f" - {found[f]}")
Expand Down Expand Up @@ -711,7 +751,6 @@ def api(port):
# If fetching the key failed, save it to retry later
retrydevices[result["id"]] = RETRYCOUNT
newdevices.append(result["id"])
forcescandone = True

if cloudsync:
cloudsync = False
Expand Down Expand Up @@ -765,3 +804,10 @@ def api(port):
print("Stopping threads...")
log.debug("Stoppping threads")
requests.get('http://localhost:%d/stop' % APIPORT, timeout=5)
except Exception as err:
log.error(f"Error in main loop: {err}")
running = False
# Close down API thread
print("Stopping threads...")
log.debug("Stoppping threads")
requests.get('http://localhost:%d/stop' % APIPORT, timeout=5)

0 comments on commit a1e35a2

Please sign in to comment.