Sharing Multisate text rendered names across metadata point scripts
-
I have several metadata point sources with data type = Multistate. These points are either replicated or evaluated by other metadata points. Using the numerical value in the script logic leads to unreadable code and increased potential for coding errors. What is the recommended way to share the Text Renderer names between metadata point scripts without incurring unnecessary computation overhead? Is there a function call that can be used? How could a global script be used without a cron jon to accomplish this?
Currently I simply declare the Javascript equivalent of an enumerated datatype, but I have to paste it into each script. Each time I add another value, I have to copy/paste it into many other scripts. This is not optimal.
var VFDRule = { // Poseidon Well VFDn valve rule: OFF: 0, RO1: 1, // valve open RO2: 2, // valve open Either: 3, // RO valve open Both: 4, // RO valves open ON: 5, // well flush FAULT: 6, // VFD fault, LowHz: 7 }; // example enum usage by one of the scripts if (my.value == VFDRule.Either) { if (Well2.value == VFDRule.Either || Well3.value == VFDRule.Either) return my.lastValue(1).value; } return my.value;
If there's computational overhead, cron latency, or excess complexity, I'm better off using the sub-optimal copy/paste.
-
Hi Pedro,
Did you consider declaring this enumerated datatype in a global script? If you need to refer to it, you can place
print(VFDRule);
in while you're adjusting your scripts. -
Thanks. I tried a global script. It's better than multiple copy/pastes, but it does not ensure consistency with each Multistate text renderer. The result is that I still paste the declaration into the script for reference, even if it's as a comment.
The print is a good idea, but it did not work:
Could not convert result "[object Object]" to Multistate
Debug did not work either:
LOG.debug(VFDRule); DEBUG 2017-12-21 09:46:20,185 - [object Object]
It seems that I would have to declare a slightly redundant print method in the global script, following the enumerated type declaration. Then I would call it with
VFDRule.print();
Instead, I decided to paste a comment:
//VFDRule is declared in global script named "Enumerated Multistates"
in each multistate script. Although the state names are not displayed in the script, I know which global script to open in another tab. I can then be assured that the declaration is consistent with the script. In contrast, anything redundantly pasted into the script could become inconsistent with the global declaration. The only inconsistency I have to watch for now is the Multistate Text renderer. -
I believe the 'Could not convert result' means you were return'ing it. But, nevertheless, I should have suggested
print(JSON.stringify(VFDRule));
-
I only tried return temporarily as the LOG.debug did not work. I sometimes do that just for the resulting print statement, then I deleted it before saving the script.
Thanks, I forgot about stringify. I'm used to lower level languages.
I'm usingLOG.debug(print(JSON.stringify(VFDRule)));
and it's working nicely. -
Glad to hear it! But, I think you may wish to get rid of the LOG.debug portion. print() doesn't return anything so you're logging undefined. My script looks like this:
LOG.debug(print("test"));
which produces the output:
test
DEBUG 2017-12-21 09:39:40,795 - undefined