-
Notifications
You must be signed in to change notification settings - Fork 0
/
connections.py
115 lines (108 loc) · 4.15 KB
/
connections.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import time,asyncio,threading,requests,json
from websockets import WebSocketException
from ui import ui
from db.api import add_conn_log
import eel
class clients_lan:
def __init__(self):
self.data={}
self.blocked_list=["0.0.0.0","127.0.0.1"]
def is_ws_connected(self,ip):
return (ip in self.data and self.data[ip]["send_queue"]!=None and self.data[ip]["ws_connected"])
def get(self,ip):
return self.data[ip] if ip in self.data else None
def add(self,ip):
if ip in self.data or ip in self.blocked_list:
return
self.data[ip]={
"birth_time" : round(time.time() * 1000),
"send_queue" : None,
"ws_connected" : False,
"battery" : {"has_battery":False},
"sys_info" : {}
}
add_conn_log(ip,hostname='',mac='',platform='')
def init_info(self,ip):
from plugins.battery import get_remote_battery_status
battery_status = get_remote_battery_status(ip)
self.set_battery_status(ip,battery_status)
self.set_sys_info(ip)
def set_sys_info(self,ip):
try:
result = requests.get("http://{}:{}/api/sys_info".format(ip,8000))
data = json.loads(result.text)
self.data[ip]["sys_info"] = data
except Exception as e:
print("get_remote_sys_info failed",e)
def set_battery_status(self,ip,data):
self.data[ip]["battery"] = data
eel.update_all_lan_clients()
def get_battery_status(self,ip):
return self.data[ip]["battery"]
def set_ws_connected(self,ip,msg_queue,conn):
if ip not in self.data:
return
self.data[ip]["send_queue"] = msg_queue
self.data[ip]["ws_conn"] = conn
self.data[ip]["ws_connected"] = True
print(self.data)
ui.show_toast("与{}建立连接".format(ip))
"""下面开始连接建立后的事件"""
from plugins.sync_folder import resync as sync_folder_resync
sync_folder_resync(ip)
self.init_info(ip)
eel.update_all_lan_clients()
ui.set_client_num(self.available_send_queue_count())
def set_ws_disconnected(self,ip):
if ip not in self.data:
return
if not self.data[ip]["ws_connected"]:
return
self.data[ip]["send_queue"]=None
self.data[ip]["ws_connected"]=False
print(self.data)
ui.show_toast("与{}连接断开".format(ip))
"""下面开始连接断开后的事件"""
eel.update_all_lan_clients()
ui.set_client_num(self.available_send_queue_count())
def all_available_send_queue(self):
result={}
for ip in self.data:
if self.data[ip]["ws_connected"]:
result[ip]=self.data[ip]
return result
def available_send_queue_count(self):
return len(self.all_available_send_queue())
def send(self,queue,msg):
if queue.full():
queue.get_nowait()
print("bad connection")
queue.put_nowait(msg)
def send_to(self,ip,msg):
if not ip in self.data or not self.data[ip]["ws_connected"]:
return
try:
queue = self.data[ip]["send_queue"]
self.send(queue,msg)
except WebSocketException as e:
print("websocket client exception:",e,ip)
print(ip,"LOSE CONNETION")
self.set_ws_disconnected(ip)
def broadcast(self,msg,target_ips=None):
if target_ips is not None:
clients = {}
for ip in target_ips:
if self.is_ws_connected(ip):
clients[ip] = self.get(ip)
else:
clients = self.all_available_send_queue()
for ip in clients:
try:
queue = clients[ip]["send_queue"]
self.send(queue,msg)
#print(msg,"->",ip)
except WebSocketException as e:
print("websocket client exception:",e,ip)
print(ip,"LOSE CONNETION")
self.set_ws_disconnected(ip)
clients_lan = clients_lan()