-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
106 lines (82 loc) · 2.13 KB
/
main.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
# coding=utf-8
# This is a Web of Things Middleware written with Python programming
# language. It mainly provides the function of gateway registration,
# device/resource registration, data uploading, heartbeat uploading and
# so on.
# Authors: Chen Shi & Yang Ping (BUPT MineLab-818)
import sys
import time
import json
import urllib
import init
import common
from gateway import WrtGateway
from heartbeat import HBThread
#from camera_thread import CameraThread
F_DEL_MW = False
F_UPT_MW = False
# prevent the error: 'ascii' codec can't decode byte, which will happen when aj-server
# send data
reload(sys)
sys.setdefaultencoding('utf8')
def parse_para():
global F_DEL_MW
global F_UPT_MW
for i in range(1,len(sys.argv)):
if sys.argv[i] == '-d':
F_DEL_MW = True
elif sys.argv[i] == '-upt':
F_UPT_MW = True
elif sys.argv[i] == '-h':
print 'available options:'
print '-d\tdelete gateway'
print '-upt\tforce update gateway property'
sys.exit(0)
else:
print 'illegal parameter'
sys.exit(-1)
if __name__ == '__main__':
# parse prompt parameter
parse_para()
init.rd_local_cfg()
# mail is given
gw = WrtGateway('mail')
# if hwid exists,then it will fail
gw.reg_hwid()
# register mwid, which is given by platform according to email and hwid
gw.reg_mwid()
if F_DEL_MW:
gw.del_mw()
sys.exit(0)
# update property of gateway
gw.update_mw(F_UPT_MW)
# read the device and resources configurations made by middleware users
gw.get_dev_property()
hb_thread = HBThread(5)
hb_thread.start()
while True:
# 防止读的时候,另一进程只写了部分数据
while True:
try:
jsondata = json.loads(common.read_file("data.txt"))
time.sleep(1)
break
except:
pass
print 'jsondata:',jsondata
print
for name in gw.res_dict.keys():
if jsondata.has_key(name):
lst = gw.res_dict[name]
if lst[0] == '0':
# 传字符
gw.upload_data(lst[1],str(jsondata[name]))
#gw.get_sensordata(lst[1])
elif lst[0] == '1':
# 传图片
gw.upload_image(lst[1],common.read_image(str(jsondata[name])))
else:
pass
time.sleep(2)
time.sleep(20)
hb_thread.join()