More error handling around manuallyAttachDevice
Some checks failed
continuous-integration/drone/tag Build is failing
continuous-integration/drone/push Build is passing

This commit is contained in:
Nathan Kellenicki 2020-06-03 20:24:03 -07:00
parent e6c6a3c18f
commit dee732c8ca
3 changed files with 9 additions and 3 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "node-poweredup", "name": "node-poweredup",
"version": "6.5.1", "version": "6.5.2",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -1,6 +1,6 @@
{ {
"name": "node-poweredup", "name": "node-poweredup",
"version": "6.5.1", "version": "6.5.2",
"description": "A Javascript module to interface with LEGO Powered Up components.", "description": "A Javascript module to interface with LEGO Powered Up components.",
"homepage": "https://github.com/nathankellenicki/node-poweredup/", "homepage": "https://github.com/nathankellenicki/node-poweredup/",
"main": "dist/node/index-node.js", "main": "dist/node/index-node.js",

View File

@ -333,11 +333,17 @@ export class BaseHub extends EventEmitter {
public manuallyAttachDevice(deviceType: number, portId: number) { public manuallyAttachDevice(deviceType: number, portId: number) {
if (!this._attachedDevices[portId]) { if (!this._attachedDevices[portId]) {
debug(`No device attached to portId ${portId}, creating and attaching device type ${deviceType}`);
const device = this._createDevice(deviceType, portId); const device = this._createDevice(deviceType, portId);
this._attachDevice(device); this._attachDevice(device);
return device; return device;
} else { } else {
return false; if (this._attachedDevices[portId].type === deviceType) {
debug(`Device of ${deviceType} already attached to portId ${portId}, returning existing device`);
return this._attachedDevices[portId];
} else {
throw new Error(`Already a different type of device attached to portId ${portId}. Only use this method when you are certain what's attached.`);
}
} }
} }