From 246e10501cd2af98499a4707bf161866311bb310 Mon Sep 17 00:00:00 2001 From: Florian Hotze Date: Mon, 16 Sep 2024 12:34:25 +0200 Subject: [PATCH] [rules] Rule Builder: Add offset support for DateTimeTrigger Signed-off-by: Florian Hotze --- README.md | 21 +++++++++++---------- src/rules/trigger-builder.js | 15 ++++++++++++++- types/rules/trigger-builder.d.ts | 9 +++++++++ types/rules/trigger-builder.d.ts.map | 2 +- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 8807c8af3..e7e798022 100644 --- a/README.md +++ b/README.md @@ -1295,11 +1295,11 @@ See [Examples](#rule-builder-examples) for further patterns. - `when()` - `or()` - - `.channel(channelName)` Specifies a channel event as a source for the rule to fire. - - `.triggered(event)` Trigger on a specific event name - - `.cron(cronExpression)` Specifies a cron schedule for the rule to fire. - - `.timeOfDay(time)` Specifies a time of day in `HH:mm` for the rule to fire. - - `.item(itemName)` Specifies an Item as the source of changes to trigger a rule. + - `.channel(channelName)`: Specifies a channel event as a source for the rule to fire. + - `.triggered(event)`: Trigger on a specific event name + - `.cron(cronExpression)`: Specifies a cron schedule for the rule to fire. + - `.timeOfDay(time)`: Specifies a time of day in `HH:mm` for the rule to fire. + - `.item(itemName)`: Specifies an Item as the source of changes to trigger a rule. - `.for(duration)` - `.from(state)` - `.fromOn()` @@ -1310,7 +1310,7 @@ See [Examples](#rule-builder-examples) for further patterns. - `.receivedCommand()` - `.receivedUpdate()` - `.changed()` - - `.memberOf(groupName)` Specifies a group Item as the source of changes to trigger the rule. + - `.memberOf(groupName)`: Specifies a group Item as the source of changes to trigger the rule. - `.for(duration)` - `.from(state)` - `.fromOn()` @@ -1321,20 +1321,21 @@ See [Examples](#rule-builder-examples) for further patterns. - `.receivedCommand()` - `.receivedUpdate()` - `.changed()` - - `.system()` Specifies a system event as a source for the rule to fire. + - `.system()`: Specifies a system event as a source for the rule to fire. - `.ruleEngineStarted()` - `.rulesLoaded()` - `.startupComplete()` - `.thingsInitialized()` - `.userInterfacesStarted()` - `.startLevel(level)` - - `.thing(thingName)` Specifies a Thing event as a source for the rule to fire. + - `.thing(thingName)`: Specifies a Thing event as a source for the rule to fire. - `changed()` - `updated()` - `from(state)` - `to(state)` - - `.dateTime(itemName)` Specifies a DateTime Item whose (optional) date and time schedule the rule to fire. - - `.timeOnly()` Only the time of the Item should be compared, the date should be ignored. + - `.dateTime(itemName)`: Specifies a DateTime Item whose (optional) date and time schedule the rule to fire. + - `.timeOnly()`: Only the time of the Item should be compared, the date should be ignored. + - `.offset()`: The offset in seconds to add to the time of the DateTime Item. Additionally, all the above triggers have the following functions: diff --git a/src/rules/trigger-builder.js b/src/rules/trigger-builder.js index 27057c823..2a2fc66ae 100644 --- a/src/rules/trigger-builder.js +++ b/src/rules/trigger-builder.js @@ -642,9 +642,11 @@ class DateTimeTriggerConfig extends TriggerConf { /** @private */ this._timeOnly = false; /** @private */ + this._offset = 0; + /** @private */ this._complete = () => true; /** @private */ - this._toOHTriggers = () => [triggers.DateTimeTrigger(this._itemName, this._timeOnly)]; + this._toOHTriggers = () => [triggers.DateTimeTrigger(this._itemName, this._timeOnly, this._offset)]; /** @private */ this.describe = (compact) => compact ? `dateTime_${this._itemName}` : `matches ${this._timeOnly ? 'time' : 'date and time'} of Item "${this._itemName}"`; } @@ -659,6 +661,17 @@ class DateTimeTriggerConfig extends TriggerConf { this._timeOnly = timeOnly; return this; } + + /** + * Specifies the offset in seconds to add to the time of the DateTime Item. + * + * @param {number} [offset=0] + * @returns {DateTimeTriggerConfig} + */ + offset (offset = 0) { + this._offset = offset; + return this; + } } module.exports = { diff --git a/types/rules/trigger-builder.d.ts b/types/rules/trigger-builder.d.ts index c1af92483..d37ab7e09 100644 --- a/types/rules/trigger-builder.d.ts +++ b/types/rules/trigger-builder.d.ts @@ -363,6 +363,8 @@ declare class DateTimeTriggerConfig extends TriggerConf { /** @private */ private _timeOnly; /** @private */ + private _offset; + /** @private */ private _complete; /** @private */ private _toOHTriggers; @@ -375,6 +377,13 @@ declare class DateTimeTriggerConfig extends TriggerConf { * @returns {DateTimeTriggerConfig} */ timeOnly(timeOnly?: boolean): DateTimeTriggerConfig; + /** + * Specifies the offset in seconds to add to the time of the DateTime Item. + * + * @param {number} [offset=0] + * @returns {DateTimeTriggerConfig} + */ + offset(offset?: number): DateTimeTriggerConfig; } import operations = require("./operation-builder"); import conditions = require("./condition-builder"); diff --git a/types/rules/trigger-builder.d.ts.map b/types/rules/trigger-builder.d.ts.map index 5fb521ad8..e53cbc772 100644 --- a/types/rules/trigger-builder.d.ts.map +++ b/types/rules/trigger-builder.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"trigger-builder.d.ts","sourceRoot":"","sources":["../../src/rules/trigger-builder.js"],"names":[],"mappings":"AAgNA;;;;;;GAMG;AACH;IACE,+CAUC;IARC,eAAe;IACf,gBAAsB;IACtB,eAAe;IACf,kBAA2B;IAC3B,eAAe;IACf,sBAAsE;IACtE,eAAe;IACf,iBAAgG;CAEnG;AAnED;;;;GAIG;AACH;IACE,mDAIC;IAFC,iBAA8B;IAC9B,mCAA2F;IAG7F,eAAe;IACf,iBAMC;IAED;;;;;OAKG;IACH,cAHW,MAAM,GACJ,oBAAoB,CAIhC;IAED;;;;;OAKG;IACH,qBAHW,MAAM,GACJ,oBAAoB,CAKhC;IAFC,kBAAgC;IAIlC,eAAe;IACf,kBAEC;CACF;AA4CD;;;;;;GAMG;AACH;IACE,gEAYC;IAVC,aAAyC;IAKzC,eAAe;IACf,kBAA2B;IA8G7B,eAAe;IACf,iBA6BC;IAzIC,oBAOW,iBAAiB,CAPX;IAGnB;;;;;OAKG;IACH,gBAFa,iBAAiB,CAK7B;IAFC,cAAqB;IAIvB;;;;OAIG;IACH,kBAFa,iBAAiB,CAQ7B;IAFC,gBAAuB;IAIzB;;;;OAIG;IACH,SAFa,iBAAiB,CAI7B;IAED;;;;OAIG;IACH,QAFa,iBAAiB,CAI7B;IAED;;;;OAIG;IACH,WAFa,iBAAiB,CAI7B;IAED;;;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AED;;;;;OAKG;IACH,qBAHW,MAAM,GACJ,iBAAiB,CAI7B;IAED;;;;;OAKG;IACH,gBAHW,MAAM,GACJ,sBAAsB,CAIlC;IAED;;;;;OAKG;IACH,eAHW,MAAM,GACJ,iBAAiB,CAI7B;IAED;;;;;OAKG;IACH,oBAHW,MAAM,GACJ,iBAAiB,CAI7B;IAED;;;;;OAKG;IACH,gBAHW,MAAM,GACJ,kBAAkB,CAI9B;IAED;;;;;OAKG;IACH,UAFa,mBAAmB,CAI/B;IAED;;;;;OAKG;IACH,mBAHW,MAAM,GACJ,qBAAqB,CAIjC;CACF;AAED;;;GAGG;AACH;IACE,iCAGC;IAFC,eAAe;IACf,uBAAoC;IAGtC;;;;OAIG;IACH,MAFa,cAAc,CAI1B;IAED;;;;;OAKG;IACH,gBAFa,WAAW,gBAAgB,CAIvC;IAED;;;;;OAKG;IACH,cAFa,WAAW,gBAAgB,CAIvC;CACF;AAuED;;;;;;GAMG;AACH;IACE,+CAUC;IARC,eAAe;IACf,gBAAsB;IACtB,eAAe;IACf,kBAA2B;IAC3B,eAAe;IACf,sBAAoE;IACpE,eAAe;IACf,iBAA4G;CAE/G;AA6XD;;;;;;GAMG;AACH;IACE,gDAYC;IAVC,eAAe;IACf,kBAAyB;IACzB,eAAe;IACf,kBAAsB;IACtB,eAAe;IACf,kBAA2B;IAC3B,eAAe;IACf,sBAAqF;IACrF,eAAe;IACf,iBAAwJ;IAG1J;;;;;OAKG;IACH,oBAHW,OAAO,GACL,qBAAqB,CAKjC;CACF"} \ No newline at end of file +{"version":3,"file":"trigger-builder.d.ts","sourceRoot":"","sources":["../../src/rules/trigger-builder.js"],"names":[],"mappings":"AAgNA;;;;;;GAMG;AACH;IACE,+CAUC;IARC,eAAe;IACf,gBAAsB;IACtB,eAAe;IACf,kBAA2B;IAC3B,eAAe;IACf,sBAAsE;IACtE,eAAe;IACf,iBAAgG;CAEnG;AAnED;;;;GAIG;AACH;IACE,mDAIC;IAFC,iBAA8B;IAC9B,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