Extending Blockly with new openHAB commands

Save values for retriggered rules.

As I was working so comprehensively on the named timers I finally thought it would be nice to have the functionality to be able to save own variables in a way that they survive a rule retrigger which is something I use a lot when I am developing DSL rules. So I added that as well which is now provided as follows.

Note that it does not persist the values in openhab but just remembers the value in a this-variable-Collection (see code) similar to the timers. I might eventually use the API “PersistenceExtensions” to provide that functionality if this is asked by the community.

@ysc As I know you like looking at how the blocks present themselves and what the naming is like -please respond if you like it to be changed (e.g. we could use store instead of save). These are the last things I will do before I do the finally push to the git repo for your final review.

Here is the blockly representation

and this is the code that is generated (it uses its own Set named globalvars to keep track of the variables)

The first time the rule is triggered it says “Stefan”, the next time it says “Rich” which proves the rule remembers the content of that variable

if (typeof this.globalvars === 'undefined') {
	 this.globalvars =[]
}
var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID)
if ((this.globalvars['myName']) == 'Stefan') {
  this.globalvars['myName'] = 'Rich'
} else {
  this.globalvars['myName'] = 'Stefan'
}
logger.info((this.globalvars['myName']))

This following one is a pretty confusing one: It actually uses a Blockly Variable as an indirection that contains the variable name (hence the named slot) into which the value should be stored. I would rather say that this a seldom situation where you would use something like that and usually I wouldn’t recommend doing that but it shows that even this is working and I am sure it will trigger your creativity to do something weird with it :wink:

var theVariableName;
if (typeof this.globalvars === 'undefined') {
	 this.globalvars =[]
}
var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID)


theVariableName = 'varname';
if ((this.globalvars[theVariableName]) == 'Stefan') {
  this.globalvars[theVariableName] = 'Rich'
} else {
  this.globalvars[theVariableName] = 'Stefan'
}
logger.info((this.globalvars[theVariableName]))

I hope that this all concludes the first drop for the new blockly enhancements.

@ysc let me know if you are fine with (or not), so I can push it. Note that I tried to squash the multiple commits but it tells that some of the commits already have multiple parents so I failed and gave up doing it. Hopefully you can do it when merging the pull request. Thanks for the support.

cheers
Stefan