Skip to content

Commit

Permalink
[blockly] Add timer context (openhab#2575)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
stefan-hoehn authored May 15, 2024
1 parent 88015e3 commit a32fd66
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand All @@ -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')
}
}
Expand All @@ -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 {
Expand All @@ -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
*
Expand All @@ -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')
}
Expand All @@ -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')
}
}
Expand All @@ -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':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@
</shadow>
</value>
</block>
<block type="oh_timer_context" v-if="isGraalJs" />
<block type="oh_timer_cancel">
<value name="timerName">
<shadow type="text">
Expand Down

0 comments on commit a32fd66

Please sign in to comment.