-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·130 lines (116 loc) · 3.88 KB
/
app.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
#!/bin/python3
import keybow
import time
import requests
import json
import sys
import logging
log_level_info = {'logging.DEBUG': logging.DEBUG,
'logging.INFO': logging.INFO,
'logging.WARNING': logging.WARNING,
'logging.ERROR': logging.ERROR,
}
config = {}
myButtonState = [0,0,0]
keybow.setup(keybow.MINI)
def setup():
# Init Keybow light to Green
keybow.set_pixel(0, 0, 255, 0) # -- Green
keybow.set_pixel(1, 0, 255, 0) # -- Green
keybow.set_pixel(2, 0, 255, 0) # -- Green
def activate(URL):
error = 0
if URL=="":
logging.info("URL is empty, nothing to activate")
return
else:
logging.info('Sending request to: ' + URL)
try:
response = requests.get(URL)
response.raise_for_status()
except requests.exceptions.HTTPError as errh:
error = 1
logging.error("An Http Error occurred:" + repr(errh))
except requests.exceptions.ConnectionError as errc:
error = 1
logging.error("An Error Connecting to the API occurred:" + repr(errc))
except requests.exceptions.Timeout as errt:
error = 1
logging.error("A Timeout Error occurred:" + repr(errt))
except requests.exceptions.RequestException as err:
error = 1
logging.error("An Unknown Error occurred" + repr(err))
if error==0:
logging.info('Response: ' + response.text)
logging.info('Status code: ' + str(response.status_code))
@keybow.on(index=0)
def handle_key(index, state):
logging.info("{}: Key {} has been {}".format(
time.time(),
index,
'pressed' if state else 'released'))
if state:
keybow.set_led(index, 255, 0, 0)
else:
if myButtonState[index] == 1:
keybow.set_led(index, 0, 255, 0)
keybow.show()
myButtonState[index] = 0
activate(config["devices"]["device0"]["URL-off"])
else:
keybow.set_led(index, 255, 0, 0)
keybow.show()
myButtonState[index] = 1
activate(config["devices"]["device0"]["URL-on"])
@keybow.on(index=1)
def handle_key(index, state):
logging.info("{}: Key {} has been {}".format(
time.time(),
index,
'pressed' if state else 'released'))
if state:
keybow.set_led(index, 255, 0, 0)
else:
if myButtonState[index] == 1:
keybow.set_led(index, 0, 255, 0)
keybow.show()
myButtonState[index] = 0
activate(config["devices"]["device1"]["URL-off"])
else:
keybow.set_led(index, 255, 0, 0)
keybow.show()
myButtonState[index] = 1
activate(config["devices"]["device1"]["URL-on"])
@keybow.on(index=2)
def handle_key(index, state):
logging.info("{}: Key {} has been {}".format(
time.time(),
index,
'pressed' if state else 'released'))
if state:
keybow.set_led(index, 255, 0, 0)
else:
if myButtonState[index] == 1:
keybow.set_led(index, 0, 255, 0)
keybow.show()
myButtonState[index] = 0
activate(config["devices"]["device2"]["URL-off"])
else:
keybow.set_led(index, 255, 0, 0)
keybow.show()
myButtonState[index] = 1
activate(config["devices"]["device2"]["URL-on"])
try:
with open("./config.json") as f:
config = json.loads(f.read())
except Exception as e:
sys.stderr.write("Error: {}".format(e))
sys.exit(1)
my_log_level_from_config = config['logger']['level']
my_log_level = log_level_info.get(my_log_level_from_config, logging.ERROR)
logging.basicConfig(filename=config["logger"]["loggingFileName"],format='%(levelname)s:%(asctime)s:%(message)s', level=my_log_level)
logging.info("Starting")
setup()
while True:
keybow.show()
time.sleep(1.0 / 60.0)