Skip to content

Commit

Permalink
V8
Browse files Browse the repository at this point in the history
  • Loading branch information
Luligu committed Jul 2, 2024
1 parent 255270a commit c6dae97
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
39 changes: 28 additions & 11 deletions src/matterbridgeDeviceV8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import { MutableEndpoint, EndpointType } from '@project-chip/matter.js/endpoint/
import { Behavior } from '@project-chip/matter.js/behavior';
import { ClusterBehavior } from '@project-chip/matter.js/behavior/cluster';
import { SupportedBehaviors, Behaviors } from '@project-chip/matter.js/endpoint/properties';
import { DescriptorServer, DescriptorBehavior } from '@project-chip/matter.js/behavior/definitions/descriptor';
import { IdentifyServer, IdentifyBehavior } from '@project-chip/matter.js/behavior/definitions/identify';
import { GroupsServer, GroupsBehavior } from '@project-chip/matter.js/behavior/definitions/groups';
import { ScenesServer, ScenesBehavior } from '@project-chip/matter.js/behavior/definitions/scenes';
import { OnOffServer, OnOffBehavior } from '@project-chip/matter.js/behavior/definitions/on-off';
import { TemperatureMeasurementServer } from '@project-chip/matter.js/behavior/definitions/temperature-measurement';
import { RelativeHumidityMeasurementServer } from '@project-chip/matter.js/behavior/definitions/relative-humidity-measurement';
import { PressureMeasurementServer } from '@project-chip/matter.js/behavior/definitions/pressure-measurement';
import { BridgedDeviceBasicInformationServer, BridgedDeviceBasicInformationBehavior } from '@project-chip/matter.js/behavior/definitions/bridged-device-basic-information';

// Old API imports
Expand Down Expand Up @@ -82,7 +84,7 @@ import { AtLeastOne, MakeMandatory, NamedHandler } from '@project-chip/matter-no
import { ClusterId, EndpointNumber, VendorId } from '@project-chip/matter-node.js/datatype';

// Matterbridge imports
import { AnsiLogger, CYAN, TimestampFormat, db, hk, zb } from 'node-ansi-logger';
import { AnsiLogger, CYAN, TimestampFormat, db, hk, rs, zb } from 'node-ansi-logger';
import { BooleanStateConfiguration, BooleanStateConfigurationCluster } from './cluster/BooleanStateConfigurationCluster.js';
import { PowerTopology, PowerTopologyCluster } from './cluster/PowerTopologyCluster.js';
import { ElectricalPowerMeasurement, ElectricalPowerMeasurementCluster } from './cluster/ElectricalPowerMeasurementCluster.js';
Expand Down Expand Up @@ -207,13 +209,7 @@ export class MatterbridgeDeviceV8 extends Endpoint {
// Map ClusterId to Behavior.Type
const behaviorTypes: Behavior.Type[] = [];
clusterServerList.forEach((clusterId) => {
if (clusterId === Identify.Cluster.id) behaviorTypes.push(IdentifyServer);
if (clusterId === Groups.Cluster.id) behaviorTypes.push(GroupsServer);
if (clusterId === Scenes.Cluster.id) behaviorTypes.push(ScenesServer);
if (clusterId === OnOff.Cluster.id) behaviorTypes.push(OnOffServer);
if (clusterId === TemperatureMeasurement.Cluster.id) behaviorTypes.push(TemperatureMeasurementServer);
if (clusterId === RelativeHumidityMeasurement.Cluster.id) behaviorTypes.push(RelativeHumidityMeasurementServer);
if (clusterId === BridgedDeviceBasicInformation.Cluster.id) behaviorTypes.push(BridgedDeviceBasicInformationServer);
behaviorTypes.push(MatterbridgeDeviceV8.getBehaviourTypeFromClusterServerId(clusterId));
});
return behaviorTypes;
}
Expand All @@ -227,6 +223,19 @@ export class MatterbridgeDeviceV8 extends Endpoint {
return behaviorTypes;
}

static getBehaviourTypeFromClusterServerId(clusterId: ClusterId) {
// Map ClusterId to Behavior.Type
if (clusterId === Identify.Cluster.id) return IdentifyServer;
if (clusterId === Groups.Cluster.id) return GroupsServer;
if (clusterId === Scenes.Cluster.id) return ScenesServer;
if (clusterId === OnOff.Cluster.id) return OnOffServer;
if (clusterId === TemperatureMeasurement.Cluster.id) return TemperatureMeasurementServer;
if (clusterId === RelativeHumidityMeasurement.Cluster.id) return RelativeHumidityMeasurementServer;
if (clusterId === PressureMeasurement.Cluster.id) return PressureMeasurementServer.with(PressureMeasurement.Feature.Extended);
if (clusterId === BridgedDeviceBasicInformation.Cluster.id) return BridgedDeviceBasicInformationServer;
return IdentifyServer;
}

/**
* Loads an instance of the MatterbridgeDevice class.
*
Expand All @@ -246,6 +255,14 @@ export class MatterbridgeDeviceV8 extends Endpoint {
addDeviceType(deviceType: DeviceTypeDefinition) {
if (!this.deviceTypes.has(deviceType.code)) {
this.log.debug(`addDeviceType: ${zb}${deviceType.code}${db}-${zb}${deviceType.name}${db}`);
/*
this.act((agent) =>
agent.get(DescriptorServer).addDeviceTypes({
deviceType: deviceType.code,
revision: deviceType.revision,
}),
);
*/
this.deviceTypes.set(deviceType.code, deviceType);
}
}
Expand Down Expand Up @@ -321,9 +338,9 @@ export class MatterbridgeDeviceV8 extends Endpoint {
options[(attribute as AttributeServer<A>).name] = (attribute as any).value;
}
}
this.log.debug(`addClusterServer: ${cluster.name} with options:`, options);
const behaviorTypes = MatterbridgeDeviceV8.getBehaviourTypesFromClusterServerIds([cluster.id]);
this.behaviors.require(behaviorTypes[0], options);
this.log.debug(`addClusterServer: ${cluster.name} with options:${rs}\n`, options);
const behavior = MatterbridgeDeviceV8.getBehaviourTypeFromClusterServerId(cluster.id);
this.behaviors.require(behavior, options);
this.clusterServers.set(cluster.id, cluster);
}

Expand Down
18 changes: 16 additions & 2 deletions src/matterbridgeV8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { AggregatorEndpoint } from '@project-chip/matter.js/endpoints/Aggregator
import { BridgedNodeEndpoint } from '@project-chip/matter.js/endpoints/BridgedNodeEndpoint';

// Behaviour servers
import { DescriptorServer, DescriptorBehavior } from '@project-chip/matter.js/behavior/definitions/descriptor';
import { IdentifyServer } from '@project-chip/matter.js/behavior/definitions/identify';
import { OnOffServer } from '@project-chip/matter.js/behavior/definitions/on-off';
import { GroupsServer } from '@project-chip/matter.js/behavior/definitions/groups';
Expand All @@ -70,7 +71,7 @@ import EventEmitter from 'events';
import path from 'path';
import { promises as fs } from 'fs';
import { MatterbridgeDeviceV8 } from './matterbridgeDeviceV8.js';
import { Actions, BasicInformationCluster, Identify, RelativeHumidityMeasurement, SwitchCluster, TemperatureMeasurement } from '@project-chip/matter-node.js/cluster';
import { Actions, BasicInformationCluster, Identify, PressureMeasurement, RelativeHumidityMeasurement, SwitchCluster, TemperatureMeasurement } from '@project-chip/matter-node.js/cluster';
import { bridgedNode, dimmableSwitch } from './matterbridgeDevice.js';
import { RegisteredPlugin } from './matterbridge.js';
import { MatterbridgePlatform, PlatformConfig } from './matterbridgePlatform.js';
Expand Down Expand Up @@ -452,6 +453,18 @@ export class MatterbridgeV8 extends EventEmitter {
reachable: true,
},
});
// await lightEndpoint1.construction;
/*
lightEndpoint1.act((agent) =>
agent.get(DescriptorServer).addDeviceTypes({
deviceType: DeviceTypeId(0x0302),
revision: 2,
}),
);
*/

// logEndpoint(EndpointServer.forEndpoint(lightEndpoint1));

await this.matterAggregator.add(lightEndpoint1);

log.notice(`Adding switchEnpoint2 to ${await storageContext.get<string>('storeId')} aggregator`);
Expand Down Expand Up @@ -485,6 +498,7 @@ export class MatterbridgeV8 extends EventEmitter {
log.notice(`Adding matterbridge device to ${await storageContext.get<string>('storeId')} aggregator`);
const matterbridgeDevice3 = new MatterbridgeDeviceV8(DeviceTypes.TEMPERATURE_SENSOR, { uniqueStorageKey: 'TemperatureSensor' });
matterbridgeDevice3.addDeviceTypeWithClusterServer([DeviceTypes.HUMIDITY_SENSOR], [TemperatureMeasurement.Cluster.id, RelativeHumidityMeasurement.Cluster.id]);
matterbridgeDevice3.addDeviceTypeWithClusterServer([DeviceTypes.PRESSURE_SENSOR], [PressureMeasurement.Cluster.id]);
/*
matterbridgeDevice3.behaviors.require(IdentifyServer, {
identifyTime: 5,
Expand All @@ -511,7 +525,7 @@ export class MatterbridgeV8 extends EventEmitter {

await this.startServerNode(storageContext);

// logEndpoint(EndpointServer.forEndpoint(matterbridgeDevice3));
logEndpoint(EndpointServer.forEndpoint(matterbridgeDevice3));

await lightEndpoint1.set({
onOff: {
Expand Down

0 comments on commit c6dae97

Please sign in to comment.