Skip to content

Commit 0d9e172

Browse files
committed
increase robustness for SSID with spaces
1 parent 0c558b7 commit 0d9e172

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

mozloc.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,47 @@
1616
from time import sleep
1717

1818
URL='https://location.services.mozilla.com/v1/geolocate?key=test'
19-
NMCMD = ['nmcli','-fields','SSID,BSSID,FREQ,SIGNAL','device','wifi']
19+
NMCMD = ['nmcli','-g','SSID,BSSID,FREQ,SIGNAL','device','wifi']
2020
NMSCAN = ['nmcli','device','wifi','rescan']
2121

2222

2323
def get_nmcli():
2424

25-
25+
2626
ret = subprocess.check_output(NMCMD)
2727
sleep(0.5) # nmcli crashed for less than about 0.2 sec.
2828
try:
2929
subprocess.check_call(NMSCAN) # takes several seconds to update, so do it now.
3030
except subprocess.CalledProcessError as e:
3131
print('consider slowing scan cadence. {}'.format(e))
32-
33-
dat = pandas.read_csv(BytesIO(ret), sep='\s+', index_col=False,
34-
header=0,usecols=[0,1,2,4], encoding='utf8',
35-
names=['ssid','macAddress','frequency','signalStrength'])
32+
33+
dat = pandas.read_csv(BytesIO(ret), sep=r'(?<!\\):', index_col=False,
34+
header=0, encoding='utf8',engine='python',
35+
dtype=str,usecols=[0,1,3],
36+
names=['ssid','macAddress','signalStrength'])
3637
# %% optout
3738
dat = dat[~dat['ssid'].str.endswith('_nomap')]
38-
# %% JSON
39+
dat['macAddress'] = dat['macAddress'].str.replace(r'\\:',':')
40+
# %% JSON
3941
jdat = dat.to_json(orient='records')
4042
jdat = '{ "wifiAccessPoints":' + jdat + '}'
4143
# print(jdat)
4244
# %% cloud MLS
4345
try:
4446
req = requests.post(URL, data=jdat)
4547
if req.status_code != 200:
46-
logging.error(ret.text)
48+
logging.error(req.text)
49+
return
4750
except requests.exceptions.ConnectionError as e:
48-
logging.error('no network connection. {}'.format(e))
51+
logging.error('no network connection. {}'.format(e))
52+
return
4953
# %% process MLS response
5054
jres = req.json()
5155
loc = jres['location']
5256
loc['accuracy'] = jres['accuracy']
5357
loc['N'] = dat.shape[0] # number of BSSIDs used
5458
loc['t'] = datetime.now()
55-
59+
5660
return loc
5761

5862

@@ -75,7 +79,10 @@ def get_nmcli():
7579
print('updating every {} seconds'.format(T))
7680
while True:
7781
loc = get_nmcli()
78-
82+
if loc is None:
83+
sleep(T)
84+
continue
85+
7986
stat = '{} {} {} {} {}'.format(loc['t'].strftime('%xT%X'),
8087
loc['lat'], loc['lng'], loc['accuracy'], loc['N'])
8188
print(stat)

0 commit comments

Comments
 (0)