Logic Machine Forum
New JavaScript API in the upcoming firmware - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: New JavaScript API in the upcoming firmware (/showthread.php?tid=321)



New JavaScript API in the upcoming firmware - admin - 13.06.2016

We've added several JavaScript functions to simplify object access in visualization.
Let us know about any functionality that you are missing on the JavaScript side.

Docs

Alias is either object group address or object name. (e.g. '1/1/1' or 'My object').
If object is not found you will get a warning in JavaScript console.

grp.all()
Returns all known objects.

grp.tag(tags[, mode = 'or'])
Returns objects matching the given tags. Tags parameter can be either an array or a string. Mode parameter can be either 'or' (default — returns objects that have any of given tags) or 'and' (return objects that have all of given tags).

grp.find(alias)
Returns single object for the given alias.

grp.getvalue(alias)
Returns value for the given alias or undefined when object cannot be found.

grp.gettags(alias)
Returns an array with all tags that are set for the given alias or false when object is not found.

grp.write(alias, value)
grp.update(alias, value)
Sends group write request to the given alias.

grp.listen(alias, callback[, all=false])
Executes callback when the given alias value changes, only when new value is different, unless all is set to true (Note: in this case callback might get called when no value change occured, for example, when values are re-read after lost connection).
Callback receives two parameters - object reference and state. State can be either 'init' (called when callback is registered) or 'value' (called when value changes).

grp.taglisten(tag, callback[, all=false])
Same as grp.listen but listens for all objects with matching tags.

Code:
grp.listen('My object', function(object, state) {
  console.log('New object value', object.value, state);
});



RE: New JavaScript API in the upcoming firmware - Erwin van der Zwart - 14.06.2016

Hi Admin,

I would like to request the following items:

tag.listen() returns object.name, object.address, object.value, state from event trigger object of tag group so we can listen to a tag group and know what object triggers the listen event.

grp.timediff(alias) returns time difference of updatetime and os.time() as client time is not the same as server time, this way you have a cleaner value

Also add to grp.listen() / tag.listen() the option to set a deadband for callback, for setting a minimal/maximal change of value before callback is triggered. 

Maybe all the other functions from App engine should be available from API like:

grp.all()
grp.find(alias) 
grp.tag(tags [, mode])
grp.read(alias)
grp.gettags(alias) 
grp.addtags(alias, tags)  
grp.removetags(alias, tags) 
grp.removealltags(alias) 
grp.create(config) 

Does App engine already have grp.listen()? Would be good to align those commands (:

BR,

Erwin