Skip to content

Commit

Permalink
Blockly: Support String/Number on eventcontext state/command (#1992)
Browse files Browse the repository at this point in the history
Fixes #1991.

Support conversion of event context types itemState, oldItemState,
itemCommand as String or Number by supplying a mutating block with a
choise of Number / String.

Also-by: Florian Hotze <[email protected]>
Signed-off-by: Stefan Höhn <[email protected]>
  • Loading branch information
stefan-hoehn authored Jul 27, 2023
1 parent 3245032 commit edc124e
Showing 1 changed file with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export default function defineOHBlocks_Scripts (f7, isGraalJs, scripts) {
['received command', 'itemCommand'],
['triggered channel', 'channel'],
['triggered event', 'event']
]),
], this.handleTypeSelection.bind(this)),
'contextInfo')
this.contextInfo = this.getFieldValue('contextInfo')
this.setInputsInline(true)
Expand All @@ -195,23 +195,67 @@ export default function defineOHBlocks_Scripts (f7, isGraalJs, scripts) {
this.setHelpUrl('https://www.openhab.org/docs/configuration/blockly/rules-blockly-run-and-process.html#retrieve-rule-context-information')
},
onchange: function (event) {
let contextInfo = this.getFieldValue('contextInfo')
const contextInfo = this.getFieldValue('contextInfo')
const asType = this.getFieldValue('asType')
if (this.contextInfo !== contextInfo) {
this.contextInfo = contextInfo
if (contextInfo === 'itemName') {
this.setOutput(true, 'oh_item')
console.log('type = oh_item')
} else {
this.setOutput(true, 'String')
console.log('type = State String')
}
}

if (this.asType !== asType) {
this.asType = asType
if (this.methodName === 'itemState' || this.methodName === 'oldItemState' || this.methodName === 'itemCommand') {
if (asType === 'asNumber') {
this.setOutput(true, 'Number')
} else if (asType === 'asQuantity') {
this.setOutput(true, 'oh_quantity')
} else {
this.setOutput(true, 'String')
}
}
}
},
handleTypeSelection: function (methodName) {
if (this.methodName !== methodName) {
this.methodName = methodName
this.updateShape()
}
},
updateShape: function () {
if (this.methodName === 'itemState' || this.methodName === 'oldItemState' || this.methodName === 'itemCommand') {
if (!this.getInput('asTypeInput')) {
this.appendDummyInput('asTypeInput').appendField(new Blockly.FieldDropdown([
['as String', 'asString'],
['as Number', 'asNumber'],
['as Quantity', 'asQuantity']
]),
'asType')
}
} else {
if (this.getInput('asTypeInput')) {
this.removeInput('asTypeInput')
}
}
}
}

javascriptGenerator['oh_context_info'] = function (block) {
const contextInfo = block.getFieldValue('contextInfo')
const type = block.getFieldValue('asType')
if (contextInfo === 'ruleUID') return ['ctx.ruleUID', javascriptGenerator.ORDER_ATOMIC]
if (contextInfo === 'itemState' || contextInfo === 'oldItemState' || contextInfo === 'itemCommand') {
if (type === 'asNumber') {
return [`parseFloat(event.${contextInfo}.toString())`, javascriptGenerator.ORDER_ATOMIC]
} else if (type === 'asQuantity') {
return [`Quantity(event.${contextInfo}.toString())`, javascriptGenerator.ORDER_ATOMIC]
} else {
return [`event.${contextInfo}.toString()`, javascriptGenerator.ORDER_ATOMIC]
}
}
return [`event.${contextInfo}`, javascriptGenerator.ORDER_ATOMIC]
}

Expand Down

0 comments on commit edc124e

Please sign in to comment.