Skip to content

Commit 8f51075

Browse files
committed
-
1 parent 8cff924 commit 8f51075

File tree

3 files changed

+167
-1
lines changed

3 files changed

+167
-1
lines changed

templates/config.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ <h1>CoderBot</h1>
1616
<li><a href="#t_start" data-ajax="true">{% trans %}Start{% endtrans %}</a></li>
1717
<li><a href="#t_button" data-ajax="true">{% trans %}Button{% endtrans %}</a></li>
1818
<li><a href="#t_ui" data-ajax="true">{% trans %}User Interface{% endtrans %}</a></li>
19+
<li><a href="#t_wifi" data-ajax="true">{% trans %}WiFi{% endtrans %}</a></li>
1920
</ul>
2021
</div>
2122
<div id="t_control" class="ui-body-d ui-content">
@@ -128,6 +129,20 @@ <h1>CoderBot</h1>
128129
<input type="checkbox" name="show_page_prefs" id="c_show_page_prefs" value="true" {%if config.show_page_prefs=='true'%}checked="checked"{%endif%}>
129130
</fieldset>
130131
</div>
132+
<div id="t_wifi" class="ui-body-d ui-content">
133+
<fieldset data-role="controlgroup" data-mini="true">
134+
<legend>{% trans %}WiFi mode{% endtrans %}</legend>
135+
<input type="radio" name="wifi_mode" id="r_wifi_mode_a" value="ap" {%if config.wifi_mode=='ap'%}checked="checked"{%endif%}>
136+
<label for="r_wifi_mode_a">{% trans %}Access Point{% endtrans %}</label>
137+
<input type="radio" name="wifi_mode" id="r_wifi_mode_b" value="client" {%if config.wifi_mode=='client'%}checked="checked"{%endif%}>
138+
<label for="r_wifi_mode_b">{% trans %}Client{% endtrans %}</label>
139+
</fieldset>
140+
<legend>WiFi Client mode access data</legend>
141+
<label for="i_wifi_ssid">{% trans %}WiFi name{% endtrans %}</label>
142+
<input type="text" id="i_wifi_ssid" name="wifi_ssid" value="{{config.wifi_ssid}}">
143+
<label for="i_wifi_psk">{% trans %}WiFi password{% endtrans %}</label>
144+
<input type="text" id="i_wifi_psk" name="wifi_psk" value="{{config.wifi_psk}}">
145+
</div>
131146
<div class="ui-grid-a">
132147
<div class="ui-block-a"><a href="#" data-rel="close" class="ui-btn ui-shadow ui-corner-all ui-btn-b ui-mini">{% trans %}Cancel{% endtrans %}</a></div>
133148
<div class="ui-block-b"><button type="submit" data-rel="close" class="ui-btn ui-shadow ui-corner-all ui-btn-a ui-mini">{% trans %}Save{% endtrans %}</button></div>

viz/image.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
MIN_MATCH_COUNT = 10
1010

1111
class Image():
12-
_face_cascade = cv2.CascadeClassifier('/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml')
12+
#_face_cascade = cv2.CascadeClassifier('/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml')
13+
_face_cascade = cv2.CascadeClassifier('/usr/local/share/OpenCV/lbpcascades/lbpcascade_frontalface.xml')
1314
_kernel = np.ones((3,3),np.uint8)
1415

1516
def __init__(self, array):

wifi.py

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#!/usr/bin/python
2+
3+
import socket
4+
import subprocess
5+
import shutil
6+
import sys
7+
import os
8+
import urllib2
9+
import fcntl
10+
import struct
11+
import json
12+
13+
class WiFi():
14+
15+
CONFIG_FILE = "/etc/coderbot_wifi.conf"
16+
adapters = ["RT5370", "RTL8188CUS"]
17+
hostapds = {"RT5370": "hostapd.RT5370", "RTL8188CUS": "hostapd.RTL8188"}
18+
web_url = "http://coderbotsrv.appspot.com/register_ip"
19+
wifi_client_conf_file = "/etc/wpa_supplicant/wpa_supplicant.conf"
20+
_config = {}
21+
22+
@classmethod
23+
def load_config(cls):
24+
f = open(cls.CONFIG_FILE)
25+
cls._config = json.load(f)
26+
return cls._config
27+
28+
@classmethod
29+
def save_config(cls):
30+
f = open(cls.CONFIG_FILE, 'w')
31+
json.dump(cls._config, f)
32+
return cls._config
33+
34+
@classmethod
35+
def get_config(cls):
36+
return cls._config
37+
38+
@classmethod
39+
def get_adapter_type(cls):
40+
lsusb_out = subprocess.check_output("lsusb")
41+
for a in cls.adapters:
42+
if a in lsusb_out:
43+
return a
44+
return None
45+
46+
@classmethod
47+
def start_hostapd(cls):
48+
adapter = cls.get_adapter_type()
49+
hostapd_type = cls.hostapds.get(adapter)
50+
try:
51+
print "starting hostapd..."
52+
os.system("start-stop-daemon --start --oknodo --quiet --exec /usr/sbin/" + hostapd_type + " -- /etc/hostapd/" + hostapd_type + " &")
53+
54+
except subprocess.CalledProcessError as e:
55+
print e.output
56+
57+
@classmethod
58+
def stop_hostapd(cls):
59+
try:
60+
out = subprocess.check_output(["pkill", "-9", "hostapd"])
61+
print out
62+
except subprocess.CalledProcessError as e:
63+
print e.output
64+
65+
@classmethod
66+
def get_ipaddr(cls, ifname):
67+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
68+
return socket.inet_ntoa(fcntl.ioctl(
69+
s.fileno(),
70+
0x8915, # SIOCGIFADDR
71+
struct.pack('256s', ifname[:15])
72+
)[20:24])
73+
ipaddr = socket.gethostbyname(socket.gethostname())
74+
return ipaddr
75+
76+
@classmethod
77+
def register_ipaddr(cls, ipaddr, botname):
78+
try:
79+
ret = urllib2.urlopen(cls.web_url + "?name=" + botname + "&ipaddr=" + ipaddr)
80+
if ret.getcode() != 200:
81+
raise Exception()
82+
except URLError as e:
83+
print e
84+
raise
85+
print botname, ": ", ipaddr
86+
87+
@classmethod
88+
def get_wlans(cls):
89+
out = subprocess.check_output(["iwlist", "wlan0", "scan"])
90+
91+
@classmethod
92+
def set_client_params(cls, wssid, wpsk):
93+
f = open (cls.wifi_client_conf_file, "w+")
94+
f.write("""ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
95+
update_config=1
96+
network={\n""")
97+
f.write(" ssid=\""+wssid+"\"\n")
98+
f.write(" psk=\""+wpsk+"\"\n")
99+
f.write("}")
100+
101+
@classmethod
102+
def set_start_as_client(cls):
103+
shutil.copy("/etc/network/interfaces_cli", "/etc/network/interfaces")
104+
cls._config["wifi_mode"] = "client"
105+
cls.save_config()
106+
107+
@classmethod
108+
def start_as_client(cls):
109+
cls.stop_hostapd()
110+
try:
111+
out = subprocess.check_output(["ifdown", "wlan0"])
112+
out = subprocess.check_output(["ifup", "wlan0"])
113+
except subprocess.CalledProcessError as e:
114+
print e.output
115+
raise
116+
117+
@classmethod
118+
def set_start_as_ap(cls):
119+
shutil.copy("/etc/network/interfaces_ap", "/etc/network/interfaces")
120+
cls._config["wifi_mode"] = "ap"
121+
cls.save_config()
122+
123+
@classmethod
124+
def start_as_ap(cls):
125+
out = subprocess.check_output(["ifdown", "wlan0"])
126+
out = subprocess.check_output(["ifup", "wlan0"])
127+
cls.start_hostapd()
128+
129+
130+
def main():
131+
print "starting wifi service..."
132+
w = WiFi()
133+
config = w.load_config()
134+
135+
if config["wifi_mode"] == "ap":
136+
print "starting as ap..."
137+
w.start_as_ap()
138+
elif config["wifi_mode"] == "client":
139+
print "starting as client..."
140+
w.start_as_client()
141+
try:
142+
print "registering ip..."
143+
w.register_ipaddr(w.get_ipaddr("wlan0"), "CoderBot")
144+
except:
145+
print "Unable to register ip, revert to ap mode"
146+
w.start_as_ap()
147+
148+
if __name__ == "__main__":
149+
main()
150+

0 commit comments

Comments
 (0)