• M
    MaP

    well I think this works for anyone interested ->

    Python 2.7.15+ (default, Nov 27 2018, 23:36:35)
    [GCC 7.3.0] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    >>> import requests
    >>> import json
    >>> bearertoken = 'Bearer <your token here>'
    >>> headers = {'Accept':'application/json','Content-Type': 'application/json','Authorization': bearertoken }
    >>> curtailxid = 'DP_05bf6e60-8a86-42a7-9284-0e74cf56f902'
    >>> xid = curtailxid
    >>> currentprice = 5555
    >>> dd =  {"dataType": "NUMERIC","timestamp": 0,"value": currentprice,"xid": "DP_05bf6e60-8a86-42a7-9284-0e74cf56f90"}
    >>> putDPurl = 'http://192.168.10.130:8080/rest/v1/point-values/' + xid + '?unitConversion=false'
    >>> #putDPurl2 = 'http://192.168.10.130:8080/rest/v2/point-values/DP_05bf6e60-8a86-42a7-9284-0e74cf56f902/attributes'
    ... r = requests.put(putDPurl,headers = headers, data=json.dumps(dd))
    >>>
    >>> r.status_code
    201
    >>> r.text
    u'{"dataType":"NUMERIC","value":5555,"timestamp":1591858602770,"annotation":null}'
    >>>
    

    posted in User help read more
  • M
    MaP

    I am struggling with updating a point value using python. I get a 200 status code and everything looks kosher but the value does not update... as you can see i write to the value get a 200 response then check the value using the v1 rest address and its still the old value.. i was able to get it to change but this may have been via the swagger testing suite. Below i try to change the value to 12.5 - get a 200 status code then read it back and see its still 55.

    map@SE2526:/$ python
    Python 2.7.15+ (default, Nov 27 2018, 23:36:35)
    [GCC 7.3.0] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import requests
    >>> import json
    >>> bearertoken = 'Bearer <insert your token here>'
    >>> headers = {'Accept':'application/json','Content-Type': 'application/json','Authorization': bearertoken }
    >>> curtailxid = 'DP_05bf6e60-8a86-42a7-9284-0e74cf56f902'
    >>> xid = curtailxid
    >>> dd =  {"annotation": "Help!","dataType": "NUMERIC","timestamp": 0,"value": 12.5,"xid": "DP_05bf6e60-8a86-42a7-9284-0e74cf56f90"}
    >>> putDPurl = 'http://192.168.10.130:8080/rest/v1/point-values/' + xid + '?unitConversion=false'
    >>> putDPurl2 = 'http://192.168.10.130:8080/rest/v2/point-values/DP_05bf6e60-8a86-42a7-9284-0e74cf56f902/attributes'
    >>> r = requests.put(putDPurl2,headers = headers, data=json.dumps(dd))
    >>>
    >>> r.status_code
    200
    >>> r.text
    u'{"annotation":"Help!","xid":"DP_05bf6e60-8a86-42a7-9284-0e74cf56f90","dataType":"NUMERIC","value":12.5,"timestamp":0}'
    >>> getDPurl = 'http://192.168.10.130:8080/rest/v1/point-values/' + xid + '/latest?useRendered=false&unitConversion=false&limit=1&useCache=true'
    >>> r = requests.get(getDPurl, timeout=5, headers=headers);
    >>> r.status_code
    200
    >>> r.text
    u'[{"cached":true,"dataType":"NUMERIC","value":55.5,"timestamp":1591856389181,"annotation":null}]'
    >>>
    

    posted in User help read more
  • M
    MaP

    I have to log the API data and add it to an existing modbus connection the mango has with a PLC so the PLC and a SCADA system can make decisions/control options based on the data.

    Things are so much easier with less secure connections! Certificate chains and such give me headaches. Its sounding like I will need to perhaps make a python script on the mango OS call it on boot and then also add a API connection to write from the python script into a mango data point. Was hoping for an easy win, alas.

    posted in User help read more
  • M
    MaP

    I noticed you could POST to https requests using httpbuilding - such as the slack integration.

    function notifyToSlack(text, attachments) {
    var url = "https://hooks.slack.com/services/xxxxxxx/yyyyyy/zzzzzzzzzzzzzzzzzzzzzzzzzz";
    var headers = {
    "Content-Type": "application/json"
    };
    var data = {
    "text": text,
    "attachments": attachments
    };
    HttpBuilder.post(url, headers, data).err(function(status, headers, data) {

        throw "Script failed with HTTP status : " + status;
    
    }).resp(function(status, headers, data) {
    
        "Hallo" 
    
    }).execute();
    

    }

    posted in User help read more
  • M
    MaP

    Apprently I need to Generate a Certificate Signing Request (openSSL?) - Then
    the Organisation Validates CSR.

    Organisation Generates the SSL certificate from the CSR.
    Distributes the Certificate details:
    a. Users public certificate
    b. Gateway public certificate
    c. CA certificate

    posted in User help read more
  • M
    MaP

    I have done a bit of quick research into Basic Auth and Client Certs and unfortunately its a valid combination apparently. Im double checking but the documentation says you need your basic auth credentials and a certificate. The api requests are all https requests.

    I was hoping mango would be able to manage it without having to introduce another cloud server or service to manage the data feed. Trying to figure out if it can be done in a meta data source, a scripting source, or perhaps a python script. Keeping it in mango reduces networking requirements and complexity.

    The documentation recommends testing with Postman where you can specify Basic Auth as the Authentication for base64 encoded credentials added to the headers and then add a Client side certificate through the Postman Security settings for client SSL certificates.

    posted in User help read more
  • M
    MaP

    Does mango's httpbuilder support client certificates? I would like mango to connect to an API using Basic Auth and a SSL/TLS client certificate to get some information to transfer to an existing PLC to control a plant. There is an existing modbus connection between Mango and the PLC. Does anyone have a recommendation on the best method to achieve the API integration in Mango?

    posted in User help read more
  • M
    MaP

    updated code snippet with 3 points and a timestamp array concatenated then emailed

    //your code here
    //Query to Retrieve and rollup previous 24 Hours Data
    var endDate = CONTEXT.getTimestamp();
    var startDate = endDate - CONTEXT.millisInPast(MINUTE, 4); //handles DST shenanigans
    var results = [];
    var callback = function(idValueTime) {
      results.push(idValueTime);};
    //var pointsToQuery = [ p341.getDataPointWrapper().getId() ];
    PointValueQuery.rollupQuery( [341,342,282], startDate, endDate, callback, AVERAGE, 1, MINUTE);
    print(results);
    
    //Date and time Variables
    var d = new Date();
    var t = new Date().getTime();
    var y = d.getYear()-70;
    var m = d.getMonth();
    var dayinmonth = d.getDate();
    var hours = d.getHours();
    //ECMA function to convert date to locale string YYYY-MM-DD
    var datestring = d.toLocaleDateString();
    //Substr fX to make date DD-MM-YYYY time format
    var ddmmyyyy = datestring.substr(8,2)+datestring.substr(4,4)+datestring.substr(0,4);
    var CSVname = "Performance_"+ddmmyyyy+".csv";
    
    //Creating DateTime For Array
    // 31556926000 mS in Year
    // 2629743000 ms in Month
    // 86400000 ms in Day
    //3600000 ms in an Hour
    //print(results[0].toString()+d.toString()+"\r\n");
    var coeff = 1000 * 60 * 1;
    var date = new Date();
    //var TimeAgo = new Date((Math.round((date.getTime()-(hours*3600000)) / coeff) * coeff)+index*60000);
    
    //var csvData =[];
    var csvData ="kW,Timestamp\r\n";
    var TimeWatch = [];
    results.forEach(function(results,index){
        //csvData[index]=results+","+TimeAgo+"\r\n";
        var TimeAgo = new Date((Math.round((date.getTime()-(hours*3600000)) / coeff) * coeff)+index*60000);
        TimeWatch[index]=TimeAgo;
        if((index+1)%3<1){
        TimeAgo = new Date((Math.round((date.getTime()-(hours*3600000)) / coeff) * coeff)+((index-2)/3)*60000);    
          csvData = csvData+results+","+TimeAgo+"\r\n";  
        }
        else{
            csvData = csvData+results+",";
        }
    });
    //csvDataString = csvData.toString();
    print(csvData);
    //print(TimeWatch);
    /*
    var recipients = ["funtimes84@live.com"];
    var emailContent = new com.serotonin.web.mail.EmailContent(null, 
      "Email body content", com.serotonin.m2m2.Common.UTF8);
    var emailAttachment = new com.serotonin.web.mail.EmailAttachment.ByteArrayAttachment(
      "PerformanceCSV"+datestring+".csv", csvDataString.getBytes());
    
    emailContent.addAttachment(emailAttachment);
    com.serotonin.m2m2.rt.maint.work.EmailWorkItem.queueEmail(recipients, "NorthamCSV_"+ddmmyyyy, emailContent, null);
    */
    

    posted in User help read more
  • M
    MaP

    this seems to have worked i had trouble using arrays with a ,toString function where the "," was appearing in the final csv string - ie)
    Head1,Head2
    Val1,time1
    ,Val2,time2
    ,Val3,time3

    Using Strings seems to ahve gotten the correct output but im worried a string variable might get angry when it contains 1440*7 Values/time

    code so far --

    //Query to Retrieve and rollup previous 24 Hours Data
    var endDate = CONTEXT.getTimestamp();
    var startDate = endDate - CONTEXT.millisInPast(MINUTE, 4); //handles DST shenanigans
    var results = [];
    var callback = function(idValueTime) {
      results.push(idValueTime);};
    var pointsToQuery = [ p341.getDataPointWrapper().getId() ];
    PointValueQuery.rollupQuery( [341], startDate, endDate, callback, AVERAGE, 1, MINUTE);
    print(results);
    
    //Date and time Variables
    var d = new Date();
    var t = new Date().getTime();
    var y = d.getYear()-70;
    var m = d.getMonth();
    var dayinmonth = d.getDate();
    var hours = d.getHours();
    //ECMA function to convert date to locale string YYYY-MM-DD
    var datestring = d.toLocaleDateString();
    //Substr fX to make date DD-MM-YYYY time format
    var ddmmyyyy = datestring.substr(8,2)+datestring.substr(4,4)+datestring.substr(0,4);
    var CSVname = "Performance_"+ddmmyyyy+".csv";
    
    //Creating DateTime For Array
    // 31556926000 mS in Year
    // 2629743000 ms in Month
    // 86400000 ms in Day
    //3600000 ms in an Hour
    //print(results[0].toString()+d.toString()+"\r\n");
    var coeff = 1000 * 60 * 1;
    var date = new Date();
    //var TimeAgo = new Date((Math.round((date.getTime()-(hours*3600000)) / coeff) * coeff)+index*60000);
    
    //var csvData =[];
    var csvData ="kW,Timestamp\r\n"
    
    results.forEach(function(results,index){
        //csvData[index]=results+","+TimeAgo+"\r\n";
        var TimeAgo = new Date((Math.round((date.getTime()-(hours*3600000)) / coeff) * coeff)+index*60000);
        csvData = csvData+results+","+TimeAgo+"\r\n";
        
    });
    //csvDataString = csvData.toString();
    print(csvData)
    

    posted in User help read more