From a32fd66544c252ba0cef990e294bf6b7002f66ea Mon Sep 17 00:00:00 2001 From: stefan-hoehn Date: Wed, 15 May 2024 11:07:08 +0200 Subject: [PATCH] [blockly] Add timer context (#2575) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds the possibility to provide any information to the timer's callback block that can be used when the callback is eventually executed. Signed-off-by: Stefan Höhn --- .../definitions/blockly/blocks-timers.js | 41 ++++++++++++++++--- .../config/controls/blockly-editor.vue | 1 + 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.ui/web/src/assets/definitions/blockly/blocks-timers.js b/bundles/org.openhab.ui/web/src/assets/definitions/blockly/blocks-timers.js index 2743a78988..4e69bddd53 100644 --- a/bundles/org.openhab.ui/web/src/assets/definitions/blockly/blocks-timers.js +++ b/bundles/org.openhab.ui/web/src/assets/definitions/blockly/blocks-timers.js @@ -60,6 +60,8 @@ export default function defineOHBlocks_Timers (f7, isGraalJs) { .appendField('do with') .appendField(new Blockly.FieldDropdown([['private', 'private'], ['shared', 'shared']]), 'cache') .appendField('timer') + this.appendValueInput('context') + .appendField('with context') } else { tn.appendField('do with timer') } @@ -68,7 +70,7 @@ export default function defineOHBlocks_Timers (f7, isGraalJs) { .setCheck(null) this.setPreviousStatement(true, null) this.setNextStatement(true, null) - this.setTooltip('Create a named timer') + this.setTooltip('Create a named timer.\nUse the timer-context block to access the provided context.') this.setHelpUrl('https://www.openhab.org/docs/configuration/blockly/rules-blockly-timers-and-delays.html#after-period-of-time-do-with-timer') } } @@ -83,13 +85,14 @@ export default function defineOHBlocks_Timers (f7, isGraalJs) { const delay = javascriptGenerator.valueToCode(block, 'delay', javascriptGenerator.ORDER_ATOMIC) const timerName = javascriptGenerator.valueToCode(block, 'timerName', javascriptGenerator.ORDER_ATOMIC) const timerCode = javascriptGenerator.statementToCode(block, 'timerCode') + const context = javascriptGenerator.valueToCode(block, 'context', javascriptGenerator.ORDER_ATOMIC) if (isGraalJs) { const cacheType = getCacheType(this) let code = `if (cache.${cacheType}.exists(${timerName}) === false || cache.${cacheType}.get(${timerName}).hasTerminated()) {\n` - code += ` cache.${cacheType}.put(${timerName}, actions.ScriptExecution.createTimer(${timerName}, time.ZonedDateTime.now().${delayUnits}(${delay}), function () {\n` + code += ` cache.${cacheType}.put(${timerName}, actions.ScriptExecution.createTimer(${timerName}, time.ZonedDateTime.now().${delayUnits}(${delay}), function (timer_context) {\n` code += timerCode.replace(/^/gm, ' ') - code += ' }));\n' + code += ` }, ${context}));\n` code += '};\n' return code } else { @@ -105,6 +108,29 @@ export default function defineOHBlocks_Timers (f7, isGraalJs) { } } + /* + * Provided timer-context + * Blockly part + */ + Blockly.Blocks['oh_timer_context'] = { + init: function () { + this.appendDummyInput() + .appendField('timer context') + this.setOutput(true) + this.setColour(0) + this.setTooltip('Returns the provided context of the timer to be used within the timer statement block.') + this.setHelpUrl('https://www.openhab.org/docs/configuration/blockly/rules-blockly-timers-and-delays.html#timer-context') + } + } + + /* + * Provided timer-context + * Code part + */ + javascriptGenerator.forBlock['oh_timer_context'] = function (block) { + return ['timer_context', javascriptGenerator.ORDER_NONE] + } + /* * Simple Timer creation with cancel & reschedule on rule retriggering * @@ -124,6 +150,8 @@ export default function defineOHBlocks_Timers (f7, isGraalJs) { .appendField('do with') .appendField(new Blockly.FieldDropdown([['private', 'private'], ['shared', 'shared']]), 'cache') .appendField('timer') + this.appendValueInput('context') + .appendField('with context') } else { tn.appendField('do with timer') } @@ -134,7 +162,7 @@ export default function defineOHBlocks_Timers (f7, isGraalJs) { this.setPreviousStatement(true, null) this.setNextStatement(true, null) this.setColour(0) - this.setTooltip('Simple Timer creation with control over rule retriggering action') + this.setTooltip('Simple Timer creation with control over rule retriggering action.\nUse the timer-context block to access the provided context.') this.setHelpUrl('https://www.openhab.org/docs/configuration/blockly/rules-blockly-timers-and-delays.html#after-period-of-time-do-with-timer-with-options-on-retriggering-rule') } } @@ -150,13 +178,14 @@ export default function defineOHBlocks_Timers (f7, isGraalJs) { const timerName = javascriptGenerator.valueToCode(block, 'timerName', javascriptGenerator.ORDER_ATOMIC) const timerCode = javascriptGenerator.statementToCode(block, 'timerCode') const retrigger = block.getFieldValue('retrigger') + const context = javascriptGenerator.valueToCode(block, 'context', javascriptGenerator.ORDER_ATOMIC) if (isGraalJs) { const cacheType = getCacheType(this) let code = `if (cache.${cacheType}.exists(${timerName}) === false || cache.${cacheType}.get(${timerName}).hasTerminated()) {\n` - code += ` cache.${cacheType}.put(${timerName}, actions.ScriptExecution.createTimer(${timerName}, time.ZonedDateTime.now().${delayUnits}(${delay}), function () {\n` + code += ` cache.${cacheType}.put(${timerName}, actions.ScriptExecution.createTimer(${timerName}, time.ZonedDateTime.now().${delayUnits}(${delay}), function (timer_context) {\n` code += timerCode.replace(/^/gm, ' ') - code += ' }));\n' + code += ` }, ${context}));\n` code += '} else {\n' switch (retrigger) { case 'reschedule': diff --git a/bundles/org.openhab.ui/web/src/components/config/controls/blockly-editor.vue b/bundles/org.openhab.ui/web/src/components/config/controls/blockly-editor.vue index 17da0dc09b..6c5651f065 100644 --- a/bundles/org.openhab.ui/web/src/components/config/controls/blockly-editor.vue +++ b/bundles/org.openhab.ui/web/src/components/config/controls/blockly-editor.vue @@ -551,6 +551,7 @@ +