-
Notifications
You must be signed in to change notification settings - Fork 0
/
homeiot.py
115 lines (97 loc) · 3.14 KB
/
homeiot.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import os
import glob
import time
import json
from bluetooth import *
from firebase import firebase
server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)
# used json.dumps to get pretty json
# used json.loads to load json from a string
# used json.load to load json from json object
# used ntplib to download time from a remote server
# lesson: rpi automatically updates it's time from ntp server setting it to utc
# lesson: if you have included datetime like [from datetime import datetime] then no need to say datetime.datetime... same for all libs
port = server_sock.getsockname()[1]
username = ''
FIREBASE_REF = 'https://homeiot.firebaseio.com/'
firebase = firebase.FirebaseApplication(FIREBASE_REF, None)
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
advertise_service( server_sock, "AquaPiServer",
service_id = uuid,
service_classes = [ uuid, SERIAL_PORT_CLASS ],
profiles = [ SERIAL_PORT_PROFILE ],
# protocols = [ OBEX_UUID ]
)
def init():
global username
with open('user/config.json') as json_file:
user_data = json.load(json_file)
username = user_data['username']
print 'Logging in as ' + username + '...'
return;
'''
def get_utc_time():
import ntplib, datetime
import os
from time import ctime
client = ntplib.NTPClient()
response = client.request('pool.ntp.org')
d = datetime.datetime.strptime(ctime(response.tx_time), '%a %b %d %H:%M:%S %Y')
e = d.strftime('%a %b %d +%H:%M:%S %Y')
timestamp = d.strftime('%Y%m%d%H%M%S')
os.system('date -s %s' % d)
print d
return timestamp;
'''
def get_time():
from datetime import datetime
now = datetime.now()
d = now.strftime('%Y%m%d%H%M%S')
return d;
def new_unlock(unlocker):
return;
def sync_lock_history():
#try:
global username
history_path = username + '/lock' + '/history'
with open('user/lock/history.json') as json_file:
history_local = json.load(json_file)
print username
history_remote = firebase.get(history_path, None)
print json.dumps(history_remote)
if history_local != history_remote:
for key in history_remote:
print json.dumps(history_remote[key])
#test_unlock = {'username': 'shehzad', 'time': get_utc_time()}
firebase.post(history_path, { get_time() : 'badar' })
return;
#except:
# print 'Error encountered'
# try:
def search_mobile():
print "Waiting for connection on RFCOMM channel %d" % port
client_sock, client_info = server_sock.accept()
print "Accepted connection from ", client_info
try:
data = client_sock.recv(1024)
if len(data) == 0: return;
print "received [%s]" % data
except IOError:
pass
except KeyboardInterrupt:
print "disconnected"
client_sock.close()
server_sock.close()
print "all done"
return;
init()
sync_lock_history()
get_time()
'''
get_utc_time()
sync_lock_history()
while True:
check_mobile()
'''