-
Notifications
You must be signed in to change notification settings - Fork 1
/
lightning_web.py
164 lines (128 loc) · 4.64 KB
/
lightning_web.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
import os
from flask import Flask, render_template
from flask.ext.socketio import SocketIO, emit
from RPi_AS3935 import RPi_AS3935
import RPi.GPIO as GPIO
import time
import datetime
import ConfigParser
from random import randint
app = Flask(__name__, static_url_path='/static')
app.config['SECRET_KEY'] = 'super secret key!'
socketio = SocketIO(app)
GPIO.setmode(GPIO.BCM)
config = ConfigParser.RawConfigParser()
config.read('settings.cfg')
editable_fields = ['disturber', 'noise-floor', 'min-strikes', 'indoors']
date_format = config.get('interface', 'date_format')
sensor = RPi_AS3935(address=int(config.get('as3935', 'address'), 0), bus=config.getint('pi', 'bus'))
sensor.calibrate(tun_cap=int(config.get('as3935', 'tuning_cap'), 0))
event_history = list()
debug_mode = config.getboolean('interface', 'debug_mode')
def register_strike(channel):
timestamp = datetime.datetime.now().strftime(date_format)
time.sleep(0.004)
global sensor, event_history
reason = sensor.get_interrupt()
if reason == 0x08:
data = {
'type': 'strike',
'distance': sensor.get_distance(),
'timestamp': timestamp,
}
elif reason == 0x04:
data = {
'type': 'disturber',
'message': 'Disturber',
'timestamp': timestamp,
}
elif reason == 0x01:
data = {
'type': 'noise',
'message': 'Noise',
'timestamp': timestamp,
}
try:
event_history.append(data)
event_history = event_history[-5:]
socketio.emit('sensor-interrupt',
data,
namespace='/lightning_sensor'
)
except Exception:
pass
GPIO.setup(17, GPIO.IN)
GPIO.add_event_detect(17, GPIO.RISING, callback=register_strike)
@app.route('/')
def index():
settings = dict()
settings['disturber'] = sensor.get_mask_disturber()
settings['indoors'] = sensor.get_indoors()
settings['min_strikes'] = sensor.get_min_strikes()
settings['noise_floor'] = sensor.get_noise_floor()
settings['read_only'] = config.getboolean('interface', 'read_only')
settings['editable_fields'] = editable_fields
settings['debug_mode'] = debug_mode
settings['units'] = config.get('interface', 'units')
return render_template('index.html', settings=settings)
@socketio.on('connect', namespace='/lightning_sensor')
def connected():
timestamp = datetime.datetime.now().strftime(date_format)
for event in event_history:
emit('lightning', event)
emit('sensor-interrupt',
{
'type': 'message',
'message': 'Connected',
'timestamp': timestamp,
})
@socketio.on('simulate-lightning', namespace='/lightning_sensor')
def simulate_lightning(json):
global event_history
timestamp = datetime.datetime.now().strftime(date_format)
data = {
'type': 'strike',
'distance': randint(0, 15),
'timestamp': timestamp,
}
event_history.append(data)
event_history = event_history[-5:]
emit('sensor-interrupt',
data,
broadcast=True,
)
@socketio.on('adjust-setting', namespace='/lightning_sensor')
def adjust_setting(json):
response = {'setting': json['setting']}
if config.getboolean('interface', 'read_only'):
pass
else:
if json['setting'] == 'disturber':
disturber = not sensor.get_mask_disturber()
sensor.set_mask_disturber(disturber)
response['state'] = 'Masked' if disturber else 'Unmasked'
elif json['setting'] == 'indoors':
indoors = not sensor.get_indoors()
sensor.set_indoors(indoors)
response['state'] = 'Indoors' if indoors else 'Outdoors'
elif json['setting'] == 'noise-floor':
noise_floor = sensor.get_noise_floor()
noise_floor = (noise_floor + 1) % 8
sensor.set_noise_floor(noise_floor)
response['state'] = sensor.get_noise_floor()
elif json['setting'] == 'min-strikes':
min_strikes = sensor.get_min_strikes()
if min_strikes == 1:
sensor.set_min_strikes(5)
elif min_strikes == 5:
sensor.set_min_strikes(9)
elif min_strikes == 9:
sensor.set_min_strikes(16)
elif min_strikes == 16:
sensor.set_min_strikes(1)
response['state'] = sensor.get_min_strikes()
emit('adjust-setting', response, broadcast=True)
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.debug = debug_mode
socketio.run(app, host='0.0.0.0', port=port)