How to set a datapoint value by XID ?
-
We learned from the Forum that we can use mango core java call to get the datapoint value like this:
function getDataPointValueByXID(xid){
var pvd = this.pvd;
if(!pvd) {
pvd = this.pvd = com.serotonin.m2m2.Common.databaseProxy.newPointValueDao();
}
var dpid = com.serotonin.m2m2.db.dao.DataPointDao.instance.getDataPoint(xid).getId();
var returnValue = String(pvd.getLatestPointValues(dpid,1)[0].value);
return returnValue;
}Is there a way to setDataPointValueByXid(xid) ?
Thank's !
-
Hi Jenny,
Yes! Such a function in the Mango scripting environment could work like this,
function setDataPointValueByXID(xid, value) { var dataPoints = DataPointQuery.query("eq(xid,"+xid+")"); if( dataPoints.length !== 1 ) throw "Data point with xid '" + xid + "' not found."; if( dataPoints[0].runtime === null ) throw "Data point with xid '" + xid + "' is not running."; dataPoints[0].runtime.set(value); }
This will go out like a normal set from a scripting environment to devices, and is subject to logging types. If you're saving history data, you could add a time argument and put this on a scripting data source with 'Saves historic' which will not send out the sets nor be subject to logging properties.
-
@phildunlap
Dear Phil , thank's ! awesome!
It looks like the version 3.3.x call system function has changed. Is there any documentation for us to refer to? The above getDataPointValueByXID function, how to write it will be more in line with the future standard fucntion call ? -
I would have to see the context for the code you posted to know what it was specifically trying to achieve. That's a good way to go if you want the last value stored in the database. If you know the point is running and you want cached values in the result, you can do something like,
function getDataPointValueByXID(xid) { var dataPoints = DataPointQuery.query("eq(xid,"+xid+")"); if( dataPoints.length !== 1 ) throw "Data point with xid '" + xid + "' not found."; if( dataPoints[0].runtime === null ) throw "Data point with xid '" + xid + "' is not running."; return dataPoints[0].runtime.value; //runtime object is like a context point //i.e. runtime.pointValuesSince(time) }
-
Hi Phil,
Just now we upgraded to 3.6.2, and we found these call functions getDataPointValueByXID(xid) not function anymore !!Can you help us to check where the problem is?
Thanks !
Vincent.
-
Hi Phil,
It's really weird!
It only happens in the newly defined scripting, but the original defined scripting can be executed!Vincent.
-
Hi Vincent,
That sounds to me like maybe the new script that you're using doesn't have adequate script permissions to the data point in question. Perhaps your old script has superadmin permission?
-
Hi Phil,
Thanks ! you are right !
It seems that the new version (3.6) Script permissions does not automatically bring in the author's permissions. It stays blank.Vincent.
-
It should, but it doesn't look like it does in all cases. For instance, scripting appears to, but meta data points don't. I will bring this up, thanks for bringing it to our attention.