Move from thread to rel dispatcher
This commit is contained in:
@@ -3,12 +3,14 @@ from mqtt_manager import MqttManager
|
|||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
|
||||||
host = "192.168.0.100"
|
host = "192.168.0.100"
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
tevolve = Tevolve()
|
tevolve = Tevolve()
|
||||||
|
|
||||||
token_thread_event = threading.Event()
|
token_thread_event = threading.Event()
|
||||||
|
|||||||
100
src/pytevolve.py
100
src/pytevolve.py
@@ -3,6 +3,8 @@ import json
|
|||||||
import threading
|
import threading
|
||||||
import websocket
|
import websocket
|
||||||
import time
|
import time
|
||||||
|
import rel
|
||||||
|
import datetime
|
||||||
|
|
||||||
data = "username=1202283%40uad.ac.uk&password=24e76d8e4&grant_type=password"
|
data = "username=1202283%40uad.ac.uk&password=24e76d8e4&grant_type=password"
|
||||||
url = "https://api-tevolve.termoweb.net/client/token"
|
url = "https://api-tevolve.termoweb.net/client/token"
|
||||||
@@ -46,14 +48,17 @@ class Tevolve:
|
|||||||
|
|
||||||
current_time = time.time()
|
current_time = time.time()
|
||||||
time_since_refresh = current_time - self.time_token_start
|
time_since_refresh = current_time - self.time_token_start
|
||||||
|
print(time_since_refresh)
|
||||||
|
|
||||||
if self.need_token or time_since_refresh > 3600:
|
if time_since_refresh > 1000:
|
||||||
|
|
||||||
x = requests.post(url, headers=headers, data=data)
|
x = requests.post(url, headers=headers, data=data)
|
||||||
|
|
||||||
if x.status_code == 200:
|
if x.status_code == 200:
|
||||||
self.token_primary = x.json()["access_token"]
|
self.token_primary = x.json()["access_token"]
|
||||||
self.time_token_start = time.time()
|
self.time_token_start = time.time()
|
||||||
|
print("GOT TOKEN")
|
||||||
|
|
||||||
event.set()
|
event.set()
|
||||||
else:
|
else:
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
@@ -101,10 +106,15 @@ class Tevolve:
|
|||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
self.sid = json.loads(response.text[5:])["sid"]
|
self.sid = json.loads(response.text[5:])["sid"]
|
||||||
|
print("got SID" + str(self.sid))
|
||||||
|
else:
|
||||||
|
print("NO SID")
|
||||||
|
print(response.status_code)
|
||||||
# else:
|
# else:
|
||||||
# self.token_primary = ""
|
# self.token_primary = ""
|
||||||
# Tevolve.token_manager()
|
# Tevolve.token_manager()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
print("SID EXCEPTION")
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
|
|
||||||
@@ -129,8 +139,11 @@ class Tevolve:
|
|||||||
# Tevolve.token_manager()
|
# Tevolve.token_manager()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
print("PREVIOUS SID: " + str(self.sid))
|
||||||
self.get_sid()
|
self.get_sid()
|
||||||
|
|
||||||
|
print("NEW SID: " + str(self.sid))
|
||||||
|
|
||||||
|
|
||||||
url = "https://api-tevolve.termoweb.net/socket.io/?token=" + self.token_primary + "&dev_id=" + str(self.dev_id) + "&EIO" \
|
url = "https://api-tevolve.termoweb.net/socket.io/?token=" + self.token_primary + "&dev_id=" + str(self.dev_id) + "&EIO" \
|
||||||
"=3&transport=polling&t=Ntb6xXn" \
|
"=3&transport=polling&t=Ntb6xXn" \
|
||||||
@@ -255,11 +268,14 @@ class Tevolve:
|
|||||||
|
|
||||||
def create_websocket(self, mqtt_connection):
|
def create_websocket(self, mqtt_connection):
|
||||||
|
|
||||||
|
global start_time
|
||||||
|
global end_time
|
||||||
|
|
||||||
websocket_url = "wss://api-tevolve.termoweb.net/socket.io/?token=" + self.token_primary + \
|
websocket_url = "wss://api-tevolve.termoweb.net/socket.io/?token=" + self.token_primary + \
|
||||||
"&dev_id=" +str(self.dev_id)+ "&EIO=3&transport=websocket&sid=" + self.sid + ""
|
"&dev_id=" +str(self.dev_id)+ "&EIO=3&transport=websocket&sid=" + self.sid + ""
|
||||||
|
|
||||||
def on_message(ws, message):
|
def on_message(ws, message):
|
||||||
|
print("MESSAGE")
|
||||||
self.websocket_message = message
|
self.websocket_message = message
|
||||||
print(message)
|
print(message)
|
||||||
|
|
||||||
@@ -272,55 +288,89 @@ class Tevolve:
|
|||||||
|
|
||||||
|
|
||||||
def on_open(ws):
|
def on_open(ws):
|
||||||
|
start_time = datetime.datetime.now()
|
||||||
try:
|
try:
|
||||||
self.post_websocket()
|
self.post_websocket()
|
||||||
# self.create_websocket(mqtt_connection)
|
print("postwebsocket")
|
||||||
except Exception as e:
|
|
||||||
print(e)
|
|
||||||
ws.close()
|
|
||||||
|
|
||||||
def run():
|
|
||||||
print("websocket (run)")
|
print("websocket (run)")
|
||||||
counter = 0
|
counter = 0
|
||||||
ws.send("2probe")
|
ws.send("2probe")
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
ws.send(str("5"))
|
ws.send(str("5"))
|
||||||
while 1:
|
# while 1:
|
||||||
ws.send("2")
|
# ws.send("2")
|
||||||
counter = counter + 1
|
# counter = counter + 1
|
||||||
if counter >= 360:
|
# if counter >= 360:
|
||||||
break
|
# break
|
||||||
time.sleep(10)
|
# time.sleep(10)
|
||||||
|
|
||||||
|
# self.create_websocket(mqtt_connection)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
ws.close()
|
||||||
|
|
||||||
|
|
||||||
r = threading.Thread(target=run, daemon=True)
|
|
||||||
r.start()
|
|
||||||
|
|
||||||
def on_close(ws, close_status_code, close_msg):
|
def on_close(ws, close_status_code, close_msg):
|
||||||
print("WEB SOCKET ClOSED")
|
print("WEB SOCKET ClOSED")
|
||||||
|
global end_time
|
||||||
|
end_time = datetime.datetime.now()
|
||||||
|
print("starttime: " + str(start_time))
|
||||||
|
print("endtime: " + str(end_time))
|
||||||
|
breakpoint()
|
||||||
|
|
||||||
# mqtt_connection.publish_heater()
|
# mqtt_connection.publish_heater()
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
print("post websocket")
|
|
||||||
try:
|
try:
|
||||||
self.post_websocket()
|
self.post_websocket()
|
||||||
self.create_websocket(mqtt_connection)
|
# self.create_websocket(mqtt_connection)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
def on_error(ws, err):
|
def on_error(ws, err):
|
||||||
print("WEB SOCKET ERROR")
|
print("WEB SOCKET ERROR")
|
||||||
|
breakpoint()
|
||||||
# self.post_websocket()
|
# self.post_websocket()
|
||||||
# self.create_websocket(mqtt_connection
|
# self.create_websocket(mqtt_connection
|
||||||
|
|
||||||
|
def on_pong(ws, message):
|
||||||
|
ws.send("2")
|
||||||
|
print(message)
|
||||||
|
|
||||||
|
|
||||||
def on_reconnect(ws):
|
def on_reconnect(ws):
|
||||||
print("Reconnect")
|
try:
|
||||||
|
self.post_websocket()
|
||||||
|
|
||||||
ws = websocket.WebSocketApp(websocket_url)
|
print("ReconnectHERE")
|
||||||
websocket.enableTrace(False)
|
except Exception as e:
|
||||||
ws.on_message = on_message
|
print(e)
|
||||||
ws.on_open = on_open
|
|
||||||
ws.on_close = on_close
|
def run():
|
||||||
|
print("websocket (run)")
|
||||||
|
counter = 0
|
||||||
|
ws.send("2probe")
|
||||||
|
time.sleep(2)
|
||||||
|
ws.send(str("5"))
|
||||||
|
while 1:
|
||||||
|
ws.send("2")
|
||||||
|
counter = counter + 1
|
||||||
|
if counter >= 360:
|
||||||
|
break
|
||||||
|
time.sleep(10)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
ws = websocket.WebSocketApp(websocket_url, on_open=on_open, on_close=on_close, on_error=on_error, on_message=on_message, on_pong=on_pong)
|
||||||
|
websocket.enableTrace(True)
|
||||||
|
# ws.on_message = on_message
|
||||||
|
# ws.on_open = on_open
|
||||||
|
# ws.on_close = on_close
|
||||||
# ws.on_error = on_error
|
# ws.on_error = on_error
|
||||||
|
# ws.on_reconnect = on_reconnect
|
||||||
|
|
||||||
ws.run_forever(ping_interval=10, ping_payload='42/api/v2/socket_io,["message","ping"]', reconnect=5)
|
ws.run_forever(dispatcher=rel, ping_interval=10, ping_payload='42/api/v2/socket_io,["message","ping"]', reconnect=5)
|
||||||
|
rel.signal(2, rel.abort)
|
||||||
|
rel.dispatch()
|
||||||
|
# r = threading.Thread(target=run)
|
||||||
|
# r.start()
|
||||||
|
|||||||
Reference in New Issue
Block a user