Skip to content

Commit ff0d423

Browse files
committed
wip #40
1 parent bd6bcac commit ff0d423

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

wifi.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class WiFi():
1616
CONFIG_FILE = "/etc/coderbot_wifi.conf"
1717
adapters = ["default", "RT5370", "RTL8188CUS"]
1818
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/"
2020
wifi_client_conf_file = "/etc/wpa_supplicant/wpa_supplicant.conf"
2121
_config = {}
2222

@@ -94,14 +94,20 @@ def get_ipaddr(cls, ifname):
9494
)[20:24])
9595

9696
@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):
98104
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,
102108
"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"})
105111
ret = urllib2.urlopen(req)
106112
if ret.getcode() != 200:
107113
raise Exception()
@@ -144,7 +150,7 @@ def start_as_client(cls):
144150
out = os.system("wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf > /dev/null 2>&1")
145151
out += os.system("dhclient -1 wlan0")
146152
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]")
148154
print "registered bot, ip: " + str(cls.get_ipaddr("wlan0") + " name: " + cls.get_config().get('bot_name', 'CoderBot'))
149155
except subprocess.CalledProcessError as e:
150156
print e.output

0 commit comments

Comments
 (0)