This commit is contained in:
parent
8066824f90
commit
2ecc2e7235
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,4 +1,6 @@
|
|||||||
node_modules/
|
node_modules/
|
||||||
dist/
|
dist/
|
||||||
|
coverage/
|
||||||
.vscode/
|
.vscode/
|
||||||
*.tgz
|
*.tgz
|
||||||
|
.DS_Store
|
||||||
|
18
.npmignore
18
.npmignore
@ -1,17 +1,5 @@
|
|||||||
boostmovehub.ts
|
src/
|
||||||
consts.ts
|
|
||||||
duplotrainbase.ts
|
|
||||||
hub.ts
|
|
||||||
index.ts
|
|
||||||
lpf2hub.ts
|
|
||||||
port.ts
|
|
||||||
poweredup.ts
|
|
||||||
puphub.ts
|
|
||||||
pupremote.ts
|
|
||||||
package-lock.json
|
|
||||||
tsconfig.json
|
|
||||||
tslint.json
|
|
||||||
utils.ts
|
|
||||||
wedo2smarthub.ts
|
|
||||||
.vscode/
|
.vscode/
|
||||||
examples/
|
examples/
|
||||||
|
coverage/
|
||||||
|
.DS_Store
|
20
jest.config.js
Normal file
20
jest.config.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
module.exports = {
|
||||||
|
preset: "ts-jest",
|
||||||
|
testEnvironment: "node",
|
||||||
|
collectCoverage: true,
|
||||||
|
roots: [
|
||||||
|
"<rootDir>/src"
|
||||||
|
],
|
||||||
|
transform: {
|
||||||
|
"^.+\\.tsx?$": "ts-jest"
|
||||||
|
},
|
||||||
|
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
|
||||||
|
moduleFileExtensions: [
|
||||||
|
"ts",
|
||||||
|
"tsx",
|
||||||
|
"js",
|
||||||
|
"jsx",
|
||||||
|
"json",
|
||||||
|
"node"
|
||||||
|
]
|
||||||
|
};
|
2592
package-lock.json
generated
2592
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -6,11 +6,12 @@
|
|||||||
"main": "dist/node/index-node.js",
|
"main": "dist/node/index-node.js",
|
||||||
"types": "dist/node/index-node.d.ts",
|
"types": "dist/node/index-node.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
"test": "jest",
|
||||||
"build:node": "tslint -c tslint.json \"./src/*.ts\" && tsc",
|
"build:node": "tslint -c tslint.json \"./src/*.ts\" && tsc",
|
||||||
"build:browser": "tslint -c tslint.json \"./src/*.ts\" && webpack --mode=production",
|
"build:browser": "tslint -c tslint.json \"./src/*.ts\" && webpack --mode=production",
|
||||||
"build:all": "tslint -c tslint.json \"./src/*.ts\" && tsc && webpack --mode=production",
|
"build:all": "tslint -c tslint.json \"./src/*.ts\" && tsc && webpack --mode=production",
|
||||||
"docs": "jsdoc -d docs -c jsdoc.conf.json -t ./node_modules/ink-docstrap/template -R README.md dist/node/consts.js dist/node/poweredup-node.js dist/node/lpf2hub.js dist/node/wedo2smarthub.js dist/node/boostmovehub.js dist/node/puphub.js dist/node/pupremote.js dist/node/duplotrainbase.js dist/node/hub.js dist/node/consts.js",
|
"docs": "jsdoc -d docs -c jsdoc.conf.json -t ./node_modules/ink-docstrap/template -R README.md dist/node/consts.js dist/node/poweredup-node.js dist/node/lpf2hub.js dist/node/wedo2smarthub.js dist/node/boostmovehub.js dist/node/puphub.js dist/node/pupremote.js dist/node/duplotrainbase.js dist/node/hub.js dist/node/consts.js",
|
||||||
"all": "npm run build:all && npm run docs",
|
"all": "npm run test && npm run build:all && npm run docs",
|
||||||
"prepublishOnly": "npm run build:node"
|
"prepublishOnly": "npm run build:node"
|
||||||
},
|
},
|
||||||
"author": "Nathan Kellenicki <nathan@kellenicki.com>",
|
"author": "Nathan Kellenicki <nathan@kellenicki.com>",
|
||||||
@ -22,12 +23,15 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/debug": "0.0.31",
|
"@types/debug": "0.0.31",
|
||||||
|
"@types/jest": "^24.0.11",
|
||||||
"@types/noble": "0.0.38",
|
"@types/noble": "0.0.38",
|
||||||
"@types/node": "^10.12.21",
|
"@types/node": "^10.12.21",
|
||||||
"@types/web-bluetooth": "0.0.4",
|
"@types/web-bluetooth": "0.0.4",
|
||||||
"ink-docstrap": "^1.3.2",
|
"ink-docstrap": "^1.3.2",
|
||||||
|
"jest": "^24.7.1",
|
||||||
"jsdoc": "^3.5.5",
|
"jsdoc": "^3.5.5",
|
||||||
"jsdoc-to-markdown": "^4.0.1",
|
"jsdoc-to-markdown": "^4.0.1",
|
||||||
|
"ts-jest": "^24.0.1",
|
||||||
"ts-loader": "^5.3.3",
|
"ts-loader": "^5.3.3",
|
||||||
"tslint": "^5.12.1",
|
"tslint": "^5.12.1",
|
||||||
"typescript": "^3.3.1",
|
"typescript": "^3.3.1",
|
||||||
|
64
src/__tests__/puphub.test.ts
Normal file
64
src/__tests__/puphub.test.ts
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
import * as Consts from "../consts";
|
||||||
|
import { PUPHub } from "../puphub";
|
||||||
|
import { TestDevice } from "../testdevice";
|
||||||
|
|
||||||
|
const device = new TestDevice()
|
||||||
|
const hub = new PUPHub(device);
|
||||||
|
|
||||||
|
|
||||||
|
beforeAll((done) => {
|
||||||
|
device.on("discoverComplete", async () => {
|
||||||
|
await hub.connect();
|
||||||
|
device.clearOutbox();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
afterAll(async (done) => {
|
||||||
|
await hub.disconnect();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Set motor speed", async (done) => {
|
||||||
|
hub.setMotorSpeed("A", 52);
|
||||||
|
expect(await device.readFromOutbox()).toEqual(Buffer.from([0x0a, 0x00, 0x81, 0x00, 0x11, 0x60, 0x00, 0x34, 0x00, 0x00]));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Set motor speed for specific amount of time", async (done) => {
|
||||||
|
hub.setMotorSpeed("A", 52, 3000);
|
||||||
|
expect(await device.readFromOutbox()).toEqual(Buffer.from([0x0a, 0x00, 0x81, 0x00, 0x11, 0x60, 0x00, 0x34, 0x00, 0x00]));
|
||||||
|
expect(await device.readFromOutbox()).toEqual(Buffer.from([0x0a, 0x00, 0x81, 0x00, 0x11, 0x60, 0x00, 0x00, 0x00, 0x00]));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Brake a motor", async (done) => {
|
||||||
|
hub.brakeMotor("A");
|
||||||
|
expect(await device.readFromOutbox()).toEqual(Buffer.from([0x0a, 0x00, 0x81, 0x00, 0x11, 0x60, 0x00, 0x7f, 0x00, 0x00]));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Attempt to set two speeds on a port that only accepts one", async (done) => {
|
||||||
|
expect(() => hub.setMotorSpeed("A", [52, 32])).toThrow("Port A can only accept a single speed");
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Set light brightness", async (done) => {
|
||||||
|
hub.setLightBrightness("B", 74);
|
||||||
|
expect(await device.readFromOutbox()).toEqual(Buffer.from([0x08, 0x00, 0x81, 0x01, 0x11, 0x51, 0x00, 0x4a]));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Set light brightness for specific amount of time", async (done) => {
|
||||||
|
hub.setLightBrightness("B", 74, 2000);
|
||||||
|
expect(await device.readFromOutbox()).toEqual(Buffer.from([0x08, 0x00, 0x81, 0x01, 0x11, 0x51, 0x00, 0x4a]));
|
||||||
|
expect(await device.readFromOutbox()).toEqual(Buffer.from([0x08, 0x00, 0x81, 0x01, 0x11, 0x51, 0x00, 0x00]));
|
||||||
|
done();
|
||||||
|
});
|
45
src/__tests__/pupremote.test.ts
Normal file
45
src/__tests__/pupremote.test.ts
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
import * as Consts from "../consts";
|
||||||
|
import { PUPRemote } from "../pupremote";
|
||||||
|
import { TestDevice } from "../testdevice";
|
||||||
|
|
||||||
|
const device = new TestDevice()
|
||||||
|
const remote = new PUPRemote(device);
|
||||||
|
|
||||||
|
|
||||||
|
beforeAll((done) => {
|
||||||
|
device.on("discoverComplete", async () => {
|
||||||
|
await remote.connect();
|
||||||
|
device.clearOutbox();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
afterAll(async (done) => {
|
||||||
|
await remote.disconnect();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Set LED color to BLUE via discrete value", async (done) => {
|
||||||
|
remote.setLEDColor(Consts.Color.BLUE);
|
||||||
|
expect(await device.readFromOutbox()).toEqual(Buffer.from([0x0a, 0x00, 0x41, 0x34, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]));
|
||||||
|
expect(await device.readFromOutbox()).toEqual(Buffer.from([0x08, 0x00, 0x81, 0x34, 0x11, 0x51, 0x00, 0x03]));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Turn off LED", async (done) => {
|
||||||
|
remote.setLEDColor(false);
|
||||||
|
expect(await device.readFromOutbox()).toEqual(Buffer.from([0x0a, 0x00, 0x41, 0x34, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]));
|
||||||
|
expect(await device.readFromOutbox()).toEqual(Buffer.from([0x08, 0x00, 0x81, 0x34, 0x11, 0x51, 0x00, 0x00]));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("Set LED color to 127, 32, 233 via RGB values", async (done) => {
|
||||||
|
remote.setLEDRGB(127, 32, 233);
|
||||||
|
expect(await device.readFromOutbox()).toEqual(Buffer.from([0x0a, 0x00, 0x41, 0x34, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00]));
|
||||||
|
expect(await device.readFromOutbox()).toEqual(Buffer.from([0x0a, 0x00, 0x81, 0x34, 0x11, 0x51, 0x01, 0x7f, 0x20, 0xe9]));
|
||||||
|
done();
|
||||||
|
});
|
127
src/testdevice.ts
Normal file
127
src/testdevice.ts
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
import Debug = require("debug");
|
||||||
|
import { EventEmitter } from "events";
|
||||||
|
import { IBLEDevice } from "./interfaces";
|
||||||
|
const debug = Debug("testdevice");
|
||||||
|
|
||||||
|
|
||||||
|
export class TestDevice extends EventEmitter implements IBLEDevice {
|
||||||
|
|
||||||
|
private _uuid: string;
|
||||||
|
private _name: string = "";
|
||||||
|
|
||||||
|
private _listeners: {[uuid: string]: any} = {};
|
||||||
|
private _characteristics: {[uuid: string]: any} = {};
|
||||||
|
|
||||||
|
private _queue: Promise<any> = Promise.resolve();
|
||||||
|
private _mailbox: Buffer[] = [];
|
||||||
|
private _outbox: Buffer[] = [];
|
||||||
|
private _outboxCallback: (() => void) | null = null;
|
||||||
|
|
||||||
|
private _connected: boolean = false;
|
||||||
|
private _connecting: boolean = false;
|
||||||
|
|
||||||
|
|
||||||
|
constructor () {
|
||||||
|
super();
|
||||||
|
this._uuid = "test-device";
|
||||||
|
this._name = "Test Device";
|
||||||
|
setTimeout(() => {
|
||||||
|
this.emit("discoverComplete");
|
||||||
|
}, 2000);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public get uuid () {
|
||||||
|
return this._uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public get name () {
|
||||||
|
return this._name;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public get connecting () {
|
||||||
|
return this._connecting;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public get connected () {
|
||||||
|
return this._connected;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public connect () {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this._connected = true;
|
||||||
|
return resolve();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public disconnect () {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
return resolve();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public discoverCharacteristicsForService (uuid: string) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
return resolve();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public subscribeToCharacteristic (uuid: string, callback: (data: Buffer) => void) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public addToCharacteristicMailbox (uuid: string, data: Buffer) {
|
||||||
|
this._mailbox.push(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public readFromCharacteristic (uuid: string, callback: (err: string | null, data: Buffer | null) => void) {
|
||||||
|
callback(null, Buffer.alloc(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public writeToCharacteristic (uuid: string, data: Buffer, callback?: () => void) {
|
||||||
|
this._outbox.push(data);
|
||||||
|
if (this._outboxCallback) {
|
||||||
|
this._outboxCallback();
|
||||||
|
this._outboxCallback = null;
|
||||||
|
}
|
||||||
|
if (callback) {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public readFromOutbox () {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (this._outbox.length >= 1) {
|
||||||
|
return resolve(this._outbox.shift());
|
||||||
|
} else {
|
||||||
|
this._outboxCallback = () => {
|
||||||
|
return resolve(this._outbox.shift());
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public clearOutbox () {
|
||||||
|
this._outbox = [];
|
||||||
|
this._outboxCallback = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private _sanitizeUUID (uuid: string) {
|
||||||
|
return uuid.replace(/-/g, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user