-
Notifications
You must be signed in to change notification settings - Fork 1
/
DB410c_server.py
300 lines (258 loc) · 9.39 KB
/
DB410c_server.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 5 15:27:38 2016
@author: Anum Sheraz
"""
import json, time, psutil
from flask import render_template, request
import flask, socket
import threading
from wifi import Cell
from GPIOLibrary import GPIOProcessor
GP = GPIOProcessor()
app = flask.Flask(__name__)
ipaddress=socket.gethostbyname(socket.gethostname())
#print ipaddress
my_ip = "localhost"
my_port = 80
count = 0
red_led_T_lock = threading.Lock()
yellow_led_T_lock = threading.Lock()
green_led_T_lock = threading.Lock()
my_dict={
"status":{
"Computation":{
"Cores":4,
"CPU_Usage":"35%",
"Memory_usage":"15.04%"
},
"Networks":{
"Bluetooth":True,
"Location_Service":"true",
"Wifi":{
"SSID": "\"Samsung S4\"",
"status": True
}
},
"GPIO":[
{
"GPIO number": "23",
"value":0,
"Direction":"out",
},
{
"GPIO number": "24",
"value":0,
"Direction":"out"
},
{
"GPIO number": "25",
"value":0,
"Direction":"out"
},
{
"GPIO number": "26",
"value":0,
"Direction":"out"
},
{
"GPIO number": "27",
"value":0,
"Direction":"out"
},
{
"GPIO number": "28",
"value":0,
"Direction":"out"
},
{
"GPIO number": "29",
"value":0,
"Direction":"out"
},
{
"GPIO number": "30",
"value":0,
"Direction":"out"
},
{
"GPIO number": "31",
"value":0,
"Direction":"out"
},
{
"GPIO number": "32",
"value":0,
"Direction":"out"
},
{
"GPIO number": "33",
"value":0,
"Direction":"out"
},
],
"Peripherals":{
"USB":[
{
"Device":"usb devices"
}
]
},
"Last_update":"time"
}
}
try:
a=Cell.all('wlan0')
if len(a) > 0:
ssid = a[0] #Cell(ssid=Maxcotec)
ssid = str(ssid)
ssid = ssid[10:ssid.index(")")]
my_dict["status"]["Networks"]["Wifi"]["SSID"] = ssid
my_dict["status"]["Networks"]["Wifi"]["status"] = "Connected"
else:
my_dict["status"]["Networks"]["Wifi"]["SSID"] = "--"
my_dict["status"]["Networks"]["Wifi"]["status"] = "Not Connected"
except Exception as e:
print e
@app.route('/status/update_status', methods=['POST'])
def keep_alive():
global my_dict
my_dict["status"]["Last_update"] = (time.ctime())
#print my_dict["status"].get("Last_update")
parsed_json = json.dumps(my_dict)
return (parsed_json)
@app.route("/", methods=['GET','POST'])
def login():
#print 'HELLO DragonBoard'
if request.args.get('led'):
led=request.args.get('led')
if request.args.get('times'):
times=request.args.get('times')
print ' times:', times
led_result=LED_control(led,times)
led_json = {"status":led_result}
return flask.jsonify(**led_json)
else:
led_result=LED_control(led,"1")
led_json = {"status":led_result}
return flask.jsonify(**led_json)
else:
print 'got no LED'
return render_template('Hello.html')
@app.route("/status")
def status():
global my_dict
my_dict["status"]["Last_update"] = (time.ctime())
parsed_json = json.dumps(my_dict ,indent=4, separators=(',', ':'), sort_keys=True)
print parsed_json
return (parsed_json)
@app.route("/status/preety")
def preety():
global my_dict
my_dict["status"]["Last_update"] = (time.ctime())
return flask.jsonify(**my_dict)
@app.route("/status/live")
def live():
print 'sending live.html'
return render_template('live.html')
def processes():
global my_dict
while 1:
time.sleep(0.05)
my_dict["status"]["Computation"]["Cores"] = psutil.NUM_CPUS
CPU_raw = psutil.cpu_percent()
if CPU_raw != 0:
CPU_Usage = (CPU_raw/(100))*100
my_dict["status"]["Computation"]["CPU_Usage"] = str(CPU_Usage) + "%"
RAM = psutil.virtual_memory()
my_dict["status"]["Computation"]["Memory_usage"] = str(RAM[2]) + "%"
def LED_control(led,time):
global red_led_T_lock, green_led_T_lock, yellow_led_T_lock
if led == 'red':
time = int(time)
if red_led_T_lock.locked() == False:
red_thread = threading.Thread(target=red_led, args=(time,))
red_thread.start()
return 'ok'
else:
return 'red LED already in use'
if led == 'yellow':
time = int(time)
if yellow_led_T_lock.locked() == False:
yellow_thread = threading.Thread(target=yellow_led, args=(time,))
yellow_thread.start()
return 'ok'
else:
return 'yellow LED already in use'
if led == 'green':
time = int(time)
if green_led_T_lock.locked() == False:
green_thread = threading.Thread(target=green_led, args=(time,))
green_thread.start()
return 'ok'
else:
return 'green LED already in use'
if led == 'all':
time = int(time)
if red_led_T_lock.locked() == False and green_led_T_lock.locked() == False and yellow_led_T_lock.locked() == False:
red_thread = threading.Thread(target=red_led, args=(time,))
yellow_thread = threading.Thread(target=yellow_led, args=(time,))
green_thread = threading.Thread(target=green_led, args=(time,))
red_thread.start()
green_thread.start()
yellow_thread.start()
return 'ok'
else:
return 'All LED\'s are already in use'
def red_led(times):
Pin25 = GP.getPin25()
Pin25.out()
red_led_T_lock.acquire()
try:
for n in range(times):
print 'RED high ' + str(n+1)
time.sleep(0.5)
Pin25.high()
my_dict["status"]["GPIO"][2]["value"] = Pin25.getValue()
time.sleep(0.5)
Pin25.low()
my_dict["status"]["GPIO"][2]["value"] = Pin25.getValue()
finally:
Pin25.closePin()
red_led_T_lock.release()
def yellow_led(times):
Pin24 = GP.getPin24()
Pin24.out()
yellow_led_T_lock.acquire()
try:
for n in range(times):
print 'yellow high ' + str(n+1)
time.sleep(0.5)
Pin24.high()
my_dict["status"]["GPIO"][1]["value"] = Pin24.getValue()
time.sleep(0.5)
Pin24.low()
my_dict["status"]["GPIO"][1]["value"] = Pin24.getValue()
finally:
Pin24.closePin()
yellow_led_T_lock.release()
def green_led(times):
Pin23 = GP.getPin23()
Pin23.out()
green_led_T_lock.acquire()
try:
for n in range(times):
print 'green high ' + str(n+1)
time.sleep(0.5)
Pin23.high()
my_dict["status"]["GPIO"][0]["value"] = Pin23.getValue()
time.sleep(0.5)
Pin23.low()
my_dict["status"]["GPIO"][0]["value"] = Pin23.getValue()
finally:
Pin23.closePin()
green_led_T_lock.release()
if __name__ == '__main__':
get_data = threading.Thread(target=processes)
get_data.start()
app.run(my_ip, threaded=True, debug=False, port=my_port)