-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweb_controller.py
More file actions
executable file
·130 lines (97 loc) · 4.07 KB
/
Copy pathweb_controller.py
File metadata and controls
executable file
·130 lines (97 loc) · 4.07 KB
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
#!/usr/bin/env python
#
# Raspberry Pi Home Security System - web_controller
__author__ = "Caleb Madrigal"
import zmq
import settings
import logging
from common import setup_logger
from flask import Flask, request
from flask.ext.restful import Resource, Api
logger = setup_logger("web", settings.web_log_file, logging.DEBUG)
############################################################################ Interaction with master
def send_recv_message(json_msg):
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect(settings.web_controller_conn_str)
socket.send_json(json_msg)
return socket.recv_json()
def set_switch(switch_id, switch_value):
logger.info("Request master set switch {0} to {1}".format(switch_id, switch_value))
state = send_recv_message({'command':'set_switch', 'switch_id':switch_id, 'value':switch_value})
return state
def set_all_switches(switch_value):
logger.info("Request master set all to {0}".format(switch_value))
state = send_recv_message({'command':'set_all', 'value':switch_value})
return state
def set_automation_mode(automation_mode_value):
logger.info("Request master set automation mode to {0}".format(automation_mode_value))
state = send_recv_message({'command':'set_automation_mode', 'value':automation_mode_value})
return state
def get_state():
logger.debug("Get state")
state = send_recv_message({'command':'get_state'})
return state
# TODO: add alarm:on, alarm:off
############################################################################################ Classes
class StateController(Resource):
def get(self):
state = get_state()
return state
class AutomationModeController(Resource):
def get(self):
state = get_state()
return {'automation_mode': state['automation_mode']}
def put(self):
automation_mode_value = request.form['value'].lower()
if automation_mode_value in ['on', 'off']:
state = set_automation_mode(automation_mode_value)
return state, 200
else:
return {'error': 'Automation mode must be on or off'}, 400
class AllController(Resource):
def put(self, switch_value):
switch_value = switch_value.lower()
if switch_value in ['on', 'off']:
state = set_all_switches(switch_value)
return state, 200
else:
return {'error': 'Switch value must be on or off'}, 400
class SwitchController(Resource):
def get(self, switch_id):
switch_id = switch_id.lower()
state = get_state()
if switch_id == 'list' or switch_id == 'all':
return state['switches']
if switch_id in state['switches']:
return {switch_id: state['switches'][switch_id]}
else:
return {'error': 'Invalid switch name'}, 400
def put(self, switch_id):
switch_id = switch_id.lower()
switch_value = request.form['value'].lower()
if switch_id not in settings.switches:
valid_switches = ','.join(settings.switches)
return {'error': 'Invalid switch number - must be one of these: '+valid_switches}, 400
elif switch_value not in ['on', 'off']:
return {'error': 'Invalid switch value - must be on or off'}, 400
else:
state = set_switch(switch_id, switch_value)
return state, 200
####################################################################################### RESTful API
# Setup RESTful API
app = Flask(__name__)
api = Api(app)
api.add_resource(StateController, '/state/')
api.add_resource(AutomationModeController, '/automation_mode/')
api.add_resource(AllController, '/all/<string:switch_value>')
api.add_resource(SwitchController, '/switch/<string:switch_id>')
@app.route('/')
def main_page():
with open(settings.index_page_file, "r") as page:
return page.read()
def run():
logger.info("Web controller started - serving on {0}:{1}".format(settings.hostname, settings.port))
app.run(host=settings.hostname, port=settings.port, debug=False)
if __name__ == '__main__':
run()