UNPKG

node-red-contrib-chronos

Version:

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

650 lines (611 loc) 27.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-repeat"> <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="padding-top: 4px;"> <label for="node-input-mode"><i class="fa fa-sliders"></i> <span data-i18n="repeat.label.mode"></span></label> <select id="node-input-mode" style="width: auto;"> <option value="simple" data-i18n="repeat.list.mode.simple"></option> <option value="advanced" data-i18n="repeat.list.mode.advanced"></option> <option value="custom" data-i18n="repeat.list.mode.custom"></option> </select> </div> <div id="form-row-simple" class="form-row"> <label for="node-input-interval"><i class="fa fa-clock-o"></i> <span data-i18n="repeat.label.interval"></span></label> <input id="node-input-interval" style="width: 60px !important; float: left;" value="1"> <select id="node-input-intervalUnit" style="width: auto;"> <option value="seconds" data-i18n="node-red-contrib-chronos/chronos-config:common.list.unit.seconds"></option> <option value="minutes" data-i18n="node-red-contrib-chronos/chronos-config:common.list.unit.minutes"></option> <option value="hours" data-i18n="node-red-contrib-chronos/chronos-config:common.list.unit.hours"></option> </select> </div> <div id="form-row-advanced" class="form-row"> <label for="node-input-crontab"><i class="fa fa-table"></i> <span data-i18n="repeat.label.crontab"></span></label> <input type="text" id="node-input-crontab" placeholder="* * * * * * *"> </div> <div id="form-row-custom" class="form-row"> <label for="node-input-customRepetition"><i class="fa fa-repeat"></i> <span data-i18n="repeat.label.repetition"></span></label> <input type="text" id="node-input-customRepetition" style="width: 70%;"> <input id="node-input-customRepetitionType" type="hidden"> <input id="node-input-customRepetitionValue" type="hidden"> </div> <div class="form-row"> <label for="node-input-until"><i class="fa fa-step-forward"></i> <span data-i18n="repeat.label.until"></span></label> <input type="text" id="node-input-until" style="width: 70%;"> <input id="node-input-untilType" type="hidden"> <input id="node-input-untilValue" type="hidden"> </div> <div class="form-row" id="form-row-untilDate"> <label for="node-input-untilDate"><i class="fa fa-calendar"></i> <span data-i18n="repeat.label.date"></span></label> <input type="text" id="node-input-untilDate" style="width: 70%;" data-i18n="[placeholder]repeat.label.datePlaceholder"> </div> <div class="form-row" id="form-row-offsetBox"> <label for="node-input-untilOffset"><span data-i18n="node-red-contrib-chronos/chronos-config:common.label.offset"></span></label> <input id="node-input-untilOffset" style="width: 35px !important;"> <label for="node-input-untilRandom" style="width: auto;"><i class='fa fa-random' aria-hidden='true'></i></label> <input id="node-input-untilRandom" style="width: 35px !important;"> </div> <div class="form-row" style="margin-bottom: 4px; padding-top: 10px"> <label for="node-input-msgIngress" style="width: auto;"><i class="fa fa-sign-in"></i> <span data-i18n="repeat.label.msgIngress"></span></label> </div> <div class="form-row"> <select id="node-input-msgIngress" style="margin-left: 104px; width: auto;"> <option value="noop" data-i18n="repeat.list.msgIngress.noop"></option> <option value="forward" data-i18n="repeat.list.msgIngress.forward"></option> <option value="forward:forced" data-i18n="repeat.list.msgIngress.forcedForward"></option> </select> </div> <div class="form-row" style="padding-top: 10px"> <input id="node-input-preserveCtrlProps" type="checkbox" style="margin-top: 0px; margin-bottom: 1px; width: auto;"> <label for="node-input-preserveCtrlProps" style="margin-bottom: 0px; width: auto;" data-i18n="node-red-contrib-chronos/chronos-config:common.label.preserveCtrlProps"></label> </div> <div class="form-row"> <input id="node-input-ignoreCtrlProps" type="checkbox" style="margin-top: 0px; margin-bottom: 1px; width: auto;"> <label for="node-input-ignoreCtrlProps" style="margin-bottom: 0px; width: auto;" data-i18n="node-red-contrib-chronos/chronos-config:common.label.ignoreCtrlProps"></label> </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_DATE = /^([2-9]\d\d\d)-([1-9]|0[1-9]|1[0-2])-([1-9]|0[1-9]|[12]\d|3[01])$/; const PATTERN_CUSTOM_TIME = /^[a-zA-Z][0-9a-zA-Z_]*$/; const PATTERN_CRONTAB = /^[0-9a-zA-Z ,#@*/?-]+$/; RED.nodes.registerType("chronos-repeat", { category: "chronos", color: "#DEB887", icon: "chronos_repeat.svg", inputs: 1, outputs: 1, paletteLabel: "repeat", label: function() { if (this.name) { return this.name; } else if ((this.mode == "simple") && this.interval && this.intervalUnit) { return this._("repeat.label.every") + " " + this.interval + " " + this._("node-red-contrib-chronos/chronos-config:common.list.unit." + this.intervalUnit); } else if ((this.mode == "advanced") && this.crontab) { return this.crontab; } else if (this.mode == "custom") { if (typeof this.expression == "string") // backward compatibility to v1.18.x and earlier { if (this.expression.length <= 20) { return this.expression; } else { return this.expression.substring(0, 19) + "..."; } } else if (this.customRepetitionType == "jsonata") { if (this.customRepetitionValue.length <= 20) { return this.customRepetitionValue; } else { return this.customRepetitionValue.substring(0, 19) + "..."; } } else if (this.customRepetitionType == "env") { if (this.customRepetitionValue.includes("${")) { return this.customRepetitionValue; } else { return "${" + this.customRepetitionValue + "}"; } } else { return this.customRepetitionType + "." + this.customRepetitionValue; } } else { return this._("repeat.label.node"); } }, labelStyle: function() { return (this.name ? "node_label_italic" : ""); }, inputLabels: function() { return this._("node-red-contrib-chronos/chronos-config:common.label.inputPort"); }, outputLabels: function(index) { return this._("repeat.label.outputPort"); }, defaults: { name: { value: "" }, config: { value: "", type: "chronos-config", required: true }, mode: { value: "simple" }, interval: { value: 1 }, intervalUnit: { value: "minutes" }, crontab: { value: "", validate: function(v, opt) { let res = true; if (($("#node-input-mode").val() == "advanced") && !PATTERN_CRONTAB.test(v)) { res = opt ? this._("node-red-contrib-chronos/chronos-config:common.error.invalidCrontab") : false; } return res; } }, customRepetitionType: { value: "jsonata" }, customRepetitionValue: { value: "", validate: function(v, opt) { let res = true; if ($("#node-input-mode").val() == "custom") { res = RED.utils.validateTypedProperty(v, $("#node-input-customRepetition").typedInput("type"), opt); } return res; } }, untilType: { value: "nextMsg" }, untilValue: { value: "", validate: function(v, opt) { let res = true; const untilType = $("#node-input-until").typedInput("type"); if ((untilType == "time") && !PATTERN_TIME.test(v)) { res = opt ? this._("node-red-contrib-chronos/chronos-config:common.error.invalidTime") : false; } else if ((untilType == "custom") && !PATTERN_CUSTOM_TIME.test(v)) { res = opt ? this._("node-red-contrib-chronos/chronos-config:common.error.invalidName") : false; } else if (/^jsonata|env|global|flow|msg$/.test(untilType)) { res = RED.utils.validateTypedProperty(v, untilType, opt); } return res; } }, untilDate: { value: "", validate: function(v, opt) { if (!v || PATTERN_DATE.test(v)) { return true; } else { return opt ? this._("node-red-contrib-chronos/chronos-config:common.error.invalidDate") : false; } } }, untilOffset: { value: "0" }, untilRandom: { value: "0" }, msgIngress: { value: "forward" }, preserveCtrlProps: { value: false }, ignoreCtrlProps: { value: false } }, oneditprepare: function() { const node = this; 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 nextMsgInput = { value: "nextMsg", label: node._("repeat.label.nextMsg"), 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 interval = $("#node-input-interval") .spinner({ min: 1, max: 59, step: 1, change: validateNumericInput}); const customRepetition = $("#node-input-customRepetition") .typedInput({ types: [ "jsonata", "env", "global", "flow", "msg"], typeField: $("#node-input-customRepetitionType")}); customRepetition._prevType = $("#node-input-customRepetitionType").val(); customRepetition.on("change", function() { const type = customRepetition.typedInput("type"); if (type != customRepetition._prevType) { customRepetition._prevType = type; customRepetition.typedInput("value", ""); } }); const untilDate = $("#node-input-untilDate") .datepicker({ dateFormat: "yy-mm-dd", minDate: "2000-01-01", maxDate: "9999-12-31", changeYear: true, firstDay: 1}); const until = $("#node-input-until") .typedInput({ types: [ nextMsgInput, timeInput, sunTimeInput, moonTimeInput, customInput, "jsonata", "env", "global", "flow", "msg"], typeField: $("#node-input-untilType")}); until._prevType = $("#node-input-untilType").val(); until.on("change", function() { const type = until.typedInput("type"); if (type != until._prevType) { until._prevType = type; if ((type != "nextMsg") && (type != "sun") && (type != "moon")) { until.typedInput("value", ""); } } }); const untilOffset = $("#node-input-untilOffset") .spinner({ min: -300, max: 300, step: 1, change: validateNumericInput}); const untilRandom = $("#node-input-untilRandom") .spinner({ min: 0, max: 300, step: 1, change: validateNumericInput}); $("#node-input-intervalUnit").change(function() { const value = $(this).val(); switch (value) { case "seconds": { interval.spinner("option", "max", 59); break; } case "minutes": { interval.spinner("option", "max", 59); break; } case "hours": { interval.spinner("option", "max", 24); break; } } validateNumericInput.call(interval); }); $("#node-input-mode").change(function() { const value = $(this).val(); switch (value) { case "simple": { $("#form-row-simple").show(); $("#form-row-advanced").hide(); $("#form-row-custom").hide(); break; } case "advanced": { $("#form-row-simple").hide(); $("#form-row-advanced").show(); $("#form-row-custom").hide(); break; } case "custom": { $("#form-row-simple").hide(); $("#form-row-advanced").hide(); $("#form-row-custom").show(); break; } } }); until.on("change", function() { const type = until.typedInput("type"); if ((type === "nextMsg") || (type === "jsonata")) { $("#form-row-untilDate").hide(); $("#form-row-offsetBox").hide(); } else { $("#form-row-untilDate").show(); $("#form-row-offsetBox").show(); } }); // backward compatibility to v1.14 and earlier if (typeof node.mode == "undefined") { node.mode = "simple"; $("#node-input-mode").val(node.mode); } if (typeof node.msgIngress == "undefined") { node.msgIngress = "forward:forced"; $("#node-input-msgIngress").val(node.msgIngress); } // backward compatibility to v1.18 and earlier if (typeof node.customRepetitionType == "undefined") { node.customRepetitionType = "jsonata"; $("#node-input-customRepetitionType").val(node.customRepetitionType); node.customRepetitionValue = node.expression; $("#node-input-customRepetitionValue").val(node.customRepetitionValue); } // backward compatibility to v1.26 and earlier if (typeof node.untilRandom == "boolean") { if (node.untilRandom) { if (node.untilOffset < 0) { node.untilRandom = -node.untilOffset; node.untilOffset = Math.floor(node.untilOffset / 2); } else { node.untilRandom = node.untilOffset; node.untilOffset = Math.ceil(node.untilRandom / 2); } } else { node.untilRandom = 0; } untilOffset.spinner("value", node.untilOffset); untilRandom.spinner("value", node.untilRandom); } $("#node-input-mode").change(); customRepetition.typedInput("value", $("#node-input-customRepetitionValue").val()); until.typedInput("value", $("#node-input-untilValue").val()); }, oneditsave: function() { $("#node-input-customRepetitionValue").val($("#node-input-customRepetition").typedInput("value")); $("#node-input-untilValue").val($("#node-input-until").typedInput("value")); } }); })(); </script>