particle-iot / particle-usb

A library for accessing Particle USB devices
Apache License 2.0
5 stars 1 forks source link

Complete implementation and cut particle-usb@2.1.0 (scan wifi networks, join one, clear wifi networks) #59

Closed joegoggins closed 2 years ago

joegoggins commented 2 years ago

Overview

Executes the technical game-plan Julien, Mirande, Sergey, and Joe aligned on in this PR https://github.com/particle-iot/particle-usb/pull/57 (which will be closed in favor of the approach here).

Key outcomes from this PR:

  1. setDevicePrototype which determines inheritance hierarchy for a given hardware device now has unit tests
  2. WifiDeviceLegacy is used for gen1 (core) and gen2 (photon, p1) devices, WifiDevice is used for argon and P2.
  3. Introduced src/*.test.js for each major sub-class to validate they provide expected interface.

There are some known limitations/questions I encountered while implementing this:

  1. I think the way Mocha tests make assertions against mixin's is by asserting function definitions will work. In ruby-land, I'm used to the idea of asserting that a given instance has has a mixin included (i.e. foo.include?(Mixin), but I don't think this is possible with ES6 classes.
  2. I wanted to move test/device.js into src/**/*.test.js file, but it's mocking approach is way too different than the one I've been using. fakeUSB and proxyquire usage seems odd to me, feels like we're mocking out way to much and that we'd be better off doing this with vanilla Sinon instead; I deliberately didn't tackle this question/refactor because it's a big can o worms
  3. I considered moving _sendAndHandleProtobufRequest into device.js so ANY child class could use it rather than it only being used as the backend of these 3 new wifi methods. But wanted to discuss this before diving into it. I think this method could be the replacement for the private sendRequest and this work could happen while seeking to eliminate the technical debt in particle-usb before device-os-protobuf existed. Hoping to explore this in depth with Sergey and enqueue a chore story to do this later for a future release.
  4. I tried to get passwordless wifi network joining to work, but couldn't, created a bug since it doesn't block P2 PVT, see sc-96826

See sc-96740, for more details.

How to test

  1. Observe new Mocha tests that validate WifiDeviceLegacy interface, WifiDevice interfaces, and others
  2. Run npm run coverage; observe ~95% Mocha test coverage over WifiDevice; missing 5% is because there is a bug in so we don't exercise it; sc-96826
  3. Validate functionality on real devices by running demo's on Argon or P2 in listening mode.

scan-demo.js

const particleUSB = require('./src/particle-usb');
async function main() {
    const devices = await particleUSB.getDevices();
    const device = devices[0];
    await device.open();
    const reply = await device.scanWifiNetworks();
    console.log(reply);
    await device.close();
}

main();

join-demo.js

const particleUSB = require('./src/particle-usb');
async function main() {
    const devices = await particleUSB.getDevices();
    const device = devices[0];
    await device.open();
    const reply = await device.joinNewWifiNetwork({
        ssid: '__TODO__PUT_SSID_HERE__',
        password: ''__TODO__PUT_PASSWORD_HERE__'
    });
    console.log(reply);
    await device.close();
}

main();

clear-demo.js

const particleUSB = require('./src/particle-usb');
async function main() {
    const devices = await particleUSB.getDevices();
    const device = devices[0];
    await device.open();
    const reply = await device.clearWifiNetworks();
    console.log(reply);
    await device.close();
}

main();