FIX: Finding wifi shield through network discovery working - getting their IPV4 and names - #51
Esse commit está contido em:
+23
-25
@@ -28,6 +28,7 @@ import glob
|
|||||||
import ssdp
|
import ssdp
|
||||||
import urllib2
|
import urllib2
|
||||||
import xmltodict
|
import xmltodict
|
||||||
|
import re
|
||||||
|
|
||||||
SAMPLE_RATE = 0 # Hz
|
SAMPLE_RATE = 0 # Hz
|
||||||
|
|
||||||
@@ -153,48 +154,45 @@ class OpenBCIWifi(object):
|
|||||||
self.packets_dropped = 0
|
self.packets_dropped = 0
|
||||||
self.time_last_packet = timeit.default_timer()
|
self.time_last_packet = timeit.default_timer()
|
||||||
|
|
||||||
def find_wifi_shield(self):
|
def find_wifi_shield(self, shield_name=None):
|
||||||
"""Detects Ganglion board MAC address -- if more than 1 around, will select first. Needs root privilege."""
|
"""Detects Ganglion board MAC address -- if more than 1 around, will select first. Needs root privilege."""
|
||||||
|
|
||||||
print("Try to find WiFi shields on your local wireless network")
|
print("Try to find WiFi shields on your local wireless network")
|
||||||
scan_time = 5
|
scan_time = 5
|
||||||
print("Scanning for 5 seconds nearby devices...")
|
print("Scanning for 5 seconds nearby devices...")
|
||||||
|
|
||||||
ssdp_hits = ssdp.discover("urn:schemas-upnp-org:device:Basic:1", timeout=3)
|
|
||||||
list_ip = []
|
list_ip = []
|
||||||
list_id = []
|
list_id = []
|
||||||
device_descriptions = []
|
found_shield = False
|
||||||
if len(ssdp_hits) > 0:
|
def wifi_shield_found(response):
|
||||||
for ssdp_hit in ssdp_hits:
|
res = urllib2.urlopen(response.location).read()
|
||||||
res = urllib2.urlopen(ssdp_hit.location).read()
|
device_description = xmltodict.parse(res)
|
||||||
device_description = xmltodict.parse(res)
|
cur_shield_name = str(device_description['root']['device']['serialNumber'])
|
||||||
list_id.append(str(device_description['root']['device']['serialNumber']))
|
cur_base_url = str(device_description['root']['URLBase'])
|
||||||
list_ip.append(str(device_description['root']['URLBase']))
|
cur_ip_address = re.findall(r'[0-9]+(?:\.[0-9]+){3}', cur_base_url )[0]
|
||||||
|
list_id.append(cur_shield_name)
|
||||||
|
list_ip.append(cur_ip_address)
|
||||||
|
if shield_name is not None:
|
||||||
|
if shield_name == cur_shield_name:
|
||||||
|
found_shield = True
|
||||||
|
return cur_ip_address
|
||||||
|
|
||||||
nb_devices = len(devices)
|
|
||||||
list_ip = []
|
ssdp_hits = ssdp.discover("urn:schemas-upnp-org:device:Basic:1", timeout=3, wifi_found_cb=wifi_shield_found)
|
||||||
list_id = []
|
|
||||||
if nb_devices < 1:
|
|
||||||
print("No WiFi Shield found. Check connectivity.")
|
|
||||||
return ""
|
|
||||||
else:
|
|
||||||
print("Found " + str(nb_devices) + ", detecting wifi shields")
|
|
||||||
|
|
||||||
nb_wifi_shields = 0
|
nb_wifi_shields = 0
|
||||||
|
if not found_shield:
|
||||||
for dev in devices:
|
nb_wifi_shields = len(list_id)
|
||||||
# "Ganglion" should appear inside the "value" associated to "Complete Local Name", e.g. "Ganglion-b2a6"
|
else:
|
||||||
print(dev)
|
nb_wifi_shields = 1
|
||||||
|
|
||||||
if nb_wifi_shields < 1:
|
if nb_wifi_shields < 1:
|
||||||
print("No WiFi Shield found ;(")
|
print("No WiFi Shield found ;(")
|
||||||
raise OSError('Cannot find OpenBCI WiFi Shield with local name')
|
raise OSError('Cannot find OpenBCI WiFi Shield with local name')
|
||||||
|
|
||||||
if nb_wifi_shields > 1:
|
if nb_wifi_shields > 1:
|
||||||
print("Found " + str(nb_wifi_shields) + ", selecting first")
|
print("Found " + str(nb_wifi_shields) + ", selecting first named: " + list_id[0] + " with IPV4: " + list_ip[0])
|
||||||
|
return list_ip[0]
|
||||||
print("Selecting Shield named " + list_ip[0] + " for " + list_id[0])
|
|
||||||
return list_ip[0]
|
|
||||||
|
|
||||||
def ser_write(self, b):
|
def ser_write(self, b):
|
||||||
"""Access serial port object for write"""
|
"""Access serial port object for write"""
|
||||||
|
|||||||
@@ -51,6 +51,8 @@ def discover(service, timeout=5, retries=1, mx=3, wifi_found_cb=None):
|
|||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
response = SSDPResponse(sock.recv(1024))
|
response = SSDPResponse(sock.recv(1024))
|
||||||
|
if wifi_found_cb is not None:
|
||||||
|
wifi_found_cb(response)
|
||||||
responses[response.location] = response
|
responses[response.location] = response
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
break
|
break
|
||||||
|
|||||||
Referência em uma Nova Issue
Bloquear um usuário