More error handling around manuallyAttachDevice
This commit is contained in:
parent
e6c6a3c18f
commit
dee732c8ca
2
package-lock.json
generated
2
package-lock.json
generated
@ -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": {
|
||||||
|
@ -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",
|
||||||
|
@ -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.`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user