Created the logic for saving and fetching times.

This commit is contained in:
2017-02-10 16:19:46 +01:00
parent 7e0f60996d
commit 8871359085
2 changed files with 51 additions and 51 deletions

BIN
home.db

Binary file not shown.

View File

@@ -1,66 +1,66 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import subprocess
from time import time, sleep
import sqlite3 import sqlite3
from subprocess import check_output
from time import time
from re import findall
def getOnlineClients():
arpOutput = subprocess.check_output("sudo arp-scan -l", shell=True)
arpOutput = arpOutput.decode()
macAdr = findall('(([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2}))', arpOutput)
return [i[0] for i in macAdr]
def getAddr(): def getAddr():
c.execute('SELECT adr FROM clients') c.execute('SELECT adr FROM clients')
for adr in c.fetchall():
print(adr[0]) return [i[0] for i in c.fetchall()]
def getTimes(): def getTimes():
conn = sqlite3.connect('home.db')
c = conn.cursor()
c.execute('SELECT c.name, l.timesince FROM lastonline AS l JOIN clients AS c WHERE l.clientadr=c.adr') c.execute('SELECT c.name, l.timesince FROM lastonline AS l JOIN clients AS c WHERE l.clientadr=c.adr')
print(c.fetchall())
def checkNameExistance(): returnList = []
c.execute('SELECT name, addr FROM macAddr') for name, time in c.fetchall():
test = c.fetchall() returnList.append({"name": name, "time": convertTime(time)})
for name, addr in test:
print(name)
# Define the users to lookup conn.commit()
addr = { "elias": '38:ca:da:eb:3f:da', "kevin": '2c:33:61:aa:e6:a9', "nora": 'cc:29:f5:b8:2d:a2', conn.close()
"inge": 'ac:5f:3e:28:2a:c0', "bazzinga": 'f0:79:59:70:a4:a6' }
conn = sqlite3.connect('home.db') return returnList
c = conn.cursor()
getAddr() def convertTime(seconds):
getTimes() delta = time() - seconds
# checkNameExistance() if delta >= 86400:
return str(delta//86400) + ' days'
# def whosHome(): elif delta >= 3600:
# # Get the output of the command 'arp-scan -l' if delta//3600 < 10:
# arpOutput = subprocess.check_output("sudo arp-scan -l", shell=True).split('\n') parent = str(delta//3600)
# # Strip away first three lines and last 5 lines child = str((delta - (3600 * (delta//3600)))//60)
# arpOutput = arpOutput[2:-4] if len(child) == 1:
child = '0' + child
# # Open file times.txt and read lines to 'logFile' return parent + ':' + child + ' hours'
# with open('/home/kevin/homeCheck/times.txt', 'r') as file: else:
# logFile = file.readlines() return str(delta//3600) + ' hours'
elif delta >= 60:
# i = 0 return str(delta//60) + ' minutes'
# # Go through each element in list 'addr' else:
# for mac in addr: return str(delta) + ' seconds'
# # Then iterate through each line in arpOutput
# for line in arpOutput:
# line_mac = str(line.split('\t')[1])
# # For each line we check after a matching mac addr
# if (mac in line_mac):
# logFile[i] = str(users[i]) + ':' + str(time()) + '\n'
# # print mac
# # print users[i]
# # print str(i) + '\n'
# # print logFile
# i+=1
# # Write changes to file
# with open('/home/kevin/homeCheck/times.txt', 'w') as file:
# file.writelines(logFile)
# print logFile
# whosHome() def updateTimes():
curTime = time()
conn = sqlite3.connect('home.db')
c = conn.cursor()
online = list(set(getOnlineClients()) & set(getAddr()))
for adr in online:
c.execute('UPDATE lastonline SET timesince='+ str(curTime) +' WHERE clientadr="cc:29:f5:b8:2d:a2"')
conn.commit()
conn.close()