Skip to content

Commit 583b864

Browse files
committed
-
1 parent 8f51075 commit 583b864

File tree

5 files changed

+56
-15
lines changed

5 files changed

+56
-15
lines changed

coderbot.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"move_tr_speed": "75", "move_fw_elapse": "1.5", "show_page_program": "true", "prog_saveonrun": "true", "load_at_start": "", "move_tr_elapse": "1", "sound_start": "$startup.mp3", "sound_stop": "$shutdown.mp3", "camera_exposure_mode": "auto", "prog_video_rec": "true", "prog_level": "basic_move", "prog_scrollbars": "true", "ctrl_fw_speed": "100", "move_fw_speed": "100", "sound_shutter": "$shutter.mp3", "show_page_prefs": "true", "prog_maxblocks": "5", "ctrl_hud_image": "", "button_func": "none", "move_motor_mode": "dc", "ctrl_fw_elapse": "-1", "ctrl_tr_elapse": "-1", "show_page_control": "true", "ctrl_tr_speed": "80"}
1+
{"wifi_psk": "catoblepa catoblepa io ti dono le mie tepa", "move_tr_speed": "75", "move_fw_elapse": "1.5", "move_motor_mode": "dc", "show_page_program": "true", "load_at_start": "", "move_tr_elapse": "1", "sound_start": "$startup.mp3", "sound_stop": "$shutdown.mp3", "camera_exposure_mode": "auto", "prog_video_rec": "true", "prog_level": "basic_move", "prog_scrollbars": "true", "ctrl_fw_speed": "100", "move_fw_speed": "100", "sound_shutter": "$shutter.mp3", "show_page_prefs": "true", "prog_maxblocks": "5", "ctrl_hud_image": "coderbot_hud_2.png", "button_func": "none", "wifi_mode": "ap", "ctrl_fw_elapse": "-1", "ctrl_tr_elapse": "-1", "wifi_ssid": "kanaha", "show_page_control": "true", "ctrl_tr_speed": "80"}

main.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ def handle_config():
4343
app.bot_config = Config.get()
4444
return "ok";
4545

46+
@app.route("/wifi", methods=["POST"])
47+
def handle_wifi():
48+
mode = request.form.get("wifi_mode")
49+
ssid = request.form.get("wifi_ssid")
50+
psk = request.form.get("wifi_psk")
51+
print "mode ", mode, " ssid: ", ssid, " psk: ", psk
52+
client_params = " \"" + ssid + "\" \"" + psk + "\"" if ssid != "" and psk != "" else ""
53+
print client_params
54+
os.system("sudo python wifi.py updatecfg " + mode + client_params)
55+
if mode == "ap":
56+
return "http://coder.bot:8080";
57+
else:
58+
return "http://coderbotsrv.appspot.com/"
59+
4660
@app.route("/bot", methods=["GET"])
4761
def handle_bot():
4862
cmd = request.args.get('cmd')

static/js/control.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,15 @@ $(document).on( "pagecreate", '#page-preferences', function( event ) {
110110
});
111111
return false;
112112
});
113+
$('#b_wifi_apply').on("click", function (){
114+
var form_data = $(this).parents("form").serialize();
115+
$.post(url='/wifi', form_data, success=function(data){
116+
alert(BotMessages.Saved);
117+
location.href=data;
118+
});
119+
return false;
120+
});
121+
113122
});
114123

115124
$(document).on( "pageshow", '#page-photos', function( event ) {

templates/config.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ <h1>CoderBot</h1>
142142
<input type="text" id="i_wifi_ssid" name="wifi_ssid" value="{{config.wifi_ssid}}">
143143
<label for="i_wifi_psk">{% trans %}WiFi password{% endtrans %}</label>
144144
<input type="text" id="i_wifi_psk" name="wifi_psk" value="{{config.wifi_psk}}">
145+
<button type="button" id="b_wifi_apply" class="ui-btn ui-shadow ui-corner-all ui-btn-a ui-mini">{% trans %}Apply Wifi config{% endtrans %}</button>
145146
</div>
146147
<div class="ui-grid-a">
147148
<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>

wifi.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def get_ipaddr(cls, ifname):
7777
def register_ipaddr(cls, ipaddr, botname):
7878
try:
7979
ret = urllib2.urlopen(cls.web_url + "?name=" + botname + "&ipaddr=" + ipaddr)
80+
print str(ret.getcode())
8081
if ret.getcode() != 200:
8182
raise Exception()
8283
except URLError as e:
@@ -110,6 +111,8 @@ def start_as_client(cls):
110111
try:
111112
out = subprocess.check_output(["ifdown", "wlan0"])
112113
out = subprocess.check_output(["ifup", "wlan0"])
114+
print "registering ip..."
115+
cls.register_ipaddr(cls.get_ipaddr("wlan0"), "CoderBot")
113116
except subprocess.CalledProcessError as e:
114117
print e.output
115118
raise
@@ -126,24 +129,38 @@ def start_as_ap(cls):
126129
out = subprocess.check_output(["ifup", "wlan0"])
127130
cls.start_hostapd()
128131

132+
@classmethod
133+
def start_service(cls):
134+
config = cls.load_config()
135+
if config["wifi_mode"] == "ap":
136+
print "starting as ap..."
137+
cls.start_as_ap()
138+
elif config["wifi_mode"] == "client":
139+
print "starting as client..."
140+
try:
141+
cls.start_as_client()
142+
except:
143+
print "Unable to register ip, revert to ap mode"
144+
cls.start_as_ap()
129145

130146
def main():
131-
print "starting wifi service..."
132147
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"
148+
if len(sys.argv) > 2 and sys.argv[1] == "updatecfg":
149+
if len(sys.argv) > 2 and sys.argv[2] == "ap":
150+
w.set_start_as_ap()
146151
w.start_as_ap()
152+
elif len(sys.argv) > 2 and sys.argv[2] == "client":
153+
if len(sys.argv) > 3:
154+
w.set_client_params(sys.argv[3], sys.argv[4])
155+
w.set_start_as_client()
156+
w.stop_hostapd()
157+
try:
158+
w.start_as_client()
159+
except:
160+
print "Unable to register ip, revert to ap mode"
161+
w.start_as_ap()
162+
else:
163+
w.start_service()
147164

148165
if __name__ == "__main__":
149166
main()

0 commit comments

Comments
 (0)