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] Allow item xxx to be used for get xxx of item #2037

Merged
Merged
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 @@ -8,6 +8,7 @@ import { javascriptGenerator } from 'blockly/javascript'
import { FieldItemModelPicker } from './fields/item-field'
import { FieldThingPicker } from './fields/thing-field'
import api from '@/js/openhab/api'
import { blockGetCheckedInputType } from './utils'

export default function (f7, isGraalJs) {
/* Helper block to allow selecting an item */
Expand Down Expand Up @@ -190,7 +191,7 @@ export default function (f7, isGraalJs) {
block._updateType(newMode)
})
this.appendValueInput('item')
.setCheck('oh_itemtype')
.setCheck(['oh_itemtype', 'oh_item'])
.appendField('get ')
.appendField(dropdown, 'attributeName')
.appendField('of item')
Expand All @@ -201,7 +202,7 @@ export default function (f7, isGraalJs) {
this.setTooltip('Retrieve a specific attribute from the item. Note that groups and tags return a list and should be used with the loops-block \'for each item ... in list\'. ')
this.setTooltip(function () {
const attributeName = block.getFieldValue('attributeName')
const TIP = {
let TIP = {
'Name': 'name of the Item (string)',
'Label': 'label of the Item (string)',
'State': 'state of the Item (string)',
Expand All @@ -212,7 +213,7 @@ export default function (f7, isGraalJs) {
'NumericState': 'numeric state of the Item (number)',
'QuantityState': 'Unit of Measurement / quantity state of Item (Quantity)'
}
return TIP[attributeName]
return TIP[attributeName] + ' \n Note: make sure to use "get item xxx"-Block for the connected block when working with Variables, not "item xxx"-Block'
})
this.setHelpUrl('https://www.openhab.org/docs/configuration/blockly/rules-blockly-items-things.html#get-particular-attributes-of-an-item')
},
Expand Down Expand Up @@ -256,11 +257,13 @@ export default function (f7, isGraalJs) {
*/
javascriptGenerator['oh_getitem_attribute'] = function (block) {
const theItem = javascriptGenerator.valueToCode(block, 'item', javascriptGenerator.ORDER_ATOMIC)
const inputType = blockGetCheckedInputType(block, 'item')
let attributeName = block.getFieldValue('attributeName')

if (isGraalJs) {
attributeName = attributeName.charAt(0).toLowerCase() + attributeName.slice(1)
return [`${theItem}.${attributeName}`, 0]
let code = (inputType === 'oh_item') ? `items.getItem(${theItem}).${attributeName}` : `${theItem}.${attributeName}`
return [code, 0]
} else {
if (attributeName === 'Tags' || attributeName === 'GroupNames') {
return [`Java.from(${theItem}.get${attributeName}())`, 0]
Expand Down