@@ -16,7 +16,7 @@ class WiFi():
16
16
CONFIG_FILE = "/etc/coderbot_wifi.conf"
17
17
adapters = ["default" , "RT5370" , "RTL8188CUS" ]
18
18
hostapds = {"default" : "hostapd" , "RT5370" : "hostapd.RT5370" , "RTL8188CUS" : "hostapd.RTL8188" }
19
- web_url = "http://my.coderbot.org/coderbot/v1 .0/bot/new "
19
+ web_url = "http://my.coderbot.org/api/ coderbot/1 .0/bot/"
20
20
wifi_client_conf_file = "/etc/wpa_supplicant/wpa_supplicant.conf"
21
21
_config = {}
22
22
@@ -94,14 +94,20 @@ def get_ipaddr(cls, ifname):
94
94
)[20 :24 ])
95
95
96
96
@classmethod
97
- def register_ipaddr (cls , botname , ipaddr ):
97
+ def get_macaddr (cls , ifname ):
98
+ s = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
99
+ info = fcntl .ioctl (s .fileno (), 0x8927 , struct .pack ('256s' , ifname [:15 ]))
100
+ return '' .join (['%02x:' % ord (char ) for char in info [18 :24 ]])[:- 1 ]
101
+
102
+ @classmethod
103
+ def register_ipaddr (cls , bot_uid , bot_name , bot_ipaddr , user_email ):
98
104
try :
99
- data = {"bot_uid" : "ABCDFGHI" ,
100
- "bot_name" : botname ,
101
- "bot_ip" : ipaddr ,
105
+ data = {"bot_uid" : bot_uid ,
106
+ "bot_name" : bot_name ,
107
+ "bot_ip" : bot_ipaddr ,
102
108
"bot_version" : "1.0" ,
103
- "user_email" :
"[email protected] " }
104
- req = urllib2 .Request (cls .web_url , json .dumps (data ))
109
+ "user_email" : user_email }
110
+ req = urllib2 .Request (cls .web_url + bot_uid , json .dumps (data ), headers = { "Authorization" : "CoderBot 123456" } )
105
111
ret = urllib2 .urlopen (req )
106
112
if ret .getcode () != 200 :
107
113
raise Exception ()
@@ -144,7 +150,7 @@ def start_as_client(cls):
144
150
out = os .system ("wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf > /dev/null 2>&1" )
145
151
out += os .system ("dhclient -1 wlan0" )
146
152
print out
147
- cls .register_ipaddr (cls .get_config ().get ('bot_name' , 'CoderBot' ), cls .get_ipaddr ("wlan0" ))
153
+ cls .
register_ipaddr (
cls .
get_macaddr ( "wlan0" ), cls . get_config ().
get (
'bot_name' ,
'CoderBot' ),
cls .
get_ipaddr (
"wlan0" )
, "[email protected] " )
148
154
print "registered bot, ip: " + str (cls .get_ipaddr ("wlan0" ) + " name: " + cls .get_config ().get ('bot_name' , 'CoderBot' ))
149
155
except subprocess .CalledProcessError as e :
150
156
print e .output
0 commit comments