project-chip / matter.js

A complete typescript implementation of the Matter protocol specification (https://buildwithmatter.com). Includes full support for controller, device, commissioning, secure communications, device types, and cluster definitions.
Apache License 2.0
354 stars 62 forks source link

TemperatureMeasurement cluster definition error #534

Closed antonio-gabriele closed 11 months ago

antonio-gabriele commented 11 months ago

I got this error:

error TS2345: Argument of type 'Definition<{ readonly id: 1026; readonly name: "TemperatureMeasurement"; readonly revision: 4; readonly attributes: { readonly measuredValue: Attribute<number, any>; readonly minMeasuredValue: Attribute<number, any>; readonly maxMeasuredValue: Attribute<...>; readonly tolerance: OptionalAttribute<...>; }; }>' is not assignable to parameter of type 'Cluster<BitSchema, TypeFromPartialBitSchema<BitSchema>, Merge<{ readonly measuredValue: Attribute<number, any>; readonly minMeasuredValue: Attribute<...>; readonly maxMeasuredValue: Attribute<...>; readonly tolerance: OptionalAttribute<...>; }, GlobalAttributes<...>>, Commands, Events>'.
  Types of property 'features' are incompatible.
    Type 'unknown' is not assignable to type 'BitSchema'.
      Index signature for type 'string' is missing in type '{}'.

on this line:

this.addClusterServer(
      ClusterServer(TemperatureMeasurement.Cluster,
        {
          maxMeasuredValue: 100,
          minMeasuredValue: 0,
          measuredValue: 0,
          tolerance: 0          
        },
        WrapCommandHandler(({}), this.commandHandler)));

This line works well:

this.addClusterServer(
      ClusterServer(LevelControl.Cluster,
        {
          currentLevel: 0,
          options: undefined,
          onLevel: 0
        },
        WrapCommandHandler(levelControlClusterHandler, this.commandHandler)));
Apollon77 commented 11 months ago

Hm ... I think i need more from your code to see potential issues, also please check that allyour imports are correct!

I can not reproduce this error locally in my system. I testwise added that line to Onoffdevice ... no error.

antonio-gabriele commented 11 months ago

Definition of TemperatureMeasurement.Cluster is different from LevelControl.Cluster and OnOff.Cluster. in OnOff and LevelControl export const Cluster = ClusterFactory.Extensible( ... in TemperatureMeasurement export const Cluster = ClusterFactory.Definition( ...

ClusterServer method go in error with ClusterFactory.Definition.

Apollon77 commented 11 months ago

Yes and this is because this cluster do not have features, thats all correct ... can you give me a full file please to reproduce this? As said I can not reproduce this typing error you get

antonio-gabriele commented 11 months ago

This is:

import {
  ClusterServer,
  ClusterServerHandlers,
  Identify,
  LevelControl,
  OnOff,
  TemperatureMeasurement,
  createDefaultGroupsClusterServer,
  createDefaultIdentifyClusterServer,
  createDefaultScenesClusterServer
} from "@project-chip/matter-node.js/cluster";
import { extendPublicHandlerMethods } from "@project-chip/matter-node.js/util";
import { Aggregator, Device, DeviceTypeDefinition, OnOffLightDevice, WrapCommandHandler } from "@project-chip/matter-node.js/device";

type BaseDeviceCommands = {
  identify: ClusterServerHandlers<typeof Identify.Cluster>["identify"];
};

export class AbstractionOfDevice extends extendPublicHandlerMethods<typeof Device, BaseDeviceCommands>(Device) {

  public name: string;
  public uniqueId: string;
  constructor(name: string, deviceTypeDefinition: DeviceTypeDefinition, uniqueId: string = null) {
    super(deviceTypeDefinition);
    this.name = name;
    this.uniqueId = uniqueId ?? name.replaceAll(" ", "_").toLocaleLowerCase();;
    this.addClusterServer(
      createDefaultIdentifyClusterServer({
        identify: async data => await this._executeHandler("identify", data),
      }),
    );
    this.addClusterServer(createDefaultGroupsClusterServer());
    this.addClusterServer(createDefaultScenesClusterServer());
  }

  addOnOffServer(onOffClusterHandler: ClusterServerHandlers<typeof OnOff.Base>) {
    this.addClusterServer(
      ClusterServer(OnOff.Cluster,
        {
          onOff: false,
        },
        WrapCommandHandler(onOffClusterHandler, this.commandHandler)));
  }

  addLevelControlServer(levelControlClusterHandler: ClusterServerHandlers<typeof LevelControl.Base>) {
    this.addClusterServer(
      ClusterServer(LevelControl.Cluster,
        {
          currentLevel: 0,
          options: { executeIfOff: true, coupleColorTempToLevel: true },
          onLevel: 0
        },
        WrapCommandHandler(levelControlClusterHandler, this.commandHandler)));
  }

  addTemperatureMeasurementServer() {
    this.addClusterServer(
      ClusterServer(TemperatureMeasurement.Cluster,
        {
          currentLevel: 0,
          options: { executeIfOff: true, coupleColorTempToLevel: true },
          onLevel: 0
        }, this.commandHandler));
  }

  addBridgedDevice(aggregator: Aggregator) {
    aggregator.addBridgedDevice(this, {
      nodeLabel: this.name,
      productName: this.name,
      productLabel: this.name,
      serialNumber: this.uniqueId,
      reachable: true,
    });
  }

}
Apollon77 commented 11 months ago

Ok, there are several typing issues in this code ...

And then I get mainly this issue Bildschirmfoto 2023-11-21 um 12 31 00

If I paste in your TemperatureCluster code from above I get no error there ...

antonio-gabriele commented 11 months ago

tsconfig.json error. FYI correct version is:

{
    "compilerOptions": {
        "composite": true,
        "esModuleInterop": true,
        "incremental": true,
        "target": "es2018",
        "module": "node16",
        "lib": ["ES2021"],
        "moduleResolution": "node16",
        "preserveConstEnums": true,
        "sourceMap": true,
        "declarationMap": true,
        "forceConsistentCasingInFileNames": true,
        "noImplicitAny": true,
        "noImplicitOverride": true,
        "noUnusedParameters": true,
        "noUnusedLocals": true,
        "strict": true,
        "outDir": "../dist/esm",
        "types": ["node"]
    }
}