-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartfan.py
215 lines (182 loc) · 6.42 KB
/
smartfan.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
import threading
import multiprocessing
import time
import json
import signal
import sys
import os
from flask import Flask, request
import config
import datetime
import hardware
from sensor import sensor
from outside_data import outside_data
import networking
from profile import profile
'''
'''
network_test_info = 'not_done'
application = Flask(__name__, static_url_path='')
application.debug = True
hardware = hardware.Hardware(profile)
lock = threading.Lock()
def update_data():
profile.inside_temp = str(sensor.get_inside_temp())
if profile.lon is not None and profile.lat is not None and profile.connect_button['active']:
temp, error = outside_data.get_outside_temp(profile.lat, profile.lon)
profile.weather_api_error = error
profile.outside_temp = str(temp)
if profile.set_temperature_preferences_button['active'] and not profile.turn_automatic_mode_on_button['active']:
profile.turn_automatic_mode_on_button['disabled'] = False
@application.route("/hard_reset")
def hard_reset():
turn_fan_off()
profile.hard_reset()
return get_status()
@application.route("/get_status")
def get_status():
update_data()
status = profile.export()
profile.save_state()
return json.dumps(status)
@application.route("/")
def start():
return application.send_static_file('page.html')
@application.route('/set_location', methods=['POST'])
def set_location():
lat = request.form['lat']
lon = request.form['lon']
profile.lat = lat
profile.lon = lon
profile.set_location_button['active'] = True
return get_status()
@application.route('/set_temperature_preferences', methods=['POST'])
def set_temperature_preferences():
min_temp = request.form['min_temp']
temp_margin = request.form['temp_margin']
min_cycle_time = request.form['min_cycle_time']
profile.min_cycle_time = min_cycle_time
profile.min_temp = min_temp
profile.temp_margin = temp_margin
profile.set_temperature_preferences_button['active'] = True
status = get_status()
return status
@application.route('/connect', methods=['POST'])
def connect():
password = request.form['password']
network_name = request.form['network_name']
profile.password = password
profile.network_name = network_name
profile.connect_button['active'] = False
profile.connect_button['spinning'] = True
profile.connect_button['disabled'] = True
networking.set_access_point_info(network_name, password)
thread = threading.Thread(target=networking.set_up_network, name='check_info')
thread.start()
return 'bla'
@application.route("/network_status")
def network_status():
status = get_status()
return status
@application.route("/turn_off_network")
def turn_off_network():
status = get_status()
error = networking.turn_off_access_point()
return error
@application.route("/turn_fan_on")
def turn_fan_on():
lock.acquire()
profile.turn_fan_on_button['active'] = True
profile.turn_automatic_mode_on_button['active'] = False
profile.turn_fan_off_button['active'] = False
profile.connect_button['disabled'] = False
profile.set_location_button['disabled'] = False
profile.set_temperature_preferences_button['disabled'] = False
profile.mode = "manual on"
hardware.turn_fan_on()
status = get_status()
lock.release()
return status
@application.route("/turn_fan_off")
def turn_fan_off_endpoint():
status = turn_fan_off()
return status
def turn_fan_off():
lock.acquire()
profile.turn_fan_on_button['active'] = False
profile.turn_automatic_mode_on_button['active'] = False
profile.turn_fan_off_button['active'] = True
profile.connect_button['disabled'] = False
profile.set_location_button['disabled'] = False
profile.set_temperature_preferences_button['disabled'] = False
profile.mode = "manual off"
hardware.turn_fan_off()
status = get_status()
lock.release()
return status
@application.route("/turn_automatic_mode_on")
def turn_automatic_mode_on_end_point():
lock.acquire()
turn_automatic_mode_on()
status = get_status()
lock.release()
return status
def turn_automatic_mode_on():
profile.turn_automatic_mode_on_button['active'] = True
profile.turn_automatic_mode_on_button['disabled'] = True
profile.turn_fan_on_button['active'] = False
profile.turn_fan_off_button['active'] = False
profile.connect_button['disabled'] = True
profile.set_location_button['disabled'] = True
profile.set_temperature_preferences_button['disabled'] = True
profile.mode = "automatic"
t = threading.Thread(target=run_automatic_mode, name='check_info')
t.start()
def run_automatic_mode():
'''
'''
while True:
lock.acquire()
if not profile.turn_automatic_mode_on_button['active']:
lock.release()
break
outside_temp, error = outside_data.get_outside_temp(profile.lat, profile.lon)
profile.weather_api_error = error
inside_temp = sensor.get_inside_temp()
inside_temp = float(inside_temp)
outside_temp = float(outside_temp)
if config.record_temp:
record_temp(inside_temp, outside_temp)
cooler_outside = outside_temp < inside_temp - float(profile.temp_margin)
if cooler_outside and inside_temp > float(profile.min_temp):
hardware.turn_fan_on()
else:
hardware.turn_fan_off()
lock.release()
time.sleep(float(profile.min_cycle_time))
def record_temp(inside_temp, outside_temp):
with open(config.temperature_file_path, 'a') as temp_file:
temp_data = {'inside_temp': inside_temp,
'outside_temp': outside_temp,
'time': str(datetime.datetime.utcnow()),
'state': profile.fan_state,
}
temp_data = json.dumps(temp_data)
temp_file.write(temp_data + '\n')
def handle_boot():
# make sure connection still works
if profile.connect_button['active']:
networking.set_up_network()
if profile.turn_automatic_mode_on_button['active']:
if profile.connect_button['active']:
turn_automatic_mode_on()
else: # reconnecting after reboot failed so swap to off mode
turn_fan_off()
elif profile.turn_fan_on_button['active']:
turn_fan_on()
else:
turn_fan_off()
handle_boot()
if __name__ == "__main__":
pass
#application.run(host='0.0.0.0', debug=True)