Skip to content

Commit

Permalink
move some vars to base class
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollon77 committed Jul 8, 2024
1 parent d8fa49a commit 6ac7609
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 35 deletions.
23 changes: 21 additions & 2 deletions src/matter/BaseServerNode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { GeneralNode, MessageResponse } from './GeneralNode';
import type { MatterAdapter } from '../main';
import { ServerNode } from '@project-chip/matter.js/node';

export interface BaseCreateOptions {
adapter: MatterAdapter;
}

export enum NodeStates {
Creating = 'creating',
Expand All @@ -21,8 +27,21 @@ export interface NodeStateResponse {
}

export abstract class BaseServerNode implements GeneralNode {
abstract advertise(): Promise<void>;
abstract factoryReset(): Promise<void>;
protected adapter: MatterAdapter;
protected serverNode?: ServerNode;

protected constructor(options: BaseCreateOptions) {
this.adapter = options.adapter;
}

async advertise(): Promise<void> {
await this.serverNode?.advertiseNow();
}

async factoryReset(): Promise<void> {
await this.serverNode?.factoryReset();
}

abstract getState(): Promise<NodeStateResponse>;

async handleCommand(command: string, _message: ioBroker.MessagePayload): Promise<MessageResponse> {
Expand Down
21 changes: 5 additions & 16 deletions src/matter/BridgedDevicesNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import { AggregatorEndpoint } from '@project-chip/matter.js/endpoint/definitions
import {
BridgedDeviceBasicInformationServer
} from '@project-chip/matter.js/behavior/definitions/bridged-device-basic-information';
import { BaseServerNode, ConnectionInfo, NodeStateResponse, NodeStates } from './BaseServerNode';
import { BaseCreateOptions, BaseServerNode, ConnectionInfo, NodeStateResponse, NodeStates } from './BaseServerNode';

export interface BridgeCreateOptions {
export interface BridgeCreateOptions extends BaseCreateOptions {
adapter: MatterAdapter;
parameters: BridgeOptions,
devices: GenericDevice[];
Expand All @@ -36,14 +36,11 @@ export interface BridgeOptions {
class BridgedDevices extends BaseServerNode {
private parameters: BridgeOptions;
private readonly devices: GenericDevice[];
private serverNode?: ServerNode;
private devicesOptions: BridgeDeviceDescription[];
private adapter: MatterAdapter;
private commissioned: boolean | null = null;

constructor(options: BridgeCreateOptions) {
super();
this.adapter = options.adapter;
super(options);
this.parameters = options.parameters;
this.devices = options.devices;
this.devicesOptions = options.devicesOptions;
Expand Down Expand Up @@ -188,7 +185,7 @@ class BridgedDevices extends BaseServerNode {
if (!this.serverNode?.lifecycle.isCommissioned) {
if (this.commissioned !== false) {
this.commissioned = false;
await this.adapter.setStateAsync(`bridges.${this.parameters.uuid}.commissioned`, this.commissioned, true);
await this.adapter.setState(`bridges.${this.parameters.uuid}.commissioned`, this.commissioned, true);
}
const { qrPairingCode, manualPairingCode } = this.serverNode?.state.commissioning.pairingCodes;
return {
Expand All @@ -199,7 +196,7 @@ class BridgedDevices extends BaseServerNode {
} else {
if (this.commissioned !== true) {
this.commissioned = true;
await this.adapter.setStateAsync(`bridges.${this.parameters.uuid}.commissioned`, this.commissioned, true);
await this.adapter.setState(`bridges.${this.parameters.uuid}.commissioned`, this.commissioned, true);
}

const activeSessions = Object.values(this.serverNode.state.sessions.sessions);
Expand Down Expand Up @@ -240,14 +237,6 @@ class BridgedDevices extends BaseServerNode {
}
}

async advertise(): Promise<void> {
await this.serverNode?.advertiseNow();
}

async factoryReset(): Promise<void> {
await this.serverNode?.factoryReset();
}

async start(): Promise<void> {
if (!this.serverNode) {
return;
Expand Down
22 changes: 5 additions & 17 deletions src/matter/DeviceNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import VENDOR_IDS from './vendorIds';
import type { MatterAdapter } from '../main';

Check failure on line 10 in src/matter/DeviceNode.ts

View workflow job for this annotation

GitHub Actions / check-and-lint

'MatterAdapter' is defined but never used

Check failure on line 10 in src/matter/DeviceNode.ts

View workflow job for this annotation

GitHub Actions / check-and-lint

'MatterAdapter' is defined but never used
import { ServerNode } from '@project-chip/matter.js/node';
import { SessionsBehavior } from '@project-chip/matter.js/behavior/system/sessions';
import { BaseServerNode, NodeStateResponse, NodeStates } from './BaseServerNode';
import { BaseCreateOptions, BaseServerNode, NodeStateResponse, NodeStates } from './BaseServerNode';

export interface DeviceCreateOptions {
adapter: MatterAdapter;
export interface DeviceCreateOptions extends BaseCreateOptions {
parameters: DeviceOptions,
device: GenericDevice;
deviceOptions: DeviceDescription;
Expand All @@ -31,14 +30,11 @@ export interface DeviceOptions {
class Device extends BaseServerNode {
private parameters: DeviceOptions;
private readonly device: GenericDevice;
private serverNode?: ServerNode;
private deviceOptions: DeviceDescription;
private adapter: MatterAdapter;
private commissioned: boolean | null = null;

constructor(options: DeviceCreateOptions) {
super();
this.adapter = options.adapter;
super(options);
this.parameters = options.parameters;
this.device = options.device;
this.deviceOptions = options.deviceOptions;
Expand Down Expand Up @@ -170,7 +166,7 @@ class Device extends BaseServerNode {
if (!this.serverNode?.lifecycle.isCommissioned) {
if (this.commissioned !== false) {
this.commissioned = false;
await this.adapter.setStateAsync(`devices.${this.parameters.uuid}.commissioned`, this.commissioned, true);
await this.adapter.setState(`devices.${this.parameters.uuid}.commissioned`, this.commissioned, true);
}
const { qrPairingCode, manualPairingCode } = this.serverNode?.state.commissioning.pairingCodes;
return {
Expand All @@ -181,7 +177,7 @@ class Device extends BaseServerNode {
} else {
if (this.commissioned !== true) {
this.commissioned = true;
await this.adapter.setStateAsync(`devices.${this.parameters.uuid}.commissioned`, this.commissioned, true);
await this.adapter.setState(`devices.${this.parameters.uuid}.commissioned`, this.commissioned, true);
}

const activeSessions = Object.values(this.serverNode.state.sessions.sessions);
Expand Down Expand Up @@ -222,14 +218,6 @@ class Device extends BaseServerNode {
}
}

async advertise(): Promise<void> {
await this.serverNode?.advertiseNow();
}

async factoryReset(): Promise<void> {
await this.serverNode?.factoryReset();
}

async start(): Promise<void> {
if (!this.serverNode) return;
await this.serverNode.bringOnline();
Expand Down

0 comments on commit 6ac7609

Please sign in to comment.