13
13
class WiFi ():
14
14
15
15
CONFIG_FILE = "/etc/coderbot_wifi.conf"
16
- adapters = ["RT5370" , "RTL8188CUS" ]
16
+ adapters = ["RT5370" , "RTL8188CUS" , "RT3572" ]
17
17
hostapds = {"RT5370" : "hostapd.RT5370" , "RTL8188CUS" : "hostapd.RTL8188" }
18
18
web_url = "http://coderbotsrv.appspot.com/register_ip"
19
19
wifi_client_conf_file = "/etc/wpa_supplicant/wpa_supplicant.conf"
@@ -45,13 +45,13 @@ def get_adapter_type(cls):
45
45
46
46
@classmethod
47
47
def start_hostapd (cls ):
48
- adapter = cls .get_adapter_type ()
49
- hostapd_type = cls .hostapds .get (adapter )
48
+ # adapter = cls.get_adapter_type()
49
+ # hostapd_type = cls.hostapds.get(adapter)
50
50
try :
51
51
print "starting hostapd..."
52
52
#os.system("start-stop-daemon --start --oknodo --quiet --exec /usr/sbin/" + hostapd_type + " -- /etc/hostapd/" + hostapd_type + " &")
53
- os .system ("/usr/sbin/" + hostapd_type + " /etc/hostapd/" + hostapd_type + " -B" )
54
-
53
+ # os.system("/usr/sbin/" + hostapd_type + " /etc/hostapd/" + hostapd_type + " -B")
54
+ out = subprocess . check_output ([ "service" , "hostapd" , "start" ])
55
55
except subprocess .CalledProcessError as e :
56
56
print e .output
57
57
@@ -100,12 +100,14 @@ def set_client_params(cls, wssid, wpsk):
100
100
f .write (" psk=\" " + wpsk + "\" \n " )
101
101
f .write ("}" )
102
102
103
+ # set WIFI client mode
103
104
@classmethod
104
105
def set_start_as_client (cls ):
105
106
shutil .copy ("/etc/network/interfaces_cli" , "/etc/network/interfaces" )
106
107
cls ._config ["wifi_mode" ] = "client"
107
108
cls .save_config ()
108
109
110
+ # start as client to coderbot server
109
111
@classmethod
110
112
def start_as_client (cls ):
111
113
cls .stop_hostapd ()
@@ -118,6 +120,25 @@ def start_as_client(cls):
118
120
print e .output
119
121
raise
120
122
123
+ # set WIFI client mode and record as "local_client"
124
+ @classmethod
125
+ def set_start_as_local_client (cls ):
126
+ shutil .copy ("/etc/network/interfaces_cli" , "/etc/network/interfaces" )
127
+ cls ._config ["wifi_mode" ] = "local_client"
128
+ cls .save_config ()
129
+
130
+ # start as client without registering to coderbot server
131
+ @classmethod
132
+ def start_as_local_client (cls ):
133
+ cls .stop_hostapd ()
134
+ try :
135
+ out = subprocess .check_output (["ifdown" , "wlan0" ])
136
+ out = subprocess .check_output (["ifup" , "wlan0" ])
137
+ except :
138
+ print e .output
139
+ raise
140
+
141
+
121
142
@classmethod
122
143
def set_start_as_ap (cls ):
123
144
shutil .copy ("/etc/network/interfaces_ap" , "/etc/network/interfaces" )
@@ -139,9 +160,16 @@ def start_service(cls):
139
160
elif config ["wifi_mode" ] == "client" :
140
161
print "starting as client..."
141
162
try :
142
- cls .start_as_client ()
163
+ cls .start_as_start_as_local_clientclient ()
143
164
except :
144
165
print "Unable to register ip, revert to ap mode"
166
+ cls .start_as_astart_as_local_clientp ()
167
+ elif config ["wifi_mode" ] == "local_client" :
168
+ print "starting as local client..."
169
+ try :
170
+ cls .start_as_local_client ()
171
+ except :
172
+ print "Unable to connect to WLAN, rever to ap mode"
145
173
cls .start_as_ap ()
146
174
147
175
def main ():
@@ -160,6 +188,16 @@ def main():
160
188
except :
161
189
print "Unable to register ip, revert to ap mode"
162
190
w .start_as_ap ()
191
+ elif len (sys .argv ) > 2 and sys .argv [2 ] == "local_client" :
192
+ if len (sys .argv ) > 3 :
193
+ w .set_client_params (sys .argv [3 ], sys .argv [4 ])
194
+ w .set_start_as_client ()
195
+ try :
196
+ w .start_as_local_client ()
197
+ except :
198
+ print "Unable to connect to WLAN, revert to ap mode"
199
+ w .start_as_ap ()
200
+
163
201
else :
164
202
w .start_service ()
165
203
0 commit comments