• 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.com
     1v99 (c) 2018 G.Williams
    >L1 ---(scanned): [
      {
        "rssi": "rAB",
        "ssid": "sWW"
       },
      {
        "rssi": "rAC",
        "ssid": "sAC"
       },
      {
        "rssi": "rNP",
        "ssid": "sXX"
       },
      {
        "rssi": "rNQ",
        "ssid": "sPQ"
       }
     ]
    L2 ---(sorted): [
      {
        "rssi": "rNQ",
        "ssid": "sPQ"
       },
      {
        "rssi": "rNP",
        "ssid": "sXX"
       },
      {
        "rssi": "rAC",
        "ssid": "sAC"
       },
      {
        "rssi": "rAB",
        "ssid": "sWW"
       }
     ]
    LX: {"rssi":"rNQ","ssid":"sPQ"}
    LX: {"ssid":"sWW","pwd":"pWW"}
    LXX: {"rssi":"rNQ","ssid":"sPQ"} {"ssid":"sWW","pwd":"pWW"} false
    LX: {"ssid":"sXX","pwd":"pXX"}
    LXX: {"rssi":"rNQ","ssid":"sPQ"} {"ssid":"sXX","pwd":"pXX"} false
    LX: {"ssid":"sYY","pwd":"pYY"}
    LXX: {"rssi":"rNQ","ssid":"sPQ"} {"ssid":"sYY","pwd":"pYY"} false
    LX: {"rssi":"rNP","ssid":"sXX"}
    LX: {"ssid":"sWW","pwd":"pWW"}
    LXX: {"rssi":"rNP","ssid":"sXX"} {"ssid":"sWW","pwd":"pWW"} false
    LX: {"ssid":"sXX","pwd":"pXX"}
    LXX: {"rssi":"rNP","ssid":"sXX"} {"ssid":"sXX","pwd":"pXX"} true
    LX: {"ssid":"sYY","pwd":"pYY"}
    LX: {"rssi":"rAC","ssid":"sAC"}
    LX: {"rssi":"rAB","ssid":"sWW"}
    L3 ---(found): {"ssid":"sXX","pwd":"pXX"}
    Connection success to: sXX
    =undefined
    >
    
About

Avatar for allObjects @allObjects started