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

    Triggering rule through the HTTP request

    Scheduled Pinned Locked Moved General Discussion
    14 Posts 5 Posters 3.8k 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

      Where could I lookup REST API samples or docs?

      jkandasaJ 1 Reply Last reply Reply Quote 0
      • jkandasaJ Offline
        jkandasa @benya
        last edited by

        @benya For now there is no doc for REST API. You have to refer source code.

        B 1 Reply Last reply Reply Quote 0
        • B Offline
          benya @jkandasa
          last edited by

          @jkandasa said:

          @benya For now there is no doc for REST API. You have to refer source code.

          Are there any samples?
          I need to pass credentials and trigger some operation.

          Besides REST API, MyController supports any other IPC?
          May I trigger operation from external app or shell script on the same host?

          1 Reply Last reply Reply Quote 0
          • B Offline
            benya
            last edited by

            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?

            jkandasaJ 1 Reply Last reply Reply Quote 0
            • jkandasaJ Offline
              jkandasa @benya
              last edited by

              @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

                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

                  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 Reply Quote 0
                  • B Offline
                    benya @benya
                    last edited by benya

                    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

                      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 ?

                      jkandasaJ 1 Reply Last reply Reply Quote 0
                      • jkandasaJ Offline
                        jkandasa @websterwong8
                        last edited by

                        @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

                          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 Reply Quote 0
                          • N Offline
                            ncollins @Daniele
                            last edited by

                            @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

                              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

                              589

                              Users

                              529

                              Topics

                              3.4k

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