Skip to content
Snippets Groups Projects

Backend

Merged Martin Petráček requested to merge backend into master
6 unresolved threads
Viewing commit fdc2c516
Show latest version
2 files
+ 60
4
Preferences
Compare changes
Files
2
macAddrStore.py 0 → 100644
+ 51
0
#!/usr/bin/python
import time
import json
import shutil
import os
from threading import Timer
class macAddrStore():
def __init__(self):
self.__load()
self.save_interval=30*60
self.mac_timeout=60*24*30
t = Timer(self.save_interval, self.__timeout)
t.setDaemon(True) #thread will be canceled on exit (it hangs on exit without this)
t.start()
def __del__(self):
self.__save()
def update(self, mac):
if mac in self.known:
self.known[mac]=int(time.time())
else:
print "new MAC"
self.known[mac]=int(time.time())
self.__save()
os.system(u"/usr/bin/create_notification -s news 'Ve vasi siti se objevilo nove zarizeni (MAC adresa %s)' 'New device (MAC address %s) appeared on your network.'" % (mac, mac)) #TBD: Czech non-ascii characters...
def __save(self):
f = open('/tmp/known_macs.txt', 'w')
print >>f, json.dumps(self.known)
print json.dumps(self.known)
f.close()
shutil.move('/tmp/known_macs.txt', '/usr/share/known_macs.txt')
def __load(self):
try:
f = open('/usr/share/known_macs.txt', 'r')
self.known=json.load(f)
f.close()
except:
self.known={}
def __timeout(self):
for mac in self.known.keys():
if self.known[mac] < int(time.time()) - self.mac_timeout:
del self.known_macs[mac]
self.__save()
t = Timer(self.save_interval, self.__timeout)
t.start()