<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[A script JsonEmport example]]></title><description><![CDATA[<p dir="auto">Here's a simple script to add a particular event detector to all data points identified by either a static or a query watchlist:</p>
<pre><code>var xidPrefix = "ED_15m_status_"; //Used to see if a point already has the detector
var watchListXid = "WL_5275e48f-5819-4971-8462-0d1ed8ea8264"; //Identify the points to add to

var dataPoints = JSON.parse(JsonEmport.getConfiguration("dataPoints")).dataPoints;
var watchLists = JSON.parse(JsonEmport.getConfiguration("watchLists")).watchLists;

function findByXid(list, xid) {
    for( var k = 0; k &lt; list.length; k+=1 )
        if( list[k].xid === xid )
            return list[k];
    return undefined;
}

var wl = findByXid(watchLists, watchListXid);
if(typeof wl === 'undefined')
    throw "Couldn't find watchlist with XID " + watchListXid + " to add detectors to";

var importPoints = [];
function addDetectorToDataPoint(dp) {
    for( var l = 0; l &lt; dp.eventDetectors.length; l+=1 ) {
        if(dp.eventDetectors[l].xid.startsWith(xidPrefix)) //Don't add it twice
            return;
    }
    
    var ed = { //Gotten by configuring one and exporting it
               "type":"BINARY_STATE",
               "sourceType":"DATA_POINT",
               "xid":com.serotonin.m2m2.Common.generateXid(xidPrefix),
               "name":"",
               "alarmLevel":"URGENT",
               "durationType":"MINUTES",
               "duration":15,
               "state": (dp.textRenderer.type === "BINARY" 
                      &amp;&amp; dp.textRenderer.oneLabel === "Device Ok")
            };
            
    //Note that compatibility is not being checked, but it should just fail
    // the individual points during the import if isn't a binary point.
    dp.eventDetectors.push(ed);
    importPoints.push(dp);
}

if(wl.type === 'static') {
    //Okay, just look over an XID list
    for(var j = 0; j &lt; wl.dataPoints.length; j+=1) {
        var dp = findByXid(dataPoints, wl.dataPoints[j]);
        addDetectorToDataPoint(dp);
    }
} else if(wl.type === 'query') {
    //Okay, use the query to get the data points from the DataPointQuery
    var addToPoints = DataPointQuery.query(wl.query);
    for(var j = 0; j &lt; addToPoints.length; j+=1) {
        var dp = findByXid(dataPoints, addToPoints[j].getXid());
        addDetectorToDataPoint(dp);
    }
}

//Mango 3.5 will allow us to
//JsonEmport.setImportDuringValidation( true );

JsonEmport.doImport(JSON.stringify({"dataPoints":importPoints}));
</code></pre>
<p dir="auto">In Mango 3.5 there will be a way to tell the JsonEmport utility to run even during validation, but for now this script would have to actually run to perform the import.</p>
<p dir="auto">Edit: I said there would be a way to import during validation, and there is! The commented line about setImportDuringValidation can be uncommented to make the importer work even during script validation.</p>
<p dir="auto">8/23/19 edit: I believe the name field an no longer be blank, as in the original posting.</p>
]]></description><link>https://forum.mango-os.com/topic/3708/a-script-jsonemport-example</link><generator>RSS for Node</generator><lastBuildDate>Fri, 13 Nov 2026 02:21:27 GMT</lastBuildDate><atom:link href="https://forum.mango-os.com/topic/3708.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 09 Aug 2018 18:51:31 GMT</pubDate><ttl>60</ttl></channel></rss>