/** * @license * Copyright 2022-2026 Matter.js Authors * SPDX-License-Identifier: Apache-2.0 */ import { DeviceClassification } from "../common/DeviceClassification.js"; import { EndpointComposition } from "../common/EndpointComposition.js"; import { DeviceTypeElement } from "../elements/index.js"; import { ModelTraversal } from "../logic/ModelTraversal.js"; import { ConditionModel } from "./ConditionModel.js"; import { FieldModel } from "./FieldModel.js"; import { Model } from "./Model.js"; import { RequirementModel } from "./RequirementModel.js"; export class DeviceTypeModel extends Model implements DeviceTypeElement { override tag: DeviceTypeElement.Tag = DeviceTypeElement.Tag; classification?: DeviceClassification; /** * How this device type composes its endpoint's `PartsList`, as this device type declares it. * {@link effectiveComposition} answers what applies, inheritance and default included. */ composition?: EndpointComposition; /** * How this device type composes its endpoint's `PartsList`. * * A device type that declares nothing takes its base's answer, and the tree pattern where no * ancestor declares one either — the specification defines full-family as the exception a device * type opts into (§ 9.2.3). */ get effectiveComposition(): EndpointComposition { let composition: EndpointComposition | undefined; // A single traversal, whose cycle detection then spans the whole inheritance chain: a // definition that derives from itself must be reported rather than followed new ModelTraversal().visitInheritance(this, model => { if (model instanceof DeviceTypeModel && model.composition !== undefined) { composition = model.composition; return false; } }); return composition ?? EndpointComposition.Tree; } get requirements() { return this.all(RequirementModel); } /** * The revision this device type reports, taken from the Descriptor `DeviceTypeList` default it sends over the * wire, or from {@link DeviceTypeElement.revision} for a device type with no such entry. */ get revision(): number { return ( this?.get(RequirementModel, "Descriptor")?.get(RequirementModel, "DeviceTypeList")?.default[0].revision ?? this.#revision ?? 1 ); } /** * The revision this device type states for itself, where it states one. {@link revision} answers what applies; * this distinguishes a device type that records no revision from one that records revision 1. */ get declaredRevision() { return this.#revision; } #revision?: number; constructor(definition: Model.Definition, ...children: Model.ChildDefinition[]) { super(definition, ...children); this.classification = definition.classification as DeviceClassification; this.composition = definition.composition as EndpointComposition; // Reading revision from another model would take the resolved value and store it as a declaration of its own this.#revision = definition instanceof DeviceTypeModel ? definition.declaredRevision : (definition as DeviceTypeElement).revision; } override toElement(omitResources = false, extra?: Record) { return super.toElement(omitResources, { classification: this.classification, composition: this.composition, revision: this.#revision, ...extra, }); } override get id() { return super.id as number; } override set id(id: number) { super.id = id; } static Tag = DeviceTypeElement.Tag; } DeviceTypeModel.register(); export namespace DeviceTypeModel { export type Child = RequirementModel | FieldModel | ConditionModel; }