And here is the test... (cannot stand code that has not been tested or at least has run once...).
// findFirstInArrWForEachTest.js
// @user94507 - 20181002
var lg = false; // log to console
var scanSetIdx = 1; // 1 finds, 0 does not find Ap in aMyAp
function js(o) { return JSON.stringify(o); }
function findFirstIn(arr,matcher) {
var r; arr.forEach(function(elt){
if (lg) console.log("LX:",js(elt));
if (!r && matcher(elt)) r = elt; });
return r; }
function findMyAp(aAp,aMyAp) {
var myAp;
findFirstIn(aAp,function(elt){
myAp = myAp || findFirstIn(aMyAp,function(elt2){
if (lg) console.log("LXX:",js(elt),js(elt2),elt.ssid == elt2.ssid);
return elt.ssid == elt2.ssid; });
return myAp; });
return myAp; }
// mocking aMyAp, oWifi w/ aAp scan result and test/validation
var aMyAp = // test data for myAps as provided before scanning for Aps
[ { ssid:"sWW", pwd:"pWW" }
, { ssid:"sXX", pwd:"pXX" }
, { ssid:"sYY", pwd:"pYY" }
];
var /* oWifi = require('Wifi'),*/ myAp;
var oWifi =
{ aAp: // test data for aAp as scanned from oWifi
[ [ { rssi:"rHB", ssid:"sNN" } // w/o myAp
, { rssi:"rHC", ssid:"sQC" }
, { rssi:"rUP", ssid:"sSS" }
, { rssi:"rUQ", ssid:"sBQ" }
]
, [ { rssi:"rAB", ssid:"sWW" } // w/ myAp
, { rssi:"rAC", ssid:"sAC" }
, { rssi:"rNP", ssid:"sXX" }
, { rssi:"rNQ", ssid:"sPQ" }
]
]
, scan: function(cb) {
cb(this.aAp[scanSetIdx]);
}
, connect: function(ssid, options, cb) {
var err = ((ssid == "sXX") && (options.password == "pXX"))
? null
: "No matching ssid and password";
cb(err);
}
};
oWifi.scan(function(aAp) {
if (lg) console.log("L1 ---(scanned):", aAp);
aAp.sort(function(a,b){return a.rssi===b.rssi?0:(a.rssi<b.rssi?1:-1);});
if (lg) console.log("L2 ---(sorted):", aAp);
if ((myAp = findMyAp(aAp,aMyAp))) {
if (lg) console.log("L3 ---(found):",js(myAp));
oWifi.connect(myAp.ssid, {password: myAp.pwd}, function(err) {
if (err===null) {
console.log('Connection success to:', myAp.ssid);
} else {
console.log('Connect error: ',err);
}
});
} else {
console.log('no Ap found');
}
});
Code executes immediately on upload... since it is in level 0... not recommended (by me, especially not when it contains some async / callback and communication stuff, like connect to AP / Wifi)... all level 0 code - code that is executed on uploade / on reception by the Espruino board should always all go into an appInit() method that is called from the onInit() method. But in this (test code) case, it works... at least my (1v3 HW Version) PICO.
You can play with the scanSetIdx values 1 and 0, and of course with the 'test' data in mocked oWifi and in level 0 (aMyAp).
If you want to track visually what is going on, set lg = true; to log in console:
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
And here is the test... (cannot stand code that has not been tested or at least has run once...).
Code executes immediately on upload... since it is in level 0... not recommended (by me, especially not when it contains some async / callback and communication stuff, like connect to AP / Wifi)... all level 0 code - code that is executed on uploade / on reception by the Espruino board should always all go into an appInit() method that is called from the onInit() method. But in this (test code) case, it works... at least my (1v3 HW Version) PICO.
You can play with the
scanSetIdx
values 1 and 0, and of course with the 'test' data in mocked oWifi and in level 0 (aMyAp).If you want to track visually what is going on, set
lg = true;
to log in console: