Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[blockly] Add Quantity support to round block #2000

Merged
merged 2 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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