Skip to content

Commit

Permalink
Fix scripts category (rename to "Run & Process")
Browse files Browse the repository at this point in the history
Signed-off-by: Yannick Schaus <github@schaus.net>
  • Loading branch information
ghys committed Dec 4, 2021
1 parent f249ccb commit eb40c1d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Additionally there is a block that allows transformations based on the Map-File functionality, regular-expressions and applying JSON-paths
*/
import Blockly from 'blockly'
import { addOSGiService } from './utils'

export default function defineOHBlocks_Scripts (f7, scripts) {
/*
Expand All @@ -15,7 +16,7 @@ export default function defineOHBlocks_Scripts (f7, scripts) {
init: function () {
this.appendValueInput('scriptfile')
.setCheck('String')
.appendField('Call Script File')
.appendField('call script file')
this.setInputsInline(true)
this.setPreviousStatement(true, null)
this.setNextStatement(true, null)
Expand All @@ -32,9 +33,9 @@ export default function defineOHBlocks_Scripts (f7, scripts) {
Blockly.JavaScript['oh_callscriptfile'] = function (block) {
const scriptExecution = Blockly.JavaScript.provideFunction_(
'scriptExecution',
['var ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER + ' = Java.type("org.openhab.core.model.script.actions.ScriptExecution");'])
['var ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + ' = Java.type(\'org.openhab.core.model.script.actions.ScriptExecution\');'])
let scriptfile = Blockly.JavaScript.valueToCode(block, 'scriptfile', Blockly.JavaScript.ORDER_ATOMIC)
let code = `scriptExecution.callScript(${scriptfile});\n`
let code = `${scriptExecution}.callScript(${scriptfile});\n`
return code
}

Expand All @@ -43,19 +44,20 @@ export default function defineOHBlocks_Scripts (f7, scripts) {
* Parameters can be provided with the special parameter block oh_scriptparam
* Blockly part
*/
Blockly.Blocks['oh_callscript'] = {
Blockly.Blocks['oh_runrule'] = {
init: function () {
this.appendValueInput('scriptname')
this.appendValueInput('ruleUID')
.setCheck('String')
.appendField('Call Script')
.appendField('run rule or script')
this.appendValueInput('parameters')
.appendField('with parameters')
.setCheck('Dictionary')
this.setInputsInline(true)
this.setInputsInline(false)
this.setPreviousStatement(true, null)
this.setNextStatement(true, null)
this.setColour(0)
this.setTooltip('Calls a script provided via the openhab ui scripts section')
this.setHelpUrl('') // TODO provide a openhab documentation URL
this.setTooltip('Run a rule or script with a certain UID, and optional parameters')
// this.setHelpUrl('') // TODO provide a openhab documentation URL
}
}

Expand All @@ -64,11 +66,10 @@ export default function defineOHBlocks_Scripts (f7, scripts) {
* Parameters can be provided with the special parameter block oh_scriptparam
* Code part
*/
Blockly.JavaScript['oh_callscript'] = function (block) {
Blockly.JavaScript['oh_runrule'] = function (block) {
const ruleManager = addOSGiService('ruleManager', 'org.openhab.core.automation.RuleManager')
let scriptname = Blockly.JavaScript.valueToCode(block, 'scriptname', Blockly.JavaScript.ORDER_ATOMIC)
const ruleUID = Blockly.JavaScript.valueToCode(block, 'ruleUID', Blockly.JavaScript.ORDER_ATOMIC)
const scriptParameters = Blockly.JavaScript.valueToCode(block, 'parameters', Blockly.JavaScript.ORDER_ATOMIC)
const scriptName = Blockly.JavaScript.valueToCode(block, 'scriptname', Blockly.JavaScript.ORDER_ATOMIC)

// create a function for the generated code that maps json key-values into a map structure
const convertDictionaryToHashMap = Blockly.JavaScript.provideFunction_(
Expand All @@ -84,30 +85,10 @@ export default function defineOHBlocks_Scripts (f7, scripts) {
'}'
])

let code = `${ruleManager}.runNow(${scriptName}, true, ${convertDictionaryToHashMap}(${scriptParameters}));\n`
let code = `${ruleManager}.runNow(${ruleUID}, true, ${convertDictionaryToHashMap}(${scriptParameters}));\n`
return code
}

/*
* function that allow to call classes within the osgi container
* e.g. service -> 'ruleManager', class -> 'org.openhab.core.automation.RuleManager'
*
* currently used only for script blocks but eventually be moved into a more general place
*/
function addOSGiService (serviceName, serviceClass) {
const addServiceName = Blockly.JavaScript.provideFunction_(
'addFrameworkService', [
'function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + ' (serviceClass) {',
' var bundleContext = Java.type(\'org.osgi.framework.FrameworkUtil\').getBundle(scriptExtension.class).getBundleContext();',
' var serviceReference = bundleContext.getServiceReference(serviceClass);',
' return bundleContext.getService(serviceReference);',
'}'
])
return Blockly.JavaScript.provideFunction_(
serviceName,
[`var ${Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_} = ${addServiceName}('${serviceClass}');`])
}

/*
* Allow transformations via different methods
* inputs
Expand All @@ -122,34 +103,35 @@ export default function defineOHBlocks_Scripts (f7, scripts) {
Blockly.Blocks['oh_transformation'] = {
init: function () {
this.appendValueInput('value')
.setAlign(Blockly.ALIGN_RIGHT)
.appendField('transform')
this.appendValueInput('input')
.appendField('with method')
.appendField(new Blockly.FieldDropdown([['map', 'MAP'], ['regex', 'REGEX'], ['jsonpath', 'JSONPATH']]), 'type')
.appendField('based on')
this.appendValueInput('function')
.appendField('apply')
.appendField(new Blockly.FieldTextInput('MAP'), 'type')
// .appendField(new Blockly.FieldDropdown([['MAP', 'MAP'], ['REGEX', 'REGEX'], ['JSONPATH', 'JSONPATH']]), 'type')
.appendField('with')

this.setInputsInline(true)
this.setInputsInline(false)
this.setOutput(true, null)
this.setColour(0)

let thisBlock = this
this.setTooltip(function () {
let type = thisBlock.getFieldValue('type')
let TIP = {
'MAP': 'transforms an input via a map-file config',
'REGEX': 'transforms / filters an input by applying a regular expression',
'JSONPATH': 'transforms / filters an input, e.g. a json item state, by applying a json path'
const type = thisBlock.getFieldValue('type')
switch (type) {
case 'MAP':
return 'transforms an input via a map file. Specify the file as the function'
case 'REGEX':
return 'transforms / filters an input by applying the provided regular expression'
case 'JSONPATH':
return 'transforms / filters an JSON input by executing the provided JSONPath query'
default:
return 'transforms the input with the ' + type + ' transformation'
}
return TIP[type]
})
this.setHelpUrl(function () {
let type = thisBlock.getFieldValue('type')
let URL = {
'MAP': 'https://www.openhab.org/addons/transformations/map/',
'REGEX': 'https://www.openhab.org/addons/transformations/regex/',
'JSONPATH': 'https://www.openhab.org/addons/transformations/jsonpath/'
}
return URL[type]
const type = thisBlock.getFieldValue('type')
return 'https://www.openhab.org/addons/transformations/' + type.toLowerCase() + '/'
})
}
}
Expand All @@ -159,20 +141,20 @@ export default function defineOHBlocks_Scripts (f7, scripts) {
* Code part
*/
Blockly.JavaScript['oh_transformation'] = function (block) {
addTransformation()
const value = Blockly.JavaScript.valueToCode(block, 'value', Blockly.JavaScript.ORDER_ATOMIC)
let input = Blockly.JavaScript.valueToCode(block, 'input', Blockly.JavaScript.ORDER_ATOMIC)
const type = block.getFieldValue('type')
const transformation = addTransformation()
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)

let code = `transformation.transform('${type}', ${input}, ${value})`
let code = `${transformation}.transform('${transformationType}', ${transformationFunction}, ${transformationValue})`
return [code, 0]
}

/*
* add transformation class to rule
*/
function addTransformation () {
Blockly.JavaScript.provideFunction_(
return Blockly.JavaScript.provideFunction_(
'transformation',
['var ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + ' = Java.type(\'org.openhab.core.transform.actions.Transformation\');'])
}
Expand Down
19 changes: 19 additions & 0 deletions bundles/org.openhab.ui/web/src/assets/definitions/blockly/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Blockly from 'blockly'

/*
* function that allow to call classes within the osgi container
* e.g. service -> 'ruleManager', class -> 'org.openhab.core.automation.RuleManager'
*/
export function addOSGiService (serviceName, serviceClass) {
const addServiceName = Blockly.JavaScript.provideFunction_(
'addFrameworkService', [
'function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + ' (serviceClass) {',
' var bundleContext = Java.type(\'org.osgi.framework.FrameworkUtil\').getBundle(scriptExtension.class).getBundleContext();',
' var serviceReference = bundleContext.getServiceReference(serviceClass);',
' return bundleContext.getService(serviceReference);',
'}'
])
return Blockly.JavaScript.provideFunction_(
serviceName,
[`var ${Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_} = ${addServiceName}('${serviceClass}');`])
}
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@
</value>
</block>
</category>

<category name="Value Storage">
<block type="oh_store_value">
<value name="value">
Expand All @@ -601,23 +602,41 @@
</block>
</category>

<category name="Scripts">
<category name="Run &amp; Process">
<block type="oh_callscriptfile">
<value name="message">
<value name="scriptfile">
<shadow type="text">
<field name="TEXT">scriptname.script</field>
</shadow>
</value>
</block>
<block type="oh_callscript">
<value name="message">
<block type="oh_runrule">
<value name="ruleUID">
<shadow type="text">
<field name="TEXT">ruleUID</field>
</shadow>
</value>
<value name="parameters">
<shadow type="dicts_create_with">
<mutation items="0" />
</shadow>
</value>
</block>
<sep gap="48" />
<block type="oh_transformation">
<value name="function">
<shadow type="text">
<field name="TEXT">function</field>
</shadow>
</value>
<value name="value">
<shadow type="text">
<field name="TEXT">scriptname</field>
<field name="TEXT">value</field>
</shadow>
</value>
</block>
<block type="oh_transformation" />
</category>

<category name="Logging &amp; Output">
<block type="oh_log">
<value name="message">
Expand Down

0 comments on commit eb40c1d

Please sign in to comment.