Introduction
-
Using scripts we can configure
Rule>>Condition,Operations,Dashboard custom widgetdata.List of supported API's
API's are added in script environment. You can access any supported API's with
mcApiobject.Note: Here I am listing only few API's. For complete set of API's please refer API's document.
Examples:
Java scriptTake 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.
JavaImporteris used to import packages.
You can import any numbe of packages with comma separated.
<2> Add our importswithloop
<3> Create aHashMapto add our query filter values.
<4> Adding a filterorderByaslastSeen
<5> Adding another filterorderasdesc
<6> Adding another filterpageLimitas5
<7> Store queried results in the variablenodes, can be used in templates(refer templates section)
mcApialready binded with script engine, you can use it to query/set values from/to MyController.Groovy scriptI 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_TYPEenum, will be used to filter
<2> Create filter list with filter values, Filtering with gateway
namecontainsSerial-gateway(case sensitive),typeasGATEWAY_TYPE.SERIAL
and limiting count on request as 1010Lwith the keypageLimit
<3> Calling MyController API(mcApi.gateway().getAll(filters)) with our filter
<4> Printing result undermycontroller/log/mycontroller.logasINFOlog.NOTE: When log level set at
ERROR,INFOlogs will not be printed!Python scriptIn 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
HashMapshould be used to send filter queries
<2> ImportLongused to send javaLongtype
<3> Create filter withpageLimitof20
<4> Query MyController withMcApiand store result ingateways
<5> Print result inmycontroller/log/mycontroller.logfile.