Implemented methods for retrieving hubs and devices

This commit is contained in:
Nathan Kellenicki 2019-12-11 17:20:35 +09:00
parent 1793eb05e0
commit 6beeda5df7
3 changed files with 64 additions and 22 deletions

View File

@ -168,6 +168,26 @@ export class Hub extends EventEmitter {
} }
public getDeviceAtPort (portName: string) {
const portId = this._portNames[portName];
if (portId) {
return this._attachedDevices[portId];
} else {
throw new Error(`Port ${portName} does not exist on this hub type`);
}
}
public getDevices () {
return Object.values(this._attachedDevices);
}
public getDevicesByType (deviceType: number) {
return this.getDevices().filter((device) => device.type === deviceType);
}
public getPortNameForPortId (portId: number) { public getPortNameForPortId (portId: number) {
for (const port of Object.keys(this._portNames)) { for (const port of Object.keys(this._portNames)) {
if (this._portNames[port] === portId) { if (this._portNames[port] === portId) {

View File

@ -74,44 +74,55 @@ export class PoweredUP extends EventEmitter {
/** /**
* Retrieve a list of Powered UP Hubs. * Retrieve a list of Powered UP Hubs.
* @method PoweredUP#getConnectedHubs * @method PoweredUP#getHubs
* @returns {Hub[]} * @returns {Hub[]}
*/ */
public getConnectedHubs () { public getHubs () {
return Object.keys(this._connectedHubs).map((uuid) => this._connectedHubs[uuid]); return Object.values(this._connectedHubs);
} }
/** /**
* Retrieve a Powered UP Hub by UUID. * Retrieve a Powered UP Hub by UUID.
* @method PoweredUP#getConnectedHubByUUID * @method PoweredUP#getHubByUUID
* @param {string} uuid * @param {string} uuid
* @returns {Hub | null} * @returns {Hub | null}
*/ */
public getConnectedHubByUUID (uuid: string) { public getHubByUUID (uuid: string) {
return this._connectedHubs[uuid]; return this._connectedHubs[uuid];
} }
/** /**
* Retrieve a Powered UP Hub by primary MAC address. * Retrieve a Powered UP Hub by primary MAC address.
* @method PoweredUP#getConnectedHubByPrimaryMACAddress * @method PoweredUP#getHubByPrimaryMACAddress
* @param {string} address * @param {string} address
* @returns {Hub} * @returns {Hub}
*/ */
public getConnectedHubByPrimaryMACAddress (address: string) { public getHubByPrimaryMACAddress (address: string) {
return Object.keys(this._connectedHubs).map((uuid) => this._connectedHubs[uuid]).filter((hub) => hub.primaryMACAddress === address)[0]; return Object.values(this._connectedHubs).filter((hub) => hub.primaryMACAddress === address)[0];
} }
/** /**
* Retrieve a list of Powered UP Hub by name. * Retrieve a list of Powered UP Hub by name.
* @method PoweredUP#getConnectedHubsByName * @method PoweredUP#getHubsByName
* @param {string} name * @param {string} name
* @returns {Hub[]} * @returns {Hub[]}
*/ */
public getConnectedHubsByName (name: string) { public getHubsByName (name: string) {
return Object.keys(this._connectedHubs).map((uuid) => this._connectedHubs[uuid]).filter((hub) => hub.name === name); return Object.values(this._connectedHubs).filter((hub) => hub.name === name);
}
/**
* Retrieve a list of Powered UP Hub by type.
* @method PoweredUP#getHubsByType
* @param {string} name
* @returns {Hub[]}
*/
public getHubsByType (hubType: number) {
return Object.values(this._connectedHubs).filter((hub) => hub.type === hubType);
} }

View File

@ -92,44 +92,55 @@ export class PoweredUP extends EventEmitter {
/** /**
* Retrieve a list of Powered UP Hubs. * Retrieve a list of Powered UP Hubs.
* @method PoweredUP#getConnectedHubs * @method PoweredUP#getHubs
* @returns {Hub[]} * @returns {Hub[]}
*/ */
public getConnectedHubs () { public getHubs () {
return Object.keys(this._connectedHubs).map((uuid) => this._connectedHubs[uuid]); return Object.values(this._connectedHubs);
} }
/** /**
* Retrieve a Powered UP Hub by UUID. * Retrieve a Powered UP Hub by UUID.
* @method PoweredUP#getConnectedHubByUUID * @method PoweredUP#getHubByUUID
* @param {string} uuid * @param {string} uuid
* @returns {Hub | null} * @returns {Hub | null}
*/ */
public getConnectedHubByUUID (uuid: string) { public getHubByUUID (uuid: string) {
return this._connectedHubs[uuid]; return this._connectedHubs[uuid];
} }
/** /**
* Retrieve a Powered UP Hub by primary MAC address. * Retrieve a Powered UP Hub by primary MAC address.
* @method PoweredUP#getConnectedHubByPrimaryMACAddress * @method PoweredUP#getHubByPrimaryMACAddress
* @param {string} address * @param {string} address
* @returns {Hub} * @returns {Hub}
*/ */
public getConnectedHubByPrimaryMACAddress (address: string) { public getHubByPrimaryMACAddress (address: string) {
return Object.keys(this._connectedHubs).map((uuid) => this._connectedHubs[uuid]).filter((hub) => hub.primaryMACAddress === address)[0]; return Object.values(this._connectedHubs).filter((hub) => hub.primaryMACAddress === address)[0];
} }
/** /**
* Retrieve a list of Powered UP Hub by name. * Retrieve a list of Powered UP Hub by name.
* @method PoweredUP#getConnectedHubsByName * @method PoweredUP#getHubsByName
* @param {string} name * @param {string} name
* @returns {Hub[]} * @returns {Hub[]}
*/ */
public getConnectedHubsByName (name: string) { public getHubsByName (name: string) {
return Object.keys(this._connectedHubs).map((uuid) => this._connectedHubs[uuid]).filter((hub) => hub.name === name); return Object.values(this._connectedHubs).filter((hub) => hub.name === name);
}
/**
* Retrieve a list of Powered UP Hub by type.
* @method PoweredUP#getHubsByType
* @param {string} name
* @returns {Hub[]}
*/
public getHubsByType (hubType: number) {
return Object.values(this._connectedHubs).filter((hub) => hub.type === hubType);
} }