-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignalRHandler.py
76 lines (67 loc) · 2.46 KB
/
SignalRHandler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from signalrcore.hub_connection_builder import HubConnectionBuilder
from signalrcore.protocol.messagepack_protocol import MessagePackHubProtocol
import logging
import time
class SignalRHandler:
def __init__(self,server_root:str,monitorName:str,onConnection=None,debugLogging=False):
self.onConnection = onConnection
self.connected = False
self.server_url=f'{server_root}/NotificationHub'
builder= HubConnectionBuilder()\
.with_url(self.server_url,options={"headers": {"monitor-name": monitorName}})\
.with_hub_protocol(MessagePackHubProtocol())\
.with_automatic_reconnect({
"type": "raw",
"keep_alive_interval": 10,
"reconnect_interval": 5,
"max_attempts": 100000
})
if debugLogging:
builder = builder.configure_logging(logging.DEBUG)
self.hub_connection = builder.build()
def onOpen():
self.connected=True
if self.onConnection:
self.onConnection(self.connected)
print(f"Connection opened [{self.server_url}]")
def onClose():
self.connected=False
if self.onConnection:
self.onConnection(self.connected)
print("Connection closed")
self.hub_connection.on_open(onOpen)
self.hub_connection.on_close(onClose)
def start(self):
cont = True
firstException=True
while cont:
try:
self.hub_connection.start()
cont = False
except:
if firstException:
print(f"Waiting to connect to server [{self.server_url}]")
firstException=False
time.sleep(1)
def stop(self):
self.hub_connection.stop()
def uploadPreview(self,st):
if self.connected:
state=st.__dict__
self.hub_connection.send(
"PreviewState", # Method
[state], # Params
)
def logMessage(self,mess):
if self.connected:
self.hub_connection.send(
"LogMessage", # Method
[mess], # Params
)
def uploadConfig(self,config):
print(f'UploadConfig')
print(config)
if self.connected:
print(f'Sending monito config')
monitorConfig = config.shortDict()
self.hub_connection.send("MonitorConfig",[monitorConfig])