Skip to content

Commit

Permalink
Merge pull request #14 from doubleedesign/feature/light-groups
Browse files Browse the repository at this point in the history
Add ability to get the group a light is in
  • Loading branch information
jorgeclaro authored Nov 16, 2024
2 parents 061b37f + 437cd3a commit 36e12e1
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/light.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
} from './errors/lightErrors';
import { ER_CLIENT_INVALID_ARGUMENT } from './errors/clientErrors';
import { SetTileState64Request, SetUserPositionRequest, StateDeviceChainResponse } from './packets/tiles/tiles';
import { Group } from './packets/group/group';

export enum LightEvents {
CONECTIVITY = 'connectivity',
Expand Down Expand Up @@ -71,6 +72,7 @@ export class Light extends EventEmitter {
private _client: Client;
private _connectivity: boolean;
private _label: string;
private _group: Group;
private _power: boolean;
private _color: ColorHSBK;

Expand Down Expand Up @@ -791,6 +793,47 @@ export class Light extends EventEmitter {
});
}

public getGroup(cache = false, timeout: number = DEFAULT_MSG_REPLY_TIMEOUT): Promise<Group> {
const ctx = this;

return new Promise((resolve, reject) => {
if (!ctx.connectivity) {
return reject(new ServiceErrorBuilder(ER_LIGHT_OFFLINE).withContextualMessage(`Id: ${ctx.id}`).build());
}

if (cache === true) {
if (ctx._group) {
return resolve(ctx._group);
}
}

const cmdReq = { target: this.id };
const packetObj = createObject(packet.getGroup.type, cmdReq, ctx._client.source);
const sqnNumber = ctx._client.send(packetObj);
const timeoutHandle = setTimeout(() => {
reject(new ServiceErrorBuilder(ER_LIGHT_CMD_TIMEOUT).withContextualMessage(`Id: ${ctx.id}`).build());
}, timeout);

ctx._client.addMessageHandler(
packet.stateGroup.name,
(err: Error, data) => {
if (err) {
return reject(err);
}

clearTimeout(timeoutHandle);

return resolve({
group: data.group,
label: data.label,
updatedAt: data.updatedAt
});
},
sqnNumber
);
});
}

public getTags(timeout: number = DEFAULT_MSG_REPLY_TIMEOUT) {
const ctx = this;

Expand Down

0 comments on commit 36e12e1

Please sign in to comment.