zwave-js
Version:
Z-Wave driver written entirely in JavaScript/TypeScript
243 lines (242 loc) • 9.29 kB
JavaScript
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var WindowCovering_exports = {};
__export(WindowCovering_exports, {
WindowCoveringCCBehaviors: () => WindowCoveringCCBehaviors
});
module.exports = __toCommonJS(WindowCovering_exports);
var import_WindowCoveringCC = require("@zwave-js/cc/WindowCoveringCC");
var import_core = require("@zwave-js/core");
var import_shared = require("@zwave-js/shared");
var import_MockTransition = require("./MockTransition.js");
const defaultCapabilities = {
supportedParameters: [],
travelTime: 5e3
};
const STATE_KEY_PREFIX = "WindowCovering_";
function currentValueKey(param) {
return `${STATE_KEY_PREFIX}currentValue_${param}`;
}
__name(currentValueKey, "currentValueKey");
function transitionKey(param) {
return `${STATE_KEY_PREFIX}transition_${param}`;
}
__name(transitionKey, "transitionKey");
function stopParameterTransition(self, param) {
const existing = self.state.get(transitionKey(param));
if (existing) {
const value = (0, import_MockTransition.stopTransition)(existing);
self.state.set(currentValueKey(param), value);
self.state.delete(transitionKey(param));
return { wasSupervised: existing.supervised };
}
}
__name(stopParameterTransition, "stopParameterTransition");
function beginTransition(controller, self, receivedCC, param, targetValue, travelTime, supervised, onCompleteSupervised, onAbortSupervised) {
stopParameterTransition(self, param);
const currentValue = self.state.get(currentValueKey(param)) ?? 0;
const transition = (0, import_MockTransition.startTransition)({
currentValue,
targetValue,
duration: travelTime,
supervised,
onComplete: /* @__PURE__ */ __name(async () => {
self.state.set(currentValueKey(param), targetValue);
self.state.delete(transitionKey(param));
if (supervised) {
await onCompleteSupervised?.();
return;
}
const cc = new import_WindowCoveringCC.WindowCoveringCCReport({
nodeId: controller.ownNodeId,
parameter: param,
currentValue: targetValue,
targetValue,
duration: new import_core.Duration(0, "seconds")
});
await self.sendResponse(receivedCC, {
action: "sendCC",
cc
});
}, "onComplete"),
onAbort: /* @__PURE__ */ __name(async () => {
if (!supervised)
return;
await onAbortSupervised?.();
}, "onAbort")
});
if (transition) {
self.state.set(transitionKey(param), transition);
return transition.durationMs;
} else {
self.state.set(currentValueKey(param), targetValue);
return 0;
}
}
__name(beginTransition, "beginTransition");
const respondToWindowCoveringSupportedGet = {
handleCC(controller, self, receivedCC) {
if (receivedCC instanceof import_WindowCoveringCC.WindowCoveringCCSupportedGet) {
const capabilities = {
...defaultCapabilities,
...self.getCCCapabilities(import_core.CommandClasses["Window Covering"], receivedCC.endpointIndex)
};
const cc = new import_WindowCoveringCC.WindowCoveringCCSupportedReport({
nodeId: controller.ownNodeId,
supportedParameters: capabilities.supportedParameters
});
return { action: "sendCC", cc };
}
}
};
const respondToWindowCoveringGet = {
handleCC(controller, self, receivedCC) {
if (receivedCC instanceof import_WindowCoveringCC.WindowCoveringCCGet) {
const param = receivedCC.parameter;
const transition = self.state.get(transitionKey(param));
let currentValue;
let targetValue;
let duration;
if (transition) {
currentValue = (0, import_MockTransition.getTransitionCurrentValue)(transition);
targetValue = transition.targetValue;
duration = (0, import_MockTransition.getTransitionRemainingDuration)(transition);
} else {
currentValue = self.state.get(currentValueKey(param)) ?? 0;
targetValue = currentValue;
duration = new import_core.Duration(0, "seconds");
}
const cc = new import_WindowCoveringCC.WindowCoveringCCReport({
nodeId: controller.ownNodeId,
parameter: param,
currentValue,
targetValue,
duration
});
return { action: "sendCC", cc };
}
}
};
const respondToWindowCoveringSet = {
handleCC(controller, self, receivedCC) {
if (receivedCC instanceof import_WindowCoveringCC.WindowCoveringCCSet) {
const capabilities = {
...defaultCapabilities,
...self.getCCCapabilities(import_core.CommandClasses["Window Covering"], receivedCC.endpointIndex)
};
const supervised = receivedCC.isEncapsulatedWith(import_core.CommandClasses.Supervision);
const supervisedState = supervised ? {
activeParams: /* @__PURE__ */ new Set(),
finished: false
} : void 0;
let durationMs = 0;
for (const { parameter, value } of receivedCC.targetValues) {
const completeSupervised = /* @__PURE__ */ __name(async () => {
if (!supervisedState || supervisedState.finished)
return;
supervisedState.finished = true;
await self.sendResponse(receivedCC, {
action: "ok"
});
}, "completeSupervised");
const abortSupervised = /* @__PURE__ */ __name(async () => {
if (!supervisedState || supervisedState.finished)
return;
supervisedState.finished = true;
supervisedState.activeParams.delete(parameter);
for (const otherParam of supervisedState.activeParams) {
stopParameterTransition(self, otherParam);
}
supervisedState.activeParams.clear();
await self.sendResponse(receivedCC, {
action: "fail"
});
}, "abortSupervised");
const transitionDurationMs = beginTransition(controller, self, receivedCC, parameter, value, capabilities.travelTime, supervised, completeSupervised, abortSupervised);
durationMs = Math.max(durationMs, transitionDurationMs);
if (transitionDurationMs > 0 && supervisedState) {
supervisedState.activeParams.add(parameter);
}
}
return {
action: "ok",
durationMs
};
}
}
};
const respondToWindowCoveringStartLevelChange = {
handleCC(controller, self, receivedCC) {
if (receivedCC instanceof import_WindowCoveringCC.WindowCoveringCCStartLevelChange) {
const capabilities = {
...defaultCapabilities,
...self.getCCCapabilities(import_core.CommandClasses["Window Covering"], receivedCC.endpointIndex)
};
const targetValue = receivedCC.direction === "up" ? 99 : 0;
const supervised = receivedCC.isEncapsulatedWith(import_core.CommandClasses.Supervision);
const durationMs = beginTransition(controller, self, receivedCC, receivedCC.parameter, targetValue, capabilities.travelTime, supervised, async () => self.sendResponse(receivedCC, {
action: "ok"
}), async () => self.sendResponse(receivedCC, {
action: "fail"
}));
return {
action: "ok",
durationMs
};
}
}
};
const respondToWindowCoveringStopLevelChange = {
handleCC(controller, self, receivedCC) {
if (receivedCC instanceof import_WindowCoveringCC.WindowCoveringCCStopLevelChange) {
const param = receivedCC.parameter;
const stoppedTransition = stopParameterTransition(self, param);
const currentValue = self.state.get(currentValueKey(param)) ?? 0;
if (!stoppedTransition?.wasSupervised) {
(0, import_shared.setTimer)(async () => {
const cc = new import_WindowCoveringCC.WindowCoveringCCReport({
nodeId: controller.ownNodeId,
parameter: param,
currentValue,
targetValue: currentValue,
duration: new import_core.Duration(0, "seconds")
});
await self.sendResponse(receivedCC, {
action: "sendCC",
cc
});
}, 100).unref();
}
return { action: "ok" };
}
}
};
const WindowCoveringCCBehaviors = [
respondToWindowCoveringSupportedGet,
respondToWindowCoveringGet,
respondToWindowCoveringSet,
respondToWindowCoveringStartLevelChange,
respondToWindowCoveringStopLevelChange
];
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
WindowCoveringCCBehaviors
});
//# sourceMappingURL=WindowCovering.js.map