Skip to content

Commit

Permalink
[blockly] Add Quantity support to round block (#2000)
Browse files Browse the repository at this point in the history
Round block now also takes a Qty block as input.
Output type changes according to input.

Also-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Stefan Höhn <mail@stefanhoehn.com>
  • Loading branch information
stefan-hoehn authored Aug 6, 2023
1 parent edc124e commit ffb4ce6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import Blockly from 'blockly'
import { javascriptGenerator } from 'blockly/javascript'
import { blockGetCheckedInputType } from './utils'

export default function (f7, isGraalJs) {
Blockly.Blocks['oh_bit_not'] = {
Expand Down Expand Up @@ -88,13 +89,25 @@ export default function (f7, isGraalJs) {
block.updateType(operation)
})
this.appendValueInput('NUM')
.setCheck('Number')
.setCheck(['Number', 'oh_quantity'])
.appendField(dropDown, 'op')

this.setColour('%{BKY_MATH_HUE}')
this.setInputsInline(false)
this.setTooltip('Round a number up or down')
this.setHelpUrl('https://www.openhab.org/docs/configuration/blockly/rules-blockly-math.html#round')
this.setOutput(true, 'Number')
this.setOutput(true, null)
},
updateShape_: function () {
if (this.getInput('NUM')) {
let type = blockGetCheckedInputType(this, 'NUM')
if (type) {
this.setOutput(true, type)
}
}
},
onchange: function () {
this.updateShape_()
},
updateType: function (type) {
if (type === 'toFixed') {
Expand All @@ -111,13 +124,20 @@ export default function (f7, isGraalJs) {
this.removeInput('declabel')
this.setInputsInline(false)
}
this.updateShape_()
}
}

javascriptGenerator['math_round'] = function (block) {
const math_number = javascriptGenerator.valueToCode(block, 'NUM', javascriptGenerator.ORDER_FUNCTION_CALL)
const inputType = blockGetCheckedInputType(block, 'NUM')
const math_number_input = javascriptGenerator.valueToCode(block, 'NUM', javascriptGenerator.ORDER_FUNCTION_CALL)
let math_number = math_number_input
if (inputType === 'oh_quantity') {
math_number = math_number_input + '.float'
}
const decimals = javascriptGenerator.valueToCode(block, 'DECIMALS', javascriptGenerator.ORDER_NONE)
const operand = block.getFieldValue('op')

let code = ''
if (operand !== 'toFixed') {
let method = ''
Expand All @@ -136,6 +156,10 @@ export default function (f7, isGraalJs) {
} else {
code = `(${math_number}).toFixed(${decimals})`
}

if (inputType === 'oh_quantity') {
code = `Quantity((${code}).toString() + ' ' + ${math_number_input}.symbol)`
}
return [code, 0]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export function blockGetCheckedInputType (block, inputName) {
// Get the input type checks for this block
const thisBlock = block.getInput(inputName).connection.getCheck()
// Get the output type checks for the connected block
const connectedBlock = block.getInput(inputName).connection.targetBlock().outputConnection.getCheck()
const connectedBlock = block.getInput(inputName).connection.targetBlock()?.outputConnection.getCheck()
// Skip if no checks are available
if (!thisBlock || !connectedBlock) return ''
// Find any intersection in the checklist
Expand Down

0 comments on commit ffb4ce6

Please sign in to comment.