UNPKG

node-red-contrib-chronos

Version:

Time-based Node-RED scheduling, repeating, queueing, routing, filtering and manipulating nodes

698 lines (639 loc) 33.7 kB
<!-- Copyright (c) 2020 - 2025 Jens-Uwe Rossbach This code is licensed under the MIT License. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> <script type="text/html" data-template-name="chronos-scheduler"> <div class="form-row"> <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="node-red-contrib-chronos/chronos-config:common.label.name"></span></label> <input type="text" id="node-input-name" data-i18n="[placeholder]node-red-contrib-chronos/chronos-config:common.label.name"> </div> <div class="form-row"> <label for="node-input-config"><i class="fa fa-cog"></i> <span data-i18n="node-red-contrib-chronos/chronos-config:common.label.config"></span></label> <input type="text" id="node-input-config" data-i18n="[placeholder]node-red-contrib-chronos/chronos-config:common.label.config"> </div> <div class="form-row" style="margin-bottom: 0px;"> <ul style="min-width: 600px; margin-bottom: 20px;" id="tab-row"></ul> </div> <div id="tabs-content"> <div id="tab-schedule" style="display: none;"> <div class="form-row list-row"> <div class="form-row"> <ol id="node-input-scheduleList"></ol> </div> </div> </div> <div id="tab-options" style="display: none;"> <div class="form-row"> <input id="node-input-disabled" type="checkbox" style="margin-top: 0px; margin-bottom: 1px; width: auto;"> <label for="node-input-disabled" style="margin-bottom: 0px; width: auto;" data-i18n="scheduler.label.disabled"></label> </div> <div class="form-row"> <input id="node-input-multiPort" type="checkbox" style="margin-top: 0px; margin-bottom: 1px; width: auto;"> <label for="node-input-multiPort" style="margin-bottom: 0px; width: auto;" data-i18n="scheduler.label.multiPort"></label> </div> <div class="form-row"> <input id="node-input-nextEventPort" type="checkbox" style="margin-top: 0px; margin-bottom: 1px; width: auto;"> <label for="node-input-nextEventPort" style="margin-bottom: 0px; width: auto;" data-i18n="scheduler.label.nextEventPort"></label> </div> <div class="form-row"> <input id="node-input-delayOnStart" type="checkbox" style="margin-top: 0px; margin-bottom: 1px; width: auto;"> <label for="node-input-delayOnStart" style="margin-bottom: 0px; width: auto;" data-i18n="scheduler.label.delayOnStart"></label>&nbsp; <input id="node-input-onStartDelay" type="text" placeholder="0.1" style="width: 45px; height: 28px;">&nbsp; <span data-i18n="node-red-contrib-chronos/chronos-config:common.list.unit.seconds"></span> </div> </div> </div> </script> <script type="text/javascript"> (function() { const PATTERN_TIME = /^(?:\d|0\d|1\d|2[0-3]):(?:[0-5]\d)(?::(?:[0-5]\d))?\s*(?:a|am|A|AM|p|pm|P|PM)?$/; const PATTERN_CUSTOM_TIME = /^[a-zA-Z][0-9a-zA-Z_]*$/; const PATTERN_CRONTAB = /^[0-9a-zA-Z ,#@*/?-]+$/; RED.nodes.registerType("chronos-scheduler", { category: "chronos", color: "#DEB887", icon: "chronos_scheduler.svg", inputs: 1, outputs: 1, paletteLabel: "scheduler", label: function() { return (this.name || this._("scheduler.label.node")); }, labelStyle: function() { return (this.name ? "node_label_italic" : ""); }, inputLabels: function() { return this._("scheduler.label.inputPort"); }, outputLabels: function(index) { if (this.multiPort) { if (this.nextEventPort) { if (index == (this.outputs - 1)) { return this._("scheduler.label.eventTimes"); } else { return this.schedule[index] ? this.schedule[index].label : this._("scheduler.status.noSchedule"); } } else { return this.schedule[index] ? this.schedule[index].label : this._("scheduler.status.noSchedule"); } } else { if (this.nextEventPort) { return (index == 0) ? this._("scheduler.label.outputPort") : this._("scheduler.label.eventTimes"); } else { return this._("scheduler.label.outputPort"); } } }, defaults: { name: { value: "" }, config: { value: "", type: "chronos-config", required: true }, schedule: { value: [{ trigger: { type: "time", value: "", offset: 0, random: false}, output: { type: "msg", property: { name: "payload", type: "str", value: ""}}}], validate: function(items, opt) { if (!items || (items.length == 0)) { return this._("scheduler.error.noSchedule"); } const errors = []; for (const item of items) { if ((item.trigger.type == "time") && !PATTERN_TIME.test(item.trigger.value)) { errors.push(this._("node-red-contrib-chronos/chronos-config:common.error.invalidTime")); } else if ((item.trigger.type == "custom") && !PATTERN_CUSTOM_TIME.test(item.trigger.value)) { errors.push(this._("node-red-contrib-chronos/chronos-config:common.error.invalidName")); } else if ((item.trigger.type == "crontab") && !PATTERN_CRONTAB.test(item.trigger.value)) { errors.push(this._("node-red-contrib-chronos/chronos-config:common.error.invalidCrontab")); } else if (/^env|global|flow$/.test(item.trigger.type)) { const res = RED.utils.validateTypedProperty(item.trigger.value, item.trigger.type, opt) if (res !== true) { errors.push(res); } } if (/^msg|flow|global$/.test(item.output.type)) { let res = RED.utils.validatePropertyExpression(item.output.property.name, opt); if (res !== true) { errors.push(res); } res = RED.utils.validateTypedProperty(item.output.property.value, item.output.property.type, opt) if (res !== true) { errors.push(res); } } else { const res = RED.utils.validateTypedProperty(item.output.value, item.output.contentType, opt); if (res !== true) { errors.push(res); } } } return (errors.length > 0) ? errors : true; } }, disabled: { value: false }, multiPort: { value: false }, nextEventPort: { value: false }, delayOnStart: { value: true }, onStartDelay: { value: 0.1, validate: RED.validators.number(true) }, outputs: { value: 1 } }, oneditprepare: function() { const node = this; const tabs = RED.tabs.create( { id: "tab-row", onchange: function(tab) { $("#tabs-content").children().hide(); $("#" + tab.id).show(); RED.tray.resize(); } }); tabs.addTab({id: "tab-schedule", iconClass: "fa fa-calendar", label: node._("scheduler.label.schedule")}); tabs.addTab({id: "tab-options", iconClass: "fa fa-cogs", label: node._("scheduler.label.options")}); tabs.activateTab("tab-schedule"); const scheduleList = $("#node-input-scheduleList").css("min-width", "500px").css("min-height", "150px").editableList( { removable: true, sortable: true, addItem: function(item, index, data) { const sunTimes = [ { value: "nightEnd", label: node._("node-red-contrib-chronos/chronos-config:common.list.sun.nightEnd") }, { value: "nauticalDawn", label: node._("node-red-contrib-chronos/chronos-config:common.list.sun.nauticalDawn") }, { value: "dawn", label: node._("node-red-contrib-chronos/chronos-config:common.list.sun.dawn") }, { value: "sunrise", label: node._("node-red-contrib-chronos/chronos-config:common.list.sun.sunrise") }, { value: "sunriseEnd", label: node._("node-red-contrib-chronos/chronos-config:common.list.sun.sunriseEnd") }, { value: "goldenHourEnd", label: node._("node-red-contrib-chronos/chronos-config:common.list.sun.goldenHourEnd") }, { value: "solarNoon", label: node._("node-red-contrib-chronos/chronos-config:common.list.sun.solarNoon") }, { value: "goldenHour", label: node._("node-red-contrib-chronos/chronos-config:common.list.sun.goldenHour") }, { value: "sunsetStart", label: node._("node-red-contrib-chronos/chronos-config:common.list.sun.sunsetStart") }, { value: "sunset", label: node._("node-red-contrib-chronos/chronos-config:common.list.sun.sunset") }, { value: "dusk", label: node._("node-red-contrib-chronos/chronos-config:common.list.sun.dusk") }, { value: "nauticalDusk", label: node._("node-red-contrib-chronos/chronos-config:common.list.sun.nauticalDusk") }, { value: "night", label: node._("node-red-contrib-chronos/chronos-config:common.list.sun.night") }, { value: "nadir", label: node._("node-red-contrib-chronos/chronos-config:common.list.sun.nadir") } ]; const moonTimes = [ { value: "rise", label: node._("node-red-contrib-chronos/chronos-config:common.list.moon.rise") }, { value: "set", label: node._("node-red-contrib-chronos/chronos-config:common.list.moon.set") } ]; const timeInput = { value: "time", label: node._("node-red-contrib-chronos/chronos-config:common.label.time"), icon: "fa fa-clock-o", hasValue: true, validate: PATTERN_TIME }; const sunTimeInput = { value: "sun", label: node._("node-red-contrib-chronos/chronos-config:common.label.sun"), icon: "fa fa-sun-o", options: sunTimes }; const moonTimeInput = { value: "moon", label: node._("node-red-contrib-chronos/chronos-config:common.label.moon"), icon: "fa fa-moon-o", options: moonTimes }; const customInput = { value: "custom", label: node._("node-red-contrib-chronos/chronos-config:common.label.custom"), icon: "fa fa-user-o", hasValue: true, validate: PATTERN_CUSTOM_TIME }; const cronInput = { value: "crontab", label: node._("scheduler.label.crontab"), icon: "fa fa-table", hasValue: true, validate: PATTERN_CRONTAB }; const fullMessageInput = { value: "fullMsg", label: node._("node-red-contrib-chronos/chronos-config:common.label.fullMessage"), hasValue: false }; const validateNumericInput = function(event, ui) { const value = parseInt($(this).spinner("value"), 10); const min = $(this).spinner("option", "min"); const max = $(this).spinner("option", "max"); if (isNaN(value) || (value < min)) { $(this).spinner("value", min); } else if (value > max) { $(this).spinner("value", max); } } const fragment = document.createDocumentFragment(); const triggerRow = $("<div/>").appendTo(fragment); const outputRow = $("<div/>", {style: "margin-top: 5px; "}).appendTo(fragment); const triggerType = $("<input/>", {type: "text", class: "node-input-triggerType"}) .appendTo(triggerRow) .typedInput({types: [timeInput, sunTimeInput, moonTimeInput, customInput, cronInput, "env", "global", "flow"]}); triggerType.typedInput("width", "280px"); triggerType._prevType = "time"; const offsetBox = $("<div/>", {style: "display: inline-block;"}).appendTo(triggerRow); const offsetLabel = $("<label/>", {title: node._("node-red-contrib-chronos/chronos-config:common.tooltip.offset"), style: "display: inline-block; width: auto; margin-bottom: 0px;"}) .html("<span style='margin-left: 6px; margin-right: 6px;'>⇔</span>") .appendTo(offsetBox); const triggerOffset = $("<input/>", {class: "node-input-triggerOffset", style: "width: 35px !important;"}) .appendTo(offsetLabel) .spinner({min: -300, max: 300, step: 1, change: validateNumericInput}); const randomLabel = $("<label/>", {title: node._("node-red-contrib-chronos/chronos-config:common.tooltip.random"), style: "width: auto; margin-left: 6px; margin-bottom: 0px;"}) .html("<i class='fa fa-random' aria-hidden='true'></i>") .appendTo(offsetBox); const triggerRandomBox = $("<div/>", {style: "display: inline-block; width: auto; margin-left: 6px;"}) .appendTo(randomLabel); const triggerRandom = $("<input/>", {class: "node-input-triggerRandom", style: "width: 35px !important;"}) .appendTo(triggerRandomBox) .spinner({min: 0, max: 300, step: 1, change: validateNumericInput}); $("<div/>", {style: "display: inline-block; width: auto; margin-left: 20px;"}) .text(node._("node-red-contrib-chronos/chronos-config:common.label.output")) .appendTo(outputRow); const outputBox = $("<div/>", {style: "display: inline-block; vertical-align: middle; width: auto; margin-left: 8px;"}).appendTo(outputRow); const outTypeRow = $("<div/>").appendTo(outputBox); const outValueRow = $("<div/>", {style: "margin-top: 5px; "}).appendTo(outputBox); const outType = $("<input/>", {type: "text", class: "node-input-outType"}) .appendTo(outTypeRow) .typedInput({types: ["global", "flow", "msg", fullMessageInput]}); outType.typedInput("width", "280px"); outType._prevType = "global"; const propValue = $("<input/>", {type: "text", class: "node-input-propValue"}) .appendTo(outValueRow) .typedInput({types: ["str", "num", "bool", "json", "jsonata", "bin", "date"]}); propValue.typedInput("width", "280px"); const msgValue = $("<input/>", {type: "text", class: "node-input-msgValue"}) .appendTo(outValueRow) .typedInput({types: ["json", "jsonata"]}); msgValue.typedInput("width", "280px"); triggerType.on("change", function() { const type = triggerType.typedInput("type"); if (type === "crontab") { offsetBox.hide(); } else { offsetBox.show(); } if (type != triggerType._prevType) { triggerType._prevType = type; if ((type != "sun") && (type != "moon")) { triggerType.typedInput("value", ""); } } }); outType.on("change", function() { const type = outType.typedInput("type"); if (type === "fullMsg") { propValue.typedInput("hide"); msgValue.typedInput("show"); } else { propValue.typedInput("show"); msgValue.typedInput("hide"); } if (type != outType._prevType) { outType._prevType = type; outType.typedInput("value", ""); } }); if (!("trigger" in data)) { data = {trigger: {type: "time", value: "", offset: 0, random: false}, output: {type: "msg", property: {name: "payload", type: "str", value: ""}}}; } else if (!("type" in data.output)) // backward compatibility to v1.8 and earlier { data.output = {type: "msg", property: {name: "payload", type: "str", value: ""}}; } triggerType.typedInput("value", data.trigger.value); triggerType._prevType = data.trigger.type; triggerType.typedInput("type", data.trigger.type); if (data.trigger.type != "crontab") { let offset = (typeof data.trigger.offset != "undefined") ? data.trigger.offset : 0; let random = 0; if (data.trigger.random === true) // backward compatibility to v1.26 and earlier { if (offset < 0) { random = -offset; offset = Math.floor(offset / 2); } else { random = offset; offset = Math.ceil(random / 2); } } else if (typeof data.trigger.random == "number") // backward compatibility to v1.23 and earlier { random = data.trigger.random; } triggerOffset.spinner("value", offset); triggerRandom.spinner("value", random); } else { triggerOffset.spinner("value", 0); triggerRandom.spinner("value", 0); } if (data.output.type == "fullMsg") { msgValue.typedInput("type", data.output.contentType ? data.output.contentType : "json"); msgValue.typedInput("value", data.output.value); } else { outType.typedInput("value", data.output.property.name); propValue.typedInput("type", data.output.property.type); propValue.typedInput("value", data.output.property.value); } outType._prevType = data.output.type; outType.typedInput("type", data.output.type); item[0].appendChild(fragment); } }); // backward compatibility to v1.23 and earlier if (typeof node.delayOnStart == "undefined") { $("#node-input-delayOnStart").prop("checked", true); } $("#node-input-delayOnStart").on("change", function() { $("#node-input-onStartDelay").attr("disabled", !$("#node-input-delayOnStart").prop("checked")); }); node.schedule.forEach(data => { scheduleList.editableList("addItem", data); }); }, oneditsave: function() { const node = this; const multiPort = $("#node-input-multiPort").prop("checked"); const nextEventPort = $("#node-input-nextEventPort").prop("checked"); node.schedule = []; node.outputs = 0; $("#node-input-scheduleList").editableList("items").each(function(index) { const data = {trigger: {}, output: {}}; const triggerType = $(this).find(".node-input-triggerType"); const triggerOffset = $(this).find(".node-input-triggerOffset"); const triggerRandom = $(this).find(".node-input-triggerRandom"); const outType = $(this).find(".node-input-outType"); const propValue = $(this).find(".node-input-propValue"); const msgValue = $(this).find(".node-input-msgValue"); data.trigger.type = triggerType.typedInput("type"); data.trigger.value = triggerType.typedInput("value"); if (data.trigger.type != "crontab") { data.trigger.offset = triggerOffset.spinner("value"); data.trigger.random = triggerRandom.spinner("value"); } data.output.type = outType.typedInput("type"); if (data.output.type == "fullMsg") { data.output.contentType = msgValue.typedInput("type"); data.output.value = msgValue.typedInput("value"); } else { data.output.property = {name: outType.typedInput("value"), type: propValue.typedInput("type"), value: propValue.typedInput("value")}; } if ((data.output.type == "msg") || (data.output.type == "fullMsg")) { if (multiPort) { data.port = node.outputs++; if (data.trigger.type == "env") { if (data.trigger.value.includes("${")) { data.label = data.trigger.value; } else { data.label = "${" + data.trigger.value + "}"; } } else if ((data.trigger.type == "global") || (data.trigger.type == "flow")) { let ctx = RED.utils.parseContextKey(data.trigger.value); data.label = data.trigger.type + "." + ctx.key + (ctx.store ? " (" + ctx.store + ")" : ""); } else { function fmtOffset(offset) { return (offset > 0) ? ("+" + offset) : offset; } if ((data.trigger.type == "time") || (data.trigger.type == "custom") || (data.trigger.type == "crontab")) { data.label = data.trigger.value; } else { data.label = node._("node-red-contrib-chronos/chronos-config:common.list." + data.trigger.type + "." + data.trigger.value); } if (data.trigger.random > 0) { data.label += " ⇔ " + fmtOffset(data.trigger.offset - data.trigger.random / 2) + "/" + fmtOffset(data.trigger.offset + data.trigger.random / 2) + " " + node._("node-red-contrib-chronos/chronos-config:common.label.min"); } else if (data.trigger.offset != 0) { data.label += " ⇔ " + fmtOffset(data.trigger.offset) + " " + node._("node-red-contrib-chronos/chronos-config:common.label.min"); } } } else { node.outputs = 1; } } node.schedule.push(data); }); if (nextEventPort) { ++node.outputs; } }, oneditresize: function(size) { const listRow = $("#dialog-form div.list-row"); const otherRows = $("#dialog-form>div"); let height = size.height; for (let i=0; i<otherRows.length; ++i) { let row = $(otherRows[i]); if (row.is(":visible") && (row.attr("id") != "tabs-content")) { height -= row.outerHeight(true); } } height -= (parseInt(listRow.css("marginTop")) + parseInt(listRow.css("marginBottom"))); if ($("#tab-schedule").is(":visible")) { $("#node-input-scheduleList").editableList("height", height); } } }); })(); </script>