Track the local ip address of the wlan0 interface and report information to the BLE service.

This commit is contained in:
mmcw-dev 2018-01-04 16:10:38 -05:00
parent 7d5f6c6601
commit 3af05e442d
1 changed files with 21 additions and 0 deletions

View File

@ -22,6 +22,7 @@ const bleno = require('bleno')
let currentWifi = 'disconnected'
let currentAddr = null
let getState
const chars = []
@ -78,6 +79,7 @@ function onWifiWrite (data, offset, withoutResponse, callback) {
return callback(result)
}
currentWifi = ssid
currentAddr = getIp()
log.info(`Connected to AP`, { ssid : ssid })
result = bleno.Characteristic.RESULT_SUCCESS
return callback(result)
@ -96,6 +98,7 @@ function onWifiRead (offset, callback) {
}
wifiRes.available = list
wifiRes.current = currentWifi
wifiRes.ip = currentAddr
log.info('Discovered available APs', { found : list.length })
data = new Buffer(JSON.stringify(wifiRes))
callback(result, data.slice(offset, data.length))
@ -133,6 +136,23 @@ function spoofMac () {
return MACTMP
}
function getIp () {
let addr = null
let ipv4
const ifaces = os.networkInterfaces()
if (ifaces && ifaces.wlan0) {
ipv4 = ifaces.wlan0.filter(iface => {
if (iface.family === 'IPv4') {
return iface
}
})
if (ipv4.length === 1) {
addr = ipv4.address
}
}
return addr
}
function capitalize (s) {
return s[0].toUpperCase() + s.slice(1)
@ -186,6 +206,7 @@ class BLE {
return log.error('wifi.getNetwork', err)
}
currentWifi = ssid
currentAddr = getIp()
log.info('wifi.getNetwork', {ssid : ssid})
})
}