-
Notifications
You must be signed in to change notification settings - Fork 0
/
inoutboard.py
80 lines (70 loc) · 2.17 KB
/
inoutboard.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
###### Sourced in part from https://raw.github.com/paulbarber/raspi-gpio/master/inoutboard.py ######
###### Threading info from https://pymotw.com/2/threading/ #######
#!/usr/bin/python
import bluetooth
import time
import datetime
import threading
import logging
#logging.basicConfig(level=logging.DEBUG, format='[%(levelname)s] (%(threadName)-10s) %(message)s',)
def worker(num):
i = num
#logging.debug(users[i])
print "Looking for " + users[i] + "..."
result = bluetooth.lookup_name(bids[i], timeout = 2)
localTime = time.strftime("%I:%M", time.localtime())
ampm = time.strftime("%p", time.localtime()).lower()
ret = ''
if localTime[0] == '0':
splitTime = localTime.split('0', 1)
ret = splitTime[1] + ampm
else:
ret = localTime + ampm
times[i] = ret
if result != None:
status[i] = "IN"
else:
status[i] = "OUT"
#lock
data[i] = "%s %s %s %s|\n" % (users[i], status[i], times[i], bids[i])
#unlock
#logging.debug('Done')
print "Done"
#return
users = ["user1", "user2", "user3", "user4"]
userCount = len(users)
data = [""] * userCount
status = [""] * userCount
times = [""] * userCount
bids = ['D4:F4:6F:32:B0:AF', 'C8:69:CD:6A:F2:21', '78:7E:61:52:32:2B', 'C9:69:CD:6A:D2:21']
while True:
print "Checking " + time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())
localTime = time.strftime("%I:%M", time.localtime())
ampm = time.strftime("%p", time.localtime()).lower()
ret = ''
if localTime[0] == '0':
splitTime = localTime.split('0', 1)
ret = splitTime[1] + ampm
else:
ret = localTime + ampm
#print ret
threads = []
for i in range(userCount):
t = threading.Thread(target=worker, args=(i,))
threads.append(t)
t.start()
time.sleep(5)
file = open("data.txt", "r+")
text = file.read()
arr = text.splitlines()
newArr = [""] * userCount
for i in range(0, userCount):
if status[i] not in arr[i]:
newArr[i] = data[i]
else:
newArr[i] = arr[i] + '\n'
file.seek(0)
for i in range(0, userCount):
file.write(newArr[i])
file.close()
#time.sleep(2)