-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.py
197 lines (165 loc) · 7.21 KB
/
router.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Piment Router - a router for IR and GPIO
You may use any Piment project under the terms
of the GNU General Public License (GPL) Version 3.
(c) 2016 Emilio Mariscal (emi420 [at] gmail.com)
'''
from ir import IR
from gpio import Relay
from os import path as ospath
import pexpect
import re
import string
import urllib
'''
Router routes addresses to actions
'''
class Router(object):
ir = None
relay = None
def __init__(self):
self.ir = IR()
self.relay = Relay()
def stop(self):
self.relay.stop()
def post(self, path, data):
"""Respond to a POST request."""
if path == "/api/ir/save-ui-config/":
result = 'Not saved.'
with open("remotes-ui.json", "w") as f:
f.write(data)
result = 'Saved.'
return result
elif path == "/api/save-relay-ui-config/":
result = 'Not saved.'
with open("remotes-relay-ui.json", "w") as f:
f.write(data)
result = 'Saved.'
return result
elif path == "/api/ir/save-config/":
return self.ir.save_config(data)
else:
return "Not found."
def get(self, path):
"""Respond to a GET request."""
response = ""
try:
if path == "/api/ir/":
if self.ir.launched:
self.ir.kill()
response = self.ir.launch()
elif path.startswith("/api/relay/off/"):
response = self.relay.off(path.replace("/api/relay/off/", ""))
elif path.startswith("/api/relay/on/"):
response = self.relay.on(path.replace("/api/relay/on/", ""))
elif path.startswith("/api/relay/onoff/"):
response = self.relay.onoff(path.replace("/api/relay/onoff/", ""))
elif path.startswith("/api/relay/offon/"):
response = self.relay.offon(path.replace("/api/relay/offon/", ""))
elif path.startswith("/api/bluetooth/wait/"):
print "Bluetooth wait"
response = self.relay.btpair.wait()
elif path.startswith("/api/bluetooth/stop/"):
response = self.relay.btpair.stop()
elif path.startswith("/api/config/wifi/scan/"):
content = ""
networks = []
p = pexpect.spawn('iwlist wlan0 scan')
while True:
out = p.readline()
if out == '' and p.eof() is not None:
break
if out:
content = content + out
find_networks = re.findall("ESSID.*$",content,re.MULTILINE)
find_levels = re.findall("Signal level.*$",content,re.MULTILINE)
index = 0;
for item in find_networks:
networks.append(item.replace('ESSID:"','').replace('"\r','') + ':' + find_levels[index].replace("Signal level=","").replace(" dBm \r",""))
index+=1
response = string.join(networks,",")
elif path.startswith("/api/config/wifi/get/"):
p = pexpect.spawn('iwgetid')
response = p.readline().replace('wlan0 ESSID:"',"").replace('"','').replace('\r','').replace('\n','')
p = pexpect.spawn('hostname -I')
response += "," + p.readline()
elif path.startswith("/api/config/wifi/set/"):
data = urllib.unquote(path.replace('/api/config/wifi/set/','')).split(":")
with open("/etc/wpa_supplicant/wpa_supplicant.conf", "w") as f:
f.write("country=GB \n")
f.write("ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev \n")
f.write("update_config=1 \n")
f.write("network={ \n")
f.write("ssid=\"" + data[0] + "\" \n")
f.write("psk=\"" + data[1] + "\" \n")
f.write("proto=RSN \n")
f.write("key_mgmt=WPA-PSK \n")
f.write("pairwise=CCMP \n")
f.write("auth_alg=OPEN \n")
f.write("} \n")
f.close()
p = pexpect.spawn('ifdown wlan0')
p.readline()
p = pexpect.spawn('ifup wlan0')
p.readline()
response = "Connected."
elif path == "/api/get-relay-ui-config/":
if ospath.isfile("remotes-relay-ui.json"):
content = ""
with open("remotes-relay-ui.json", "r") as f:
for line in f:
content = content + line
else:
content = "File not found. \n"
response = content
elif path == "/api/get-ui-config/":
if ospath.isfile("remotes-ui.json"):
content = ""
with open("remotes-ui.json", "r") as f:
for line in f:
content = content + line
else:
content = "File not found. \n"
response = content
elif path.startswith("/api/lirc/send/"):
split_path = path.split('/')
send = split_path[len(split_path)-1]
cmd = send.split(',')
self.ir.send_once(cmd[0], cmd[1])
response = "Command sent."
elif path.startswith("/api/ir/"):
if path.endswith("/send/"):
split_path = path.split('/')
response = self.ir.send(split_path[len(split_path)-3])
if path == "/api/ir/get-last-config/":
response = self.ir.get_last_config()
if path == "/api/ir/get-config/":
response = self.ir.get_config()
elif path == "/api/ir/get-namespace/":
response = self.ir.get_namespace()
elif path == "/api/ir/mode2/":
response = self.ir.mode2()
elif path == "/api/ir/stop-lirc-service/":
response = self.ir.stop_lirc_service()
elif path == "/api/ir/start-lirc-service/":
response = self.ir.start_lirc_service()
elif path.endswith("/save-last-config/"):
split_path = path.split('/');
name = split_path[len(split_path)-3]
response = self.ir.save_last_config(name)
else:
if not self.ir.launched:
response = "IRRecord not launched."
else:
if path == "/api/ir/enter/":
response = self.ir.enter()
elif path == "/api/ir/status/":
response = self.ir.status()
elif path == "/api/ir/kill/":
response = self.ir.kill()
except IOError:
response = "Error."
#self.send_error(404,'File Not Found: %s' % path)
return response