|
| 1 | +/** |
| 2 | + * Handler for add mode prompts |
| 3 | + * |
| 4 | + * Automates Z-Wave node inclusion (adding devices to the network). |
| 5 | + * For S2 inclusion, uses event-driven flow via WebSocket: |
| 6 | + * 1. Send controller.begin_inclusion |
| 7 | + * 2. Listen for "grant security classes" event and respond |
| 8 | + * 3. Wait for PIN from CTT log message |
| 9 | + * 4. Listen for "validate dsk and enter pin" event and respond with PIN |
| 10 | + */ |
| 11 | + |
| 12 | +import { |
| 13 | + createDeferredPromise, |
| 14 | + type DeferredPromise, |
| 15 | +} from "alcalzone-shared/deferred-promise"; |
| 16 | +import { registerHandler } from "../../prompt-handlers.ts"; |
| 17 | +import { wait } from "alcalzone-shared/async"; |
| 18 | +import { InclusionStrategy } from "zwave-js"; |
| 19 | + |
| 20 | +const PIN_PROMISE = "pin promise"; |
| 21 | + |
| 22 | +// Module-level variable to track cleanup function (persists across test state clears) |
| 23 | +let currentInclusionCleanup: (() => void) | undefined; |
| 24 | + |
| 25 | +registerHandler(/.*/, { |
| 26 | + onTestStart: async () => { |
| 27 | + // Clean up any leftover listeners from previous tests |
| 28 | + if (currentInclusionCleanup) { |
| 29 | + currentInclusionCleanup(); |
| 30 | + currentInclusionCleanup = undefined; |
| 31 | + } |
| 32 | + }, |
| 33 | + |
| 34 | + onPrompt: async (ctx) => { |
| 35 | + // Handle ACTIVATE_NETWORK_MODE for ADD mode |
| 36 | + if ( |
| 37 | + ctx.message?.type === "ACTIVATE_NETWORK_MODE" && |
| 38 | + ctx.message.mode === "ADD" |
| 39 | + ) { |
| 40 | + const { client, state, message } = ctx; |
| 41 | + state.set(PIN_PROMISE, createDeferredPromise<string>()); |
| 42 | + |
| 43 | + // Clean up any existing listeners first |
| 44 | + if (currentInclusionCleanup) { |
| 45 | + currentInclusionCleanup(); |
| 46 | + currentInclusionCleanup = undefined; |
| 47 | + } |
| 48 | + |
| 49 | + // Set up S2 inclusion event handlers |
| 50 | + const grantSecurityClasses = async (data: { requested: unknown }) => { |
| 51 | + await client.sendCommand("controller.grant_security_classes", { |
| 52 | + inclusionGrant: data.requested, |
| 53 | + }); |
| 54 | + }; |
| 55 | + |
| 56 | + const validateDsk = async () => { |
| 57 | + const pinPromise = state.get(PIN_PROMISE) as DeferredPromise<string> | undefined; |
| 58 | + if (pinPromise) { |
| 59 | + const pin = await pinPromise; |
| 60 | + await client.sendCommand("controller.validate_dsk_and_enter_pin", { |
| 61 | + pin, |
| 62 | + }); |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + const cleanup = () => { |
| 67 | + client.off("grant security classes", grantSecurityClasses); |
| 68 | + client.off("validate dsk and enter pin", validateDsk); |
| 69 | + client.off("node added", onNodeAdded); |
| 70 | + currentInclusionCleanup = undefined; |
| 71 | + }; |
| 72 | + |
| 73 | + const onNodeAdded = () => { |
| 74 | + cleanup(); |
| 75 | + }; |
| 76 | + |
| 77 | + // Note: We do NOT clean up on "inclusion stopped" because that event fires |
| 78 | + // after the initial NWI phase but BEFORE S2 negotiation completes. |
| 79 | + // The S2 events (grant security classes, validate dsk) come after "inclusion stopped". |
| 80 | + |
| 81 | + client.on("grant security classes", grantSecurityClasses); |
| 82 | + client.on("validate dsk and enter pin", validateDsk); |
| 83 | + client.on("node added", onNodeAdded); |
| 84 | + |
| 85 | + // Store cleanup function at module level |
| 86 | + currentInclusionCleanup = cleanup; |
| 87 | + |
| 88 | + // Determine inclusion strategy based on forceS0 flag |
| 89 | + const strategy = message.forceS0 |
| 90 | + ? InclusionStrategy.Security_S0 |
| 91 | + : InclusionStrategy.Default; |
| 92 | + |
| 93 | + for (let attempt = 1; attempt <= 5; attempt++) { |
| 94 | + try { |
| 95 | + const result = await client.sendCommand("controller.begin_inclusion", { |
| 96 | + options: { |
| 97 | + strategy, |
| 98 | + }, |
| 99 | + }); |
| 100 | + |
| 101 | + if ((result as { success: boolean }).success !== false) break; |
| 102 | + } catch (error) { |
| 103 | + console.error(`Inclusion attempt ${attempt} failed:`, error); |
| 104 | + } |
| 105 | + |
| 106 | + // Backoff in case another in-/exclusion process is still busy |
| 107 | + if (attempt < 5) { |
| 108 | + await wait(1000 * attempt); |
| 109 | + } else { |
| 110 | + throw new Error("Failed to start inclusion after 5 attempts"); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return "Ok"; |
| 115 | + } |
| 116 | + |
| 117 | + // Let other prompts fall through to manual handling |
| 118 | + return undefined; |
| 119 | + }, |
| 120 | + |
| 121 | + onLog: async (ctx) => { |
| 122 | + if (ctx.message?.type === "S2_PIN_CODE") { |
| 123 | + const pinPromise = ctx.state.get(PIN_PROMISE) as |
| 124 | + | DeferredPromise<string> |
| 125 | + | undefined; |
| 126 | + if (!pinPromise) return; |
| 127 | + |
| 128 | + console.log("Detected PIN code:", ctx.message.pin); |
| 129 | + pinPromise.resolve(ctx.message.pin); |
| 130 | + return true; |
| 131 | + } |
| 132 | + }, |
| 133 | +}); |
0 commit comments