• Recent
    • Tags
    • Popular
    • Register
    • Login

    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 Mango 5 Documentation Website

    How to call a global script from dashboard?

    User help
    2
    8
    1.7k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • cbyrneC
      cbyrne
      last edited by

      Could anyone tell me how to call a global script from a dashboard?

      The layout would be pretty simple:

      • point selector
      • date range picker
      • email recipients
      • button

      The script takes values from the point selector, date range picker and email recipients. This I know how to do but I just don't know how to call the global script as the on-click action of the button.

      Thanks!

      Software Developer for GLAS Energy Technology, Ireland

      1 Reply Last reply Reply Quote 0
      • phildunlapP
        phildunlap
        last edited by

        Hi cbyrne,

        You'd want to POST to /rest/v2/script/run with an invocation of the global script function that you want to execute.

        cbyrneC 1 Reply Last reply Reply Quote 0
        • cbyrneC
          cbyrne @phildunlap
          last edited by

          @phildunlap thanks Phil, could you give me an example of how exactly to call the $http.post request? Sorry, I'm not fluent with angular at all. What I can find online does the whole var app = angular.module... boilerplate but I know I'm already in that context in the dashboard designer so I'm note sure how to define the function as I usually would in a scripting context.

          Software Developer for GLAS Energy Technology, Ireland

          1 Reply Last reply Reply Quote 0
          • cbyrneC
            cbyrne
            last edited by

            @phildunlap After some work I got a little further but I've hit a stumbling block with the POST data spec

            {
              "context": [
                {
                  "contextUpdate": true,
                  "variableName": "string",
                  "xid": "string"
                }
              ],
              "logLevel": "TRACE",
              "permissions": [
                "string"
              ],
              "resultDataType": "string",
              "script": "string",
              "wrapInFunction": true
            }
            

            The global function I want to call is something like send_http(point, start_time, end_time, email_addresses) and I have the four parameters. The point I presume I can send as the context, but I don't know how to send the other parameters.

            Software Developer for GLAS Energy Technology, Ireland

            1 Reply Last reply Reply Quote 0
            • phildunlapP
              phildunlap
              last edited by

              I was thinking you would just put them directly into the script, in the call to the function, but you're right that you'd have to pass the point as an XID or something, or place it in the context. Even then you'd have a point in the sense of Mango scripting, which has different functions available than a point in dashboard code.

              cbyrneC 1 Reply Last reply Reply Quote 0
              • cbyrneC
                cbyrne @phildunlap
                last edited by

                Finally got a working solution. Posted the basic custom module below for anyone else who comes across this thread.

                define(['angular', 'require'], function(angular, require) {
                'use strict';
                
                var userModule = angular.module('userModule', ['maUiApp']);
                
                userModule.component('userComponent', {
                    bindings: {},
                    controller: ['$scope', '$http', 
                        function($scope, $http){
                            $scope.msg = null;
                            $scope.sendPost = function(point_xid, date_from, date_to, email_recipients){
                                var from = JSON.stringify(date_from);
                                var to = JSON.stringify(date_to);
                                var emails = JSON.stringify(email_recipients);
                                var script = "send_HTTP_debug(point_xid, " + from + ", " + to + ", " + emails + ")"
                
                                var address= "/rest/v2/script/run";
                                var content = {'Content-Type': 'application/json;charset=UTF-8'};
                                var data = {
                                      "context": [
                                        {
                                          "contextUpdate": true,
                                          "variableName": "point_xid",
                                          "xid": point_xid
                                        },
                                      ],
                                      "logLevel": "DEBUG",
                                      "permissions": [
                                        "admin"
                                      ],
                                      "script": script,
                                      "wrapInFunction": true
                                };
                                function success(response){$scope.msg=response;};
                                function error(response){$scope.msg=response;};
                
                                $http.post(address, JSON.stringify(data),content).then(success, error);
                            };
                        }],
                    template: '\
                    <div flex layout="column">\
                        <md-input-container md-no-float="">\
                            <label>Point</label>\
                            <ma-point-list ng-model="point" query="query_tmpVal" start="start_tmpVal" limit="limit_tmpVal" sort="sort_tmpVal"></ma-point-list>\
                        </md-input-container>\
                        <div flex layout="row">\
                            <md-input-container style="flex-grow:1">\
                            <label>From</label>\
                                <ma-date-picker ng-model="date_from" mode="date" format="DD/MM/YYYY"></ma-date-picker>\
                            </md-input-container>\
                            <div style="width="10vw">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>\
                            <md-input-container style="flex-grow:1">\
                                <label>To</label>\
                                <ma-date-picker ng-model="date_to" mode="date" format="DD/MM/YYYY"></ma-date-picker>\
                            </md-input-container>\
                        </div>\
                        <md-input-container>\
                            <ma-email-recipients ng-model="email_recipients"></ma-email-recipients>\
                        </md-input-container>\
                        <md-button class="md-primary md-raised" ng-click="sendPost(point.xid, date_from, date_to, email_recipients)">Request Report</md-button>\
                        <p>{{msg}}</p>\
                    </div>\
                    '
                });
                
                return userModule;
                
                }); // define
                

                Software Developer for GLAS Energy Technology, Ireland

                1 Reply Last reply Reply Quote 2
                • phildunlapP
                  phildunlap
                  last edited by

                  Thanks for sharing what you came up with!

                  I will offer calling the component "userComponent" could lead to confusion in the future, but maybe that's just for the sake of the example.

                  Maybe also for posterity i'll offer the script permission being used is "admin" instead of "superadmin", in case someone tries leveraging this and doesn't have an "admin" permission group defined.

                  Thanks!

                  cbyrneC 1 Reply Last reply Reply Quote 0
                  • cbyrneC
                    cbyrne @phildunlap
                    last edited by

                    @phildunlap Thanks Phil! The naming was just for show, it's named differently in our system for the exact reason you gave. Thanks for the note about permissions, fixed now.

                    Software Developer for GLAS Energy Technology, Ireland

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post