-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
366 lines (324 loc) · 12.2 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2016 NXEZ.COM.
# http://www.nxez.com
#
# Licensed under the GNU General Public License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.gnu.org/licenses/gpl-2.0.html
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
__author__ = 'Spoony'
__version__ = 'version 0.0.1'
__license__ = 'Copyright (c) 2016 NXEZ.COM'
from sakshat import SAKSHAT
import time
import commands
import sys, urllib, urllib2, json
import requests
import re
#Declare the SAKS Board
SAKS = SAKSHAT()
__current_mode = 0
__current_command = 0
__modes = [ 'display', 'dummy' ]
__commands = { 'display': [ '.time', '.cputemp', '.roomtemp', '.citytemp', '.pm25', '.stockmarketindex' ], 'dummy': [ '.dummy', '.dummy' ] }
__commands = { 'display': [ '.time', '.cputemp', '.roomtemp', '.stockmarketindex' ], 'dummy': [ '.dummy', '.dummy' ] }
__current_command_name = ''
__processing_list = {}
def display_cpu_temp():
global __current_command_name
while True:
if __current_command_name == 'display.cputemp':
t = get_cpu_temp()
# Uncomment if need print the temperature to screen
#print("%.2f" % t)
SAKS.digital_display.show("%.2f" % t)
# set any value you want
if t > 50:
SAKS.buzzer.beepAction(0.02, 0.02, 20)
time.sleep(1)
else:
__processing_list['display.cputemp'] = False
break
time.sleep(0.5)
def display_pm25():
global __current_command_name
while True:
if __current_command_name == 'display.pm25':
pm25 = get_pm25()
if pm25 == -1:
time.sleep(30)
continue
#严重污染,红灯亮蜂鸣器Beep
if pm25 >= 250:
SAKS.ledrow.off()
SAKS.ledrow.on_for_index(7)
SAKS.buzzer.beepAction(0.05,0.05,3)
#重度污染,红灯亮
if pm25 < 250:
SAKS.ledrow.off()
SAKS.ledrow.on_for_index(7)
#中度污染,红灯亮
if pm25 < 150:
SAKS.ledrow.off()
SAKS.ledrow.on_for_index(7)
#轻度污染,黄灯亮
if pm25 < 115:
SAKS.ledrow.off()
SAKS.ledrow.on_for_index(5)
#良,1绿灯亮
if pm25 < 75:
SAKS.ledrow.off()
SAKS.ledrow.on_for_index(3)
#优,2绿灯亮
if pm25 < 35:
SAKS.ledrow.off()
SAKS.ledrow.on_for_index(3)
SAKS.ledrow.on_for_index(2)
#print (("%4d" % pm25).replace(' ','#'))
#数码管显示PM2.5数值
SAKS.digital_display.show(("%4d" % pm25).replace(' ','#'))
time.sleep(1800)
else:
__processing_list['display.pm25'] = False
break
time.sleep(0.5)
def display_city_temp():
global __current_command_name
while True:
if __current_command_name == 'display.citytemp':
temp = get_city_temp()
#返回值为 -128.0 表示读取失败
if temp == -128.0 :
#10秒后再次尝试
time.sleep(30)
continue
print (("%5.1f" % temp).replace(' ','#'))
#数码管显示温度数值,5位(含小数点)、精确到小数点1后1位
SAKS.digital_display.show(("%5.1f" % temp).replace(' ','#'))
time.sleep(1800)
else:
__processing_list['display.citytemp'] = False
break
time.sleep(0.5)
def display_room_temp():
global __current_command_name
while True:
if __current_command_name == 'display.roomtemp':
#从 ds18b20 读取温度(摄氏度为单位)
temp = SAKS.ds18b20.temperature
#返回值为 -128.0 表示读取失败
if temp == -128.0 :
#10秒后再次尝试
time.sleep(10)
continue
print (("%5.1f" % temp).replace(' ','#'))
#数码管显示温度数值,5位(含小数点)、精确到小数点1后1位
SAKS.digital_display.show(("%5.1f" % temp).replace(' ','#'))
time.sleep(5)
else:
__processing_list['display.roomtemp'] = False
break
time.sleep(0.5)
def display_time():
global __current_command_name
__dp = True
__alarm_beep_status = False
__alarm_beep_times = 0
# 在这里设定闹钟定时时间
__alarm_time = "18:10:00"
while True:
if __current_command_name == 'display.time':
# 以下代码获取系统时间、时、分、秒、星期的数值
t = time.localtime()
h = t.tm_hour
m = t.tm_min
s = t.tm_sec
w = time.strftime('%w',t)
#print h,m,s,w
print "%02d:%02d:%02d" % (h, m, s)
if ("%02d:%02d:%02d" % (h, m, s)) == __alarm_time:
__alarm_beep_status = True
__alarm_beep_times = 0
if __dp:
# 数码管显示小时和分,最后一位的小点每秒闪烁一次
SAKS.digital_display.show(("%02d%02d." % (h, m)))
# 判断是否应该响起闹钟
if __alarm_beep_status:
SAKS.buzzer.on()
#SAKS.ledrow.items[6].on()
__alarm_beep_times = __alarm_beep_times + 1
# 30次没按下停止键则自动停止闹铃
if __alarm_beep_times > 30:
__alarm_beep_status = False
__alarm_beep_times = 0
else:
SAKS.digital_display.show(("%02d%02d" % (h, m)))
if __alarm_beep_status:
SAKS.buzzer.off()
#SAKS.ledrow.items[6].off()
__dp = not __dp
#time.sleep(0.5)
else:
__processing_list['display.time'] = False
break
time.sleep(0.5)
def display_stock_market_index():
global __current_command_name
while True:
if __current_command_name == 'display.stockmarketindex':
pattern = re.compile(r'(\d*\.\d*)')
r = requests.get('http://hq.sinajs.cn/list=s_sh000001', timeout = 5)
#print r.text.encode('utf-8')
#print r.encoding
match = pattern.findall(r.text)
if match:
SAKS.digital_display.show(("%04d" % int(float(match[0]))))
#return match[0]
else:
SAKS.digital_display.show("###0")
#return 0
else:
__processing_list['display.stockmarketindex'] = False
break
time.sleep(0.5)
def alarm_process():
pass
def power_management_process():
pass
def dip_switch_status_changed_handler(status):
'''
called while the status of dip switch changed
:param status: current status
:return: void
'''
print('on_dip_switch_status_changed:')
print(status)
pass
def tact_event_handler(pin, status):
'''
called while the status of tacts changed
:param pin: pin number which stauts of tact is changed
:param status: current status
:return: void
'''
print('tact_event_handler')
print("%d - %s" % (pin, status))
global __current_command
global __current_command_name
global __processing_list
if __current_command >= len(__commands['display']):
__current_command = 0
__current_command_name = __modes[__current_mode] + __commands[__modes[__current_mode]][__current_command]
print(__current_command_name)
__current_command += 1
if __current_command_name == 'display.cputemp' and not __processing_list['display.cputemp']:
time.sleep(0.05)
__processing_list['display.cputemp'] = True
display_cpu_temp()
return
elif __current_command_name == 'display.pm25' and not __processing_list['display.pm25']:
time.sleep(0.05)
__processing_list['display.pm25'] = True
display_pm25()
return
elif __current_command_name == 'display.citytemp' and not __processing_list['display.citytemp']:
time.sleep(0.05)
__processing_list['display.citytemp'] = True
display_city_temp()
return
elif __current_command_name == 'display.roomtemp' and not __processing_list['display.roomtemp']:
time.sleep(0.05)
__processing_list['display.roomtemp'] = True
display_room_temp()
return
elif __current_command_name == 'display.time' and not __processing_list['display.time']:
time.sleep(0.05)
__processing_list['display.time'] = True
display_time()
return
elif __current_command_name == 'display.stockmarketindex' and not __processing_list['display.stockmarketindex']:
time.sleep(0.05)
__processing_list['display.stockmarketindex'] = True
display_stock_market_index()
return
else:
print('ELSE:' + __current_command_name)
#def handler(signum, frame):
# SAKS.digital_display.off()
# print "receive a signal %d"%(signum)
def get_cpu_temp():
tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
cpu_temp = tempFile.read()
tempFile.close()
return float(cpu_temp)/1000
# Uncomment the next line if you want the temp in Fahrenheit
#return float(1.8*cpu_temp)+32
def get_gpu_temp():
gpu_temp = commands.getoutput( '/opt/vc/bin/vcgencmd measure_temp' ).replace( 'temp=', '' ).replace( '\'C', '' )
return float(gpu_temp)
# Uncomment the next line if you want the temp in Fahrenheit
# return float(1.8* gpu_temp)+32
'''
cityid 和 key 需要根据实际情况替换
参考 http://www.heweather.com/documents/api
http://www.heweather.com/documents/cn-city-list
'''
weather_url = 'https://api.heweather.com/x3/weather?cityid=CN101020100&key=xxx'
def get_pm25():
global weather_url
req = urllib2.Request(weather_url)
resp = urllib2.urlopen(req, timeout = 5)
content = resp.read()
if(content):
weatherJSON = json.JSONDecoder().decode(content)
#print(content)
try:
if weatherJSON['HeWeather data service 3.0'][0]['status'] == "ok":
if weatherJSON['HeWeather data service 3.0'][0].has_key('aqi'):
print(weatherJSON['HeWeather data service 3.0'][0]['aqi']['city']['pm25'])
return int(weatherJSON['HeWeather data service 3.0'][0]['aqi']['city']['pm25'])
else:
return -1
else:
return -1
except:
return -1
def get_city_temp():
global weather_url
req = urllib2.Request(weather_url)
resp = urllib2.urlopen(req, timeout = 5)
content = resp.read()
if(content):
weatherJSON = json.JSONDecoder().decode(content)
#print(content)
try:
if weatherJSON['HeWeather data service 3.0'][0]['status'] == "ok":
if weatherJSON['HeWeather data service 3.0'][0].has_key('now'):
print(weatherJSON['HeWeather data service 3.0'][0]['now']['tmp'])
return int(weatherJSON['HeWeather data service 3.0'][0]['now']['tmp'])
else:
return -128
else:
return -128
except:
return -128
if __name__ == "__main__":
print("main")
__processing_list['display.cputemp'] = False
__processing_list['display.pm25'] = False
__processing_list['display.citytemp'] = False
__processing_list['display.roomtemp'] = False
__processing_list['display.time'] = False
__processing_list['display.stockmarketindex'] = False
SAKS.dip_switch_status_changed_handler = dip_switch_status_changed_handler
SAKS.tact_event_handler = tact_event_handler
input("Enter any keys to exit...")