It happens from time to time we want to toggle a bit in the tags.
The shortest way of writing this is currently
system.tag.writeBlocking(['path/to/my/tag'], [not system.tag.readBlocking(['path/to/my/tag']).value])
This is not so good for a few reasons:
- The calls are blocking, and involve two tag accesses, so can be slow.
- The tagpath needs to be given twice
- The list creation is a bit unneccesary (I never had to toggle two tags at once)
An alternative way is using the async tunctions like this
def handleRead(tagResult): system.tag.writeAsync(['path/to/my/tag'], not tagResult.value) system.tag.readAsync(['path/to/my/tag'], handleRead)
This also has issues:
- it uses more code
- it still causes two tag accesses
- you get an extra function the scope which is hard to name and can cause naming collisions with other handlers
- if you want to wait for the write to be complete, you need to add yet another callback function defined
To me, the toggle situation is so standard, it would be nice to have a toggle function in the tag scripts:
- It should only take one tag (I can’t think of a situation where I need to toggle multiple tags at onces)
- It should do a read and write in the server scope, so not cause an extra roundtrip
- It could accept an optional callback to get back the written value and quality, for those who want to wait for the toggle result
So it could be used like this
def handleAfterToggle(qv): system.gui.showMessageBox("New value is %s" % qv.value) system.tag.toggle('path/to/my/tag', handleAfterToggle)
What do you guys think of this? Do you also run into this issue? Or is it just me?