Triggering rule through the HTTP request



  • Looks like I could replace client that pushes data through REST API with a rule that uses condition script.
    Condition script could read external file and return true/false accordingly.

    How often MyController executes condition script (linked to the rule)? Every 5sec?



  • @benya

    Looks like I could replace client that pushes data through REST API with a rule that uses condition script.
    Condition script could read external file and return true/false accordingly.

    You can use operation to execute the script and configure your operation on timer/cron.

    How often MyController executes condition script (linked to the rule)? Every 5sec?

    Yes, Condition scripts executes 5 seconds once.



  • Here is a sample REST API call that modifies MyController repository variable:

    curl -X PUT -H "Content-Type: application/json" -d '{"key":"Var1","value":"222"}' "http://demo:demo@demo.mycontroller.org/mc/rest/variables"


  • Looks like dummy sensor variable triggers rule much faster than repository variable.
    How could I change sensor variable (referring by UID tag) through REST API?
    Could you post sample HTTP payload?



  • I figured how to set dummy sensor by script called through REST API.

    I created "Set DummySensor" groovy script:

    def uid = "DummySensor"
    def value = "1"
    def sensor = mcApi.uidTag().getByUid(uid).getResource()
    sensor.setValue(value)
    mcApi.sensor().sendPayload(sensor);
    

    and call it through the REST API:

    curl "http://admin:admin@localhost:9880/mc/rest/scripts/runNow?bindings=%7B+%7D&script=operations%2FSet+DummySensor.groovy&scriptBindings=%7B%7D"
    

    I defined rule that gets triggered when dummy sensor value changes.
    No 5 sec delays anymore. Cool!



  • I have been looking high and low to trigger sensors through HTTP Request. I have tried to understand from the source code link but failed. Is it possible to trigger a sensor using HTTP Request ?



  • @websterwong8 You can send a payload to sensors via HTTP.

    For this, you can use send raw message API.

    POST : https://demo.mycontroller.org/mc/rest/sensors/sendRawMessage

    Data:

    {
      "gatewayId": "39",
      "nodeEui": "1",
      "sensorId": "1",
      "type": "Set",
      "subType": "Status",
      "payload": "0",
      "ack": "0"
    }
    

    You can get gateway Id by editing gateway.

    Let me know your specific use case. I can guide you.



  • Sorry for replying to an old Post, but this is the only one I found about API calls.
    I'd like to read & write some sensor variables from a python script, in order to integrate MyController with an external service.

    Could you give some suggestion to handle the authentication?
    I immagine that to read sensor variables I should change your json from send to request, correct?

    Thanks for your amazing work
    Daniele



  • @Daniele This is an example for a node.js script that I use to pull back sensor data.

    const https = require('https');
    
    https.get({
    	host: 'localhost' // host of your mycontroller install
    	,port: 443 // this is 443 since it's SSL
    	,path: '/mc/rest/sensors/264' // example getting sensor info for sensor 264
    	,headers: {
    		'Authorization': 'Basic ' + new Buffer('USERNAME:PASSWORD').toString('base64')
    	}
    
    }, (resp) => {
      let data = '';
      //console.log(resp);
      // A chunk of data has been recieved.
      resp.on('data', (chunk) => {
        data += chunk;
      });
    
      // The whole response has been received. Print out the result.
      resp.on('end', () => {
        console.log(JSON.parse(data).variables[0]);
      });
    
    }).on("error", (err) => {
      console.log("Error: " + err.message);
    }); 
    

    Important thing to note is the Basic Auth header. This is how you send username and password.

    The first google result I found for sending basic auth params in a python script
    https://stackoverflow.com/questions/6999565/python-https-get-with-basic-authentication

    import requests
    
    r = requests.get('https://my.website.com/rest/path', auth=('myusername', 'mybasicpass'))
    print(r.text)
    


  • Thank you very much, this is exactly the kind of hint I needed!


Log in to reply
 

Suggested Topics

4
Online

510
Users

422
Topics

2.6k
Posts