diff --git a/src/consts.ts b/src/consts.ts index 015a6d1..a08450b 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -2,7 +2,7 @@ * @typedef HubType * @property {number} UNKNOWN 0 * @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_REMOTE 4 * @property {number} DUPLO_TRAIN_HUB 5 @@ -11,11 +11,11 @@ export enum HubType { UNKNOWN = 0, WEDO2_SMART_HUB = 1, - BOOST_MOVE_HUB = 2, - POWERED_UP_HUB = 3, - POWERED_UP_REMOTE = 4, - DUPLO_TRAIN_HUB = 5, - CONTROL_PLUS_HUB = 6 + MOVE_HUB = 2, + HUB = 3, + REMOTE_CONTROL = 4, + DUPLO_TRAIN_BASE = 5, + TECHNIC_MEDIUM_HUB = 6 } @@ -155,11 +155,11 @@ export enum DuploTrainBaseSound { export enum BLEManufacturerData { - DUPLO_TRAIN_HUB_ID = 32, - BOOST_MOVE_HUB_ID = 64, - POWERED_UP_HUB_ID = 65, - POWERED_UP_REMOTE_ID = 66, - CONTROL_PLUS_LARGE_HUB = 128 + DUPLO_TRAIN_BASE_ID = 32, + MOVE_HUB_ID = 64, + HUB_ID = 65, + REMOTE_CONTROL_ID = 66, + TECHNIC_MEDIUM_HUB = 128 } diff --git a/src/devices/currentsensor.ts b/src/devices/currentsensor.ts index b078454..efa11c8 100644 --- a/src/devices/currentsensor.ts +++ b/src/devices/currentsensor.ts @@ -48,7 +48,7 @@ export namespace CurrentSensor { export const MaxCurrentValue: {[hubType: number]: number} = { [Consts.HubType.UNKNOWN]: 2444, - [Consts.HubType.CONTROL_PLUS_HUB]: 4175, + [Consts.HubType.TECHNIC_MEDIUM_HUB]: 4175, } export const MaxCurrentRaw: {[hubType: number]: number} = { diff --git a/src/devices/pupremotebutton.ts b/src/devices/remotecontrolbutton.ts similarity index 71% rename from src/devices/pupremotebutton.ts rename to src/devices/remotecontrolbutton.ts index 0f7719f..c2377c8 100644 --- a/src/devices/pupremotebutton.ts +++ b/src/devices/remotecontrolbutton.ts @@ -4,20 +4,20 @@ import { IDeviceInterface } from "../interfaces"; import * as Consts from "../consts"; -export class PUPRemoteButton extends Device { +export class RemoteControlButton extends Device { 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) { const mode = this._mode; switch (mode) { - case PUPRemoteButton.Mode.BUTTON_EVENTS: + case RemoteControlButton.Mode.BUTTON_EVENTS: /** * Emits when a button on the remote is pressed or released. - * @event PUPRemoteButton#button + * @event RemoteControlButton#button * @param {number} event */ const event = message[4]; @@ -28,14 +28,14 @@ export class PUPRemoteButton extends Device { } -export namespace PUPRemoteButton { +export namespace RemoteControlButton { export enum Mode { BUTTON_EVENTS = 0x00 } export const ModeMap: {[event: string]: number} = { - "button": PUPRemoteButton.Mode.BUTTON_EVENTS + "button": RemoteControlButton.Mode.BUTTON_EVENTS } export const ButtonState: {[state: string]: number} = { diff --git a/src/devices/voltagesensor.ts b/src/devices/voltagesensor.ts index a1a85ca..aadf7c5 100644 --- a/src/devices/voltagesensor.ts +++ b/src/devices/voltagesensor.ts @@ -48,15 +48,15 @@ export namespace VoltageSensor { export const MaxVoltageValue: {[hubType: number]: number} = { [Consts.HubType.UNKNOWN]: 9.615, - [Consts.HubType.DUPLO_TRAIN_HUB]: 6.4, - [Consts.HubType.POWERED_UP_REMOTE]: 6.4, + [Consts.HubType.DUPLO_TRAIN_BASE]: 6.4, + [Consts.HubType.REMOTE_CONTROL]: 6.4, } export const MaxVoltageRaw: {[hubType: number]: number} = { [Consts.HubType.UNKNOWN]: 3893, - [Consts.HubType.DUPLO_TRAIN_HUB]: 3047, - [Consts.HubType.POWERED_UP_REMOTE]: 3200, - [Consts.HubType.CONTROL_PLUS_HUB]: 4095, + [Consts.HubType.DUPLO_TRAIN_BASE]: 3047, + [Consts.HubType.REMOTE_CONTROL]: 3200, + [Consts.HubType.TECHNIC_MEDIUM_HUB]: 4095, } } \ No newline at end of file diff --git a/src/hubs/basehub.ts b/src/hubs/basehub.ts new file mode 100644 index 0000000..7f029e3 --- /dev/null +++ b/src/hubs/basehub.ts @@ -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>} commands Array of executing commands. + * @returns {Promise} Resolved after the commands are finished. + */ + public wait (commands: Array>) { + 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]; + } + + +} diff --git a/src/hubs/duplotrainbase.ts b/src/hubs/duplotrainbase.ts index 98eda91..727dd42 100644 --- a/src/hubs/duplotrainbase.ts +++ b/src/hubs/duplotrainbase.ts @@ -13,7 +13,7 @@ const debug = Debug("duplotrainbase"); * The DuploTrainBase is emitted if the discovered device is a Duplo Train Base. * @class DuploTrainBase * @extends LPF2Hub - * @extends Hub + * @extends BaseHub */ 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.manufacturerData && 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) { - super(device, DuploTrainBase.PortMap, Consts.HubType.DUPLO_TRAIN_HUB); + super(device, DuploTrainBase.PortMap, Consts.HubType.DUPLO_TRAIN_BASE); debug("Discovered Duplo Train Base"); } diff --git a/src/hubs/hub.ts b/src/hubs/hub.ts index 3596ed2..0e567d4 100644 --- a/src/hubs/hub.ts +++ b/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 { 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 { 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 { LPF2Hub } from "./lpf2hub"; 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 - * @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 = ""; - 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; + public static IsHub (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.HUB_ID + ); + } - protected _bleDevice: IBLEAbstraction; + protected _currentPort = 0x3b; - 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"); - }); + constructor (device: IBLEAbstraction) { + super(device, Hub.PortMap, Consts.HubType.HUB); + debug("Discovered Powered UP Hub"); } - /** - * @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(); + return new Promise(async (resolve, reject) => { + debug("Connecting to Powered UP Hub"); + await super.connect(); + debug("Connect completed"); + return resolve(); }); - } - /** - * 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`); + 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.`); } } - 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>} commands Array of executing commands. - * @returns {Promise} Resolved after the commands are finished. - */ - public wait (commands: Array>) { - 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 + } + +} \ No newline at end of file diff --git a/src/hubs/lpf2hub.ts b/src/hubs/lpf2hub.ts index 03937a3..0ece694 100644 --- a/src/hubs/lpf2hub.ts +++ b/src/hubs/lpf2hub.ts @@ -1,4 +1,4 @@ -import { Hub } from "./hub"; +import { BaseHub } from "./basehub"; import * as Consts from "../consts"; @@ -11,15 +11,9 @@ const modeInfoDebug = Debug("lpf2hubmodeinfo"); /** * @class LPF2Hub - * @extends Hub + * @extends BaseHub */ -export class LPF2Hub extends Hub { - - protected _ledPort: number = 0x32; - - private _lastTiltX: number = 0; - private _lastTiltY: number = 0; - private _lastTiltZ: number = 0; +export class LPF2Hub extends BaseHub { private _messageBuffer: Buffer = Buffer.alloc(0); @@ -381,46 +375,6 @@ export class LPF2Hub extends Hub { // if (port && port.connected) { // 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: { // if (data[4] <= 10) { // this.emit("color", port.id, data[4]); diff --git a/src/hubs/boostmovehub.ts b/src/hubs/movehub.ts similarity index 63% rename from src/hubs/boostmovehub.ts rename to src/hubs/movehub.ts index 644b2cf..a38dbcd 100644 --- a/src/hubs/boostmovehub.ts +++ b/src/hubs/movehub.ts @@ -8,38 +8,38 @@ import { LPF2Hub } from "./lpf2hub"; import * as Consts from "../consts"; 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. - * @class BoostMoveHub + * The MoveHub is emitted if the discovered device is a Move Hub. + * @class MoveHub * @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 ( 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.BOOST_MOVE_HUB_ID + peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.MOVE_HUB_ID ); } constructor (device: IBLEAbstraction) { - super(device, BoostMoveHub.PortMap, Consts.HubType.BOOST_MOVE_HUB); - debug("Discovered Boost Move Hub"); + super(device, MoveHub.PortMap, Consts.HubType.MOVE_HUB); + debug("Discovered Move Hub"); } public connect () { return new Promise(async (resolve, reject) => { - debug("Connecting to Boost Move Hub"); + debug("Connecting to Move Hub"); await super.connect(); debug("Connect completed"); return resolve(); @@ -49,21 +49,20 @@ export class BoostMoveHub extends LPF2Hub { protected _checkFirmware (version: string) { 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} = { "A": 0, "B": 1, "C": 2, - "D": 3, - "TILT": 58 + "D": 3 } } \ No newline at end of file diff --git a/src/hubs/puphub.ts b/src/hubs/puphub.ts deleted file mode 100644 index c376856..0000000 --- a/src/hubs/puphub.ts +++ /dev/null @@ -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 - } - -} \ No newline at end of file diff --git a/src/hubs/pupremote.ts b/src/hubs/remotecontrol.ts similarity index 72% rename from src/hubs/pupremote.ts rename to src/hubs/remotecontrol.ts index 7197f16..3d3c731 100644 --- a/src/hubs/pupremote.ts +++ b/src/hubs/remotecontrol.ts @@ -7,35 +7,32 @@ import { LPF2Hub } from "./lpf2hub"; import * as Consts from "../consts"; 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. - * @class PUPRemote + * The RemoteControl is emitted if the discovered device is a Remote Control. + * @class RemoteControl * @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 ( 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_REMOTE_ID + peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.REMOTE_CONTROL_ID ); } - protected _ledPort = 0x34; - - 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"); } @@ -52,7 +49,7 @@ export class PUPRemote extends LPF2Hub { } -export namespace PUPRemote { +export namespace RemoteControl { export const PortMap: {[portName: string]: number} = { "LEFT": 0, diff --git a/src/hubs/controlplushub.ts b/src/hubs/technicmediumhub.ts similarity index 90% rename from src/hubs/controlplushub.ts rename to src/hubs/technicmediumhub.ts index 43929ee..dc6f2b5 100644 --- a/src/hubs/controlplushub.ts +++ b/src/hubs/technicmediumhub.ts @@ -7,31 +7,31 @@ import { LPF2Hub } from "./lpf2hub"; import * as Consts from "../consts"; import Debug = require("debug"); -const debug = Debug("ControlPlusHub"); +const debug = Debug("technicmediumhub"); /** - * The ControlPlusHub is emitted if the discovered device is a Control+ Hub. - * @class ControlPlusHub + * The TechnicMediumHub is emitted if the discovered device is a Technic Medium Hub. + * @class TechnicMediumHub * @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 ( 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.CONTROL_PLUS_LARGE_HUB + peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.TECHNIC_MEDIUM_HUB ); } 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"); } @@ -108,7 +108,7 @@ export class ControlPlusHub extends LPF2Hub { } -export namespace ControlPlusHub { +export namespace TechnicMediumHub { export const PortMap: {[portName: string]: number} = { "A": 0, diff --git a/src/hubs/wedo2smarthub.ts b/src/hubs/wedo2smarthub.ts index 7cd607a..c6791da 100644 --- a/src/hubs/wedo2smarthub.ts +++ b/src/hubs/wedo2smarthub.ts @@ -2,7 +2,7 @@ import { Peripheral } from "@abandonware/noble"; import { IBLEAbstraction } from "../interfaces"; -import { Hub } from "./hub"; +import { BaseHub } from "./basehub"; 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. * @class WeDo2SmartHub - * @extends Hub + * @extends BaseHub */ -export class WeDo2SmartHub extends Hub { +export class WeDo2SmartHub extends BaseHub { public static IsWeDo2SmartHub (peripheral: Peripheral) { diff --git a/src/index-browser.ts b/src/index-browser.ts index 707e687..d38f8a3 100644 --- a/src/index-browser.ts +++ b/src/index-browser.ts @@ -2,12 +2,12 @@ import * as Consts from "./consts"; import { PoweredUP } from "./poweredup-browser"; -import { BoostMoveHub } from "./hubs/boostmovehub"; -import { ControlPlusHub } from "./hubs/controlplushub"; +import { BaseHub } from "./hubs/basehub"; import { DuploTrainBase } from "./hubs/duplotrainbase"; +import { MoveHub } from "./hubs/movehub"; import { Hub } from "./hubs/hub"; -import { PUPHub } from "./hubs/puphub"; -import { PUPRemote } from "./hubs/pupremote"; +import { RemoteControl } from "./hubs/remotecontrol"; +import { TechnicMediumHub } from "./hubs/technicmediumhub"; import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; import { ColorDistanceSensor } from "./devices/colordistancesensor"; @@ -19,7 +19,7 @@ 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 { RemoteControlButton } from "./devices/remotecontrolbutton"; import { SimpleMediumLinearMotor } from "./devices/simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor"; import { TechnicMediumHubAccelerometerSensor } from "./devices/technicmediumhubaccelerometersensor"; @@ -35,12 +35,11 @@ import { isWebBluetooth } from "./utils"; // @ts-ignore window.PoweredUP = { PoweredUP, - Hub, + BaseHub, WeDo2SmartHub, - BoostMoveHub, - ControlPlusHub, - PUPHub, - PUPRemote, + TechnicMediumHub, + Hub, + RemoteControl, DuploTrainBase, Consts, ColorDistanceSensor, @@ -49,9 +48,10 @@ window.PoweredUP = { Light, MediumLinearMotor, MotionSensor, + MoveHub, MoveHubMediumLinearMotor, MoveHubTiltSensor, - PUPRemoteButton, + RemoteControlButton, SimpleMediumLinearMotor, TechnicMediumHubAccelerometerSensor, TechnicMediumHubGyroSensor, diff --git a/src/index-node.ts b/src/index-node.ts index a01c76e..ce294e9 100644 --- a/src/index-node.ts +++ b/src/index-node.ts @@ -2,12 +2,12 @@ import * as Consts from "./consts"; import { PoweredUP } from "./poweredup-node"; -import { BoostMoveHub } from "./hubs/boostmovehub"; -import { ControlPlusHub } from "./hubs/controlplushub"; +import { BaseHub } from "./hubs/basehub"; import { DuploTrainBase } from "./hubs/duplotrainbase"; +import { MoveHub } from "./hubs/movehub"; import { Hub } from "./hubs/hub"; -import { PUPHub } from "./hubs/puphub"; -import { PUPRemote } from "./hubs/pupremote"; +import { RemoteControl } from "./hubs/remotecontrol"; +import { TechnicMediumHub } from "./hubs/technicmediumhub"; import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; import { ColorDistanceSensor } from "./devices/colordistancesensor"; @@ -19,7 +19,7 @@ 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 { RemoteControlButton } from "./devices/remotecontrolbutton"; import { SimpleMediumLinearMotor } from "./devices/simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor"; import { TechnicMediumHubAccelerometerSensor } from "./devices/technicmediumhubaccelerometersensor"; @@ -35,12 +35,11 @@ import { isWebBluetooth } from "./utils"; export default PoweredUP; export { PoweredUP, - Hub, + BaseHub, WeDo2SmartHub, - BoostMoveHub, - ControlPlusHub, - PUPHub, - PUPRemote, + TechnicMediumHub, + Hub, + RemoteControl, DuploTrainBase, Consts, ColorDistanceSensor, @@ -49,9 +48,10 @@ export { Light, MediumLinearMotor, MotionSensor, + MoveHub, MoveHubMediumLinearMotor, MoveHubTiltSensor, - PUPRemoteButton, + RemoteControlButton, SimpleMediumLinearMotor, TechnicMediumHubAccelerometerSensor, TechnicMediumHubGyroSensor, diff --git a/src/poweredup-browser.ts b/src/poweredup-browser.ts index 1156fde..339a1b5 100644 --- a/src/poweredup-browser.ts +++ b/src/poweredup-browser.ts @@ -1,11 +1,11 @@ import { WebBLEDevice } from "./webbleabstraction"; -import { BoostMoveHub } from "./hubs/boostmovehub"; -import { ControlPlusHub } from "./hubs/controlplushub"; +import { BaseHub } from "./hubs/basehub"; import { DuploTrainBase } from "./hubs/duplotrainbase"; +import { MoveHub } from "./hubs/movehub"; import { Hub } from "./hubs/hub"; -import { PUPHub } from "./hubs/puphub"; -import { PUPRemote } from "./hubs/pupremote"; +import { RemoteControl } from "./hubs/remotecontrol"; +import { TechnicMediumHub } from "./hubs/technicmediumhub"; import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; import * as Consts from "./consts"; @@ -24,7 +24,7 @@ const debug = Debug("poweredup"); export class PoweredUP extends EventEmitter { - private _connectedHubs: {[uuid: string]: Hub} = {}; + private _connectedHubs: {[uuid: string]: BaseHub} = {}; constructor () { @@ -76,7 +76,7 @@ export class PoweredUP extends EventEmitter { /** * Retrieve a list of Powered UP Hubs. * @method PoweredUP#getHubs - * @returns {Hub[]} + * @returns {BaseHub[]} */ public getHubs () { return Object.values(this._connectedHubs); @@ -87,7 +87,7 @@ export class PoweredUP extends EventEmitter { * Retrieve a Powered UP Hub by UUID. * @method PoweredUP#getHubByUUID * @param {string} uuid - * @returns {Hub | null} + * @returns {BaseHub | null} */ public getHubByUUID (uuid: string) { return this._connectedHubs[uuid]; @@ -98,7 +98,7 @@ export class PoweredUP extends EventEmitter { * Retrieve a Powered UP Hub by primary MAC address. * @method PoweredUP#getHubByPrimaryMACAddress * @param {string} address - * @returns {Hub} + * @returns {BaseHub} */ public getHubByPrimaryMACAddress (address: string) { 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. * @method PoweredUP#getHubsByName * @param {string} name - * @returns {Hub[]} + * @returns {BaseHub[]} */ public getHubsByName (name: string) { 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. * @method PoweredUP#getHubsByType * @param {string} name - * @returns {Hub[]} + * @returns {BaseHub[]} */ public getHubsByType (hubType: number) { 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) { process.nextTick(() => { switch (message[5]) { - case Consts.BLEManufacturerData.POWERED_UP_REMOTE_ID: - resolve(Consts.HubType.POWERED_UP_REMOTE); + case Consts.BLEManufacturerData.REMOTE_CONTROL_ID: + resolve(Consts.HubType.REMOTE_CONTROL); break; - case Consts.BLEManufacturerData.BOOST_MOVE_HUB_ID: - resolve(Consts.HubType.BOOST_MOVE_HUB); + case Consts.BLEManufacturerData.MOVE_HUB_ID: + resolve(Consts.HubType.MOVE_HUB); break; - case Consts.BLEManufacturerData.POWERED_UP_HUB_ID: - resolve(Consts.HubType.POWERED_UP_HUB); + case Consts.BLEManufacturerData.HUB_ID: + resolve(Consts.HubType.HUB); break; - case Consts.BLEManufacturerData.DUPLO_TRAIN_HUB_ID: - resolve(Consts.HubType.DUPLO_TRAIN_HUB); + case Consts.BLEManufacturerData.DUPLO_TRAIN_BASE_ID: + resolve(Consts.HubType.DUPLO_TRAIN_BASE); break; - case Consts.BLEManufacturerData.CONTROL_PLUS_LARGE_HUB: - resolve(Consts.HubType.CONTROL_PLUS_HUB); + case Consts.BLEManufacturerData.TECHNIC_MEDIUM_HUB: + resolve(Consts.HubType.TECHNIC_MEDIUM_HUB); break; } }); @@ -170,7 +170,7 @@ export class PoweredUP extends EventEmitter { const device = new WebBLEDevice(server); - let hub: Hub; + let hub: BaseHub; let hubType = Consts.HubType.UNKNOWN; let isLPF2Hub = false; @@ -195,20 +195,20 @@ export class PoweredUP extends EventEmitter { case Consts.HubType.WEDO2_SMART_HUB: hub = new WeDo2SmartHub(device); break; - case Consts.HubType.BOOST_MOVE_HUB: - hub = new BoostMoveHub(device); + case Consts.HubType.MOVE_HUB: + hub = new MoveHub(device); break; - case Consts.HubType.POWERED_UP_HUB: - hub = new PUPHub(device); + case Consts.HubType.HUB: + hub = new Hub(device); break; - case Consts.HubType.POWERED_UP_REMOTE: - hub = new PUPRemote(device); + case Consts.HubType.REMOTE_CONTROL: + hub = new RemoteControl(device); break; - case Consts.HubType.DUPLO_TRAIN_HUB: + case Consts.HubType.DUPLO_TRAIN_BASE: hub = new DuploTrainBase(device); break; - case Consts.HubType.CONTROL_PLUS_HUB: - hub = new ControlPlusHub(device); + case Consts.HubType.TECHNIC_MEDIUM_HUB: + hub = new TechnicMediumHub(device); break; default: return; @@ -231,7 +231,7 @@ export class PoweredUP extends EventEmitter { /** * Emits when a Powered UP Hub device is found. * @event PoweredUP#discover - * @param {WeDo2SmartHub | BoostMoveHub | ControlPlusHub | PUPHub | PUPRemote | DuploTrainBase} hub + * @param {WeDo2SmartHub | MoveHub | TechnicMediumHub | Hub | RemoteControl | DuploTrainBase} hub */ this.emit("discover", hub); diff --git a/src/poweredup-node.ts b/src/poweredup-node.ts index d5fd57c..798a77d 100644 --- a/src/poweredup-node.ts +++ b/src/poweredup-node.ts @@ -2,12 +2,12 @@ import { Peripheral } from "@abandonware/noble"; import { NobleDevice } from "./nobleabstraction"; -import { BoostMoveHub } from "./hubs/boostmovehub"; -import { ControlPlusHub } from "./hubs/controlplushub"; +import { BaseHub } from "./hubs/basehub"; import { DuploTrainBase } from "./hubs/duplotrainbase"; +import { MoveHub } from "./hubs/movehub"; import { Hub } from "./hubs/hub"; -import { PUPHub } from "./hubs/puphub"; -import { PUPRemote } from "./hubs/pupremote"; +import { RemoteControl } from "./hubs/remotecontrol"; +import { TechnicMediumHub } from "./hubs/technicmediumhub"; import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; import * as Consts from "./consts"; @@ -45,7 +45,7 @@ noble.on("stateChange", (state: string) => { export class PoweredUP extends EventEmitter { - private _connectedHubs: {[uuid: string]: Hub} = {}; + private _connectedHubs: {[uuid: string]: BaseHub} = {}; constructor () { @@ -94,7 +94,7 @@ export class PoweredUP extends EventEmitter { /** * Retrieve a list of Powered UP Hubs. * @method PoweredUP#getHubs - * @returns {Hub[]} + * @returns {BaseHub[]} */ public getHubs () { return Object.values(this._connectedHubs); @@ -105,7 +105,7 @@ export class PoweredUP extends EventEmitter { * Retrieve a Powered UP Hub by UUID. * @method PoweredUP#getHubByUUID * @param {string} uuid - * @returns {Hub | null} + * @returns {BaseHub | null} */ public getHubByUUID (uuid: string) { return this._connectedHubs[uuid]; @@ -116,7 +116,7 @@ export class PoweredUP extends EventEmitter { * Retrieve a Powered UP Hub by primary MAC address. * @method PoweredUP#getHubByPrimaryMACAddress * @param {string} address - * @returns {Hub} + * @returns {BaseHub} */ public getHubByPrimaryMACAddress (address: string) { 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. * @method PoweredUP#getHubsByName * @param {string} name - * @returns {Hub[]} + * @returns {BaseHub[]} */ public getHubsByName (name: string) { 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. * @method PoweredUP#getHubsByType * @param {string} name - * @returns {Hub[]} + * @returns {BaseHub[]} */ public getHubsByType (hubType: number) { return Object.values(this._connectedHubs).filter((hub) => hub.type === hubType); @@ -150,20 +150,20 @@ export class PoweredUP extends EventEmitter { peripheral.removeAllListeners(); const device = new NobleDevice(peripheral); - let hub: Hub; + let hub: BaseHub; if (await WeDo2SmartHub.IsWeDo2SmartHub(peripheral)) { hub = new WeDo2SmartHub(device); - } else if (await BoostMoveHub.IsBoostMoveHub(peripheral)) { - hub = new BoostMoveHub(device); - } else if (await PUPHub.IsPUPHub(peripheral)) { - hub = new PUPHub(device); - } else if (await PUPRemote.IsPUPRemote(peripheral)) { - hub = new PUPRemote(device); + } else if (await MoveHub.IsMoveHub(peripheral)) { + hub = new MoveHub(device); + } else if (await Hub.IsHub(peripheral)) { + hub = new Hub(device); + } else if (await RemoteControl.IsRemoteControl(peripheral)) { + hub = new RemoteControl(device); } else if (await DuploTrainBase.IsDuploTrainBase(peripheral)) { hub = new DuploTrainBase(device); - } else if (await ControlPlusHub.IsControlPlusHub(peripheral)) { - hub = new ControlPlusHub(device); + } else if (await TechnicMediumHub.IsTechnicMediumHub(peripheral)) { + hub = new TechnicMediumHub(device); } else { return; } @@ -189,7 +189,7 @@ export class PoweredUP extends EventEmitter { /** * Emits when a Powered UP Hub device is found. * @event PoweredUP#discover - * @param {WeDo2SmartHub | BoostMoveHub | ControlPlusHub | PUPHub | PUPRemote | DuploTrainBase} hub + * @param {WeDo2SmartHub | MoveHub | TechnicMediumHub | Hub | RemoteControl | DuploTrainBase} hub */ this.emit("discover", hub);