-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroaming.py
67 lines (54 loc) · 2.83 KB
/
roaming.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
import logging
import asyncio
import xml.etree.ElementTree as ET
import random
import time
logger = logging.getLogger(__name__)
class RoamingMonitor():
def __init__(self, udp_server):
self._udp_server = udp_server
self._udp_server.register_driver(self)
self._locations = {}
def close(self):
#TODO: Implement proper shutdown of this function.
pass
def get_addr(self, number):
if number in self._locations:
return self._locations[number]["addr"]
return None
def process(self, xml_message, addr):
if xml_message.tag == "request" and xml_message.attrib["type"] == "systeminfo":
# xml_message.tag == "request" and xml_message.attrib["type"] == "alarm": # not sure if this updates only contain connected phones...
logger.debug("Systeminfo update received")
senderdata = xml_message.find("senderdata")
for element in senderdata.findall("address"):
if element.text in self._locations:
if not self._locations[element.text]["addr"] == addr:
logger.info("Updated {} to {}".format(element.text, addr))
self._locations[element.text]["addr"] = addr
self._locations[element.text]["time"] = time.time()
else:
logger.info("Already known: {} on {}".format(element.text, addr))
self._locations[element.text]["time"] = time.time()
else:
logger.info("Added {} on {}".format(element.text, addr))
self._locations[element.text] = { "addr": addr, "time": time.time() }
if xml_message.tag == "request" and xml_message.attrib["type"] == "login":
logger.debug("Login event received")
status = xml_message.find("logindata").find("status")
address = xml_message.find("senderdata").find("address")
if status.text == "0":
if address.text in self._locations:
logger.info("{} logged out".format(address.text))
del self._locations[address.text]
else:
logger.info("{} logged out but wasn't known".format(address.text))
elif status.text == "1":
if address.text in self._locations:
logger.info("{} logged in on {} and was already known".format(address.text, addr))
self._locations[address.text]["addr"] = addr
self._locations[address.text]["time"] = time.time()
else:
logger.info("{} logged in on {} (and wasn't known until know...)".format(address.text, addr))
self._locations[address.text] = {"addr": addr, "time": time.time() }
return False