Control+ (Technic Medium Hub) devices, renamed hubs to be more inline with official naming convention
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
6e77d697fc
commit
a5a9b2e880
@ -2,7 +2,7 @@
|
|||||||
* @typedef HubType
|
* @typedef HubType
|
||||||
* @property {number} UNKNOWN 0
|
* @property {number} UNKNOWN 0
|
||||||
* @property {number} WEDO2_SMART_HUB 1
|
* @property {number} WEDO2_SMART_HUB 1
|
||||||
* @property {number} BOOST_MOVE_HUB 2
|
* @property {number} MOVE_HUB 2
|
||||||
* @property {number} POWERED_UP_HUB 3
|
* @property {number} POWERED_UP_HUB 3
|
||||||
* @property {number} POWERED_UP_REMOTE 4
|
* @property {number} POWERED_UP_REMOTE 4
|
||||||
* @property {number} DUPLO_TRAIN_HUB 5
|
* @property {number} DUPLO_TRAIN_HUB 5
|
||||||
@ -11,11 +11,11 @@
|
|||||||
export enum HubType {
|
export enum HubType {
|
||||||
UNKNOWN = 0,
|
UNKNOWN = 0,
|
||||||
WEDO2_SMART_HUB = 1,
|
WEDO2_SMART_HUB = 1,
|
||||||
BOOST_MOVE_HUB = 2,
|
MOVE_HUB = 2,
|
||||||
POWERED_UP_HUB = 3,
|
HUB = 3,
|
||||||
POWERED_UP_REMOTE = 4,
|
REMOTE_CONTROL = 4,
|
||||||
DUPLO_TRAIN_HUB = 5,
|
DUPLO_TRAIN_BASE = 5,
|
||||||
CONTROL_PLUS_HUB = 6
|
TECHNIC_MEDIUM_HUB = 6
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -155,11 +155,11 @@ export enum DuploTrainBaseSound {
|
|||||||
|
|
||||||
|
|
||||||
export enum BLEManufacturerData {
|
export enum BLEManufacturerData {
|
||||||
DUPLO_TRAIN_HUB_ID = 32,
|
DUPLO_TRAIN_BASE_ID = 32,
|
||||||
BOOST_MOVE_HUB_ID = 64,
|
MOVE_HUB_ID = 64,
|
||||||
POWERED_UP_HUB_ID = 65,
|
HUB_ID = 65,
|
||||||
POWERED_UP_REMOTE_ID = 66,
|
REMOTE_CONTROL_ID = 66,
|
||||||
CONTROL_PLUS_LARGE_HUB = 128
|
TECHNIC_MEDIUM_HUB = 128
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ export namespace CurrentSensor {
|
|||||||
|
|
||||||
export const MaxCurrentValue: {[hubType: number]: number} = {
|
export const MaxCurrentValue: {[hubType: number]: number} = {
|
||||||
[Consts.HubType.UNKNOWN]: 2444,
|
[Consts.HubType.UNKNOWN]: 2444,
|
||||||
[Consts.HubType.CONTROL_PLUS_HUB]: 4175,
|
[Consts.HubType.TECHNIC_MEDIUM_HUB]: 4175,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const MaxCurrentRaw: {[hubType: number]: number} = {
|
export const MaxCurrentRaw: {[hubType: number]: number} = {
|
||||||
|
@ -4,20 +4,20 @@ import { IDeviceInterface } from "../interfaces";
|
|||||||
|
|
||||||
import * as Consts from "../consts";
|
import * as Consts from "../consts";
|
||||||
|
|
||||||
export class PUPRemoteButton extends Device {
|
export class RemoteControlButton extends Device {
|
||||||
|
|
||||||
constructor (hub: IDeviceInterface, portId: number) {
|
constructor (hub: IDeviceInterface, portId: number) {
|
||||||
super(hub, portId, PUPRemoteButton.ModeMap, Consts.DeviceType.PUP_REMOTE_BUTTON);
|
super(hub, portId, RemoteControlButton.ModeMap, Consts.DeviceType.PUP_REMOTE_BUTTON);
|
||||||
}
|
}
|
||||||
|
|
||||||
public receive (message: Buffer) {
|
public receive (message: Buffer) {
|
||||||
const mode = this._mode;
|
const mode = this._mode;
|
||||||
|
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
case PUPRemoteButton.Mode.BUTTON_EVENTS:
|
case RemoteControlButton.Mode.BUTTON_EVENTS:
|
||||||
/**
|
/**
|
||||||
* Emits when a button on the remote is pressed or released.
|
* Emits when a button on the remote is pressed or released.
|
||||||
* @event PUPRemoteButton#button
|
* @event RemoteControlButton#button
|
||||||
* @param {number} event
|
* @param {number} event
|
||||||
*/
|
*/
|
||||||
const event = message[4];
|
const event = message[4];
|
||||||
@ -28,14 +28,14 @@ export class PUPRemoteButton extends Device {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export namespace PUPRemoteButton {
|
export namespace RemoteControlButton {
|
||||||
|
|
||||||
export enum Mode {
|
export enum Mode {
|
||||||
BUTTON_EVENTS = 0x00
|
BUTTON_EVENTS = 0x00
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ModeMap: {[event: string]: number} = {
|
export const ModeMap: {[event: string]: number} = {
|
||||||
"button": PUPRemoteButton.Mode.BUTTON_EVENTS
|
"button": RemoteControlButton.Mode.BUTTON_EVENTS
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ButtonState: {[state: string]: number} = {
|
export const ButtonState: {[state: string]: number} = {
|
@ -48,15 +48,15 @@ export namespace VoltageSensor {
|
|||||||
|
|
||||||
export const MaxVoltageValue: {[hubType: number]: number} = {
|
export const MaxVoltageValue: {[hubType: number]: number} = {
|
||||||
[Consts.HubType.UNKNOWN]: 9.615,
|
[Consts.HubType.UNKNOWN]: 9.615,
|
||||||
[Consts.HubType.DUPLO_TRAIN_HUB]: 6.4,
|
[Consts.HubType.DUPLO_TRAIN_BASE]: 6.4,
|
||||||
[Consts.HubType.POWERED_UP_REMOTE]: 6.4,
|
[Consts.HubType.REMOTE_CONTROL]: 6.4,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const MaxVoltageRaw: {[hubType: number]: number} = {
|
export const MaxVoltageRaw: {[hubType: number]: number} = {
|
||||||
[Consts.HubType.UNKNOWN]: 3893,
|
[Consts.HubType.UNKNOWN]: 3893,
|
||||||
[Consts.HubType.DUPLO_TRAIN_HUB]: 3047,
|
[Consts.HubType.DUPLO_TRAIN_BASE]: 3047,
|
||||||
[Consts.HubType.POWERED_UP_REMOTE]: 3200,
|
[Consts.HubType.REMOTE_CONTROL]: 3200,
|
||||||
[Consts.HubType.CONTROL_PLUS_HUB]: 4095,
|
[Consts.HubType.TECHNIC_MEDIUM_HUB]: 4095,
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
375
src/hubs/basehub.ts
Normal file
375
src/hubs/basehub.ts
Normal file
@ -0,0 +1,375 @@
|
|||||||
|
import { EventEmitter } from "events";
|
||||||
|
|
||||||
|
import { IBLEAbstraction } from "../interfaces";
|
||||||
|
|
||||||
|
import { ColorDistanceSensor } from "../devices/colordistancesensor";
|
||||||
|
import { CurrentSensor } from "../devices/currentsensor";
|
||||||
|
import { Device } from "../devices/device";
|
||||||
|
import { HubLED } from "../devices/hubled";
|
||||||
|
import { Light } from "../devices/light";
|
||||||
|
import { MediumLinearMotor } from "../devices/mediumlinearmotor";
|
||||||
|
import { MotionSensor } from "../devices/motionsensor";
|
||||||
|
import { MoveHubMediumLinearMotor } from "../devices/movehubmediumlinearmotor";
|
||||||
|
import { MoveHubTiltSensor } from "../devices/movehubtiltsensor";
|
||||||
|
import { RemoteControlButton } from "../devices/remotecontrolbutton";
|
||||||
|
import { SimpleMediumLinearMotor } from "../devices/simplemediumlinearmotor";
|
||||||
|
import { TechnicLargeLinearMotor } from "../devices/techniclargelinearmotor";
|
||||||
|
import { TechnicMediumHubAccelerometerSensor } from "../devices/technicmediumhubaccelerometersensor";
|
||||||
|
import { TechnicMediumHubGyroSensor } from "../devices/technicmediumhubgyrosensor";
|
||||||
|
import { TechnicMediumHubTiltSensor } from "../devices/technicmediumhubtiltsensor";
|
||||||
|
import { TechnicXLargeLinearMotor } from "../devices/technicxlargelinearmotor";
|
||||||
|
import { TiltSensor } from "../devices/tiltsensor";
|
||||||
|
import { TrainMotor } from "../devices/trainmotor";
|
||||||
|
import { VoltageSensor } from "../devices/voltagesensor";
|
||||||
|
|
||||||
|
import * as Consts from "../consts";
|
||||||
|
|
||||||
|
import Debug = require("debug");
|
||||||
|
const debug = Debug("basehub");
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @class BaseHub
|
||||||
|
* @extends EventEmitter
|
||||||
|
*/
|
||||||
|
export class BaseHub extends EventEmitter {
|
||||||
|
|
||||||
|
protected _attachedDevices: {[portId: number]: Device} = {};
|
||||||
|
// protected _virtualPorts: {[portName: string]: Port} = {};
|
||||||
|
|
||||||
|
protected _name: string = "";
|
||||||
|
protected _firmwareVersion: string = "0.0.00.0000";
|
||||||
|
protected _hardwareVersion: string = "0.0.00.0000";
|
||||||
|
protected _primaryMACAddress: string = "00:00:00:00:00:00";
|
||||||
|
protected _batteryLevel: number = 100;
|
||||||
|
protected _rssi: number = -60;
|
||||||
|
|
||||||
|
protected _bleDevice: IBLEAbstraction;
|
||||||
|
|
||||||
|
private _type: Consts.HubType;
|
||||||
|
private _portMap: {[portName: string]: number} = {};
|
||||||
|
private _attachCallbacks: ((device: Device) => void)[] = [];
|
||||||
|
|
||||||
|
constructor (device: IBLEAbstraction, portMap: {[portName: string]: number} = {}, type: Consts.HubType = Consts.HubType.UNKNOWN) {
|
||||||
|
super();
|
||||||
|
this._type = type;
|
||||||
|
this._bleDevice = device;
|
||||||
|
this._portMap = portMap;
|
||||||
|
device.on("disconnect", () => {
|
||||||
|
/**
|
||||||
|
* Emits when the hub is disconnected.
|
||||||
|
* @event Hub#disconnect
|
||||||
|
*/
|
||||||
|
this.emit("disconnect");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @readonly
|
||||||
|
* @property {string} name Name of the hub
|
||||||
|
*/
|
||||||
|
public get name () {
|
||||||
|
return this._bleDevice.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @readonly
|
||||||
|
* @property {string} type Hub type
|
||||||
|
*/
|
||||||
|
public get type () {
|
||||||
|
return this._type;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @readonly
|
||||||
|
* @property {string[]} ports Array of port names
|
||||||
|
*/
|
||||||
|
public get ports () {
|
||||||
|
return Object.keys(this._portMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @readonly
|
||||||
|
* @property {string} firmwareVersion Firmware version of the hub
|
||||||
|
*/
|
||||||
|
public get firmwareVersion () {
|
||||||
|
return this._firmwareVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @readonly
|
||||||
|
* @property {string} firmwareVersion Hardware version of the hub
|
||||||
|
*/
|
||||||
|
public get hardwareVersion () {
|
||||||
|
return this._hardwareVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @readonly
|
||||||
|
* @property {string} primaryMACAddress Primary MAC address of the hub
|
||||||
|
*/
|
||||||
|
public get primaryMACAddress () {
|
||||||
|
return this._primaryMACAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @readonly
|
||||||
|
* @property {string} uuid UUID of the hub
|
||||||
|
*/
|
||||||
|
public get uuid () {
|
||||||
|
return this._bleDevice.uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @readonly
|
||||||
|
* @property {number} batteryLevel Battery level of the hub (Percentage between 0-100)
|
||||||
|
*/
|
||||||
|
public get batteryLevel () {
|
||||||
|
return this._batteryLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @readonly
|
||||||
|
* @property {number} rssi Signal strength of the hub
|
||||||
|
*/
|
||||||
|
public get rssi () {
|
||||||
|
return this._rssi;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connect to the Hub.
|
||||||
|
* @method Hub#connect
|
||||||
|
* @returns {Promise} Resolved upon successful connect.
|
||||||
|
*/
|
||||||
|
public connect () {
|
||||||
|
return new Promise(async (connectResolve, connectReject) => {
|
||||||
|
if (this._bleDevice.connecting) {
|
||||||
|
return connectReject("Already connecting");
|
||||||
|
} else if (this._bleDevice.connected) {
|
||||||
|
return connectReject("Already connected");
|
||||||
|
}
|
||||||
|
await this._bleDevice.connect();
|
||||||
|
return connectResolve();
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disconnect the Hub.
|
||||||
|
* @method Hub#disconnect
|
||||||
|
* @returns {Promise} Resolved upon successful disconnect.
|
||||||
|
*/
|
||||||
|
public disconnect () {
|
||||||
|
return this._bleDevice.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public getDeviceAtPort (portName: string) {
|
||||||
|
const portId = this._portMap[portName];
|
||||||
|
if (portId) {
|
||||||
|
return this._attachedDevices[portId];
|
||||||
|
} else {
|
||||||
|
throw new Error(`Port ${portName} does not exist on this hub type`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public waitForDeviceAtPort (portName: string) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
const existingDevice = this.getDeviceAtPort(portName);
|
||||||
|
if (existingDevice) {
|
||||||
|
return resolve(existingDevice);
|
||||||
|
}
|
||||||
|
this._attachCallbacks.push((device) => {
|
||||||
|
if (device.portName === portName) {
|
||||||
|
return resolve(device);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public getDevices () {
|
||||||
|
return Object.values(this._attachedDevices);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public getDevicesByType (deviceType: number) {
|
||||||
|
return this.getDevices().filter((device) => device.type === deviceType);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public waitForDeviceByType (deviceType: number) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
const existingDevices = this.getDevicesByType(deviceType);
|
||||||
|
if (existingDevices.length >= 1) {
|
||||||
|
return resolve(existingDevices[0]);
|
||||||
|
}
|
||||||
|
this._attachCallbacks.push((device) => {
|
||||||
|
if (device.type === deviceType) {
|
||||||
|
return resolve(device);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public getPortNameForPortId (portId: number) {
|
||||||
|
for (const port of Object.keys(this._portMap)) {
|
||||||
|
if (this._portMap[port] === portId) {
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sleep a given amount of time.
|
||||||
|
*
|
||||||
|
* This is a helper method to make it easier to add delays into a chain of commands.
|
||||||
|
* @method Hub#sleep
|
||||||
|
* @param {number} delay How long to sleep (in milliseconds).
|
||||||
|
* @returns {Promise} Resolved after the delay is finished.
|
||||||
|
*/
|
||||||
|
public sleep (delay: number) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
global.setTimeout(resolve, delay);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wait until a given list of concurrently running commands are complete.
|
||||||
|
*
|
||||||
|
* This is a helper method to make it easier to wait for concurrent commands to complete.
|
||||||
|
* @method Hub#wait
|
||||||
|
* @param {Array<Promise<any>>} commands Array of executing commands.
|
||||||
|
* @returns {Promise} Resolved after the commands are finished.
|
||||||
|
*/
|
||||||
|
public wait (commands: Array<Promise<any>>) {
|
||||||
|
return Promise.all(commands);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public send (message: Buffer, uuid: string, callback?: () => void) {
|
||||||
|
if (callback) {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public subscribe (portId: number, deviceType: number, mode: number) {
|
||||||
|
// NK Do nothing here
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected _attachDevice (device: Device) {
|
||||||
|
this._attachedDevices[device.portId] = device;
|
||||||
|
/**
|
||||||
|
* Emits when a device is attached to the Hub.
|
||||||
|
* @event Hub#attach
|
||||||
|
* @param {Device} device
|
||||||
|
*/
|
||||||
|
this.emit("attach", device);
|
||||||
|
this._attachCallbacks.forEach((callback) => {
|
||||||
|
callback(device);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected _detachDevice (device: Device) {
|
||||||
|
delete this._attachedDevices[device.portId];
|
||||||
|
/**
|
||||||
|
* Emits when a device is detached from the Hub.
|
||||||
|
* @event Hub#attach
|
||||||
|
* @param {Device} device
|
||||||
|
*/
|
||||||
|
this.emit("detach", device);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected _createDevice (deviceType: number, portId: number) {
|
||||||
|
let device;
|
||||||
|
|
||||||
|
switch (deviceType) {
|
||||||
|
case Consts.DeviceType.LIGHT:
|
||||||
|
device = new Light(this, portId);
|
||||||
|
break;
|
||||||
|
case Consts.DeviceType.TRAIN_MOTOR:
|
||||||
|
device = new TrainMotor(this, portId);
|
||||||
|
break;
|
||||||
|
case Consts.DeviceType.SIMPLE_MEDIUM_LINEAR_MOTOR:
|
||||||
|
device = new SimpleMediumLinearMotor(this, portId);
|
||||||
|
break;
|
||||||
|
case Consts.DeviceType.MOVE_HUB_MEDIUM_LINEAR_MOTOR:
|
||||||
|
device = new MoveHubMediumLinearMotor(this, portId);
|
||||||
|
break;
|
||||||
|
case Consts.DeviceType.MOTION_SENSOR:
|
||||||
|
device = new MotionSensor(this, portId);
|
||||||
|
break;
|
||||||
|
case Consts.DeviceType.TILT_SENSOR:
|
||||||
|
device = new TiltSensor(this, portId);
|
||||||
|
break;
|
||||||
|
case Consts.DeviceType.MOVE_HUB_TILT_SENSOR:
|
||||||
|
device = new MoveHubTiltSensor(this, portId);
|
||||||
|
break;
|
||||||
|
case Consts.DeviceType.TECHNIC_MEDIUM_HUB_TILT_SENSOR:
|
||||||
|
device = new TechnicMediumHubTiltSensor(this, portId);
|
||||||
|
break;
|
||||||
|
case Consts.DeviceType.TECHNIC_MEDIUM_HUB_GYRO_SENSOR:
|
||||||
|
device = new TechnicMediumHubGyroSensor(this, portId);
|
||||||
|
break;
|
||||||
|
case Consts.DeviceType.TECHNIC_MEDIUM_HUB_ACCELEROMETER:
|
||||||
|
device = new TechnicMediumHubAccelerometerSensor(this, portId);
|
||||||
|
break;
|
||||||
|
case Consts.DeviceType.MEDIUM_LINEAR_MOTOR:
|
||||||
|
device = new MediumLinearMotor(this, portId);
|
||||||
|
break;
|
||||||
|
case Consts.DeviceType.TECHNIC_LARGE_LINEAR_MOTOR:
|
||||||
|
device = new TechnicLargeLinearMotor(this, portId);
|
||||||
|
break;
|
||||||
|
case Consts.DeviceType.TECHNIC_XLARGE_LINEAR_MOTOR:
|
||||||
|
device = new TechnicXLargeLinearMotor(this, portId);
|
||||||
|
break;
|
||||||
|
case Consts.DeviceType.COLOR_DISTANCE_SENSOR:
|
||||||
|
device = new ColorDistanceSensor(this, portId);
|
||||||
|
break;
|
||||||
|
case Consts.DeviceType.VOLTAGE_SENSOR:
|
||||||
|
device = new VoltageSensor(this, portId);
|
||||||
|
break;
|
||||||
|
case Consts.DeviceType.CURRENT_SENSOR:
|
||||||
|
device = new CurrentSensor(this, portId);
|
||||||
|
break;
|
||||||
|
case Consts.DeviceType.PUP_REMOTE_BUTTON:
|
||||||
|
device = new RemoteControlButton(this, portId);
|
||||||
|
break;
|
||||||
|
case Consts.DeviceType.HUB_LED:
|
||||||
|
device = new HubLED(this, portId);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
device = new Device(this, portId, undefined, deviceType);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return device;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected _getDeviceByPortId (portId: number) {
|
||||||
|
return this._attachedDevices[portId];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -13,7 +13,7 @@ const debug = Debug("duplotrainbase");
|
|||||||
* The DuploTrainBase is emitted if the discovered device is a Duplo Train Base.
|
* The DuploTrainBase is emitted if the discovered device is a Duplo Train Base.
|
||||||
* @class DuploTrainBase
|
* @class DuploTrainBase
|
||||||
* @extends LPF2Hub
|
* @extends LPF2Hub
|
||||||
* @extends Hub
|
* @extends BaseHub
|
||||||
*/
|
*/
|
||||||
export class DuploTrainBase extends LPF2Hub {
|
export class DuploTrainBase extends LPF2Hub {
|
||||||
|
|
||||||
@ -25,15 +25,13 @@ export class DuploTrainBase extends LPF2Hub {
|
|||||||
peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 &&
|
peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 &&
|
||||||
peripheral.advertisement.manufacturerData &&
|
peripheral.advertisement.manufacturerData &&
|
||||||
peripheral.advertisement.manufacturerData.length > 3 &&
|
peripheral.advertisement.manufacturerData.length > 3 &&
|
||||||
peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.DUPLO_TRAIN_HUB_ID
|
peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.DUPLO_TRAIN_BASE_ID
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected _ledPort = 0x11;
|
|
||||||
|
|
||||||
constructor (device: IBLEAbstraction) {
|
constructor (device: IBLEAbstraction) {
|
||||||
super(device, DuploTrainBase.PortMap, Consts.HubType.DUPLO_TRAIN_HUB);
|
super(device, DuploTrainBase.PortMap, Consts.HubType.DUPLO_TRAIN_BASE);
|
||||||
debug("Discovered Duplo Train Base");
|
debug("Discovered Duplo Train Base");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
383
src/hubs/hub.ts
383
src/hubs/hub.ts
@ -1,26 +1,9 @@
|
|||||||
import { EventEmitter } from "events";
|
import { Peripheral } from "@abandonware/noble";
|
||||||
|
import compareVersion from "compare-versions";
|
||||||
|
|
||||||
import { IBLEAbstraction } from "../interfaces";
|
import { IBLEAbstraction } from "../interfaces";
|
||||||
|
|
||||||
import { ColorDistanceSensor } from "../devices/colordistancesensor";
|
import { LPF2Hub } from "./lpf2hub";
|
||||||
import { CurrentSensor } from "../devices/currentsensor";
|
|
||||||
import { Device } from "../devices/device";
|
|
||||||
import { HubLED } from "../devices/hubled";
|
|
||||||
import { Light } from "../devices/light";
|
|
||||||
import { MediumLinearMotor } from "../devices/mediumlinearmotor";
|
|
||||||
import { MotionSensor } from "../devices/motionsensor";
|
|
||||||
import { MoveHubMediumLinearMotor } from "../devices/movehubmediumlinearmotor";
|
|
||||||
import { MoveHubTiltSensor } from "../devices/movehubtiltsensor";
|
|
||||||
import { PUPRemoteButton } from "../devices/pupremotebutton";
|
|
||||||
import { SimpleMediumLinearMotor } from "../devices/simplemediumlinearmotor";
|
|
||||||
import { TechnicLargeLinearMotor } from "../devices/techniclargelinearmotor";
|
|
||||||
import { TechnicMediumHubAccelerometerSensor } from "../devices/technicmediumhubaccelerometersensor";
|
|
||||||
import { TechnicMediumHubGyroSensor } from "../devices/technicmediumhubgyrosensor";
|
|
||||||
import { TechnicMediumHubTiltSensor } from "../devices/technicmediumhubtiltsensor";
|
|
||||||
import { TechnicXLargeLinearMotor } from "../devices/technicxlargelinearmotor";
|
|
||||||
import { TiltSensor } from "../devices/tiltsensor";
|
|
||||||
import { TrainMotor } from "../devices/trainmotor";
|
|
||||||
import { VoltageSensor } from "../devices/voltagesensor";
|
|
||||||
|
|
||||||
import * as Consts from "../consts";
|
import * as Consts from "../consts";
|
||||||
|
|
||||||
@ -29,347 +12,57 @@ const debug = Debug("hub");
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* The Hub is emitted if the discovered device is a Hub.
|
||||||
* @class Hub
|
* @class Hub
|
||||||
* @extends EventEmitter
|
* @extends LPF2Hub
|
||||||
|
* @extends BaseHub
|
||||||
*/
|
*/
|
||||||
export class Hub extends EventEmitter {
|
export class Hub extends LPF2Hub {
|
||||||
|
|
||||||
protected _attachedDevices: {[portId: number]: Device} = {};
|
|
||||||
// protected _virtualPorts: {[portName: string]: Port} = {};
|
|
||||||
|
|
||||||
protected _name: string = "";
|
public static IsHub (peripheral: Peripheral) {
|
||||||
protected _firmwareVersion: string = "0.0.00.0000";
|
return (
|
||||||
protected _hardwareVersion: string = "0.0.00.0000";
|
peripheral.advertisement &&
|
||||||
protected _primaryMACAddress: string = "00:00:00:00:00:00";
|
peripheral.advertisement.serviceUuids &&
|
||||||
protected _batteryLevel: number = 100;
|
peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 &&
|
||||||
protected _rssi: number = -60;
|
peripheral.advertisement.manufacturerData &&
|
||||||
|
peripheral.advertisement.manufacturerData.length > 3 &&
|
||||||
|
peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.HUB_ID
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
protected _bleDevice: IBLEAbstraction;
|
protected _currentPort = 0x3b;
|
||||||
|
|
||||||
private _type: Consts.HubType;
|
constructor (device: IBLEAbstraction) {
|
||||||
private _portMap: {[portName: string]: number} = {};
|
super(device, Hub.PortMap, Consts.HubType.HUB);
|
||||||
private _attachCallbacks: ((device: Device) => void)[] = [];
|
debug("Discovered Powered UP Hub");
|
||||||
|
|
||||||
constructor (device: IBLEAbstraction, portMap: {[portName: string]: number} = {}, type: Consts.HubType = Consts.HubType.UNKNOWN) {
|
|
||||||
super();
|
|
||||||
this._type = type;
|
|
||||||
this._bleDevice = device;
|
|
||||||
this._portMap = portMap;
|
|
||||||
device.on("disconnect", () => {
|
|
||||||
/**
|
|
||||||
* Emits when the hub is disconnected.
|
|
||||||
* @event Hub#disconnect
|
|
||||||
*/
|
|
||||||
this.emit("disconnect");
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @readonly
|
|
||||||
* @property {string} name Name of the hub
|
|
||||||
*/
|
|
||||||
public get name () {
|
|
||||||
return this._bleDevice.name;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @readonly
|
|
||||||
* @property {string} type Hub type
|
|
||||||
*/
|
|
||||||
public get type () {
|
|
||||||
return this._type;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @readonly
|
|
||||||
* @property {string[]} ports Array of port names
|
|
||||||
*/
|
|
||||||
public get ports () {
|
|
||||||
return Object.keys(this._portMap);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @readonly
|
|
||||||
* @property {string} firmwareVersion Firmware version of the hub
|
|
||||||
*/
|
|
||||||
public get firmwareVersion () {
|
|
||||||
return this._firmwareVersion;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @readonly
|
|
||||||
* @property {string} firmwareVersion Hardware version of the hub
|
|
||||||
*/
|
|
||||||
public get hardwareVersion () {
|
|
||||||
return this._hardwareVersion;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @readonly
|
|
||||||
* @property {string} primaryMACAddress Primary MAC address of the hub
|
|
||||||
*/
|
|
||||||
public get primaryMACAddress () {
|
|
||||||
return this._primaryMACAddress;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @readonly
|
|
||||||
* @property {string} uuid UUID of the hub
|
|
||||||
*/
|
|
||||||
public get uuid () {
|
|
||||||
return this._bleDevice.uuid;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @readonly
|
|
||||||
* @property {number} batteryLevel Battery level of the hub (Percentage between 0-100)
|
|
||||||
*/
|
|
||||||
public get batteryLevel () {
|
|
||||||
return this._batteryLevel;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @readonly
|
|
||||||
* @property {number} rssi Signal strength of the hub
|
|
||||||
*/
|
|
||||||
public get rssi () {
|
|
||||||
return this._rssi;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Connect to the Hub.
|
|
||||||
* @method Hub#connect
|
|
||||||
* @returns {Promise} Resolved upon successful connect.
|
|
||||||
*/
|
|
||||||
public connect () {
|
public connect () {
|
||||||
return new Promise(async (connectResolve, connectReject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
if (this._bleDevice.connecting) {
|
debug("Connecting to Powered UP Hub");
|
||||||
return connectReject("Already connecting");
|
await super.connect();
|
||||||
} else if (this._bleDevice.connected) {
|
debug("Connect completed");
|
||||||
return connectReject("Already connected");
|
return resolve();
|
||||||
}
|
|
||||||
await this._bleDevice.connect();
|
|
||||||
return connectResolve();
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Disconnect the Hub.
|
|
||||||
* @method Hub#disconnect
|
|
||||||
* @returns {Promise} Resolved upon successful disconnect.
|
|
||||||
*/
|
|
||||||
public disconnect () {
|
|
||||||
return this._bleDevice.disconnect();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public getDeviceAtPort (portName: string) {
|
|
||||||
const portId = this._portMap[portName];
|
|
||||||
if (portId) {
|
|
||||||
return this._attachedDevices[portId];
|
|
||||||
} else {
|
|
||||||
throw new Error(`Port ${portName} does not exist on this hub type`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public waitForDeviceAtPort (portName: string) {
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
const existingDevice = this.getDeviceAtPort(portName);
|
|
||||||
if (existingDevice) {
|
|
||||||
return resolve(existingDevice);
|
|
||||||
}
|
|
||||||
this._attachCallbacks.push((device) => {
|
|
||||||
if (device.portName === portName) {
|
|
||||||
return resolve(device);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public getDevices () {
|
protected _checkFirmware (version: string) {
|
||||||
return Object.values(this._attachedDevices);
|
if (compareVersion("1.1.00.0004", version) === 1) {
|
||||||
|
throw new Error(`Your Powered Up Hub's (${this.name}) firmware is out of date and unsupported by this library. Please update it via the official Powered Up app.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public getDevicesByType (deviceType: number) {
|
|
||||||
return this.getDevices().filter((device) => device.type === deviceType);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public waitForDeviceByType (deviceType: number) {
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
const existingDevices = this.getDevicesByType(deviceType);
|
|
||||||
if (existingDevices.length >= 1) {
|
|
||||||
return resolve(existingDevices[0]);
|
|
||||||
}
|
|
||||||
this._attachCallbacks.push((device) => {
|
|
||||||
if (device.type === deviceType) {
|
|
||||||
return resolve(device);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public getPortNameForPortId (portId: number) {
|
|
||||||
for (const port of Object.keys(this._portMap)) {
|
|
||||||
if (this._portMap[port] === portId) {
|
|
||||||
return port;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sleep a given amount of time.
|
|
||||||
*
|
|
||||||
* This is a helper method to make it easier to add delays into a chain of commands.
|
|
||||||
* @method Hub#sleep
|
|
||||||
* @param {number} delay How long to sleep (in milliseconds).
|
|
||||||
* @returns {Promise} Resolved after the delay is finished.
|
|
||||||
*/
|
|
||||||
public sleep (delay: number) {
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
global.setTimeout(resolve, delay);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Wait until a given list of concurrently running commands are complete.
|
|
||||||
*
|
|
||||||
* This is a helper method to make it easier to wait for concurrent commands to complete.
|
|
||||||
* @method Hub#wait
|
|
||||||
* @param {Array<Promise<any>>} commands Array of executing commands.
|
|
||||||
* @returns {Promise} Resolved after the commands are finished.
|
|
||||||
*/
|
|
||||||
public wait (commands: Array<Promise<any>>) {
|
|
||||||
return Promise.all(commands);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public send (message: Buffer, uuid: string, callback?: () => void) {
|
|
||||||
if (callback) {
|
|
||||||
callback();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public subscribe (portId: number, deviceType: number, mode: number) {
|
|
||||||
// NK Do nothing here
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected _attachDevice (device: Device) {
|
|
||||||
this._attachedDevices[device.portId] = device;
|
|
||||||
/**
|
|
||||||
* Emits when a device is attached to the Hub.
|
|
||||||
* @event Hub#attach
|
|
||||||
* @param {Device} device
|
|
||||||
*/
|
|
||||||
this.emit("attach", device);
|
|
||||||
this._attachCallbacks.forEach((callback) => {
|
|
||||||
callback(device);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected _detachDevice (device: Device) {
|
|
||||||
delete this._attachedDevices[device.portId];
|
|
||||||
/**
|
|
||||||
* Emits when a device is detached from the Hub.
|
|
||||||
* @event Hub#attach
|
|
||||||
* @param {Device} device
|
|
||||||
*/
|
|
||||||
this.emit("detach", device);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected _createDevice (deviceType: number, portId: number) {
|
|
||||||
let device;
|
|
||||||
|
|
||||||
switch (deviceType) {
|
|
||||||
case Consts.DeviceType.LIGHT:
|
|
||||||
device = new Light(this, portId);
|
|
||||||
break;
|
|
||||||
case Consts.DeviceType.TRAIN_MOTOR:
|
|
||||||
device = new TrainMotor(this, portId);
|
|
||||||
break;
|
|
||||||
case Consts.DeviceType.SIMPLE_MEDIUM_LINEAR_MOTOR:
|
|
||||||
device = new SimpleMediumLinearMotor(this, portId);
|
|
||||||
break;
|
|
||||||
case Consts.DeviceType.MOVE_HUB_MEDIUM_LINEAR_MOTOR:
|
|
||||||
device = new MoveHubMediumLinearMotor(this, portId);
|
|
||||||
break;
|
|
||||||
case Consts.DeviceType.MOTION_SENSOR:
|
|
||||||
device = new MotionSensor(this, portId);
|
|
||||||
break;
|
|
||||||
case Consts.DeviceType.TILT_SENSOR:
|
|
||||||
device = new TiltSensor(this, portId);
|
|
||||||
break;
|
|
||||||
case Consts.DeviceType.MOVE_HUB_TILT_SENSOR:
|
|
||||||
device = new MoveHubTiltSensor(this, portId);
|
|
||||||
break;
|
|
||||||
case Consts.DeviceType.TECHNIC_MEDIUM_HUB_TILT_SENSOR:
|
|
||||||
device = new TechnicMediumHubTiltSensor(this, portId);
|
|
||||||
break;
|
|
||||||
case Consts.DeviceType.TECHNIC_MEDIUM_HUB_GYRO_SENSOR:
|
|
||||||
device = new TechnicMediumHubGyroSensor(this, portId);
|
|
||||||
break;
|
|
||||||
case Consts.DeviceType.TECHNIC_MEDIUM_HUB_ACCELEROMETER:
|
|
||||||
device = new TechnicMediumHubAccelerometerSensor(this, portId);
|
|
||||||
break;
|
|
||||||
case Consts.DeviceType.MEDIUM_LINEAR_MOTOR:
|
|
||||||
device = new MediumLinearMotor(this, portId);
|
|
||||||
break;
|
|
||||||
case Consts.DeviceType.TECHNIC_LARGE_LINEAR_MOTOR:
|
|
||||||
device = new TechnicLargeLinearMotor(this, portId);
|
|
||||||
break;
|
|
||||||
case Consts.DeviceType.TECHNIC_XLARGE_LINEAR_MOTOR:
|
|
||||||
device = new TechnicXLargeLinearMotor(this, portId);
|
|
||||||
break;
|
|
||||||
case Consts.DeviceType.COLOR_DISTANCE_SENSOR:
|
|
||||||
device = new ColorDistanceSensor(this, portId);
|
|
||||||
break;
|
|
||||||
case Consts.DeviceType.VOLTAGE_SENSOR:
|
|
||||||
device = new VoltageSensor(this, portId);
|
|
||||||
break;
|
|
||||||
case Consts.DeviceType.CURRENT_SENSOR:
|
|
||||||
device = new CurrentSensor(this, portId);
|
|
||||||
break;
|
|
||||||
case Consts.DeviceType.PUP_REMOTE_BUTTON:
|
|
||||||
device = new PUPRemoteButton(this, portId);
|
|
||||||
break;
|
|
||||||
case Consts.DeviceType.HUB_LED:
|
|
||||||
device = new HubLED(this, portId);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
device = new Device(this, portId, undefined, deviceType);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return device;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected _getDeviceByPortId (portId: number) {
|
|
||||||
return this._attachedDevices[portId];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export namespace Hub {
|
||||||
|
|
||||||
|
export const PortMap: {[portName: string]: number} = {
|
||||||
|
"A": 0,
|
||||||
|
"B": 1
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
import { Hub } from "./hub";
|
import { BaseHub } from "./basehub";
|
||||||
|
|
||||||
import * as Consts from "../consts";
|
import * as Consts from "../consts";
|
||||||
|
|
||||||
@ -11,15 +11,9 @@ const modeInfoDebug = Debug("lpf2hubmodeinfo");
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @class LPF2Hub
|
* @class LPF2Hub
|
||||||
* @extends Hub
|
* @extends BaseHub
|
||||||
*/
|
*/
|
||||||
export class LPF2Hub extends Hub {
|
export class LPF2Hub extends BaseHub {
|
||||||
|
|
||||||
protected _ledPort: number = 0x32;
|
|
||||||
|
|
||||||
private _lastTiltX: number = 0;
|
|
||||||
private _lastTiltY: number = 0;
|
|
||||||
private _lastTiltZ: number = 0;
|
|
||||||
|
|
||||||
private _messageBuffer: Buffer = Buffer.alloc(0);
|
private _messageBuffer: Buffer = Buffer.alloc(0);
|
||||||
|
|
||||||
@ -381,46 +375,6 @@ export class LPF2Hub extends Hub {
|
|||||||
|
|
||||||
// if (port && port.connected) {
|
// if (port && port.connected) {
|
||||||
// switch (port.type) {
|
// switch (port.type) {
|
||||||
// case Consts.DeviceType.CONTROL_PLUS_TILT: {
|
|
||||||
// const tiltZ = data.readInt16LE(4);
|
|
||||||
// const tiltY = data.readInt16LE(6);
|
|
||||||
// const tiltX = data.readInt16LE(8);
|
|
||||||
// this._lastTiltX = tiltX;
|
|
||||||
// this._lastTiltY = tiltY;
|
|
||||||
// this._lastTiltZ = tiltZ;
|
|
||||||
// this.emit("tilt", "TILT", this._lastTiltX, this._lastTiltY, this._lastTiltZ);
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// case Consts.DeviceType.CONTROL_PLUS_GYRO: {
|
|
||||||
// const gyroX = Math.round(data.readInt16LE(4) * 7 / 400);
|
|
||||||
// const gyroY = Math.round(data.readInt16LE(6) * 7 / 400);
|
|
||||||
// const gyroZ = Math.round(data.readInt16LE(8) * 7 / 400);
|
|
||||||
// /**
|
|
||||||
// * Emits when gyroscope detects movement. Measured in DPS - degrees per second.
|
|
||||||
// * @event LPF2Hub#gyro
|
|
||||||
// * @param {string} port
|
|
||||||
// * @param {number} x
|
|
||||||
// * @param {number} y
|
|
||||||
// * @param {number} z
|
|
||||||
// */
|
|
||||||
// this.emit("gyro", "GYRO", gyroX, gyroY, gyroZ);
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// case Consts.DeviceType.CONTROL_PLUS_ACCELEROMETER: {
|
|
||||||
// const accelX = Math.round(data.readInt16LE(4) / 4.096);
|
|
||||||
// const accelY = Math.round(data.readInt16LE(6) / 4.096);
|
|
||||||
// const accelZ = Math.round(data.readInt16LE(8) / 4.096);
|
|
||||||
// /**
|
|
||||||
// * Emits when accelerometer detects movement. Measured in mG.
|
|
||||||
// * @event LPF2Hub#accel
|
|
||||||
// * @param {string} port
|
|
||||||
// * @param {number} x
|
|
||||||
// * @param {number} y
|
|
||||||
// * @param {number} z
|
|
||||||
// */
|
|
||||||
// this.emit("accel", "ACCEL", accelX, accelY, accelZ);
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// case Consts.DeviceType.DUPLO_TRAIN_BASE_COLOR: {
|
// case Consts.DeviceType.DUPLO_TRAIN_BASE_COLOR: {
|
||||||
// if (data[4] <= 10) {
|
// if (data[4] <= 10) {
|
||||||
// this.emit("color", port.id, data[4]);
|
// this.emit("color", port.id, data[4]);
|
||||||
|
@ -8,38 +8,38 @@ import { LPF2Hub } from "./lpf2hub";
|
|||||||
import * as Consts from "../consts";
|
import * as Consts from "../consts";
|
||||||
|
|
||||||
import Debug = require("debug");
|
import Debug = require("debug");
|
||||||
const debug = Debug("boostmovehub");
|
const debug = Debug("movehub");
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The BoostMoveHub is emitted if the discovered device is a Boost Move Hub.
|
* The MoveHub is emitted if the discovered device is a Move Hub.
|
||||||
* @class BoostMoveHub
|
* @class MoveHub
|
||||||
* @extends LPF2Hub
|
* @extends LPF2Hub
|
||||||
* @extends Hub
|
* @extends BaseHub
|
||||||
*/
|
*/
|
||||||
export class BoostMoveHub extends LPF2Hub {
|
export class MoveHub extends LPF2Hub {
|
||||||
|
|
||||||
|
|
||||||
public static IsBoostMoveHub (peripheral: Peripheral) {
|
public static IsMoveHub (peripheral: Peripheral) {
|
||||||
return (
|
return (
|
||||||
peripheral.advertisement &&
|
peripheral.advertisement &&
|
||||||
peripheral.advertisement.serviceUuids &&
|
peripheral.advertisement.serviceUuids &&
|
||||||
peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 &&
|
peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 &&
|
||||||
peripheral.advertisement.manufacturerData &&
|
peripheral.advertisement.manufacturerData &&
|
||||||
peripheral.advertisement.manufacturerData.length > 3 &&
|
peripheral.advertisement.manufacturerData.length > 3 &&
|
||||||
peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.BOOST_MOVE_HUB_ID
|
peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.MOVE_HUB_ID
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor (device: IBLEAbstraction) {
|
constructor (device: IBLEAbstraction) {
|
||||||
super(device, BoostMoveHub.PortMap, Consts.HubType.BOOST_MOVE_HUB);
|
super(device, MoveHub.PortMap, Consts.HubType.MOVE_HUB);
|
||||||
debug("Discovered Boost Move Hub");
|
debug("Discovered Move Hub");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public connect () {
|
public connect () {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
debug("Connecting to Boost Move Hub");
|
debug("Connecting to Move Hub");
|
||||||
await super.connect();
|
await super.connect();
|
||||||
debug("Connect completed");
|
debug("Connect completed");
|
||||||
return resolve();
|
return resolve();
|
||||||
@ -49,21 +49,20 @@ export class BoostMoveHub extends LPF2Hub {
|
|||||||
|
|
||||||
protected _checkFirmware (version: string) {
|
protected _checkFirmware (version: string) {
|
||||||
if (compareVersion("2.0.00.0017", version) === 1) {
|
if (compareVersion("2.0.00.0017", version) === 1) {
|
||||||
throw new Error(`Your Boost Move Hub's (${this.name}) firmware is out of date and unsupported by this library. Please update it via the official Powered Up app.`);
|
throw new Error(`Your Move Hub's (${this.name}) firmware is out of date and unsupported by this library. Please update it via the official Powered Up app.`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export namespace BoostMoveHub {
|
export namespace MoveHub {
|
||||||
|
|
||||||
export const PortMap: {[portName: string]: number} = {
|
export const PortMap: {[portName: string]: number} = {
|
||||||
"A": 0,
|
"A": 0,
|
||||||
"B": 1,
|
"B": 1,
|
||||||
"C": 2,
|
"C": 2,
|
||||||
"D": 3,
|
"D": 3
|
||||||
"TILT": 58
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,68 +0,0 @@
|
|||||||
import { Peripheral } from "@abandonware/noble";
|
|
||||||
import compareVersion from "compare-versions";
|
|
||||||
|
|
||||||
import { IBLEAbstraction } from "../interfaces";
|
|
||||||
|
|
||||||
import { LPF2Hub } from "./lpf2hub";
|
|
||||||
|
|
||||||
import * as Consts from "../consts";
|
|
||||||
|
|
||||||
import Debug = require("debug");
|
|
||||||
const debug = Debug("puphub");
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The PUPHub is emitted if the discovered device is a Powered UP Hub.
|
|
||||||
* @class PUPHub
|
|
||||||
* @extends LPF2Hub
|
|
||||||
* @extends Hub
|
|
||||||
*/
|
|
||||||
export class PUPHub extends LPF2Hub {
|
|
||||||
|
|
||||||
|
|
||||||
public static IsPUPHub (peripheral: Peripheral) {
|
|
||||||
return (
|
|
||||||
peripheral.advertisement &&
|
|
||||||
peripheral.advertisement.serviceUuids &&
|
|
||||||
peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 &&
|
|
||||||
peripheral.advertisement.manufacturerData &&
|
|
||||||
peripheral.advertisement.manufacturerData.length > 3 &&
|
|
||||||
peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_HUB_ID
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected _currentPort = 0x3b;
|
|
||||||
|
|
||||||
constructor (device: IBLEAbstraction) {
|
|
||||||
super(device, PUPHub.PortMap, Consts.HubType.POWERED_UP_HUB);
|
|
||||||
debug("Discovered Powered UP Hub");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public connect () {
|
|
||||||
return new Promise(async (resolve, reject) => {
|
|
||||||
debug("Connecting to Powered UP Hub");
|
|
||||||
await super.connect();
|
|
||||||
debug("Connect completed");
|
|
||||||
return resolve();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected _checkFirmware (version: string) {
|
|
||||||
if (compareVersion("1.1.00.0004", version) === 1) {
|
|
||||||
throw new Error(`Your Powered Up Hub's (${this.name}) firmware is out of date and unsupported by this library. Please update it via the official Powered Up app.`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
export namespace PUPHub {
|
|
||||||
|
|
||||||
export const PortMap: {[portName: string]: number} = {
|
|
||||||
"A": 0,
|
|
||||||
"B": 1
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -7,35 +7,32 @@ import { LPF2Hub } from "./lpf2hub";
|
|||||||
import * as Consts from "../consts";
|
import * as Consts from "../consts";
|
||||||
|
|
||||||
import Debug = require("debug");
|
import Debug = require("debug");
|
||||||
const debug = Debug("pupremote");
|
const debug = Debug("remotecontrol");
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The PUPRemote is emitted if the discovered device is a Powered UP Remote.
|
* The RemoteControl is emitted if the discovered device is a Remote Control.
|
||||||
* @class PUPRemote
|
* @class RemoteControl
|
||||||
* @extends LPF2Hub
|
* @extends LPF2Hub
|
||||||
* @extends Hub
|
* @extends BaseHub
|
||||||
*/
|
*/
|
||||||
export class PUPRemote extends LPF2Hub {
|
export class RemoteControl extends LPF2Hub {
|
||||||
|
|
||||||
|
|
||||||
public static IsPUPRemote (peripheral: Peripheral) {
|
public static IsRemoteControl (peripheral: Peripheral) {
|
||||||
return (
|
return (
|
||||||
peripheral.advertisement &&
|
peripheral.advertisement &&
|
||||||
peripheral.advertisement.serviceUuids &&
|
peripheral.advertisement.serviceUuids &&
|
||||||
peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 &&
|
peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 &&
|
||||||
peripheral.advertisement.manufacturerData &&
|
peripheral.advertisement.manufacturerData &&
|
||||||
peripheral.advertisement.manufacturerData.length > 3 &&
|
peripheral.advertisement.manufacturerData.length > 3 &&
|
||||||
peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_REMOTE_ID
|
peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.REMOTE_CONTROL_ID
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected _ledPort = 0x34;
|
|
||||||
|
|
||||||
|
|
||||||
constructor (device: IBLEAbstraction) {
|
constructor (device: IBLEAbstraction) {
|
||||||
super(device, PUPRemote.PortMap, Consts.HubType.POWERED_UP_REMOTE);
|
super(device, RemoteControl.PortMap, Consts.HubType.REMOTE_CONTROL);
|
||||||
debug("Discovered Powered UP Remote");
|
debug("Discovered Powered UP Remote");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,7 +49,7 @@ export class PUPRemote extends LPF2Hub {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export namespace PUPRemote {
|
export namespace RemoteControl {
|
||||||
|
|
||||||
export const PortMap: {[portName: string]: number} = {
|
export const PortMap: {[portName: string]: number} = {
|
||||||
"LEFT": 0,
|
"LEFT": 0,
|
@ -7,31 +7,31 @@ import { LPF2Hub } from "./lpf2hub";
|
|||||||
import * as Consts from "../consts";
|
import * as Consts from "../consts";
|
||||||
|
|
||||||
import Debug = require("debug");
|
import Debug = require("debug");
|
||||||
const debug = Debug("ControlPlusHub");
|
const debug = Debug("technicmediumhub");
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The ControlPlusHub is emitted if the discovered device is a Control+ Hub.
|
* The TechnicMediumHub is emitted if the discovered device is a Technic Medium Hub.
|
||||||
* @class ControlPlusHub
|
* @class TechnicMediumHub
|
||||||
* @extends LPF2Hub
|
* @extends LPF2Hub
|
||||||
* @extends Hub
|
* @extends BaseHub
|
||||||
*/
|
*/
|
||||||
export class ControlPlusHub extends LPF2Hub {
|
export class TechnicMediumHub extends LPF2Hub {
|
||||||
|
|
||||||
|
|
||||||
public static IsControlPlusHub (peripheral: Peripheral) {
|
public static IsTechnicMediumHub (peripheral: Peripheral) {
|
||||||
return (
|
return (
|
||||||
peripheral.advertisement &&
|
peripheral.advertisement &&
|
||||||
peripheral.advertisement.serviceUuids &&
|
peripheral.advertisement.serviceUuids &&
|
||||||
peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 &&
|
peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 &&
|
||||||
peripheral.advertisement.manufacturerData &&
|
peripheral.advertisement.manufacturerData &&
|
||||||
peripheral.advertisement.manufacturerData.length > 3 &&
|
peripheral.advertisement.manufacturerData.length > 3 &&
|
||||||
peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.CONTROL_PLUS_LARGE_HUB
|
peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.TECHNIC_MEDIUM_HUB
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor (device: IBLEAbstraction) {
|
constructor (device: IBLEAbstraction) {
|
||||||
super(device, ControlPlusHub.PortMap, Consts.HubType.CONTROL_PLUS_HUB);
|
super(device, TechnicMediumHub.PortMap, Consts.HubType.TECHNIC_MEDIUM_HUB);
|
||||||
debug("Discovered Control+ Hub");
|
debug("Discovered Control+ Hub");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,7 +108,7 @@ export class ControlPlusHub extends LPF2Hub {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export namespace ControlPlusHub {
|
export namespace TechnicMediumHub {
|
||||||
|
|
||||||
export const PortMap: {[portName: string]: number} = {
|
export const PortMap: {[portName: string]: number} = {
|
||||||
"A": 0,
|
"A": 0,
|
@ -2,7 +2,7 @@ import { Peripheral } from "@abandonware/noble";
|
|||||||
|
|
||||||
import { IBLEAbstraction } from "../interfaces";
|
import { IBLEAbstraction } from "../interfaces";
|
||||||
|
|
||||||
import { Hub } from "./hub";
|
import { BaseHub } from "./basehub";
|
||||||
|
|
||||||
import * as Consts from "../consts";
|
import * as Consts from "../consts";
|
||||||
|
|
||||||
@ -15,9 +15,9 @@ const debug = Debug("wedo2smarthub");
|
|||||||
/**
|
/**
|
||||||
* The WeDo2SmartHub is emitted if the discovered device is a WeDo 2.0 Smart Hub.
|
* The WeDo2SmartHub is emitted if the discovered device is a WeDo 2.0 Smart Hub.
|
||||||
* @class WeDo2SmartHub
|
* @class WeDo2SmartHub
|
||||||
* @extends Hub
|
* @extends BaseHub
|
||||||
*/
|
*/
|
||||||
export class WeDo2SmartHub extends Hub {
|
export class WeDo2SmartHub extends BaseHub {
|
||||||
|
|
||||||
|
|
||||||
public static IsWeDo2SmartHub (peripheral: Peripheral) {
|
public static IsWeDo2SmartHub (peripheral: Peripheral) {
|
||||||
|
@ -2,12 +2,12 @@ import * as Consts from "./consts";
|
|||||||
|
|
||||||
import { PoweredUP } from "./poweredup-browser";
|
import { PoweredUP } from "./poweredup-browser";
|
||||||
|
|
||||||
import { BoostMoveHub } from "./hubs/boostmovehub";
|
import { BaseHub } from "./hubs/basehub";
|
||||||
import { ControlPlusHub } from "./hubs/controlplushub";
|
|
||||||
import { DuploTrainBase } from "./hubs/duplotrainbase";
|
import { DuploTrainBase } from "./hubs/duplotrainbase";
|
||||||
|
import { MoveHub } from "./hubs/movehub";
|
||||||
import { Hub } from "./hubs/hub";
|
import { Hub } from "./hubs/hub";
|
||||||
import { PUPHub } from "./hubs/puphub";
|
import { RemoteControl } from "./hubs/remotecontrol";
|
||||||
import { PUPRemote } from "./hubs/pupremote";
|
import { TechnicMediumHub } from "./hubs/technicmediumhub";
|
||||||
import { WeDo2SmartHub } from "./hubs/wedo2smarthub";
|
import { WeDo2SmartHub } from "./hubs/wedo2smarthub";
|
||||||
|
|
||||||
import { ColorDistanceSensor } from "./devices/colordistancesensor";
|
import { ColorDistanceSensor } from "./devices/colordistancesensor";
|
||||||
@ -19,7 +19,7 @@ import { MediumLinearMotor } from "./devices/mediumlinearmotor";
|
|||||||
import { MotionSensor } from "./devices/motionsensor";
|
import { MotionSensor } from "./devices/motionsensor";
|
||||||
import { MoveHubMediumLinearMotor } from "./devices/movehubmediumlinearmotor";
|
import { MoveHubMediumLinearMotor } from "./devices/movehubmediumlinearmotor";
|
||||||
import { MoveHubTiltSensor } from "./devices/movehubtiltsensor";
|
import { MoveHubTiltSensor } from "./devices/movehubtiltsensor";
|
||||||
import { PUPRemoteButton } from "./devices/pupremotebutton";
|
import { RemoteControlButton } from "./devices/remotecontrolbutton";
|
||||||
import { SimpleMediumLinearMotor } from "./devices/simplemediumlinearmotor";
|
import { SimpleMediumLinearMotor } from "./devices/simplemediumlinearmotor";
|
||||||
import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor";
|
import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor";
|
||||||
import { TechnicMediumHubAccelerometerSensor } from "./devices/technicmediumhubaccelerometersensor";
|
import { TechnicMediumHubAccelerometerSensor } from "./devices/technicmediumhubaccelerometersensor";
|
||||||
@ -35,12 +35,11 @@ import { isWebBluetooth } from "./utils";
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
window.PoweredUP = {
|
window.PoweredUP = {
|
||||||
PoweredUP,
|
PoweredUP,
|
||||||
Hub,
|
BaseHub,
|
||||||
WeDo2SmartHub,
|
WeDo2SmartHub,
|
||||||
BoostMoveHub,
|
TechnicMediumHub,
|
||||||
ControlPlusHub,
|
Hub,
|
||||||
PUPHub,
|
RemoteControl,
|
||||||
PUPRemote,
|
|
||||||
DuploTrainBase,
|
DuploTrainBase,
|
||||||
Consts,
|
Consts,
|
||||||
ColorDistanceSensor,
|
ColorDistanceSensor,
|
||||||
@ -49,9 +48,10 @@ window.PoweredUP = {
|
|||||||
Light,
|
Light,
|
||||||
MediumLinearMotor,
|
MediumLinearMotor,
|
||||||
MotionSensor,
|
MotionSensor,
|
||||||
|
MoveHub,
|
||||||
MoveHubMediumLinearMotor,
|
MoveHubMediumLinearMotor,
|
||||||
MoveHubTiltSensor,
|
MoveHubTiltSensor,
|
||||||
PUPRemoteButton,
|
RemoteControlButton,
|
||||||
SimpleMediumLinearMotor,
|
SimpleMediumLinearMotor,
|
||||||
TechnicMediumHubAccelerometerSensor,
|
TechnicMediumHubAccelerometerSensor,
|
||||||
TechnicMediumHubGyroSensor,
|
TechnicMediumHubGyroSensor,
|
||||||
|
@ -2,12 +2,12 @@ import * as Consts from "./consts";
|
|||||||
|
|
||||||
import { PoweredUP } from "./poweredup-node";
|
import { PoweredUP } from "./poweredup-node";
|
||||||
|
|
||||||
import { BoostMoveHub } from "./hubs/boostmovehub";
|
import { BaseHub } from "./hubs/basehub";
|
||||||
import { ControlPlusHub } from "./hubs/controlplushub";
|
|
||||||
import { DuploTrainBase } from "./hubs/duplotrainbase";
|
import { DuploTrainBase } from "./hubs/duplotrainbase";
|
||||||
|
import { MoveHub } from "./hubs/movehub";
|
||||||
import { Hub } from "./hubs/hub";
|
import { Hub } from "./hubs/hub";
|
||||||
import { PUPHub } from "./hubs/puphub";
|
import { RemoteControl } from "./hubs/remotecontrol";
|
||||||
import { PUPRemote } from "./hubs/pupremote";
|
import { TechnicMediumHub } from "./hubs/technicmediumhub";
|
||||||
import { WeDo2SmartHub } from "./hubs/wedo2smarthub";
|
import { WeDo2SmartHub } from "./hubs/wedo2smarthub";
|
||||||
|
|
||||||
import { ColorDistanceSensor } from "./devices/colordistancesensor";
|
import { ColorDistanceSensor } from "./devices/colordistancesensor";
|
||||||
@ -19,7 +19,7 @@ import { MediumLinearMotor } from "./devices/mediumlinearmotor";
|
|||||||
import { MotionSensor } from "./devices/motionsensor";
|
import { MotionSensor } from "./devices/motionsensor";
|
||||||
import { MoveHubMediumLinearMotor } from "./devices/movehubmediumlinearmotor";
|
import { MoveHubMediumLinearMotor } from "./devices/movehubmediumlinearmotor";
|
||||||
import { MoveHubTiltSensor } from "./devices/movehubtiltsensor";
|
import { MoveHubTiltSensor } from "./devices/movehubtiltsensor";
|
||||||
import { PUPRemoteButton } from "./devices/pupremotebutton";
|
import { RemoteControlButton } from "./devices/remotecontrolbutton";
|
||||||
import { SimpleMediumLinearMotor } from "./devices/simplemediumlinearmotor";
|
import { SimpleMediumLinearMotor } from "./devices/simplemediumlinearmotor";
|
||||||
import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor";
|
import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor";
|
||||||
import { TechnicMediumHubAccelerometerSensor } from "./devices/technicmediumhubaccelerometersensor";
|
import { TechnicMediumHubAccelerometerSensor } from "./devices/technicmediumhubaccelerometersensor";
|
||||||
@ -35,12 +35,11 @@ import { isWebBluetooth } from "./utils";
|
|||||||
export default PoweredUP;
|
export default PoweredUP;
|
||||||
export {
|
export {
|
||||||
PoweredUP,
|
PoweredUP,
|
||||||
Hub,
|
BaseHub,
|
||||||
WeDo2SmartHub,
|
WeDo2SmartHub,
|
||||||
BoostMoveHub,
|
TechnicMediumHub,
|
||||||
ControlPlusHub,
|
Hub,
|
||||||
PUPHub,
|
RemoteControl,
|
||||||
PUPRemote,
|
|
||||||
DuploTrainBase,
|
DuploTrainBase,
|
||||||
Consts,
|
Consts,
|
||||||
ColorDistanceSensor,
|
ColorDistanceSensor,
|
||||||
@ -49,9 +48,10 @@ export {
|
|||||||
Light,
|
Light,
|
||||||
MediumLinearMotor,
|
MediumLinearMotor,
|
||||||
MotionSensor,
|
MotionSensor,
|
||||||
|
MoveHub,
|
||||||
MoveHubMediumLinearMotor,
|
MoveHubMediumLinearMotor,
|
||||||
MoveHubTiltSensor,
|
MoveHubTiltSensor,
|
||||||
PUPRemoteButton,
|
RemoteControlButton,
|
||||||
SimpleMediumLinearMotor,
|
SimpleMediumLinearMotor,
|
||||||
TechnicMediumHubAccelerometerSensor,
|
TechnicMediumHubAccelerometerSensor,
|
||||||
TechnicMediumHubGyroSensor,
|
TechnicMediumHubGyroSensor,
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
import { WebBLEDevice } from "./webbleabstraction";
|
import { WebBLEDevice } from "./webbleabstraction";
|
||||||
|
|
||||||
import { BoostMoveHub } from "./hubs/boostmovehub";
|
import { BaseHub } from "./hubs/basehub";
|
||||||
import { ControlPlusHub } from "./hubs/controlplushub";
|
|
||||||
import { DuploTrainBase } from "./hubs/duplotrainbase";
|
import { DuploTrainBase } from "./hubs/duplotrainbase";
|
||||||
|
import { MoveHub } from "./hubs/movehub";
|
||||||
import { Hub } from "./hubs/hub";
|
import { Hub } from "./hubs/hub";
|
||||||
import { PUPHub } from "./hubs/puphub";
|
import { RemoteControl } from "./hubs/remotecontrol";
|
||||||
import { PUPRemote } from "./hubs/pupremote";
|
import { TechnicMediumHub } from "./hubs/technicmediumhub";
|
||||||
import { WeDo2SmartHub } from "./hubs/wedo2smarthub";
|
import { WeDo2SmartHub } from "./hubs/wedo2smarthub";
|
||||||
|
|
||||||
import * as Consts from "./consts";
|
import * as Consts from "./consts";
|
||||||
@ -24,7 +24,7 @@ const debug = Debug("poweredup");
|
|||||||
export class PoweredUP extends EventEmitter {
|
export class PoweredUP extends EventEmitter {
|
||||||
|
|
||||||
|
|
||||||
private _connectedHubs: {[uuid: string]: Hub} = {};
|
private _connectedHubs: {[uuid: string]: BaseHub} = {};
|
||||||
|
|
||||||
|
|
||||||
constructor () {
|
constructor () {
|
||||||
@ -76,7 +76,7 @@ export class PoweredUP extends EventEmitter {
|
|||||||
/**
|
/**
|
||||||
* Retrieve a list of Powered UP Hubs.
|
* Retrieve a list of Powered UP Hubs.
|
||||||
* @method PoweredUP#getHubs
|
* @method PoweredUP#getHubs
|
||||||
* @returns {Hub[]}
|
* @returns {BaseHub[]}
|
||||||
*/
|
*/
|
||||||
public getHubs () {
|
public getHubs () {
|
||||||
return Object.values(this._connectedHubs);
|
return Object.values(this._connectedHubs);
|
||||||
@ -87,7 +87,7 @@ export class PoweredUP extends EventEmitter {
|
|||||||
* Retrieve a Powered UP Hub by UUID.
|
* Retrieve a Powered UP Hub by UUID.
|
||||||
* @method PoweredUP#getHubByUUID
|
* @method PoweredUP#getHubByUUID
|
||||||
* @param {string} uuid
|
* @param {string} uuid
|
||||||
* @returns {Hub | null}
|
* @returns {BaseHub | null}
|
||||||
*/
|
*/
|
||||||
public getHubByUUID (uuid: string) {
|
public getHubByUUID (uuid: string) {
|
||||||
return this._connectedHubs[uuid];
|
return this._connectedHubs[uuid];
|
||||||
@ -98,7 +98,7 @@ export class PoweredUP extends EventEmitter {
|
|||||||
* Retrieve a Powered UP Hub by primary MAC address.
|
* Retrieve a Powered UP Hub by primary MAC address.
|
||||||
* @method PoweredUP#getHubByPrimaryMACAddress
|
* @method PoweredUP#getHubByPrimaryMACAddress
|
||||||
* @param {string} address
|
* @param {string} address
|
||||||
* @returns {Hub}
|
* @returns {BaseHub}
|
||||||
*/
|
*/
|
||||||
public getHubByPrimaryMACAddress (address: string) {
|
public getHubByPrimaryMACAddress (address: string) {
|
||||||
return Object.values(this._connectedHubs).filter((hub) => hub.primaryMACAddress === address)[0];
|
return Object.values(this._connectedHubs).filter((hub) => hub.primaryMACAddress === address)[0];
|
||||||
@ -109,7 +109,7 @@ export class PoweredUP extends EventEmitter {
|
|||||||
* Retrieve a list of Powered UP Hub by name.
|
* Retrieve a list of Powered UP Hub by name.
|
||||||
* @method PoweredUP#getHubsByName
|
* @method PoweredUP#getHubsByName
|
||||||
* @param {string} name
|
* @param {string} name
|
||||||
* @returns {Hub[]}
|
* @returns {BaseHub[]}
|
||||||
*/
|
*/
|
||||||
public getHubsByName (name: string) {
|
public getHubsByName (name: string) {
|
||||||
return Object.values(this._connectedHubs).filter((hub) => hub.name === name);
|
return Object.values(this._connectedHubs).filter((hub) => hub.name === name);
|
||||||
@ -120,7 +120,7 @@ export class PoweredUP extends EventEmitter {
|
|||||||
* Retrieve a list of Powered UP Hub by type.
|
* Retrieve a list of Powered UP Hub by type.
|
||||||
* @method PoweredUP#getHubsByType
|
* @method PoweredUP#getHubsByType
|
||||||
* @param {string} name
|
* @param {string} name
|
||||||
* @returns {Hub[]}
|
* @returns {BaseHub[]}
|
||||||
*/
|
*/
|
||||||
public getHubsByType (hubType: number) {
|
public getHubsByType (hubType: number) {
|
||||||
return Object.values(this._connectedHubs).filter((hub) => hub.type === hubType);
|
return Object.values(this._connectedHubs).filter((hub) => hub.type === hubType);
|
||||||
@ -139,20 +139,20 @@ export class PoweredUP extends EventEmitter {
|
|||||||
if (message[2] === 0x01 && message[3] === 0x0b) {
|
if (message[2] === 0x01 && message[3] === 0x0b) {
|
||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
switch (message[5]) {
|
switch (message[5]) {
|
||||||
case Consts.BLEManufacturerData.POWERED_UP_REMOTE_ID:
|
case Consts.BLEManufacturerData.REMOTE_CONTROL_ID:
|
||||||
resolve(Consts.HubType.POWERED_UP_REMOTE);
|
resolve(Consts.HubType.REMOTE_CONTROL);
|
||||||
break;
|
break;
|
||||||
case Consts.BLEManufacturerData.BOOST_MOVE_HUB_ID:
|
case Consts.BLEManufacturerData.MOVE_HUB_ID:
|
||||||
resolve(Consts.HubType.BOOST_MOVE_HUB);
|
resolve(Consts.HubType.MOVE_HUB);
|
||||||
break;
|
break;
|
||||||
case Consts.BLEManufacturerData.POWERED_UP_HUB_ID:
|
case Consts.BLEManufacturerData.HUB_ID:
|
||||||
resolve(Consts.HubType.POWERED_UP_HUB);
|
resolve(Consts.HubType.HUB);
|
||||||
break;
|
break;
|
||||||
case Consts.BLEManufacturerData.DUPLO_TRAIN_HUB_ID:
|
case Consts.BLEManufacturerData.DUPLO_TRAIN_BASE_ID:
|
||||||
resolve(Consts.HubType.DUPLO_TRAIN_HUB);
|
resolve(Consts.HubType.DUPLO_TRAIN_BASE);
|
||||||
break;
|
break;
|
||||||
case Consts.BLEManufacturerData.CONTROL_PLUS_LARGE_HUB:
|
case Consts.BLEManufacturerData.TECHNIC_MEDIUM_HUB:
|
||||||
resolve(Consts.HubType.CONTROL_PLUS_HUB);
|
resolve(Consts.HubType.TECHNIC_MEDIUM_HUB);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -170,7 +170,7 @@ export class PoweredUP extends EventEmitter {
|
|||||||
|
|
||||||
const device = new WebBLEDevice(server);
|
const device = new WebBLEDevice(server);
|
||||||
|
|
||||||
let hub: Hub;
|
let hub: BaseHub;
|
||||||
|
|
||||||
let hubType = Consts.HubType.UNKNOWN;
|
let hubType = Consts.HubType.UNKNOWN;
|
||||||
let isLPF2Hub = false;
|
let isLPF2Hub = false;
|
||||||
@ -195,20 +195,20 @@ export class PoweredUP extends EventEmitter {
|
|||||||
case Consts.HubType.WEDO2_SMART_HUB:
|
case Consts.HubType.WEDO2_SMART_HUB:
|
||||||
hub = new WeDo2SmartHub(device);
|
hub = new WeDo2SmartHub(device);
|
||||||
break;
|
break;
|
||||||
case Consts.HubType.BOOST_MOVE_HUB:
|
case Consts.HubType.MOVE_HUB:
|
||||||
hub = new BoostMoveHub(device);
|
hub = new MoveHub(device);
|
||||||
break;
|
break;
|
||||||
case Consts.HubType.POWERED_UP_HUB:
|
case Consts.HubType.HUB:
|
||||||
hub = new PUPHub(device);
|
hub = new Hub(device);
|
||||||
break;
|
break;
|
||||||
case Consts.HubType.POWERED_UP_REMOTE:
|
case Consts.HubType.REMOTE_CONTROL:
|
||||||
hub = new PUPRemote(device);
|
hub = new RemoteControl(device);
|
||||||
break;
|
break;
|
||||||
case Consts.HubType.DUPLO_TRAIN_HUB:
|
case Consts.HubType.DUPLO_TRAIN_BASE:
|
||||||
hub = new DuploTrainBase(device);
|
hub = new DuploTrainBase(device);
|
||||||
break;
|
break;
|
||||||
case Consts.HubType.CONTROL_PLUS_HUB:
|
case Consts.HubType.TECHNIC_MEDIUM_HUB:
|
||||||
hub = new ControlPlusHub(device);
|
hub = new TechnicMediumHub(device);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
@ -231,7 +231,7 @@ export class PoweredUP extends EventEmitter {
|
|||||||
/**
|
/**
|
||||||
* Emits when a Powered UP Hub device is found.
|
* Emits when a Powered UP Hub device is found.
|
||||||
* @event PoweredUP#discover
|
* @event PoweredUP#discover
|
||||||
* @param {WeDo2SmartHub | BoostMoveHub | ControlPlusHub | PUPHub | PUPRemote | DuploTrainBase} hub
|
* @param {WeDo2SmartHub | MoveHub | TechnicMediumHub | Hub | RemoteControl | DuploTrainBase} hub
|
||||||
*/
|
*/
|
||||||
this.emit("discover", hub);
|
this.emit("discover", hub);
|
||||||
|
|
||||||
|
@ -2,12 +2,12 @@ import { Peripheral } from "@abandonware/noble";
|
|||||||
|
|
||||||
import { NobleDevice } from "./nobleabstraction";
|
import { NobleDevice } from "./nobleabstraction";
|
||||||
|
|
||||||
import { BoostMoveHub } from "./hubs/boostmovehub";
|
import { BaseHub } from "./hubs/basehub";
|
||||||
import { ControlPlusHub } from "./hubs/controlplushub";
|
|
||||||
import { DuploTrainBase } from "./hubs/duplotrainbase";
|
import { DuploTrainBase } from "./hubs/duplotrainbase";
|
||||||
|
import { MoveHub } from "./hubs/movehub";
|
||||||
import { Hub } from "./hubs/hub";
|
import { Hub } from "./hubs/hub";
|
||||||
import { PUPHub } from "./hubs/puphub";
|
import { RemoteControl } from "./hubs/remotecontrol";
|
||||||
import { PUPRemote } from "./hubs/pupremote";
|
import { TechnicMediumHub } from "./hubs/technicmediumhub";
|
||||||
import { WeDo2SmartHub } from "./hubs/wedo2smarthub";
|
import { WeDo2SmartHub } from "./hubs/wedo2smarthub";
|
||||||
|
|
||||||
import * as Consts from "./consts";
|
import * as Consts from "./consts";
|
||||||
@ -45,7 +45,7 @@ noble.on("stateChange", (state: string) => {
|
|||||||
export class PoweredUP extends EventEmitter {
|
export class PoweredUP extends EventEmitter {
|
||||||
|
|
||||||
|
|
||||||
private _connectedHubs: {[uuid: string]: Hub} = {};
|
private _connectedHubs: {[uuid: string]: BaseHub} = {};
|
||||||
|
|
||||||
|
|
||||||
constructor () {
|
constructor () {
|
||||||
@ -94,7 +94,7 @@ export class PoweredUP extends EventEmitter {
|
|||||||
/**
|
/**
|
||||||
* Retrieve a list of Powered UP Hubs.
|
* Retrieve a list of Powered UP Hubs.
|
||||||
* @method PoweredUP#getHubs
|
* @method PoweredUP#getHubs
|
||||||
* @returns {Hub[]}
|
* @returns {BaseHub[]}
|
||||||
*/
|
*/
|
||||||
public getHubs () {
|
public getHubs () {
|
||||||
return Object.values(this._connectedHubs);
|
return Object.values(this._connectedHubs);
|
||||||
@ -105,7 +105,7 @@ export class PoweredUP extends EventEmitter {
|
|||||||
* Retrieve a Powered UP Hub by UUID.
|
* Retrieve a Powered UP Hub by UUID.
|
||||||
* @method PoweredUP#getHubByUUID
|
* @method PoweredUP#getHubByUUID
|
||||||
* @param {string} uuid
|
* @param {string} uuid
|
||||||
* @returns {Hub | null}
|
* @returns {BaseHub | null}
|
||||||
*/
|
*/
|
||||||
public getHubByUUID (uuid: string) {
|
public getHubByUUID (uuid: string) {
|
||||||
return this._connectedHubs[uuid];
|
return this._connectedHubs[uuid];
|
||||||
@ -116,7 +116,7 @@ export class PoweredUP extends EventEmitter {
|
|||||||
* Retrieve a Powered UP Hub by primary MAC address.
|
* Retrieve a Powered UP Hub by primary MAC address.
|
||||||
* @method PoweredUP#getHubByPrimaryMACAddress
|
* @method PoweredUP#getHubByPrimaryMACAddress
|
||||||
* @param {string} address
|
* @param {string} address
|
||||||
* @returns {Hub}
|
* @returns {BaseHub}
|
||||||
*/
|
*/
|
||||||
public getHubByPrimaryMACAddress (address: string) {
|
public getHubByPrimaryMACAddress (address: string) {
|
||||||
return Object.values(this._connectedHubs).filter((hub) => hub.primaryMACAddress === address)[0];
|
return Object.values(this._connectedHubs).filter((hub) => hub.primaryMACAddress === address)[0];
|
||||||
@ -127,7 +127,7 @@ export class PoweredUP extends EventEmitter {
|
|||||||
* Retrieve a list of Powered UP Hub by name.
|
* Retrieve a list of Powered UP Hub by name.
|
||||||
* @method PoweredUP#getHubsByName
|
* @method PoweredUP#getHubsByName
|
||||||
* @param {string} name
|
* @param {string} name
|
||||||
* @returns {Hub[]}
|
* @returns {BaseHub[]}
|
||||||
*/
|
*/
|
||||||
public getHubsByName (name: string) {
|
public getHubsByName (name: string) {
|
||||||
return Object.values(this._connectedHubs).filter((hub) => hub.name === name);
|
return Object.values(this._connectedHubs).filter((hub) => hub.name === name);
|
||||||
@ -138,7 +138,7 @@ export class PoweredUP extends EventEmitter {
|
|||||||
* Retrieve a list of Powered UP Hub by type.
|
* Retrieve a list of Powered UP Hub by type.
|
||||||
* @method PoweredUP#getHubsByType
|
* @method PoweredUP#getHubsByType
|
||||||
* @param {string} name
|
* @param {string} name
|
||||||
* @returns {Hub[]}
|
* @returns {BaseHub[]}
|
||||||
*/
|
*/
|
||||||
public getHubsByType (hubType: number) {
|
public getHubsByType (hubType: number) {
|
||||||
return Object.values(this._connectedHubs).filter((hub) => hub.type === hubType);
|
return Object.values(this._connectedHubs).filter((hub) => hub.type === hubType);
|
||||||
@ -150,20 +150,20 @@ export class PoweredUP extends EventEmitter {
|
|||||||
peripheral.removeAllListeners();
|
peripheral.removeAllListeners();
|
||||||
const device = new NobleDevice(peripheral);
|
const device = new NobleDevice(peripheral);
|
||||||
|
|
||||||
let hub: Hub;
|
let hub: BaseHub;
|
||||||
|
|
||||||
if (await WeDo2SmartHub.IsWeDo2SmartHub(peripheral)) {
|
if (await WeDo2SmartHub.IsWeDo2SmartHub(peripheral)) {
|
||||||
hub = new WeDo2SmartHub(device);
|
hub = new WeDo2SmartHub(device);
|
||||||
} else if (await BoostMoveHub.IsBoostMoveHub(peripheral)) {
|
} else if (await MoveHub.IsMoveHub(peripheral)) {
|
||||||
hub = new BoostMoveHub(device);
|
hub = new MoveHub(device);
|
||||||
} else if (await PUPHub.IsPUPHub(peripheral)) {
|
} else if (await Hub.IsHub(peripheral)) {
|
||||||
hub = new PUPHub(device);
|
hub = new Hub(device);
|
||||||
} else if (await PUPRemote.IsPUPRemote(peripheral)) {
|
} else if (await RemoteControl.IsRemoteControl(peripheral)) {
|
||||||
hub = new PUPRemote(device);
|
hub = new RemoteControl(device);
|
||||||
} else if (await DuploTrainBase.IsDuploTrainBase(peripheral)) {
|
} else if (await DuploTrainBase.IsDuploTrainBase(peripheral)) {
|
||||||
hub = new DuploTrainBase(device);
|
hub = new DuploTrainBase(device);
|
||||||
} else if (await ControlPlusHub.IsControlPlusHub(peripheral)) {
|
} else if (await TechnicMediumHub.IsTechnicMediumHub(peripheral)) {
|
||||||
hub = new ControlPlusHub(device);
|
hub = new TechnicMediumHub(device);
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -189,7 +189,7 @@ export class PoweredUP extends EventEmitter {
|
|||||||
/**
|
/**
|
||||||
* Emits when a Powered UP Hub device is found.
|
* Emits when a Powered UP Hub device is found.
|
||||||
* @event PoweredUP#discover
|
* @event PoweredUP#discover
|
||||||
* @param {WeDo2SmartHub | BoostMoveHub | ControlPlusHub | PUPHub | PUPRemote | DuploTrainBase} hub
|
* @param {WeDo2SmartHub | MoveHub | TechnicMediumHub | Hub | RemoteControl | DuploTrainBase} hub
|
||||||
*/
|
*/
|
||||||
this.emit("discover", hub);
|
this.emit("discover", hub);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user