-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_service.py
68 lines (55 loc) · 2.08 KB
/
client_service.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
from omada import Omada
import utilities as Util
import traceback
import asyncio
import time
class ClientService:
time_out = 1 # in minutes
def __init__(self):
self.omada = Omada()
def __loop__(self):
while True:
try:
result = self.omada.login(asAdmin=True)
if result is not None:
self.getClientsLoginHistory()
self.getAllActiveClients()
asyncio.run(self.processClients())
except Exception:
self.omada = Omada() # re-initialize Omada
print(traceback.format_exc())
print('Waiting 5 seconds before processing again')
time.sleep(5)
def getAllActiveClients(self):
active_clients = []
for client in self.omada.getAllActiveClients():
if client['authStatus'] == 2:
active_clients.append(client)
self.active_clients = active_clients
def getClientsLoginHistory(self):
self.getAllActiveClients() # refresh active clients
clientsLoginHistory = []
for client in self.omada.getAllAuthedClients():
if client['valid'] == True:
clientsLoginHistory.append(client)
self.clientsLoginHistory = clientsLoginHistory
async def processClients(self):
tasks = [asyncio.create_task(self.client_task(client)) for client in self.clientsLoginHistory]
await asyncio.gather(*tasks)
async def client_task(self, client):
try:
not_found = True
mac = client['mac']
for active_client in self.active_clients:
if active_client['mac'] == client['mac']:
not_found = False
if not_found:
print(f'{mac} not Found! Disconnecting user')
self.omada.disconnectClient(mac=mac)
else:
print(f'{mac} user is online!')
except Exception:
print(traceback.format_exc())
Util.checkOmada()
clientService = ClientService()
clientService.__loop__()