2023-09-08 20:26:21 +02:00
|
|
|
import json
|
2019-10-03 17:06:40 +02:00
|
|
|
import logging
|
2019-09-19 15:15:46 +02:00
|
|
|
import requests
|
2020-04-03 19:01:40 +02:00
|
|
|
import websockets
|
|
|
|
|
2019-09-19 15:15:46 +02:00
|
|
|
from requests.auth import HTTPBasicAuth
|
2023-09-06 17:01:26 +02:00
|
|
|
import asyncio # Add asyncio for async functionality
|
|
|
|
from time import sleep # Add sleep function
|
2019-09-19 15:15:46 +02:00
|
|
|
|
|
|
|
|
2019-10-23 19:32:24 +02:00
|
|
|
def decode(r, verbose_errors=True):
|
|
|
|
try:
|
|
|
|
return r.json()
|
|
|
|
except Exception as e:
|
|
|
|
if r.status_code == 200:
|
|
|
|
logging.error("error while decoding json: error='%s' resp='%s'" % (e, r.text))
|
|
|
|
else:
|
|
|
|
err = "error %d: %s" % (r.status_code, r.text.strip())
|
|
|
|
if verbose_errors:
|
|
|
|
logging.info(err)
|
|
|
|
raise Exception(err)
|
|
|
|
return r.text
|
|
|
|
|
|
|
|
|
2019-09-19 15:15:46 +02:00
|
|
|
class Client(object):
|
|
|
|
def __init__(self, hostname='localhost', scheme='http', port=8081, username='user', password='pass'):
|
|
|
|
self.hostname = hostname
|
|
|
|
self.scheme = scheme
|
|
|
|
self.port = port
|
|
|
|
self.username = username
|
|
|
|
self.password = password
|
|
|
|
self.url = "%s://%s:%d/api" % (scheme, hostname, port)
|
2020-04-03 19:01:40 +02:00
|
|
|
self.websocket = "ws://%s:%s@%s:%d/api" % (username, password, hostname, port)
|
2019-09-19 15:15:46 +02:00
|
|
|
self.auth = HTTPBasicAuth(username, password)
|
|
|
|
|
2023-04-04 14:11:23 -07:00
|
|
|
# session takes optional argument to pull a sub-dictionary
|
|
|
|
# ex.: "session/wifi", "session/ble"
|
|
|
|
def session(self, sess="session"):
|
|
|
|
r = requests.get("%s/%s" % (self.url, sess), auth=self.auth)
|
2019-10-23 19:32:24 +02:00
|
|
|
return decode(r)
|
2019-09-19 15:15:46 +02:00
|
|
|
|
2020-04-03 19:01:40 +02:00
|
|
|
async def start_websocket(self, consumer):
|
|
|
|
s = "%s/events" % self.websocket
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
async with websockets.connect(s, ping_interval=60, ping_timeout=90) as ws:
|
|
|
|
async for msg in ws:
|
|
|
|
try:
|
|
|
|
await consumer(msg)
|
|
|
|
except Exception as ex:
|
|
|
|
logging.debug("Error while parsing event (%s)", ex)
|
|
|
|
except websockets.exceptions.ConnectionClosedError:
|
|
|
|
logging.debug("Lost websocket connection. Reconnecting...")
|
2023-09-08 01:09:40 +02:00
|
|
|
await asyncio.sleep(1) # Sleep for x seconds before reconnecting
|
2020-04-18 00:02:25 +02:00
|
|
|
except websockets.exceptions.WebSocketException as wex:
|
|
|
|
logging.debug("Websocket exception (%s)", wex)
|
2023-09-08 01:09:40 +02:00
|
|
|
await asyncio.sleep(1) # Sleep for x seconds before reconnecting
|
2023-09-05 20:29:09 +02:00
|
|
|
except Exception as e:
|
|
|
|
logging.exception("Other error while opening websocket (%s) with parameter %s", e, s)
|
2023-09-08 01:09:40 +02:00
|
|
|
await asyncio.sleep(1) # Sleep for x seconds before reconnecting
|
2023-09-05 20:29:09 +02:00
|
|
|
|
2023-09-05 21:22:44 +02:00
|
|
|
def run(self, command, verbose_errors=True):
|
|
|
|
for _ in range(0, 2):
|
|
|
|
try:
|
|
|
|
r = requests.post("%s/session" % self.url, auth=self.auth, json={'cmd': command})
|
|
|
|
except requests.exceptions.ConnectionError as e:
|
|
|
|
logging.exception("Request connection error (%s) while running command (%s)", e, command)
|
|
|
|
sleep(1) # Sleep for 1-s before trying a second time
|
|
|
|
else:
|
|
|
|
break
|
2023-09-05 20:29:09 +02:00
|
|
|
|
2023-09-05 21:22:44 +02:00
|
|
|
return decode(r, verbose_errors=verbose_errors)
|