[blockly] context attribute block & move context blocks into Run & Process (#1239)

Signed-off-by: Stefan Höhn <stefan@andreaundstefanhoehn.de>
Also-by: Yannick Schaus <github@schaus.net>
pull/1248/head
stefan-hoehn 2021-12-19 14:14:43 +01:00 committed by GitHub
parent c8f23c55e8
commit fb426697e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 51 deletions

View File

@ -28,44 +28,4 @@ export default function (f7) {
const value = Blockly.JavaScript.valueToCode(block, 'value', Blockly.JavaScript.ORDER_ATOMIC)
return 'events.' + eventType + '(' + itemName + ', ' + value + ');\n'
}
Blockly.Blocks['oh_context_info'] = {
init: function () {
this.appendDummyInput()
.appendField('contextual info')
.appendField(new Blockly.FieldDropdown([
['rule UID', 'ruleUID'],
['event type', 'type'],
['new state of item', 'itemState'],
['previous state of item', 'oldItemState'],
['triggering item name', 'itemName'],
['received command', 'itemCommand'],
['triggered channel', 'channel']]),
'contextInfo')
this.setInputsInline(true)
this.setOutput(true, null)
this.setColour(0)
let thisBlock = this
this.setTooltip(function () {
const contextData = thisBlock.getFieldValue('contextInfo')
const TIP = {
'ruleUID': 'The current rule\'s UID',
'type': 'the event type name',
'itemState': 'the new item state (only applicable for rules with triggers related to changed and updated items)',
'oldItemState': 'the old item state (only applicable for rules with triggers related to changed and updated items)',
'itemName': 'the item name that caused the event (if relevant)',
'itemCommand': 'the command name that triggered the event',
'channel': 'the channel UID that triggered the event (only applicable for rules including a "trigger channel fired" event)'
}
return TIP[contextData]
})
this.setHelpUrl('https://www.openhab.org/docs/developer/utils/events.html')
}
}
Blockly.JavaScript['oh_context_info'] = function (block) {
const contextInfo = block.getFieldValue('contextInfo')
if (contextInfo === 'ruleUID') return ['ctx.ruleUID', Blockly.JavaScript.ORDER_ATOMIC]
return [`event.${contextInfo}`, Blockly.JavaScript.ORDER_ATOMIC]
}
}

View File

@ -50,7 +50,7 @@ export default function defineOHBlocks_Scripts (f7, scripts) {
.setCheck('String')
.appendField('run rule or script')
this.appendValueInput('parameters')
.appendField('with parameters')
.appendField('with context')
.setCheck('Dictionary')
this.setInputsInline(false)
this.setPreviousStatement(true, null)
@ -95,9 +95,6 @@ export default function defineOHBlocks_Scripts (f7, scripts) {
* - value to be transformed
* - method Map, Regular Expression, JSON-Path
* - transformation method input
* - Map: map file found in openHABs transform-folder
* - Regex: regex-expression
* - JSON-Path: JSON-Path
* Blockly part
*/
Blockly.Blocks['oh_transformation'] = {
@ -141,7 +138,9 @@ export default function defineOHBlocks_Scripts (f7, scripts) {
* Code part
*/
Blockly.JavaScript['oh_transformation'] = function (block) {
const transformation = addTransformation()
const transformation = Blockly.JavaScript.provideFunction_(
'transformation',
['var ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + ' = Java.type(\'org.openhab.core.transform.actions.Transformation\');'])
const transformationType = block.getFieldValue('type')
const transformationFunction = Blockly.JavaScript.valueToCode(block, 'function', Blockly.JavaScript.ORDER_ATOMIC)
const transformationValue = Blockly.JavaScript.valueToCode(block, 'value', Blockly.JavaScript.ORDER_ATOMIC)
@ -150,12 +149,69 @@ export default function defineOHBlocks_Scripts (f7, scripts) {
return [code, 0]
}
Blockly.Blocks['oh_context_info'] = {
init: function () {
this.appendDummyInput()
.appendField('contextual info')
.appendField(new Blockly.FieldDropdown([
['rule UID', 'ruleUID'],
['event type', 'type'],
['new state of item', 'itemState'],
['previous state of item', 'oldItemState'],
['triggering item name', 'itemName'],
['received command', 'itemCommand'],
['triggered channel', 'channel']]),
'contextInfo')
this.setInputsInline(true)
this.setOutput(true, null)
this.setColour(0)
let thisBlock = this
this.setTooltip(function () {
const contextData = thisBlock.getFieldValue('contextInfo')
const TIP = {
'ruleUID': 'The current rule\'s UID',
'type': 'the event type name',
'itemState': 'the new item state (only applicable for rules with triggers related to changed and updated items)',
'oldItemState': 'the old item state (only applicable for rules with triggers related to changed and updated items)',
'itemName': 'the item name that caused the event (if relevant)',
'itemCommand': 'the command name that triggered the event',
'channel': 'the channel UID that triggered the event (only applicable for rules including a "trigger channel fired" event)'
}
return TIP[contextData]
})
this.setHelpUrl('https://www.openhab.org/docs/developer/utils/events.html')
}
}
Blockly.JavaScript['oh_context_info'] = function (block) {
const contextInfo = block.getFieldValue('contextInfo')
if (contextInfo === 'ruleUID') return ['ctx.ruleUID', Blockly.JavaScript.ORDER_ATOMIC]
return [`event.${contextInfo}`, Blockly.JavaScript.ORDER_ATOMIC]
}
/*
* add transformation class to rule
* Allows retrieving parameters provided by a rule
* Blockly part
*/
function addTransformation () {
return Blockly.JavaScript.provideFunction_(
'transformation',
['var ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + ' = Java.type(\'org.openhab.core.transform.actions.Transformation\');'])
Blockly.Blocks['oh_context_attribute'] = {
init: function () {
this.appendValueInput('key')
.appendField('get context attribute')
.setCheck('String')
this.setInputsInline(false)
this.setOutput(true, 'any')
this.setColour(0)
this.setTooltip('Retrieve a specified attribute from the context that could be set from a calling rule or script')
}
}
/*
* Allows retrieving parameters provided by a rule
* Code part
*/
Blockly.JavaScript['oh_context_attribute'] = function (block) {
const key = Blockly.JavaScript.valueToCode(block, 'key', Blockly.JavaScript.ORDER_ATOMIC)
let code = `ctx[${key}]`
return [code, 0]
}
}

View File

@ -348,7 +348,6 @@
<shadow type="oh_thing" />
</value>
</block>
<block type="oh_context_info" />
</category>
<category name="Timers &amp; Delays">
@ -673,6 +672,15 @@
</value>
</block>
<sep gap="48" />
<block type="oh_context_info" />
<block type="oh_context_attribute">
<value name="key">
<shadow type="text">
<field name="TEXT">key</field>
</shadow>
</value>
</block>
<sep gap="48" />
<block type="oh_transformation">
<value name="function">
<shadow type="text">