Introduction


  • ADMIN

    Using scripts we can configure Rule >> Condition, Operations, Dashboard custom widget data.

    List of supported API's

    API's are added in script environment. You can access any supported API's with mcApi object.

    Note: Here I am listing only few API's. For complete set of API's please refer API's document.

    Examples:

    Java script

    Take an example I want to display last seen of node details in dashboard.

    var myImports = new JavaImporter(java.io, java.lang, java.util);  // <1>
    
    with(myImports) {  // <2>
      var options = new HashMap(); // <3>
      //Sort by lastSeen.
      options.put("orderBy", "lastSeen"); // <4>
      //Order by descending
      options.put("order", "desc"); // <5>
      //Page limit, only 5 result
      options.put("pageLimit", new Long(5)); // <6>
      //Get nodes data
      var nodes = mcApi.node().getAll(options); // <7>
    }
    

    <1> Import required packages for our coding. JavaImporter is used to import packages.
    You can import any numbe of packages with comma separated.
    <2> Add our imports with loop
    <3> Create a HashMap to add our query filter values.
    <4> Adding a filter orderBy as lastSeen
    <5> Adding another filter order as desc
    <6> Adding another filter pageLimit as 5
    <7> Store queried results in the variable nodes, can be used in templates(refer templates section)
    mcApi already binded with script engine, you can use it to query/set values from/to MyController.

    Groovy script

    I want to print serial gateways on MyController log file.

    import org.mycontroller.standalone.gateway.GatewayUtils.GATEWAY_TYPE // <1>
    def filters = [name: 'Serial-gateway',  pageLimit: 10L, type: GATEWAY_TYPE.SERIAL] // <2>
    def queryResponse = mcApi.gateway().getAll(filters) // <3>
    mcApi.logger().info("Quesry Response:{}", queryResponse) // <4>
    

    <1> Import GATEWAY_TYPE enum, will be used to filter
    <2> Create filter list with filter values, Filtering with gateway
    name contains Serial-gateway (case sensitive), type as GATEWAY_TYPE.SERIAL
    and limiting count on request as 10 10L with the key pageLimit
    <3> Calling MyController API(mcApi.gateway().getAll(filters)) with our filter
    <4> Printing result under mycontroller/log/mycontroller.log as INFO log.

    NOTE: When log level set at ERROR, INFO logs will not be printed!

    Python script

    In this script we are getting gateways detail and printing in log file.

    from java.util import HashMap # <1>
    from java.lang import Long # <2>
    options = {'pageLimit': Long(20)} # <3>
    gateways = mcApi.gateway().getAll(HashMap(options)) # <4>
    mcApi.logger().info("Quesry Response:{}", gateways) # <5>
    

    <1> Import HashMap should be used to send filter queries
    <2> Import Long used to send java Long type
    <3> Create filter with pageLimit of 20
    <4> Query MyController with McApi and store result in gateways
    <5> Print result in mycontroller/log/mycontroller.log file.


Log in to reply
 

8
Online

439
Users

231
Topics

1378
Posts

Looks like your connection to MYCONTROLLER.ORG was lost, please wait while we try to reconnect.