Question about Scripting Data Source
-
Hey hey, I'm looking for an explanation for this wording in the Contextual help for the Scripting data source
Finally, scripts in the scripting data source maintain their context states for the lifecycle of the data source runtime.
I understand this to mean that if I create a variable using var it should stick around in that script until the mango is reset. This is what I did
var EG_1_Last; EPoint_I.set(EG_1.value - EG_1_Last); EG_1_Last = EG_1.value;
EG_1_Last however is reinitialized each time the script runs where I was hoping that it would maintain the value from the last run.
-
Hi psysak,
Using var is actually to indicate it should have a scope restriction if possible. I tend to assign things to
this.variableName
as the objectthis
is the scope of the scripting data source (for the outermost layer of the JavaScript, at least)So if early on you do something like...
global = this;
You can refer to global scope or assign things there by using the new variable 'global'
Also, you would need to have some if condition to prevent the assignment statement if you didn't want it to run again. You can see an example of that pattern in the script from this thread: https://forum.infiniteautomation.com/topic/3061/publish-one-day-s-data-once-a-day-to-sql/2
var pvd = this.pvd; if(!pvd) { pvd = this.pvd = com.serotonin.m2m2.Common.databaseProxy.newPointValueDao(); }
-
Thanks man! Really appreciate the help, I'm learning JavaScript by jumping in the deep end :)
-
Hey Phil, so that I can start to understand this myself, I'm creating an instance of a variable which exists in this context right?
I want to see what the current value of the point is so I'm trying to do print(pvd) but that prints out com.infiniteautomation.nosql.MangoNoSqlPointValueDao@10a2b86. If I look at that I'm led to think that the declaration you mention before is, well as the name suggests, proxy for the current database type that the system is running. So the object I created is actually a DAO for the MAngoNoSQLPoint type. To try to figure out how I can print the current value of this point I am now going to go poke around on github to see if I can't figure out what parameters and functions this NoSQL type supports.
Am I on the right track?
By the way it works, the scripting point type that I write the result to has correct values I think.
-
I'm not sure what you're attempting to achieve with the PointValueDao, so I can't say if it's the right track or not. But, you can refer to the interface class for what functions are available since the underlying NoSQL implementation is not open source. https://github.com/infiniteautomation/ma-core-public/blob/main/Core/src/com/serotonin/m2m2/db/dao/PointValueDao.java
Those would be called like,
//Get all values for point ID 1 from time 0 (1970) unto now. pvd.getPointValuesBetween(1, 0, new Date().getTime());
-
I'm just trying to picture this whole thing in my head. I guess what I'm trying to figure out right now is just what object type is pvd, what properties it has and what methods it contains. This isn't for anything other than trying to understand it.
-
I'm starting to get it man :)
-
@phildunlap said in Question about Scripting Data Source:
var pvd = this.pvd;
if(!pvd) {
pvd = this.pvd = com.serotonin.m2m2.Common.databaseProxy.newPointValueDao();
}Phil, I just noticed that this seems to also work for meta data points, is that correct? I did as you showed and even using the meta data point it remembers the variable from the last time it ran. Is there any issue with having, let's say, ten meta points all using this approach?
Essentially, what I've done is this;
Read in current value of a point NewValue = Current value - old value Old value = Current value
I'm banking on the fact that the script remembers the old value between runs.
-
Yes Meta points have this behavior as well - they reuse their script engines. Meta script execution is all within a function block, so
var
declared variables have limited scope. The pattern works though, as with my test script,var test = this.test; if(typeof test === 'undefined') { p.set(1); test = this.test = "test"; } else if(test === "test") { p.set(2); }