iobroker.alexa-timer-vis
Version:
Alexa Timer ausgeben um in der Vis anzuzeigen
293 lines (292 loc) • 9.94 kB
JavaScript
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var timer_exports = {};
__export(timer_exports, {
Timer: () => Timer,
getTimerById: () => getTimerById,
getTimerByIndex: () => getTimerByIndex
});
module.exports = __toCommonJS(timer_exports);
var import_logging = require("../lib/logging");
var import_store = __toESM(require("../app/store"));
var import_timer_data = require("../config/timer-data");
var import_ioBrokerStateAndObjects = require("../app/ioBrokerStateAndObjects");
var import_string = require("../lib/string");
var import_state = require("../lib/state");
var import_time = require("../lib/time");
class Timer {
timerIndex;
inputDeviceName;
deviceSerialNumber;
timerName;
voiceInputAsSeconds;
hours;
minutes;
seconds;
stringTimer1;
stringTimer2;
creationTime;
creationTimeString;
endTime;
endTimeString;
interval;
lengthTimer;
percent;
percent2;
extendOrShortenTimer;
remainingTimeInSeconds;
timerId;
adapter;
alexaInstance;
initialTimer;
calculatedSeconds;
_isActive = false;
get isActive() {
return this._isActive;
}
constructor({ store }) {
this.adapter = store.adapter;
this.hours = "";
this.minutes = "";
this.seconds = "";
this.stringTimer1 = "";
this.stringTimer2 = "";
this.voiceInputAsSeconds = 0;
this.timerIndex = "";
this.timerName = "";
this.creationTime = 0;
this.creationTimeString = "";
this.endTime = 0;
this.endTimeString = "";
this.inputDeviceName = "";
this.deviceSerialNumber = "";
this.interval = 1e3;
this.lengthTimer = "";
this.percent = 0;
this.percent2 = 0;
this.extendOrShortenTimer = false;
this.remainingTimeInSeconds = 0;
this.timerId = "";
this.alexaInstance = null;
this.initialTimer = "";
this.calculatedSeconds = 0;
}
setInactive() {
this._isActive = false;
}
getTimerIndex() {
return this.timerIndex;
}
getOutputProperties() {
return {
hours: this.hours,
minutes: this.minutes,
seconds: this.seconds,
stringTimer1: this.stringTimer1,
stringTimer2: this.stringTimer2,
startTimeString: this.creationTimeString,
endTimeNumber: this.endTime,
endTimeString: this.endTimeString,
inputDevice: this.inputDeviceName,
lengthTimer: this.lengthTimer,
percent: this.percent,
percent2: this.percent2,
initialTimer: this.initialTimer
};
}
isExtendOrShortenTimer() {
return this.extendOrShortenTimer;
}
getInterval() {
return this.interval;
}
outPutTimerName() {
const name = this.timerName;
return !["Timer", ""].includes(name) ? `${(0, import_string.firstLetterToUpperCase)(name)} Timer` : "Timer";
}
extendTimer(milliseconds) {
this.extendOrShortenTimer = true;
this.endTime += milliseconds;
const seconds = milliseconds / 1e3;
this.remainingTimeInSeconds += seconds;
this.endTimeString = (0, import_time.millisecondsToString)(this.endTime);
this.voiceInputAsSeconds += seconds;
this.updateInitialTimer(seconds);
}
getDataAsJson() {
return JSON.stringify({
name: this.timerName,
hours: this.hours,
minutes: this.minutes,
seconds: this.seconds,
voiceInputAsSeconds: this.voiceInputAsSeconds,
stringTimer1: this.stringTimer1,
stringTimer2: this.stringTimer2,
creationTime: this.creationTime,
creationTimeString: this.creationTimeString,
endTimeNumber: this.endTime,
endTimeString: this.endTimeString,
inputDevice: this.inputDeviceName,
serialNumber: this.deviceSerialNumber,
interval: this.interval,
lengthTimer: this.lengthTimer,
percent: this.percent,
percent2: this.percent2,
remainingTimeInSeconds: this.remainingTimeInSeconds,
timerId: this.timerId
});
}
async init({ timerIndex, newActiveTimer }) {
this.timerIndex = timerIndex;
try {
const instance = import_store.default.getAlexa2Instance();
this.alexaInstance = instance;
const nameState = await this.adapter.getForeignStateAsync(`alexa2.${instance}.History.name`);
const serialState = await this.adapter.getForeignStateAsync(`alexa2.${instance}.History.serialNumber`);
if ((0, import_state.isIobrokerValue)(nameState)) {
this.inputDeviceName = String(nameState.val);
}
if ((0, import_state.isIobrokerValue)(serialState)) {
this.deviceSerialNumber = String(serialState.val);
}
const creationTime = newActiveTimer.triggerTime - newActiveTimer.durationMillis;
await (0, import_ioBrokerStateAndObjects.setDeviceNameInObject)(this.timerIndex, this.inputDeviceName);
this.setCreationTime(creationTime);
this.setValuesFromEchoDeviceTimerList(newActiveTimer);
} catch (e) {
import_logging.errorLogger.send({ title: "Error in getInputDevice", e });
}
}
setVoiceInputAsSeconds(seconds) {
this.voiceInputAsSeconds = seconds;
}
setLengthTimer(length) {
this.lengthTimer = length;
}
setValuesFromEchoDeviceTimerList(newActiveTimer) {
if (newActiveTimer) {
const { id, label, triggerTime: endTime, durationMillis } = newActiveTimer;
this.timerId = id;
this.setTimerName(label);
if (this.endTime < 0) {
this.adapter.log.warn("Wrong endTime set");
}
this.endTime = endTime;
this.endTimeString = (0, import_time.millisecondsToString)(endTime);
this.calculatedSeconds = durationMillis / 1e3;
this.initialTimer = (0, import_time.secToHourMinSec)(this.calculatedSeconds, true).initialString;
}
}
updateInitialTimer(sec) {
this.initialTimer = (0, import_time.secToHourMinSec)(this.calculatedSeconds + sec, true).initialString;
}
setInterval(interval) {
this.interval = interval;
}
setCreationTime(creationTime) {
this.creationTime = creationTime;
this.creationTimeString = (0, import_time.millisecondsToString)(creationTime);
}
setTimerValues(props) {
this.hours = props.hours;
this.minutes = props.minutes;
this.seconds = props.seconds;
this.stringTimer1 = props.stringTimer1;
this.stringTimer2 = props.stringTimer2;
this.remainingTimeInSeconds = props.remainingSeconds;
this.lengthTimer = props.lengthTimer;
this.mathPercent();
this._isActive = true;
}
mathPercent() {
const percent = Math.round(this.remainingTimeInSeconds / this.voiceInputAsSeconds * 100);
this.percent = percent;
this.percent2 = 100 - percent;
}
setTimerName(name) {
this.timerName = name == "" || !name ? "Timer" : name.trim();
}
async stopTimerInAlexa() {
if (!this.alexaInstance) {
return;
}
const id = `alexa2.${this.alexaInstance}.Echo-Devices.${this.deviceSerialNumber}.Timer.stopTimerId`;
this.adapter.setForeignState(id, this.timerId, false);
await this.reset();
}
async reset() {
import_store.default.removeActiveTimerId(this.timerId, this.deviceSerialNumber);
this.hours = import_store.default.valHourForZero;
this.minutes = import_store.default.valMinuteForZero;
this.seconds = import_store.default.valSecondForZero;
this.stringTimer1 = "00:00:00 h";
this.stringTimer1 = "00:00:00 h";
this.stringTimer2 = "";
this.voiceInputAsSeconds = 0;
this.remainingTimeInSeconds = 0;
this.timerName = "";
this.creationTimeString = "00:00:00";
this.endTimeString = "00:00:00";
this.inputDeviceName = "";
this.interval = 0;
this.lengthTimer = "";
this.percent = 0;
this.percent2 = 0;
this.extendOrShortenTimer = false;
this.timerId = "";
this.deviceSerialNumber = "";
this.creationTime = 0;
this.endTime = 0;
this.initialTimer = "";
if (this.timerIndex) {
import_timer_data.obj.status[this.timerIndex] = false;
await (0, import_ioBrokerStateAndObjects.setDeviceNameInObject)(this.timerIndex, "");
}
this._isActive = false;
}
getTimerId() {
return this.timerId;
}
}
function getTimerByIndex(timerIndex) {
return import_timer_data.obj.timers[timerIndex];
}
function getTimerById(id) {
var _a;
const timerList = Object.keys(import_timer_data.obj.timers);
const timerIndex = timerList.find((value) => import_timer_data.obj.timers[value].getTimerId() === id);
return timerIndex ? (_a = import_timer_data.obj.timers) == null ? void 0 : _a[timerIndex] : void 0;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Timer,
getTimerById,
getTimerByIndex
});
//# sourceMappingURL=timer.js.map