• Categories
  • Recent
  • Tags
  • Popular
  • Register
  • Login
  • Categories
  • Recent
  • Tags
  • Popular
  • Register
  • Login

Triggering rule through the HTTP request

Scheduled Pinned Locked Moved General Discussion
14 Posts 5 Posters 4.2k Views 2 Watching
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.
  • B Offline
    benya
    last edited by 13 Mar 2017, 17:09

    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?

    J 1 Reply Last reply 14 Mar 2017, 03:24 Reply Quote 0
    • J Offline
      jkandasa @benya
      last edited by 14 Mar 2017, 03:24

      @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.

      1 Reply Last reply Reply Quote 0
      • B Offline
        benya
        last edited by 14 Mar 2017, 13:23

        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"
        1 Reply Last reply Reply Quote 1
        • B Offline
          benya
          last edited by 30 Mar 2017, 05:18

          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?

          B 1 Reply Last reply 30 Mar 2017, 09:45 Reply Quote 0
          • B Offline
            benya @benya
            last edited by benya 30 Mar 2017, 09:45

            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!

            1 Reply Last reply Reply Quote 1
            • W Offline
              websterwong8
              last edited by 23 Feb 2018, 12:21

              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 ?

              J 1 Reply Last reply 28 Feb 2018, 05:33 Reply Quote 0
              • J Offline
                jkandasa @websterwong8
                last edited by 28 Feb 2018, 05:33

                @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.

                1 Reply Last reply Reply Quote 0
                • D Offline
                  Daniele
                  last edited by 11 Aug 2019, 19:38

                  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

                  N 1 Reply Last reply 11 Aug 2019, 20:59 Reply Quote 0
                  • N Offline
                    ncollins @Daniele
                    last edited by 11 Aug 2019, 20:59

                    @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)
                    
                    1 Reply Last reply Reply Quote 1
                    • D Offline
                      Daniele
                      last edited by 12 Aug 2019, 09:12

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

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

                      0

                      Online

                      637

                      Users

                      532

                      Topics

                      3.4k

                      Posts
                      Copyright © 2015-2025 MyController.org | Contributors | Localization