@matter-server/dashboard 1.4.0 → 1.4.1-alpha.0-20261008-d619c2e
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -13
- package/dist/esm/client/models/descriptions.d.ts.map +1 -1
- package/dist/esm/client/models/descriptions.js +479 -28
- package/dist/esm/client/models/descriptions.js.map +1 -1
- package/dist/esm/components/avsum-ptz-strip.d.ts.map +1 -1
- package/dist/esm/components/avsum-ptz-strip.js +6 -6
- package/dist/esm/components/avsum-ptz-strip.js.map +1 -1
- package/dist/esm/components/semantic-tag-chips.d.ts +16 -0
- package/dist/esm/components/semantic-tag-chips.d.ts.map +1 -0
- package/dist/esm/components/semantic-tag-chips.js +22 -0
- package/dist/esm/components/semantic-tag-chips.js.map +6 -0
- package/dist/esm/pages/cluster-commands/clusters/access-control-commands.d.ts.map +1 -1
- package/dist/esm/pages/cluster-commands/clusters/access-control-commands.js +3 -2
- package/dist/esm/pages/cluster-commands/clusters/access-control-commands.js.map +1 -1
- package/dist/esm/pages/cluster-commands/clusters/avsum-commands.d.ts.map +1 -1
- package/dist/esm/pages/cluster-commands/clusters/avsum-commands.js +34 -34
- package/dist/esm/pages/cluster-commands/clusters/avsum-commands.js.map +1 -1
- package/dist/esm/pages/cluster-commands/clusters/basic-information-commands.d.ts.map +1 -1
- package/dist/esm/pages/cluster-commands/clusters/basic-information-commands.js +2 -1
- package/dist/esm/pages/cluster-commands/clusters/basic-information-commands.js.map +1 -1
- package/dist/esm/pages/cluster-commands/clusters/closure-control-commands.d.ts.map +1 -1
- package/dist/esm/pages/cluster-commands/clusters/closure-control-commands.js +16 -4
- package/dist/esm/pages/cluster-commands/clusters/closure-control-commands.js.map +1 -1
- package/dist/esm/pages/cluster-commands/clusters/closure-dimension-commands.d.ts +41 -0
- package/dist/esm/pages/cluster-commands/clusters/closure-dimension-commands.d.ts.map +1 -0
- package/dist/esm/pages/cluster-commands/clusters/closure-dimension-commands.js +459 -0
- package/dist/esm/pages/cluster-commands/clusters/closure-dimension-commands.js.map +6 -0
- package/dist/esm/pages/cluster-commands/clusters/commodity-tariff-commands.js +1 -1
- package/dist/esm/pages/cluster-commands/clusters/commodity-tariff-commands.js.map +1 -1
- package/dist/esm/pages/cluster-commands/clusters/device-energy-management-commands.d.ts +34 -0
- package/dist/esm/pages/cluster-commands/clusters/device-energy-management-commands.d.ts.map +1 -0
- package/dist/esm/pages/cluster-commands/clusters/device-energy-management-commands.js +324 -0
- package/dist/esm/pages/cluster-commands/clusters/device-energy-management-commands.js.map +6 -0
- package/dist/esm/pages/cluster-commands/clusters/device-energy-management-mode-commands.d.ts +30 -0
- package/dist/esm/pages/cluster-commands/clusters/device-energy-management-mode-commands.d.ts.map +1 -0
- package/dist/esm/pages/cluster-commands/clusters/device-energy-management-mode-commands.js +167 -0
- package/dist/esm/pages/cluster-commands/clusters/device-energy-management-mode-commands.js.map +6 -0
- package/dist/esm/pages/cluster-commands/clusters/door-lock-commands.d.ts +56 -0
- package/dist/esm/pages/cluster-commands/clusters/door-lock-commands.d.ts.map +1 -0
- package/dist/esm/pages/cluster-commands/clusters/door-lock-commands.js +1919 -0
- package/dist/esm/pages/cluster-commands/clusters/door-lock-commands.js.map +6 -0
- package/dist/esm/pages/cluster-commands/clusters/energy-evse-commands.d.ts +58 -0
- package/dist/esm/pages/cluster-commands/clusters/energy-evse-commands.d.ts.map +1 -0
- package/dist/esm/pages/cluster-commands/clusters/energy-evse-commands.js +886 -0
- package/dist/esm/pages/cluster-commands/clusters/energy-evse-commands.js.map +6 -0
- package/dist/esm/pages/cluster-commands/clusters/icd-management-commands.d.ts.map +1 -1
- package/dist/esm/pages/cluster-commands/clusters/icd-management-commands.js +2 -1
- package/dist/esm/pages/cluster-commands/clusters/icd-management-commands.js.map +1 -1
- package/dist/esm/pages/cluster-commands/clusters/ikea-valve-calibration-commands.d.ts +25 -0
- package/dist/esm/pages/cluster-commands/clusters/ikea-valve-calibration-commands.d.ts.map +1 -0
- package/dist/esm/pages/cluster-commands/clusters/ikea-valve-calibration-commands.js +135 -0
- package/dist/esm/pages/cluster-commands/clusters/ikea-valve-calibration-commands.js.map +6 -0
- package/dist/esm/pages/cluster-commands/clusters/media-playback-commands.d.ts +30 -0
- package/dist/esm/pages/cluster-commands/clusters/media-playback-commands.d.ts.map +1 -0
- package/dist/esm/pages/cluster-commands/clusters/media-playback-commands.js +266 -0
- package/dist/esm/pages/cluster-commands/clusters/media-playback-commands.js.map +6 -0
- package/dist/esm/pages/cluster-commands/clusters/operational-state-commands.d.ts +27 -0
- package/dist/esm/pages/cluster-commands/clusters/operational-state-commands.d.ts.map +1 -0
- package/dist/esm/pages/cluster-commands/clusters/operational-state-commands.js +215 -0
- package/dist/esm/pages/cluster-commands/clusters/operational-state-commands.js.map +6 -0
- package/dist/esm/pages/cluster-commands/clusters/service-area-commands.d.ts +47 -0
- package/dist/esm/pages/cluster-commands/clusters/service-area-commands.d.ts.map +1 -0
- package/dist/esm/pages/cluster-commands/clusters/service-area-commands.js +344 -0
- package/dist/esm/pages/cluster-commands/clusters/service-area-commands.js.map +6 -0
- package/dist/esm/pages/cluster-commands/clusters/thermostat-commands.d.ts +24 -2
- package/dist/esm/pages/cluster-commands/clusters/thermostat-commands.d.ts.map +1 -1
- package/dist/esm/pages/cluster-commands/clusters/thermostat-commands.js +368 -6
- package/dist/esm/pages/cluster-commands/clusters/thermostat-commands.js.map +1 -1
- package/dist/esm/pages/cluster-commands/clusters/water-heater-management-commands.d.ts +29 -0
- package/dist/esm/pages/cluster-commands/clusters/water-heater-management-commands.d.ts.map +1 -0
- package/dist/esm/pages/cluster-commands/clusters/water-heater-management-commands.js +262 -0
- package/dist/esm/pages/cluster-commands/clusters/water-heater-management-commands.js.map +6 -0
- package/dist/esm/pages/cluster-commands/index.d.ts +10 -0
- package/dist/esm/pages/cluster-commands/index.d.ts.map +1 -1
- package/dist/esm/pages/cluster-commands/index.js +10 -0
- package/dist/esm/pages/cluster-commands/index.js.map +1 -1
- package/dist/esm/pages/matter-cluster-view.d.ts.map +1 -1
- package/dist/esm/pages/matter-cluster-view.js +6 -24
- package/dist/esm/pages/matter-cluster-view.js.map +1 -1
- package/dist/esm/pages/matter-endpoint-view.d.ts.map +1 -1
- package/dist/esm/pages/matter-endpoint-view.js +20 -2
- package/dist/esm/pages/matter-endpoint-view.js.map +1 -1
- package/dist/esm/pages/matter-network-view.d.ts +16 -0
- package/dist/esm/pages/matter-network-view.d.ts.map +1 -1
- package/dist/esm/pages/matter-network-view.js +55 -6
- package/dist/esm/pages/matter-network-view.js.map +1 -1
- package/dist/esm/pages/matter-node-view.d.ts.map +1 -1
- package/dist/esm/pages/matter-node-view.js +33 -4
- package/dist/esm/pages/matter-node-view.js.map +1 -1
- package/dist/esm/pages/network/border-router-store.d.ts +19 -0
- package/dist/esm/pages/network/border-router-store.d.ts.map +1 -1
- package/dist/esm/pages/network/border-router-store.js +61 -3
- package/dist/esm/pages/network/border-router-store.js.map +1 -1
- package/dist/esm/pages/network/network-details.d.ts +0 -3
- package/dist/esm/pages/network/network-details.d.ts.map +1 -1
- package/dist/esm/pages/network/network-details.js +15 -22
- package/dist/esm/pages/network/network-details.js.map +1 -1
- package/dist/esm/pages/network/network-utils.d.ts +4 -6
- package/dist/esm/pages/network/network-utils.d.ts.map +1 -1
- package/dist/esm/pages/network/network-utils.js +19 -13
- package/dist/esm/pages/network/network-utils.js.map +1 -1
- package/dist/esm/pages/network/thread-graph.d.ts +0 -5
- package/dist/esm/pages/network/thread-graph.d.ts.map +1 -1
- package/dist/esm/pages/network/thread-graph.js +21 -38
- package/dist/esm/pages/network/thread-graph.js.map +1 -1
- package/dist/esm/util/closure-control.d.ts +1 -0
- package/dist/esm/util/closure-control.d.ts.map +1 -1
- package/dist/esm/util/closure-control.js +5 -0
- package/dist/esm/util/closure-control.js.map +1 -1
- package/dist/esm/util/closure-dimension.d.ts +83 -0
- package/dist/esm/util/closure-dimension.d.ts.map +1 -0
- package/dist/esm/util/closure-dimension.js +238 -0
- package/dist/esm/util/closure-dimension.js.map +6 -0
- package/dist/esm/util/commodity-tariff.d.ts +1 -2
- package/dist/esm/util/commodity-tariff.d.ts.map +1 -1
- package/dist/esm/util/commodity-tariff.js +9 -17
- package/dist/esm/util/commodity-tariff.js.map +1 -1
- package/dist/esm/util/device-energy-management-mode.d.ts +35 -0
- package/dist/esm/util/device-energy-management-mode.d.ts.map +1 -0
- package/dist/esm/util/device-energy-management-mode.js +80 -0
- package/dist/esm/util/device-energy-management-mode.js.map +6 -0
- package/dist/esm/util/device-energy-management.d.ts +90 -0
- package/dist/esm/util/device-energy-management.d.ts.map +1 -0
- package/dist/esm/util/device-energy-management.js +230 -0
- package/dist/esm/util/device-energy-management.js.map +6 -0
- package/dist/esm/util/device-icons.d.ts.map +1 -1
- package/dist/esm/util/device-icons.js +3 -3
- package/dist/esm/util/device-icons.js.map +1 -1
- package/dist/esm/util/door-lock.d.ts +249 -0
- package/dist/esm/util/door-lock.d.ts.map +1 -0
- package/dist/esm/util/door-lock.js +724 -0
- package/dist/esm/util/door-lock.js.map +6 -0
- package/dist/esm/util/endpoint-label.d.ts +13 -0
- package/dist/esm/util/endpoint-label.d.ts.map +1 -0
- package/dist/esm/util/endpoint-label.js +41 -0
- package/dist/esm/util/endpoint-label.js.map +6 -0
- package/dist/esm/util/energy-evse.d.ts +116 -0
- package/dist/esm/util/energy-evse.d.ts.map +1 -0
- package/dist/esm/util/energy-evse.js +308 -0
- package/dist/esm/util/energy-evse.js.map +6 -0
- package/dist/esm/util/enum-label.d.ts +24 -0
- package/dist/esm/util/enum-label.d.ts.map +1 -0
- package/dist/esm/util/enum-label.js +24 -0
- package/dist/esm/util/enum-label.js.map +6 -0
- package/dist/esm/util/error-text.d.ts +8 -0
- package/dist/esm/util/error-text.d.ts.map +1 -0
- package/dist/esm/util/error-text.js +12 -0
- package/dist/esm/util/error-text.js.map +6 -0
- package/dist/esm/util/ikea-valve-calibration.d.ts +21 -0
- package/dist/esm/util/ikea-valve-calibration.d.ts.map +1 -0
- package/dist/esm/util/ikea-valve-calibration.js +43 -0
- package/dist/esm/util/ikea-valve-calibration.js.map +6 -0
- package/dist/esm/util/media-playback.d.ts +47 -0
- package/dist/esm/util/media-playback.d.ts.map +1 -0
- package/dist/esm/util/media-playback.js +119 -0
- package/dist/esm/util/media-playback.js.map +6 -0
- package/dist/esm/util/operational-state.d.ts +102 -0
- package/dist/esm/util/operational-state.d.ts.map +1 -0
- package/dist/esm/util/operational-state.js +212 -0
- package/dist/esm/util/operational-state.js.map +6 -0
- package/dist/esm/util/semantic-tags.d.ts +3 -0
- package/dist/esm/util/semantic-tags.d.ts.map +1 -1
- package/dist/esm/util/semantic-tags.js +5 -1
- package/dist/esm/util/semantic-tags.js.map +1 -1
- package/dist/esm/util/service-area.d.ts +88 -0
- package/dist/esm/util/service-area.d.ts.map +1 -0
- package/dist/esm/util/service-area.js +185 -0
- package/dist/esm/util/service-area.js.map +6 -0
- package/dist/esm/util/shared-styles.d.ts +1 -0
- package/dist/esm/util/shared-styles.d.ts.map +1 -1
- package/dist/esm/util/shared-styles.js +37 -17
- package/dist/esm/util/shared-styles.js.map +1 -1
- package/dist/esm/util/thermostat-schedule.d.ts +10 -4
- package/dist/esm/util/thermostat-schedule.d.ts.map +1 -1
- package/dist/esm/util/thermostat-schedule.js +34 -10
- package/dist/esm/util/thermostat-schedule.js.map +1 -1
- package/dist/esm/util/thermostat-suggestions.d.ts +31 -0
- package/dist/esm/util/thermostat-suggestions.d.ts.map +1 -0
- package/dist/esm/util/thermostat-suggestions.js +74 -0
- package/dist/esm/util/thermostat-suggestions.js.map +6 -0
- package/dist/esm/util/time.d.ts +24 -0
- package/dist/esm/util/time.d.ts.map +1 -0
- package/dist/esm/util/time.js +42 -0
- package/dist/esm/util/time.js.map +6 -0
- package/dist/esm/util/water-heater-management.d.ts +56 -0
- package/dist/esm/util/water-heater-management.d.ts.map +1 -0
- package/dist/esm/util/water-heater-management.js +147 -0
- package/dist/esm/util/water-heater-management.js.map +6 -0
- package/dist/web/index.html +4 -0
- package/dist/web/js/{attribute-write-dialog-Dj2vnLR_.js → attribute-write-dialog-DrJZQaXy.js} +1 -1
- package/dist/web/js/{command-invoke-dialog-DJY2HePu.js → command-invoke-dialog-D_zf390T.js} +1 -1
- package/dist/web/js/{commission-node-dialog-BAzJcdYg.js → commission-node-dialog-DIBv_ihK.js} +5 -5
- package/dist/web/js/{commission-node-existing-DDtYs7R-.js → commission-node-existing-ApIugQzz.js} +1 -1
- package/dist/web/js/{commission-node-thread-Bf0XhUqn.js → commission-node-thread-DdEySLrf.js} +1 -1
- package/dist/web/js/{commission-node-wifi-DL4q7kzh.js → commission-node-wifi-BIhsmTCV.js} +1 -1
- package/dist/web/js/{dialog-box-BquhCceT.js → dialog-box-Df4y4onO.js} +1 -1
- package/dist/web/js/{fabric-label-dialog-iLm9DGea.js → fabric-label-dialog-BFs_iHTp.js} +1 -1
- package/dist/web/js/main.js +61 -28
- package/dist/web/js/{matter-dashboard-app-RcyNZLl_.js → matter-dashboard-app-DfcntGqC.js} +17911 -10233
- package/dist/web/js/{node-acl-add-dialog-CyWYXa6M.js → node-acl-add-dialog-DPG7FkNs.js} +1 -1
- package/dist/web/js/{node-binding-dialog-BjQ0KYzx.js → node-binding-dialog-CvQuClGz.js} +1 -1
- package/dist/web/js/{node-label-dialog-BNUn3Joo.js → node-label-dialog-DNEWL6Ku.js} +1 -1
- package/dist/web/js/{settings-dialog-DvAq6gKo.js → settings-dialog-BvAdX2Jo.js} +1 -1
- package/package.json +7 -7
- package/src/client/models/descriptions.ts +479 -28
- package/src/components/avsum-ptz-strip.ts +10 -9
- package/src/components/semantic-tag-chips.ts +30 -0
- package/src/pages/cluster-commands/AGENTS.md +1 -0
- package/src/pages/cluster-commands/clusters/access-control-commands.ts +3 -2
- package/src/pages/cluster-commands/clusters/avsum-commands.ts +44 -42
- package/src/pages/cluster-commands/clusters/basic-information-commands.ts +2 -1
- package/src/pages/cluster-commands/clusters/closure-control-commands.ts +16 -4
- package/src/pages/cluster-commands/clusters/closure-dimension-commands.ts +512 -0
- package/src/pages/cluster-commands/clusters/commodity-tariff-commands.ts +1 -1
- package/src/pages/cluster-commands/clusters/device-energy-management-commands.ts +426 -0
- package/src/pages/cluster-commands/clusters/device-energy-management-mode-commands.ts +172 -0
- package/src/pages/cluster-commands/clusters/door-lock-commands.ts +2120 -0
- package/src/pages/cluster-commands/clusters/energy-evse-commands.ts +1048 -0
- package/src/pages/cluster-commands/clusters/icd-management-commands.ts +2 -1
- package/src/pages/cluster-commands/clusters/ikea-valve-calibration-commands.ts +129 -0
- package/src/pages/cluster-commands/clusters/media-playback-commands.ts +313 -0
- package/src/pages/cluster-commands/clusters/operational-state-commands.ts +234 -0
- package/src/pages/cluster-commands/clusters/service-area-commands.ts +395 -0
- package/src/pages/cluster-commands/clusters/thermostat-commands.ts +418 -7
- package/src/pages/cluster-commands/clusters/water-heater-management-commands.ts +275 -0
- package/src/pages/cluster-commands/index.ts +10 -0
- package/src/pages/matter-cluster-view.ts +6 -28
- package/src/pages/matter-endpoint-view.ts +24 -2
- package/src/pages/matter-network-view.ts +67 -6
- package/src/pages/matter-node-view.ts +35 -4
- package/src/pages/network/border-router-store.ts +77 -2
- package/src/pages/network/network-details.ts +15 -26
- package/src/pages/network/network-utils.ts +15 -15
- package/src/pages/network/thread-graph.ts +32 -48
- package/src/util/closure-control.ts +5 -0
- package/src/util/closure-dimension.ts +307 -0
- package/src/util/commodity-tariff.ts +9 -23
- package/src/util/device-energy-management-mode.ts +132 -0
- package/src/util/device-energy-management.ts +378 -0
- package/src/util/device-icons.ts +4 -6
- package/src/util/door-lock.ts +1076 -0
- package/src/util/endpoint-label.ts +70 -0
- package/src/util/energy-evse.ts +472 -0
- package/src/util/enum-label.ts +39 -0
- package/src/util/error-text.ts +10 -0
- package/src/util/ikea-valve-calibration.ts +54 -0
- package/src/util/media-playback.ts +132 -0
- package/src/util/operational-state.ts +251 -0
- package/src/util/semantic-tags.ts +6 -0
- package/src/util/service-area.ts +313 -0
- package/src/util/shared-styles.ts +37 -17
- package/src/util/thermostat-schedule.ts +39 -11
- package/src/util/thermostat-suggestions.ts +93 -0
- package/src/util/time.ts +54 -0
- package/src/util/water-heater-management.ts +217 -0
|
@@ -0,0 +1,1076 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025-2026 Open Home Foundation
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { MatterClient, MatterNode } from "@matter-server/ws-client";
|
|
8
|
+
import { clusters } from "../client/models/descriptions.js";
|
|
9
|
+
import { asObject, pickArray, pickNumber, pickString } from "./attribute-shapes.js";
|
|
10
|
+
import { computeActiveClusterFeatures } from "./cluster-features.js";
|
|
11
|
+
import { getMatterStatusName, requireAttributeWriteSuccess } from "./matter-status.js";
|
|
12
|
+
import { MATTER_EPOCH_MAX_SECONDS, MATTER_EPOCH_OFFSET_SECONDS } from "./time.js";
|
|
13
|
+
|
|
14
|
+
/** Door Lock cluster (Matter spec §5.2). */
|
|
15
|
+
export const DOOR_LOCK_CLUSTER_ID = 257; // 0x0101
|
|
16
|
+
|
|
17
|
+
const ATTR_LOCK_STATE = 0x00;
|
|
18
|
+
const ATTR_LOCK_TYPE = 0x01;
|
|
19
|
+
const ATTR_ACTUATOR_ENABLED = 0x02;
|
|
20
|
+
const ATTR_DOOR_STATE = 0x03;
|
|
21
|
+
const ATTR_NUMBER_OF_TOTAL_USERS_SUPPORTED = 0x11;
|
|
22
|
+
const ATTR_NUMBER_OF_PIN_USERS_SUPPORTED = 0x12;
|
|
23
|
+
const ATTR_NUMBER_OF_WEEK_DAY_SCHEDULES_SUPPORTED_PER_USER = 0x14;
|
|
24
|
+
const ATTR_NUMBER_OF_YEAR_DAY_SCHEDULES_SUPPORTED_PER_USER = 0x15;
|
|
25
|
+
const ATTR_NUMBER_OF_HOLIDAY_SCHEDULES_SUPPORTED = 0x16;
|
|
26
|
+
const ATTR_MAX_PIN_CODE_LENGTH = 0x17;
|
|
27
|
+
const ATTR_MIN_PIN_CODE_LENGTH = 0x18;
|
|
28
|
+
const ATTR_SUPPORTED_OPERATING_MODES = 0x26;
|
|
29
|
+
const ATTR_REQUIRE_PIN_FOR_REMOTE_OPERATION = 0x33;
|
|
30
|
+
const ATTR_EXPIRING_USER_TIMEOUT = 0x35;
|
|
31
|
+
const ATTR_ACCEPTED_COMMAND_LIST = 0xfff9;
|
|
32
|
+
const ATTR_FEATURE_MAP = 0xfffc;
|
|
33
|
+
|
|
34
|
+
/** UnlockWithTimeout is optional even on locks that support unlocking (spec §5.2.10.3). */
|
|
35
|
+
export const UNLOCK_WITH_TIMEOUT_COMMAND_ID = 0x03;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Wipes every slot of the addressed table: a user's week day or year day schedules, or — since the
|
|
39
|
+
* holiday table is lock-wide — every holiday schedule on the lock.
|
|
40
|
+
*/
|
|
41
|
+
export const SCHEDULE_INDEX_ALL = 0xfe;
|
|
42
|
+
|
|
43
|
+
/** UserStatusEnum.Available — a slot the lock reports as free (spec §5.2.6.17). */
|
|
44
|
+
const USER_STATUS_AVAILABLE = 0;
|
|
45
|
+
|
|
46
|
+
/** DataOperationTypeEnum.Add — the SetUser/SetCredential operation that creates a new entry (spec §5.2.6.10). */
|
|
47
|
+
const OPERATION_TYPE_ADD = 0;
|
|
48
|
+
|
|
49
|
+
/** UserTypeEnum.ExpiringUser — access expires ExpiringUserTimeout minutes after the credential's first use. */
|
|
50
|
+
export const USER_TYPE_EXPIRING = 7;
|
|
51
|
+
|
|
52
|
+
/** UserStatusEnum.OccupiedEnabled — the status a newly created, active user is given (spec §5.2.6.17). */
|
|
53
|
+
export const USER_STATUS_OCCUPIED_ENABLED = 1;
|
|
54
|
+
|
|
55
|
+
/** CredentialTypeEnum.PIN — the only credential type this panel manages (spec §5.2.6.9). */
|
|
56
|
+
const CREDENTIAL_TYPE_PIN = 1;
|
|
57
|
+
|
|
58
|
+
/** SetUser's UserName constraint (spec §5.2.10.34.3), enforced here instead of round-tripping it. */
|
|
59
|
+
export const USER_NAME_MAX_LENGTH = 10;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Why the lock will reject this PIN, or null when it will accept it. `minLength`/`maxLength` are the lock's
|
|
63
|
+
* own MinPinCodeLength/MaxPinCodeLength, unknown on a lock that doesn't report them — in that case only
|
|
64
|
+
* non-emptiness is checked, same fallback as the schedule capacity attributes elsewhere in this file.
|
|
65
|
+
*/
|
|
66
|
+
export function pinCodeLengthError(pin: string, minLength: number | null, maxLength: number | null): string | null {
|
|
67
|
+
if (pin === "") return "Enter a PIN.";
|
|
68
|
+
const length = new TextEncoder().encode(pin).length;
|
|
69
|
+
if (minLength !== null && length < minLength) return `The PIN must be at least ${minLength} bytes.`;
|
|
70
|
+
if (maxLength !== null && length > maxLength) return `The PIN must be at most ${maxLength} bytes.`;
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Why the lock will reject this user name, or null when it will accept it. The constraint counts UTF-8
|
|
76
|
+
* bytes, so a name well inside the character limit can still be too long.
|
|
77
|
+
*/
|
|
78
|
+
export function userNameLengthError(userName: string): string | null {
|
|
79
|
+
if (userName === "") return "Enter a name for the user.";
|
|
80
|
+
if (new TextEncoder().encode(userName).length > USER_NAME_MAX_LENGTH) {
|
|
81
|
+
return `The name may be at most ${USER_NAME_MAX_LENGTH} bytes; accented and non-Latin characters take more than one.`;
|
|
82
|
+
}
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Days in display order (Mon..Sun) mapped to their DaysMaskBitmap bit (Sunday=0 .. Saturday=6). */
|
|
87
|
+
export const DAY_BITS = [1, 2, 3, 4, 5, 6, 0];
|
|
88
|
+
export const DAY_LABELS = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
|
|
89
|
+
|
|
90
|
+
const WEEKDAYS_MASK = 0b0111110;
|
|
91
|
+
const WEEKEND_MASK = 0b1000001;
|
|
92
|
+
const ALL_DAYS_MASK = 0b1111111;
|
|
93
|
+
|
|
94
|
+
const LOCK_STATE_LABELS: Record<number, string> = {
|
|
95
|
+
0: "Not Fully Locked",
|
|
96
|
+
1: "Locked",
|
|
97
|
+
2: "Unlocked",
|
|
98
|
+
3: "Unlatched",
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const LOCK_TYPE_LABELS: Record<number, string> = {
|
|
102
|
+
0: "Dead Bolt",
|
|
103
|
+
1: "Magnetic",
|
|
104
|
+
2: "Other",
|
|
105
|
+
3: "Mortise",
|
|
106
|
+
4: "Rim",
|
|
107
|
+
5: "Latch Bolt",
|
|
108
|
+
6: "Cylindrical Lock",
|
|
109
|
+
7: "Tubular Lock",
|
|
110
|
+
8: "Interconnected Lock",
|
|
111
|
+
9: "Dead Latch",
|
|
112
|
+
10: "Door Furniture",
|
|
113
|
+
11: "Eurocylinder",
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const DOOR_STATE_LABELS: Record<number, string> = {
|
|
117
|
+
0: "Open",
|
|
118
|
+
1: "Closed",
|
|
119
|
+
2: "Jammed",
|
|
120
|
+
3: "Forced Open",
|
|
121
|
+
4: "Unspecified Error",
|
|
122
|
+
5: "Ajar",
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const USER_STATUS_LABELS: Record<number, string> = {
|
|
126
|
+
0: "Available",
|
|
127
|
+
1: "Enabled",
|
|
128
|
+
3: "Disabled",
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
/** OperatingModeEnum values a Holiday schedule may switch the lock to (spec §5.2.6.15). */
|
|
132
|
+
const OPERATING_MODE_LABELS: Record<number, string> = {
|
|
133
|
+
0: "Normal",
|
|
134
|
+
1: "Vacation",
|
|
135
|
+
2: "Privacy",
|
|
136
|
+
3: "No Remote Lock/Unlock",
|
|
137
|
+
4: "Passage",
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const OPERATING_MODES = [0, 1, 2, 3, 4];
|
|
141
|
+
|
|
142
|
+
/** OperatingModeEnum.Normal, the only mode every lock must implement. */
|
|
143
|
+
const OPERATING_MODE_NORMAL = 0;
|
|
144
|
+
|
|
145
|
+
/** OperatingModeEnum.Vacation, what a holiday schedule is normally for. */
|
|
146
|
+
const OPERATING_MODE_VACATION = 1;
|
|
147
|
+
|
|
148
|
+
const USER_TYPE_LABELS: Record<number, string> = {
|
|
149
|
+
0: "Unrestricted",
|
|
150
|
+
1: "Year Day Schedule",
|
|
151
|
+
2: "Week Day Schedule",
|
|
152
|
+
3: "Programming",
|
|
153
|
+
4: "No Access",
|
|
154
|
+
5: "Forced",
|
|
155
|
+
6: "Disposable",
|
|
156
|
+
7: "Expiring",
|
|
157
|
+
8: "Schedule Restricted",
|
|
158
|
+
9: "Remote Only",
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
/** The interaction-model status of a slot the lock holds nothing in (spec §5.2.10.6.3). */
|
|
162
|
+
export const SCHEDULE_STATUS_NOT_FOUND = 0x8b;
|
|
163
|
+
|
|
164
|
+
/** A Week Day schedule slot holding a window, as reported by GetWeekDayScheduleResponse. */
|
|
165
|
+
export interface WeekDaySchedule {
|
|
166
|
+
weekDayIndex: number;
|
|
167
|
+
daysMask: number;
|
|
168
|
+
startHour: number;
|
|
169
|
+
startMinute: number;
|
|
170
|
+
endHour: number;
|
|
171
|
+
endMinute: number;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* One of a user's schedule slots. `schedule` is null when the lock reported a non-success status or
|
|
176
|
+
* omitted the window.
|
|
177
|
+
*
|
|
178
|
+
* `status` is null when the lock did not say what the slot holds: either no status came back, or it
|
|
179
|
+
* answered SUCCESS without the fields that must accompany it ("If this field is SUCCESS, the optional
|
|
180
|
+
* fields for this command shall be present", spec §5.2.10.6.3). Such a slot reads as unread, and is
|
|
181
|
+
* never collapsed away among the empty ones.
|
|
182
|
+
*/
|
|
183
|
+
export interface WeekDayScheduleSlot {
|
|
184
|
+
weekDayIndex: number;
|
|
185
|
+
status: number | null;
|
|
186
|
+
schedule: WeekDaySchedule | null;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* A Year Day schedule slot holding a fixed date range. Both bounds are Matter epoch-s (seconds since
|
|
191
|
+
* 2000-01-01) encoding the lock's *local wall clock* rather than a UTC instant, per the spec's "Epoch
|
|
192
|
+
* Time in Seconds with local time offset" — so they are neither Unix seconds nor a viewer-relative time.
|
|
193
|
+
*/
|
|
194
|
+
export interface YearDaySchedule {
|
|
195
|
+
yearDayIndex: number;
|
|
196
|
+
localStartTime: number;
|
|
197
|
+
localEndTime: number;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export interface YearDayScheduleSlot {
|
|
201
|
+
yearDayIndex: number;
|
|
202
|
+
status: number | null;
|
|
203
|
+
schedule: YearDaySchedule | null;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* A Holiday schedule slot: unlike WDSCH/YDSCH, holidays are not scoped to a user — they apply to the whole
|
|
208
|
+
* lock — and switch it to `operatingMode` for the duration of the date range.
|
|
209
|
+
*/
|
|
210
|
+
export interface HolidaySchedule {
|
|
211
|
+
holidayIndex: number;
|
|
212
|
+
localStartTime: number;
|
|
213
|
+
localEndTime: number;
|
|
214
|
+
operatingMode: number;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export interface HolidayScheduleSlot {
|
|
218
|
+
holidayIndex: number;
|
|
219
|
+
status: number | null;
|
|
220
|
+
schedule: HolidaySchedule | null;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export interface UserCredential {
|
|
224
|
+
credentialType: number;
|
|
225
|
+
credentialIndex: number;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export interface DoorLockUser {
|
|
229
|
+
userIndex: number;
|
|
230
|
+
userName: string | null;
|
|
231
|
+
userStatus: number | null;
|
|
232
|
+
userType: number | null;
|
|
233
|
+
nextUserIndex: number | null;
|
|
234
|
+
occupied: boolean;
|
|
235
|
+
credentials: UserCredential[];
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** A schedule window projected onto one display day, in minutes from midnight. */
|
|
239
|
+
export interface DayScheduleSegment {
|
|
240
|
+
weekDayIndex: number;
|
|
241
|
+
startMin: number;
|
|
242
|
+
endMin: number;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function readAttr(node: MatterNode, endpoint: number, attrId: number): unknown {
|
|
246
|
+
return node.attributes[`${endpoint}/${DOOR_LOCK_CLUSTER_ID}/${attrId}`];
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function readNumberAttr(node: MatterNode, endpoint: number, attrId: number): number | null {
|
|
250
|
+
const value = readAttr(node, endpoint, attrId);
|
|
251
|
+
return typeof value === "number" ? value : null;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** Whether the Door Lock cluster's FeatureMap includes the feature with the given code (e.g. "WDSCH", "USR"). */
|
|
255
|
+
export function isFeatureActive(node: MatterNode, endpoint: number, code: string): boolean {
|
|
256
|
+
const knownFeatures = Object.values(clusters[DOOR_LOCK_CLUSTER_ID]?.features ?? {});
|
|
257
|
+
const featureMapValue = readAttr(node, endpoint, ATTR_FEATURE_MAP);
|
|
258
|
+
if (featureMapValue === undefined) return false;
|
|
259
|
+
return computeActiveClusterFeatures(featureMapValue, knownFeatures).some(feature => feature.code === code);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export function supportsCommand(node: MatterNode, endpoint: number, commandId: number): boolean {
|
|
263
|
+
const accepted = readAttr(node, endpoint, ATTR_ACCEPTED_COMMAND_LIST);
|
|
264
|
+
return Array.isArray(accepted) && accepted.map(value => Number(value)).includes(commandId);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export function readLockState(node: MatterNode, endpoint: number): number | null {
|
|
268
|
+
return readNumberAttr(node, endpoint, ATTR_LOCK_STATE);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export function readLockType(node: MatterNode, endpoint: number): number | null {
|
|
272
|
+
return readNumberAttr(node, endpoint, ATTR_LOCK_TYPE);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export function readDoorState(node: MatterNode, endpoint: number): number | null {
|
|
276
|
+
return readNumberAttr(node, endpoint, ATTR_DOOR_STATE);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export function readActuatorEnabled(node: MatterNode, endpoint: number): boolean | null {
|
|
280
|
+
const value = readAttr(node, endpoint, ATTR_ACTUATOR_ENABLED);
|
|
281
|
+
return typeof value === "boolean" ? value : null;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export function readTotalUsersSupported(node: MatterNode, endpoint: number): number | null {
|
|
285
|
+
return readNumberAttr(node, endpoint, ATTR_NUMBER_OF_TOTAL_USERS_SUPPORTED);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export function readWeekDaySchedulesPerUser(node: MatterNode, endpoint: number): number | null {
|
|
289
|
+
return readNumberAttr(node, endpoint, ATTR_NUMBER_OF_WEEK_DAY_SCHEDULES_SUPPORTED_PER_USER);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export function readYearDaySchedulesPerUser(node: MatterNode, endpoint: number): number | null {
|
|
293
|
+
return readNumberAttr(node, endpoint, ATTR_NUMBER_OF_YEAR_DAY_SCHEDULES_SUPPORTED_PER_USER);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** Unlike WDSCH/YDSCH, HolidaySchedules is a lock-wide table: it is not scoped to a user. */
|
|
297
|
+
export function readHolidaySchedulesSupported(node: MatterNode, endpoint: number): number | null {
|
|
298
|
+
return readNumberAttr(node, endpoint, ATTR_NUMBER_OF_HOLIDAY_SCHEDULES_SUPPORTED);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
export function readNumberOfPinUsersSupported(node: MatterNode, endpoint: number): number | null {
|
|
302
|
+
return readNumberAttr(node, endpoint, ATTR_NUMBER_OF_PIN_USERS_SUPPORTED);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
export function readMinPinCodeLength(node: MatterNode, endpoint: number): number | null {
|
|
306
|
+
return readNumberAttr(node, endpoint, ATTR_MIN_PIN_CODE_LENGTH);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
export function readMaxPinCodeLength(node: MatterNode, endpoint: number): number | null {
|
|
310
|
+
return readNumberAttr(node, endpoint, ATTR_MAX_PIN_CODE_LENGTH);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Minutes an ExpiringUser's credential remains valid after its first use, lock-wide rather than per-user
|
|
315
|
+
* (spec §5.2.9.36). Absent on a lock that doesn't support ExpiringUser at all.
|
|
316
|
+
*/
|
|
317
|
+
export function readExpiringUserTimeout(node: MatterNode, endpoint: number): number | null {
|
|
318
|
+
return readNumberAttr(node, endpoint, ATTR_EXPIRING_USER_TIMEOUT);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export async function writeExpiringUserTimeout(
|
|
322
|
+
client: MatterClient,
|
|
323
|
+
nodeId: number | bigint,
|
|
324
|
+
endpoint: number,
|
|
325
|
+
minutes: number,
|
|
326
|
+
): Promise<void> {
|
|
327
|
+
requireAttributeWriteSuccess(
|
|
328
|
+
await client.writeAttribute(
|
|
329
|
+
nodeId,
|
|
330
|
+
`${endpoint}/${DOOR_LOCK_CLUSTER_ID}/${ATTR_EXPIRING_USER_TIMEOUT}`,
|
|
331
|
+
minutes,
|
|
332
|
+
),
|
|
333
|
+
"Writing the Expiring User Timeout attribute failed",
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
export function requiresPinForRemoteOperation(node: MatterNode, endpoint: number): boolean {
|
|
338
|
+
return readAttr(node, endpoint, ATTR_REQUIRE_PIN_FOR_REMOTE_OPERATION) === true;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* The operating modes a Holiday schedule may switch this lock to. OperatingModesBitmap is inverted —
|
|
343
|
+
* a bit set to zero means the mode IS supported, which the spec itself calls out as "the opposite of
|
|
344
|
+
* most other semantically similar bitmaps" (spec §5.2.9.25).
|
|
345
|
+
*/
|
|
346
|
+
export function supportedOperatingModes(node: MatterNode, endpoint: number): number[] {
|
|
347
|
+
const bitmap = readNumberAttr(node, endpoint, ATTR_SUPPORTED_OPERATING_MODES);
|
|
348
|
+
if (bitmap === null) return [...OPERATING_MODES];
|
|
349
|
+
const supported = OPERATING_MODES.filter(mode => (bitmap & (1 << mode)) === 0);
|
|
350
|
+
return supported.length > 0 ? supported : [...OPERATING_MODES];
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
export function formatLockState(state: number | null): string {
|
|
354
|
+
if (state === null) return "Unknown";
|
|
355
|
+
return LOCK_STATE_LABELS[state] ?? `State ${state}`;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
export function formatLockType(type: number | null): string | null {
|
|
359
|
+
if (type === null) return null;
|
|
360
|
+
return LOCK_TYPE_LABELS[type] ?? `Type ${type}`;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
export function formatDoorState(state: number | null): string | null {
|
|
364
|
+
if (state === null) return null;
|
|
365
|
+
return DOOR_STATE_LABELS[state] ?? `State ${state}`;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
export function formatOperatingMode(mode: number | null): string | null {
|
|
369
|
+
if (mode === null) return null;
|
|
370
|
+
return OPERATING_MODE_LABELS[mode] ?? `Mode ${mode}`;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* The modes a holiday mode picker offers. A mode the lock already stored is always among them even when
|
|
375
|
+
* SupportedOperatingModes no longer advertises it: an option list that cannot represent the current value
|
|
376
|
+
* leaves a `<select>` showing its first entry while a save still writes the old one.
|
|
377
|
+
*/
|
|
378
|
+
export function holidayModeChoices(supported: number[], current: number): number[] {
|
|
379
|
+
return supported.includes(current) ? supported : [current, ...supported];
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
export function defaultHolidayMode(supported: number[]): number {
|
|
383
|
+
if (supported.includes(OPERATING_MODE_VACATION)) return OPERATING_MODE_VACATION;
|
|
384
|
+
return supported[0] ?? OPERATING_MODE_NORMAL;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
export function formatUserStatus(status: number | null): string | null {
|
|
388
|
+
if (status === null) return null;
|
|
389
|
+
return USER_STATUS_LABELS[status] ?? `Status ${status}`;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
export function formatUserType(type: number | null): string | null {
|
|
393
|
+
if (type === null) return null;
|
|
394
|
+
return USER_TYPE_LABELS[type] ?? `Type ${type}`;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/** Whether the lock answered for this slot and reported it as holding nothing. */
|
|
398
|
+
export function isEmptyScheduleStatus(status: number | null): boolean {
|
|
399
|
+
return status === 0 || status === SCHEDULE_STATUS_NOT_FOUND;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
export function formatScheduleStatus(status: number | null): string {
|
|
403
|
+
if (status === null) return "Unreadable";
|
|
404
|
+
if (isEmptyScheduleStatus(status)) return "Empty";
|
|
405
|
+
return getMatterStatusName(status);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/** A user's display name: its own UserName when set, else its index. */
|
|
409
|
+
export function formatUserLabel(user: DoorLockUser): string {
|
|
410
|
+
return user.userName ?? `User ${user.userIndex}`;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
export function maskHasDay(mask: number, bit: number): boolean {
|
|
414
|
+
return (mask & (1 << bit)) !== 0;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
export function toggleMaskDay(mask: number, bit: number): number {
|
|
418
|
+
return mask ^ (1 << bit);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
export function formatDaysMask(mask: number): string {
|
|
422
|
+
const days = mask & ALL_DAYS_MASK;
|
|
423
|
+
if (days === 0) return "No day";
|
|
424
|
+
if (days === ALL_DAYS_MASK) return "Every day";
|
|
425
|
+
if (days === WEEKDAYS_MASK) return "Mon–Fri";
|
|
426
|
+
if (days === WEEKEND_MASK) return "Sat–Sun";
|
|
427
|
+
return DAY_LABELS.filter((_, index) => maskHasDay(days, DAY_BITS[index])).join(", ");
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
export function formatTimeOfDay(hour: number, minute: number): string {
|
|
431
|
+
return `${String(hour).padStart(2, "0")}:${String(minute).padStart(2, "0")}`;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/** Accepts the `HH:MM:SS` an `<input type="time">` emits at second precision; the cluster has no seconds. */
|
|
435
|
+
export function parseTimeOfDay(value: string): { hour: number; minute: number } | null {
|
|
436
|
+
const match = /^(\d{1,2}):(\d{2})(?::\d{2})?$/.exec(value.trim());
|
|
437
|
+
if (!match) return null;
|
|
438
|
+
const hour = Number(match[1]);
|
|
439
|
+
const minute = Number(match[2]);
|
|
440
|
+
if (hour > 23 || minute > 59) return null;
|
|
441
|
+
return { hour, minute };
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Why the schedule cannot be set as entered, or null when it is valid.
|
|
446
|
+
* A window may not span midnight: the spec constrains EndHour/EndMinute to follow the start within the day.
|
|
447
|
+
*/
|
|
448
|
+
export function weekDayScheduleRangeError(schedule: Omit<WeekDaySchedule, "weekDayIndex">): string | null {
|
|
449
|
+
if ((schedule.daysMask & ALL_DAYS_MASK) === 0) return "Select at least one day.";
|
|
450
|
+
const start = schedule.startHour * 60 + schedule.startMinute;
|
|
451
|
+
const end = schedule.endHour * 60 + schedule.endMinute;
|
|
452
|
+
if (end <= start) return "The end time must be later than the start time on the same day.";
|
|
453
|
+
return null;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
function dateRangeError(localStartTime: number, localEndTime: number): string | null {
|
|
457
|
+
if (!Number.isFinite(localStartTime) || !Number.isFinite(localEndTime)) {
|
|
458
|
+
return "Enter both a start and an end date.";
|
|
459
|
+
}
|
|
460
|
+
// Outside this the value no longer fits the uint32 epoch-s field the cluster carries it in.
|
|
461
|
+
for (const bound of [localStartTime, localEndTime]) {
|
|
462
|
+
if (!Number.isInteger(bound) || bound < 0 || bound > MATTER_EPOCH_MAX_SECONDS) {
|
|
463
|
+
return "Dates must fall between 2000-01-01 and 2136-02-07.";
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
if (localEndTime <= localStartTime) return "The end date must be later than the start date.";
|
|
467
|
+
return null;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/** Why the date range cannot be set as entered, or null when it is valid. */
|
|
471
|
+
export function yearDayScheduleRangeError(schedule: Omit<YearDaySchedule, "yearDayIndex">): string | null {
|
|
472
|
+
return dateRangeError(schedule.localStartTime, schedule.localEndTime);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/** Why the holiday schedule cannot be set as entered, or null when it is valid. */
|
|
476
|
+
export function holidayScheduleRangeError(schedule: Omit<HolidaySchedule, "holidayIndex">): string | null {
|
|
477
|
+
return dateRangeError(schedule.localStartTime, schedule.localEndTime);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
const pad = (value: number) => String(value).padStart(2, "0");
|
|
481
|
+
|
|
482
|
+
/** The instant whose UTC components spell out the wall clock `matterEpochSeconds` encodes. */
|
|
483
|
+
function wallClockOf(matterEpochSeconds: number): Date {
|
|
484
|
+
return new Date((matterEpochSeconds + MATTER_EPOCH_OFFSET_SECONDS) * 1000);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/** Encodes a wall clock, as the lock encodes its own: UTC components, shifted to the Matter epoch. */
|
|
488
|
+
function toMatterEpoch(year: number, month: number, day: number, hour: number, minute: number, second: number) {
|
|
489
|
+
return Math.floor(Date.UTC(year, month - 1, day, hour, minute, second) / 1000) - MATTER_EPOCH_OFFSET_SECONDS;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/** Reads the browser's wall clock, encoded the way the lock encodes its own local time. */
|
|
493
|
+
export function nowAsWallClock(): number {
|
|
494
|
+
const now = new Date();
|
|
495
|
+
return toMatterEpoch(
|
|
496
|
+
now.getFullYear(),
|
|
497
|
+
now.getMonth() + 1,
|
|
498
|
+
now.getDate(),
|
|
499
|
+
now.getHours(),
|
|
500
|
+
now.getMinutes(),
|
|
501
|
+
now.getSeconds(),
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/** Formats the lock's wall clock for display. Deliberately not shifted into the viewer's time zone. */
|
|
506
|
+
export function formatWallClock(matterEpochSeconds: number): string {
|
|
507
|
+
return wallClockOf(matterEpochSeconds).toLocaleString(undefined, {
|
|
508
|
+
year: "numeric",
|
|
509
|
+
month: "2-digit",
|
|
510
|
+
day: "2-digit",
|
|
511
|
+
hour: "2-digit",
|
|
512
|
+
minute: "2-digit",
|
|
513
|
+
timeZone: "UTC",
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/** Formats the lock's wall clock for an `<input type="datetime-local">`, keeping seconds when it has any. */
|
|
518
|
+
export function toDateTimeInputValue(matterEpochSeconds: number): string {
|
|
519
|
+
const date = wallClockOf(matterEpochSeconds);
|
|
520
|
+
const minutes = `${date.getUTCFullYear()}-${pad(date.getUTCMonth() + 1)}-${pad(date.getUTCDate())}T${pad(date.getUTCHours())}:${pad(date.getUTCMinutes())}`;
|
|
521
|
+
const seconds = date.getUTCSeconds();
|
|
522
|
+
return seconds === 0 ? minutes : `${minutes}:${pad(seconds)}`;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* Reads an `<input type="datetime-local">` value back as the lock's wall clock, or null when it is
|
|
527
|
+
* empty, malformed, or names a day that does not exist.
|
|
528
|
+
*/
|
|
529
|
+
export function fromDateTimeInputValue(value: string): number | null {
|
|
530
|
+
const match = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(?::(\d{2}))?$/.exec(value.trim());
|
|
531
|
+
if (match === null) return null;
|
|
532
|
+
const [year, month, day, hour, minute] = match.slice(1, 6).map(Number);
|
|
533
|
+
const second = match[6] === undefined ? 0 : Number(match[6]);
|
|
534
|
+
if (hour > 23 || minute > 59 || second > 59) return null;
|
|
535
|
+
const matterEpochSeconds = toMatterEpoch(year, month, day, hour, minute, second);
|
|
536
|
+
const roundTrip = wallClockOf(matterEpochSeconds);
|
|
537
|
+
// Date.UTC rolls 2026-02-30 forward into March rather than rejecting it.
|
|
538
|
+
if (
|
|
539
|
+
roundTrip.getUTCFullYear() !== year ||
|
|
540
|
+
roundTrip.getUTCMonth() + 1 !== month ||
|
|
541
|
+
roundTrip.getUTCDate() !== day
|
|
542
|
+
) {
|
|
543
|
+
return null;
|
|
544
|
+
}
|
|
545
|
+
return matterEpochSeconds;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/** The windows covering one display day (0 = Monday .. 6 = Sunday), ordered by start time. */
|
|
549
|
+
export function buildDaySegments(slots: WeekDayScheduleSlot[], displayDay: number): DayScheduleSegment[] {
|
|
550
|
+
const bit = DAY_BITS[displayDay];
|
|
551
|
+
if (bit === undefined) return [];
|
|
552
|
+
return slots
|
|
553
|
+
.flatMap(slot => {
|
|
554
|
+
const schedule = slot.schedule;
|
|
555
|
+
if (schedule === null || !maskHasDay(schedule.daysMask, bit)) return [];
|
|
556
|
+
return [
|
|
557
|
+
{
|
|
558
|
+
weekDayIndex: schedule.weekDayIndex,
|
|
559
|
+
startMin: schedule.startHour * 60 + schedule.startMinute,
|
|
560
|
+
endMin: schedule.endHour * 60 + schedule.endMinute,
|
|
561
|
+
},
|
|
562
|
+
];
|
|
563
|
+
})
|
|
564
|
+
.sort((a, b) => a.startMin - b.startMin);
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
export function decodeWeekDayScheduleResponse(response: unknown, weekDayIndex: number): WeekDayScheduleSlot {
|
|
568
|
+
const obj = asObject(response);
|
|
569
|
+
const status = obj !== null ? pickNumber(obj, "status") : null;
|
|
570
|
+
if (obj === null || status !== 0) {
|
|
571
|
+
return { weekDayIndex, status, schedule: null };
|
|
572
|
+
}
|
|
573
|
+
const daysMask = pickNumber(obj, "daysMask");
|
|
574
|
+
const startHour = pickNumber(obj, "startHour");
|
|
575
|
+
const startMinute = pickNumber(obj, "startMinute");
|
|
576
|
+
const endHour = pickNumber(obj, "endHour");
|
|
577
|
+
const endMinute = pickNumber(obj, "endMinute");
|
|
578
|
+
if (daysMask === null || startHour === null || startMinute === null || endHour === null || endMinute === null) {
|
|
579
|
+
return { weekDayIndex, status: null, schedule: null };
|
|
580
|
+
}
|
|
581
|
+
return {
|
|
582
|
+
weekDayIndex,
|
|
583
|
+
status,
|
|
584
|
+
schedule: {
|
|
585
|
+
weekDayIndex: pickNumber(obj, "weekDayIndex") ?? weekDayIndex,
|
|
586
|
+
daysMask,
|
|
587
|
+
startHour,
|
|
588
|
+
startMinute,
|
|
589
|
+
endHour,
|
|
590
|
+
endMinute,
|
|
591
|
+
},
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
export function decodeYearDayScheduleResponse(response: unknown, yearDayIndex: number): YearDayScheduleSlot {
|
|
596
|
+
const obj = asObject(response);
|
|
597
|
+
const status = obj !== null ? pickNumber(obj, "status") : null;
|
|
598
|
+
const localStartTime = obj !== null ? pickNumber(obj, "localStartTime") : null;
|
|
599
|
+
const localEndTime = obj !== null ? pickNumber(obj, "localEndTime") : null;
|
|
600
|
+
if (status === 0 && (localStartTime === null || localEndTime === null)) {
|
|
601
|
+
return { yearDayIndex, status: null, schedule: null };
|
|
602
|
+
}
|
|
603
|
+
if (status !== 0 || localStartTime === null || localEndTime === null) {
|
|
604
|
+
return { yearDayIndex, status, schedule: null };
|
|
605
|
+
}
|
|
606
|
+
return {
|
|
607
|
+
yearDayIndex,
|
|
608
|
+
status,
|
|
609
|
+
schedule: {
|
|
610
|
+
yearDayIndex: (obj !== null ? pickNumber(obj, "yearDayIndex") : null) ?? yearDayIndex,
|
|
611
|
+
localStartTime,
|
|
612
|
+
localEndTime,
|
|
613
|
+
},
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
export function decodeHolidayScheduleResponse(response: unknown, holidayIndex: number): HolidayScheduleSlot {
|
|
618
|
+
const obj = asObject(response);
|
|
619
|
+
const status = obj !== null ? pickNumber(obj, "status") : null;
|
|
620
|
+
const localStartTime = obj !== null ? pickNumber(obj, "localStartTime") : null;
|
|
621
|
+
const localEndTime = obj !== null ? pickNumber(obj, "localEndTime") : null;
|
|
622
|
+
const operatingMode = obj !== null ? pickNumber(obj, "operatingMode") : null;
|
|
623
|
+
if (status === 0 && (localStartTime === null || localEndTime === null || operatingMode === null)) {
|
|
624
|
+
return { holidayIndex, status: null, schedule: null };
|
|
625
|
+
}
|
|
626
|
+
if (status !== 0 || localStartTime === null || localEndTime === null || operatingMode === null) {
|
|
627
|
+
return { holidayIndex, status, schedule: null };
|
|
628
|
+
}
|
|
629
|
+
return {
|
|
630
|
+
holidayIndex,
|
|
631
|
+
status,
|
|
632
|
+
schedule: {
|
|
633
|
+
holidayIndex: (obj !== null ? pickNumber(obj, "holidayIndex") : null) ?? holidayIndex,
|
|
634
|
+
localStartTime,
|
|
635
|
+
localEndTime,
|
|
636
|
+
operatingMode,
|
|
637
|
+
},
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
function decodeCredentials(obj: Record<string, unknown>): UserCredential[] {
|
|
642
|
+
return pickArray(obj, "credentials").flatMap(entry => {
|
|
643
|
+
const obj = asObject(entry);
|
|
644
|
+
if (obj === null) return [];
|
|
645
|
+
const credentialType = pickNumber(obj, "credentialType");
|
|
646
|
+
const credentialIndex = pickNumber(obj, "credentialIndex");
|
|
647
|
+
if (credentialType === null || credentialIndex === null) return [];
|
|
648
|
+
return [{ credentialType, credentialIndex }];
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
export function decodeUserResponse(response: unknown): DoorLockUser | null {
|
|
653
|
+
const obj = asObject(response);
|
|
654
|
+
if (obj === null) return null;
|
|
655
|
+
const userIndex = pickNumber(obj, "userIndex");
|
|
656
|
+
if (userIndex === null) return null;
|
|
657
|
+
const userStatus = pickNumber(obj, "userStatus");
|
|
658
|
+
return {
|
|
659
|
+
userIndex,
|
|
660
|
+
userName: pickString(obj, "userName"),
|
|
661
|
+
userStatus,
|
|
662
|
+
userType: pickNumber(obj, "userType"),
|
|
663
|
+
nextUserIndex: pickNumber(obj, "nextUserIndex"),
|
|
664
|
+
// The spec nulls the whole record for a free slot, but locks in the field also report Available there.
|
|
665
|
+
occupied: userStatus !== null && userStatus !== USER_STATUS_AVAILABLE,
|
|
666
|
+
credentials: decodeCredentials(obj),
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/** Whether a user carries a working PIN credential, as opposed to just a UserType badge. */
|
|
671
|
+
export function hasPinCredential(user: DoorLockUser): boolean {
|
|
672
|
+
return user.credentials.some(credential => credential.credentialType === CREDENTIAL_TYPE_PIN);
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
/** The lock-wide ExpiringUserTimeout, phrased for display next to an ExpiringUser's badge. */
|
|
676
|
+
export function formatExpiringTimeoutHint(minutes: number | null): string | null {
|
|
677
|
+
if (minutes === null) return null;
|
|
678
|
+
return `Disables ${minutes} min after first credential use`;
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/** Encode a PIN for the octstr PinCode field, which reaches the lock as base64. */
|
|
682
|
+
export function encodePinCode(pin: string): string {
|
|
683
|
+
return btoa(String.fromCharCode(...new TextEncoder().encode(pin)));
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
export async function readWeekDaySchedule(
|
|
687
|
+
client: MatterClient,
|
|
688
|
+
nodeId: number | bigint,
|
|
689
|
+
endpoint: number,
|
|
690
|
+
weekDayIndex: number,
|
|
691
|
+
userIndex: number,
|
|
692
|
+
): Promise<WeekDayScheduleSlot> {
|
|
693
|
+
const response = await client.deviceCommand(nodeId, endpoint, DOOR_LOCK_CLUSTER_ID, "GetWeekDaySchedule", {
|
|
694
|
+
weekDayIndex,
|
|
695
|
+
userIndex,
|
|
696
|
+
});
|
|
697
|
+
return decodeWeekDayScheduleResponse(response, weekDayIndex);
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
export async function writeWeekDaySchedule(
|
|
701
|
+
client: MatterClient,
|
|
702
|
+
nodeId: number | bigint,
|
|
703
|
+
endpoint: number,
|
|
704
|
+
userIndex: number,
|
|
705
|
+
schedule: WeekDaySchedule,
|
|
706
|
+
): Promise<void> {
|
|
707
|
+
await client.deviceCommand(nodeId, endpoint, DOOR_LOCK_CLUSTER_ID, "SetWeekDaySchedule", {
|
|
708
|
+
weekDayIndex: schedule.weekDayIndex,
|
|
709
|
+
userIndex,
|
|
710
|
+
daysMask: schedule.daysMask,
|
|
711
|
+
startHour: schedule.startHour,
|
|
712
|
+
startMinute: schedule.startMinute,
|
|
713
|
+
endHour: schedule.endHour,
|
|
714
|
+
endMinute: schedule.endMinute,
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
export async function clearWeekDaySchedule(
|
|
719
|
+
client: MatterClient,
|
|
720
|
+
nodeId: number | bigint,
|
|
721
|
+
endpoint: number,
|
|
722
|
+
userIndex: number,
|
|
723
|
+
weekDayIndex: number,
|
|
724
|
+
): Promise<void> {
|
|
725
|
+
await client.deviceCommand(nodeId, endpoint, DOOR_LOCK_CLUSTER_ID, "ClearWeekDaySchedule", {
|
|
726
|
+
weekDayIndex,
|
|
727
|
+
userIndex,
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
export async function readYearDaySchedule(
|
|
732
|
+
client: MatterClient,
|
|
733
|
+
nodeId: number | bigint,
|
|
734
|
+
endpoint: number,
|
|
735
|
+
yearDayIndex: number,
|
|
736
|
+
userIndex: number,
|
|
737
|
+
): Promise<YearDayScheduleSlot> {
|
|
738
|
+
const response = await client.deviceCommand(nodeId, endpoint, DOOR_LOCK_CLUSTER_ID, "GetYearDaySchedule", {
|
|
739
|
+
yearDayIndex,
|
|
740
|
+
userIndex,
|
|
741
|
+
});
|
|
742
|
+
return decodeYearDayScheduleResponse(response, yearDayIndex);
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
export async function writeYearDaySchedule(
|
|
746
|
+
client: MatterClient,
|
|
747
|
+
nodeId: number | bigint,
|
|
748
|
+
endpoint: number,
|
|
749
|
+
userIndex: number,
|
|
750
|
+
schedule: YearDaySchedule,
|
|
751
|
+
): Promise<void> {
|
|
752
|
+
await client.deviceCommand(nodeId, endpoint, DOOR_LOCK_CLUSTER_ID, "SetYearDaySchedule", {
|
|
753
|
+
yearDayIndex: schedule.yearDayIndex,
|
|
754
|
+
userIndex,
|
|
755
|
+
localStartTime: schedule.localStartTime,
|
|
756
|
+
localEndTime: schedule.localEndTime,
|
|
757
|
+
});
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
export async function clearYearDaySchedule(
|
|
761
|
+
client: MatterClient,
|
|
762
|
+
nodeId: number | bigint,
|
|
763
|
+
endpoint: number,
|
|
764
|
+
userIndex: number,
|
|
765
|
+
yearDayIndex: number,
|
|
766
|
+
): Promise<void> {
|
|
767
|
+
await client.deviceCommand(nodeId, endpoint, DOOR_LOCK_CLUSTER_ID, "ClearYearDaySchedule", {
|
|
768
|
+
yearDayIndex,
|
|
769
|
+
userIndex,
|
|
770
|
+
});
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
export async function readHolidaySchedule(
|
|
774
|
+
client: MatterClient,
|
|
775
|
+
nodeId: number | bigint,
|
|
776
|
+
endpoint: number,
|
|
777
|
+
holidayIndex: number,
|
|
778
|
+
): Promise<HolidayScheduleSlot> {
|
|
779
|
+
const response = await client.deviceCommand(nodeId, endpoint, DOOR_LOCK_CLUSTER_ID, "GetHolidaySchedule", {
|
|
780
|
+
holidayIndex,
|
|
781
|
+
});
|
|
782
|
+
return decodeHolidayScheduleResponse(response, holidayIndex);
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
export async function writeHolidaySchedule(
|
|
786
|
+
client: MatterClient,
|
|
787
|
+
nodeId: number | bigint,
|
|
788
|
+
endpoint: number,
|
|
789
|
+
schedule: HolidaySchedule,
|
|
790
|
+
): Promise<void> {
|
|
791
|
+
await client.deviceCommand(nodeId, endpoint, DOOR_LOCK_CLUSTER_ID, "SetHolidaySchedule", {
|
|
792
|
+
holidayIndex: schedule.holidayIndex,
|
|
793
|
+
localStartTime: schedule.localStartTime,
|
|
794
|
+
localEndTime: schedule.localEndTime,
|
|
795
|
+
operatingMode: schedule.operatingMode,
|
|
796
|
+
});
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
export async function clearHolidaySchedule(
|
|
800
|
+
client: MatterClient,
|
|
801
|
+
nodeId: number | bigint,
|
|
802
|
+
endpoint: number,
|
|
803
|
+
holidayIndex: number,
|
|
804
|
+
): Promise<void> {
|
|
805
|
+
await client.deviceCommand(nodeId, endpoint, DOOR_LOCK_CLUSTER_ID, "ClearHolidaySchedule", { holidayIndex });
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
export async function readUser(
|
|
809
|
+
client: MatterClient,
|
|
810
|
+
nodeId: number | bigint,
|
|
811
|
+
endpoint: number,
|
|
812
|
+
userIndex: number,
|
|
813
|
+
): Promise<DoorLockUser | null> {
|
|
814
|
+
const response = await client.deviceCommand(nodeId, endpoint, DOOR_LOCK_CLUSTER_ID, "GetUser", { userIndex });
|
|
815
|
+
return decodeUserResponse(response);
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
/**
|
|
819
|
+
* The lock's occupied users, walked through NextUserIndex so free slots cost no round-trip.
|
|
820
|
+
* `maxUsers` bounds the walk on a lock that never terminates the chain.
|
|
821
|
+
*/
|
|
822
|
+
export async function readUsers(
|
|
823
|
+
client: MatterClient,
|
|
824
|
+
nodeId: number | bigint,
|
|
825
|
+
endpoint: number,
|
|
826
|
+
maxUsers: number,
|
|
827
|
+
): Promise<DoorLockUser[]> {
|
|
828
|
+
const users = new Array<DoorLockUser>();
|
|
829
|
+
const visited = new Set<number>();
|
|
830
|
+
let index: number | null = 1;
|
|
831
|
+
while (index !== null && visited.size < maxUsers) {
|
|
832
|
+
// A lock reporting a NextUserIndex it already served would otherwise loop here forever.
|
|
833
|
+
if (visited.has(index)) break;
|
|
834
|
+
visited.add(index);
|
|
835
|
+
const user = await readUser(client, nodeId, endpoint, index);
|
|
836
|
+
if (user === null) break;
|
|
837
|
+
if (user.occupied) users.push(user);
|
|
838
|
+
index = user.nextUserIndex;
|
|
839
|
+
}
|
|
840
|
+
return users;
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
/** The lowest user index in `[1, maxUsers]` not already occupied, or null when the user database is full. */
|
|
844
|
+
export function nextFreeUserIndex(users: DoorLockUser[], maxUsers: number): number | null {
|
|
845
|
+
const occupied = new Set(users.map(user => user.userIndex));
|
|
846
|
+
for (let index = 1; index <= maxUsers; index++) {
|
|
847
|
+
if (!occupied.has(index)) return index;
|
|
848
|
+
}
|
|
849
|
+
return null;
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
export async function addUser(
|
|
853
|
+
client: MatterClient,
|
|
854
|
+
nodeId: number | bigint,
|
|
855
|
+
endpoint: number,
|
|
856
|
+
userIndex: number,
|
|
857
|
+
userName: string,
|
|
858
|
+
userType: number | null = null,
|
|
859
|
+
userStatus: number | null = null,
|
|
860
|
+
): Promise<void> {
|
|
861
|
+
await client.deviceCommand(nodeId, endpoint, DOOR_LOCK_CLUSTER_ID, "SetUser", {
|
|
862
|
+
operationType: OPERATION_TYPE_ADD,
|
|
863
|
+
userIndex,
|
|
864
|
+
userName,
|
|
865
|
+
userUniqueId: null,
|
|
866
|
+
userStatus,
|
|
867
|
+
userType,
|
|
868
|
+
credentialRule: null,
|
|
869
|
+
});
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
function decodeSetCredentialResponse(response: unknown): { status: number | null; userIndex: number | null } {
|
|
873
|
+
const obj = asObject(response);
|
|
874
|
+
return {
|
|
875
|
+
status: obj !== null ? pickNumber(obj, "status") : null,
|
|
876
|
+
userIndex: obj !== null ? pickNumber(obj, "userIndex") : null,
|
|
877
|
+
};
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
/** DlStatus values SetCredential can return beyond the generic interaction-model status set (spec §5.2.6.20). */
|
|
881
|
+
const SET_CREDENTIAL_STATUS_NAMES: Record<number, string> = {
|
|
882
|
+
2: "Duplicate",
|
|
883
|
+
3: "Occupied",
|
|
884
|
+
};
|
|
885
|
+
|
|
886
|
+
function formatSetCredentialStatus(status: number): string {
|
|
887
|
+
return SET_CREDENTIAL_STATUS_NAMES[status] ?? getMatterStatusName(status);
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
function decodeCredentialStatusResponse(response: unknown): {
|
|
891
|
+
credentialExists: boolean;
|
|
892
|
+
nextCredentialIndex: number | null;
|
|
893
|
+
} {
|
|
894
|
+
const obj = asObject(response);
|
|
895
|
+
return {
|
|
896
|
+
credentialExists: obj !== null && obj["credentialExists"] === true,
|
|
897
|
+
nextCredentialIndex: obj !== null ? pickNumber(obj, "nextCredentialIndex") : null,
|
|
898
|
+
};
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
async function getCredentialStatus(
|
|
902
|
+
client: MatterClient,
|
|
903
|
+
nodeId: number | bigint,
|
|
904
|
+
endpoint: number,
|
|
905
|
+
credentialType: number,
|
|
906
|
+
credentialIndex: number,
|
|
907
|
+
) {
|
|
908
|
+
const response = await client.deviceCommand(nodeId, endpoint, DOOR_LOCK_CLUSTER_ID, "GetCredentialStatus", {
|
|
909
|
+
credential: { credentialType, credentialIndex },
|
|
910
|
+
});
|
|
911
|
+
return decodeCredentialStatusResponse(response);
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
/**
|
|
915
|
+
* The lowest unoccupied PIN credential index in `[1, maxIndex]`, or null when every slot is taken.
|
|
916
|
+
* GetCredentialStatus's NextCredentialIndex chains through the *occupied* slots, but it is an optional
|
|
917
|
+
* response field: a lock that omits it ends the chain after the first slot, so any index the chain did
|
|
918
|
+
* not reach is probed directly rather than assumed free.
|
|
919
|
+
*/
|
|
920
|
+
async function nextFreePinCredentialIndex(
|
|
921
|
+
client: MatterClient,
|
|
922
|
+
nodeId: number | bigint,
|
|
923
|
+
endpoint: number,
|
|
924
|
+
maxIndex: number,
|
|
925
|
+
): Promise<number | null> {
|
|
926
|
+
const occupied = new Set<number>();
|
|
927
|
+
// Visited is tracked separately from occupied: a lock reporting NextCredentialIndex values that never
|
|
928
|
+
// flag as existing (malformed, or genuinely free slots surfaced by mistake) must not loop forever just
|
|
929
|
+
// because `occupied` never grows. The [1, maxIndex] bound below guards the same malformed-lock case for
|
|
930
|
+
// an out-of-range index, which would otherwise keep "visited" growing on values the candidate scan below
|
|
931
|
+
// never looks at.
|
|
932
|
+
const visited = new Set<number>();
|
|
933
|
+
let index: number | null = 1;
|
|
934
|
+
while (index !== null && index >= 1 && index <= maxIndex && !visited.has(index)) {
|
|
935
|
+
visited.add(index);
|
|
936
|
+
const status = await getCredentialStatus(client, nodeId, endpoint, CREDENTIAL_TYPE_PIN, index);
|
|
937
|
+
if (status.credentialExists) occupied.add(index);
|
|
938
|
+
index = status.nextCredentialIndex;
|
|
939
|
+
}
|
|
940
|
+
for (let candidate = 1; candidate <= maxIndex; candidate++) {
|
|
941
|
+
if (occupied.has(candidate)) continue;
|
|
942
|
+
if (visited.has(candidate)) return candidate;
|
|
943
|
+
const status = await getCredentialStatus(client, nodeId, endpoint, CREDENTIAL_TYPE_PIN, candidate);
|
|
944
|
+
if (!status.credentialExists) return candidate;
|
|
945
|
+
occupied.add(candidate);
|
|
946
|
+
}
|
|
947
|
+
return null;
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
/**
|
|
951
|
+
* Attaches a PIN credential to an existing user (SetCredential's "add a credential to an existing user"
|
|
952
|
+
* use case — UserIndex given, UserStatus/UserType left null since the user already has both). `capacity`
|
|
953
|
+
* bounds the free-slot search — pass the lock's actual `readNumberOfPinUsersSupported(node, endpoint)`
|
|
954
|
+
* rather than a guessed fallback, since a guess too high can select an index beyond the lock's real table.
|
|
955
|
+
*/
|
|
956
|
+
export async function attachPinCredential(
|
|
957
|
+
client: MatterClient,
|
|
958
|
+
nodeId: number | bigint,
|
|
959
|
+
endpoint: number,
|
|
960
|
+
userIndex: number,
|
|
961
|
+
pin: string,
|
|
962
|
+
capacity: number,
|
|
963
|
+
): Promise<void> {
|
|
964
|
+
const credentialIndex = await nextFreePinCredentialIndex(client, nodeId, endpoint, capacity);
|
|
965
|
+
if (credentialIndex === null) {
|
|
966
|
+
throw new Error("The lock's PIN credential database is full.");
|
|
967
|
+
}
|
|
968
|
+
const response = await client.deviceCommand(nodeId, endpoint, DOOR_LOCK_CLUSTER_ID, "SetCredential", {
|
|
969
|
+
operationType: OPERATION_TYPE_ADD,
|
|
970
|
+
credential: { credentialType: CREDENTIAL_TYPE_PIN, credentialIndex },
|
|
971
|
+
credentialData: encodePinCode(pin),
|
|
972
|
+
userIndex,
|
|
973
|
+
userStatus: null,
|
|
974
|
+
userType: null,
|
|
975
|
+
});
|
|
976
|
+
const { status } = decodeSetCredentialResponse(response);
|
|
977
|
+
if (status !== 0) {
|
|
978
|
+
throw new Error(
|
|
979
|
+
status === null
|
|
980
|
+
? "Setting the PIN credential failed: the lock did not report a result."
|
|
981
|
+
: `Setting the PIN credential failed: ${formatSetCredentialStatus(status)} (${status})`,
|
|
982
|
+
);
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
export async function removeUser(
|
|
987
|
+
client: MatterClient,
|
|
988
|
+
nodeId: number | bigint,
|
|
989
|
+
endpoint: number,
|
|
990
|
+
userIndex: number,
|
|
991
|
+
): Promise<void> {
|
|
992
|
+
await client.deviceCommand(nodeId, endpoint, DOOR_LOCK_CLUSTER_ID, "ClearUser", { userIndex });
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
/**
|
|
996
|
+
* Why a user is not usable after createExpiringPinUser() failed.
|
|
997
|
+
*
|
|
998
|
+
* - `rolled-back`: the user was removed again, so the lock is unchanged and a retry is safe.
|
|
999
|
+
* - `orphaned`: the user exists on the lock with no PIN and could not be removed; it cannot open the
|
|
1000
|
+
* door, so the operator has to delete it before retrying.
|
|
1001
|
+
*/
|
|
1002
|
+
export interface ExpiringPinUserFailure {
|
|
1003
|
+
outcome: "rolled-back" | "orphaned";
|
|
1004
|
+
/** Why the PIN could not be set. */
|
|
1005
|
+
reason: string;
|
|
1006
|
+
/** Why the created user could not be removed again; only set when `outcome` is `orphaned`. */
|
|
1007
|
+
rollbackReason?: string;
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
/**
|
|
1011
|
+
* Creates an ExpiringUser and attaches its PIN. SetUser and SetCredential are separate commands, so a
|
|
1012
|
+
* PIN failure would otherwise leave a user that looks live in the list but can never open the door;
|
|
1013
|
+
* the user is removed again when that happens.
|
|
1014
|
+
*
|
|
1015
|
+
* @returns null when the user and its PIN are both in place, otherwise what went wrong
|
|
1016
|
+
* @throws when SetUser itself fails, i.e. before anything reaches the lock
|
|
1017
|
+
*/
|
|
1018
|
+
export async function createExpiringPinUser(
|
|
1019
|
+
client: MatterClient,
|
|
1020
|
+
nodeId: number | bigint,
|
|
1021
|
+
endpoint: number,
|
|
1022
|
+
userIndex: number,
|
|
1023
|
+
userName: string,
|
|
1024
|
+
pin: string,
|
|
1025
|
+
pinCapacity: number,
|
|
1026
|
+
): Promise<ExpiringPinUserFailure | null> {
|
|
1027
|
+
await addUser(client, nodeId, endpoint, userIndex, userName, USER_TYPE_EXPIRING, USER_STATUS_OCCUPIED_ENABLED);
|
|
1028
|
+
try {
|
|
1029
|
+
await attachPinCredential(client, nodeId, endpoint, userIndex, pin, pinCapacity);
|
|
1030
|
+
return null;
|
|
1031
|
+
} catch (credentialError) {
|
|
1032
|
+
const reason = errorMessage(credentialError);
|
|
1033
|
+
try {
|
|
1034
|
+
await removeUser(client, nodeId, endpoint, userIndex);
|
|
1035
|
+
return { outcome: "rolled-back", reason };
|
|
1036
|
+
} catch (rollbackError) {
|
|
1037
|
+
return { outcome: "orphaned", reason, rollbackReason: errorMessage(rollbackError) };
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
function errorMessage(error: unknown): string {
|
|
1043
|
+
return error instanceof Error ? error.message : String(error);
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
export async function lockDoor(
|
|
1047
|
+
client: MatterClient,
|
|
1048
|
+
nodeId: number | bigint,
|
|
1049
|
+
endpoint: number,
|
|
1050
|
+
pinCode?: string,
|
|
1051
|
+
): Promise<void> {
|
|
1052
|
+
const payload = pinCode ? { pinCode: encodePinCode(pinCode) } : {};
|
|
1053
|
+
await client.deviceCommand(nodeId, endpoint, DOOR_LOCK_CLUSTER_ID, "LockDoor", payload);
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
export async function unlockDoor(
|
|
1057
|
+
client: MatterClient,
|
|
1058
|
+
nodeId: number | bigint,
|
|
1059
|
+
endpoint: number,
|
|
1060
|
+
pinCode?: string,
|
|
1061
|
+
): Promise<void> {
|
|
1062
|
+
const payload = pinCode ? { pinCode: encodePinCode(pinCode) } : {};
|
|
1063
|
+
await client.deviceCommand(nodeId, endpoint, DOOR_LOCK_CLUSTER_ID, "UnlockDoor", payload);
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
export async function unlockWithTimeout(
|
|
1067
|
+
client: MatterClient,
|
|
1068
|
+
nodeId: number | bigint,
|
|
1069
|
+
endpoint: number,
|
|
1070
|
+
timeoutSeconds: number,
|
|
1071
|
+
pinCode?: string,
|
|
1072
|
+
): Promise<void> {
|
|
1073
|
+
const payload: Record<string, unknown> = { timeout: timeoutSeconds };
|
|
1074
|
+
if (pinCode) payload["pinCode"] = encodePinCode(pinCode);
|
|
1075
|
+
await client.deviceCommand(nodeId, endpoint, DOOR_LOCK_CLUSTER_ID, "UnlockWithTimeout", payload);
|
|
1076
|
+
}
|