Please Note This forum exists for community support for the Mango product family and the Radix IoT Platform. Although Radix IoT employees participate in this forum from time to time, there is no guarantee of a response to anything posted here, nor can Radix IoT, LLC guarantee the accuracy of any information expressed or conveyed. Specific project questions from customers with active support contracts are asked to send requests to support@radixiot.com.

Radix IoT Website Mango 3 Documentation Website Mango 4 Documentation Website

A script JsonEmport example


  • Here's a simple script to add a particular event detector to all data points identified by either a static or a query watchlist:

    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 < 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 < 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" 
                          && 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 < 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 < 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}));
    

    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.

    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.

    8/23/19 edit: I believe the name field an no longer be blank, as in the original posting.