• header.categories
    • header.recent
    • header.tags
    • header.popular
    • register
    • login

    Triggering rule through the HTTP request

    scheduled pinned locked moved General Discussion
    14 posts 5 posters 4.4k views 2 watching
    loading-more-posts
    • oldest-to-newest
    • newest-to-oldest
    • most-votes
    reply
    • reply-as-topic
    guest-login-reply
    deleted-message
    • B offline
      benya
      global: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 one-reply-to-this-post last-reply-time reply quote 0
      • jkandasaJ offline
        jkandasa @benya
        global: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.

        one-reply-to-this-post last-reply-time reply quote 0
        • B offline
          benya
          global: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"
          one-reply-to-this-post last-reply-time reply quote 1
          • B offline
            benya
            global: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 one-reply-to-this-post last-reply-time reply quote 0
            • B offline
              benya @benya
              global: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!

              one-reply-to-this-post last-reply-time reply quote 1
              • W offline
                websterwong8
                global: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 one-reply-to-this-post last-reply-time reply quote 0
                • jkandasaJ offline
                  jkandasa @websterwong8
                  global: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.

                  one-reply-to-this-post last-reply-time reply quote 0
                  • D offline
                    Daniele
                    global: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 one-reply-to-this-post last-reply-time reply quote 0
                    • N offline
                      ncollins @Daniele
                      global: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)
                      
                      one-reply-to-this-post last-reply-time reply quote 1
                      • D offline
                        Daniele
                        global:last-edited-by,

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

                        one-reply-to-this-post last-reply-time reply quote 0
                        • first-post
                          last-post

                        0

                        online

                        644

                        users

                        532

                        topics

                        3.4k

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