-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
82 lines (65 loc) · 2.41 KB
/
config.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
from flask import Flask, request, jsonify
import RPi.GPIO as GPIO
# Mapping appliances/modules to words identifying them (in commands)
words = {
"plug": {"junction", "box", "plug"},
"milight": {"mi", "light", "dim", "bright"},
"tube": {"light", "tube"},
"cfan": {"cieling", "fan"}
}
# Mapping appliances/modules to their ports
# (220V Appliance --> Relay --> GPIO port) or (5V module/LED --> GPIO port)
GPIO_ports = {
"plug": 18,
"milight": 23,
"tube": 24,
"cfan": 25
}
GPIO.setmode(GPIO.BOARD)
def init_switches(inds):
"""Initializes the ports as GPIO output ports. Initially Off.
inds : list, Port numbers.
"""
for i in inds:
GPIO.setup(i, GPIO.OUT, initial=0)
def switch(ind, status):
"""Turns a port ON/True or OFF/False
ind : int, Port number
status : str, Turns port ON if status is 'on'. Else turns port OFF.
"""
print("Switching :", ind, ">>", status == 'on')
GPIO.output(ind, status == 'on')
def trigger(command_string):
"""Uses token-matching to act on a command.
command_string : str, Command. For example : "fan off", "turn the light on", etc.
status : str (optional), Turns port ON if status is 'on'. Else turns port OFF.
"""
# Convert command to lower case and split into words
command_words = set(command_string.lower().split(" "))
# Set status to 'on' if the word 'on' is present in the command (command_words)
status = 'on' if 'on' in command_words else 'off'
# Iterate over all appliances (ie. keys in words)
for appliance in words:
# If the command contains any words referring to the appliance
if command_words.intersection(words[appliance]):
# Trigger the appliance to status
port_num = GPIO_ports[appliance]
switch(port_num, status)
return
app = Flask(__name__)
@app.route('/chamber_room', methods=['POST'])
def chamber_room():
"""Endpoint to process incoming requests.
(POST requests with JSON mapping 'obj' to command)"""
cont = request.get_json()
if cont is not None:
trigger(command_string=cont['obj'])
return jsonify({"SUCCESS": True})
if __name__ == '__main__':
print('Starting Server')
init_switches(GPIO_ports.values())
app.run(debug=True,
use_reloader=False,
host='0.0.0.0',
port=8000
) # run app in debug mode on port 5000