Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. LaMetric oder JSON-POST per IOBroker Javascript

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    LaMetric oder JSON-POST per IOBroker Javascript

    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      mdsv last edited by

      Bräuchte noch mal ne Hilfe. Bei Amazon gab es die LaMetric Time WLAN-Uhr als Angebot.

      Gerade die Notification ist wirklich genial + Statusmeldungen auf Abruf. Unscheinbare Uhr und wenn passiert, dann gibt es eine Einblendung …

      siehe auch http://lametric-documentation.readthedo … tions.html

      Gelang mir aus Raspberry CURL Aufrufe als .sh und die Notfication kommen an … ich würde es lieber aus Javascript in IOBROKER heraus machen ... wir kann man folgenden POST aus Javascipt (ohne CURL) machen.

      POST https://<device ip/address="">:4343/api/v2/device/notifications
      
      Content-Type: application/json
      Accept: applciation/json
      
      {
          "priority": "warning",
          "model": {
              "cycles": 1,
              "frames": [
                 {
                    "icon": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAUklEQVQYlWNUVFBgYGBgYBC98uE/AxJ4rSPAyMDAwMCETRJZjAnGgOlAZote+fCfCV0nOmA0+yKAYTwygJuAzQoGBgYGRkUFBQZ0dyDzGQl5EwCTESNpFb6zEwAAAABJRU5ErkJggg==",
                    "text": "HELLO!"
                 }
              ],
              "sound": {
                  "category": "notifications",
                  "id": "cat"
              }
          }
      }</device>
      
      1 Reply Last reply Reply Quote 0
      • ruhr70
        ruhr70 last edited by

        @mdsv:

        B

        Gelang mir aus Raspberry CURL Aufrufe als .sh und die Notfication kommen an … ich würde es lieber aus Javascript in IOBROKER heraus machen ... wir kann man folgenden POST aus Javascipt (ohne CURL) machen. `

        Die LaMetric hätte mich auch interessiert. Aber 149,00 EUR fand ich immer noch happig.

        Was passiert, wenn Du das hier mal versuchst:

        var https = require('https'); 
        
        var deviceIpAdress = "172.16.130.254";
        var cmd = "/api/v2/device/notifications"; // Kommandos in der URL nach der Host-Adresse
        var data = '{"priority":"warning","model":{"cycles":1,"frames":[{"icon":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAUklEQVQYlWNUVFBgYGBgYBC98uE/AxJ4rSPAyMDAwMCETRJZjAnGgOlAZote+fCfCV0nOmA0+yKAYTwygJuAzQoGBgYGRkUFBQZ0dyDzGQl5EwCTESNpFb6zEwAAAABJRU5ErkJggg==","text":"HELLO!"}],"sound":{"category":"notifications","id":"cat"}}}';
        
        var options = {
            host: deviceIpAdress,
            port: 4343,
            path: cmd,
            method: 'POST',
            headers: {
                'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
                'Content-Type': 'application/json',
                'Accept': 'applciation/json',
                'Content-Length': data.length
            }
        };
        
        function httpsReq() { // schickt an ein Alexa-Gerät (device) ein Kommando (data) per https-Request / Daten im Body
            var req = https.request(options, function(res) {
            log("http Status: " + res.statusCode);
            log('HEADERS: ' + JSON.stringify(res.headers), (res.statusCode != 200 ? "warn" : "info")); // Header (Rückmeldung vom Webserver)
        
            var bodyChunks  = [];
            var chunkLine   = 0;
            res.on('data', function(chunk) {
                chunkLine = chunkLine + 1;
                // Hier können die einzelnen Zeilen verarbeitet werden...
                bodyChunks.push(chunk);
        
            }).on('end', function() {
                //if(logOn) log("ARRAY mit den einzelnen Zeilen: " + bodyChunks);
                //if(logOn) log("ARRAY Länge: " + bodyChunks.length);
                var body = Buffer.concat(bodyChunks);
                // ...und/oder das Gesamtergebnis (body).
                log("body: " + body);
                });
            });
        
            req.on('error', function(e) { // Fehler abfangen
                log('ERROR: ' + e.message,"warn");
            });
        
            log("Data to request body: " + data);
            // write data to request body
            (data ? req.write(data) : log("Daten: keine Daten im Body angegeben angegeben"));
            req.end();
        
        }
        
        // ********
        
        httpsReq();
        
        
        1 Reply Last reply Reply Quote 0
        • M
          mdsv last edited by

          Danke für super Unterstützung … Code ist bestimmt richtig, allerdings fehlt nun wohl noch die Anmeldung .. hatte ich übersehen. Siehe Errorcode unten ...

          Hatte versucht diese einzubauen, irgendwie scheint das nicht zu gehen (im Header eingebaut) oder ist diese extra???

          HEADERS: {"www-authenticate":"Basic realm=\"global\"","content-type":"application/json;charset=UTF8","content-length":"96","connection":"close","date":"Tue, 02 May 2017 15:48:14 GMT","server":"lighttpd/1.4.35"}
          javascript.0 script.js.LeMetric.php: body: { "errors":[ { "message":"Authorization is required" } ] }
          
          

          Per CURL geht es so ….

          curl -X POST -u "dev:<your api/key/here="">" -H "Content-Type: application/json" -d " { \"model\": { \"frames\": [ { \"icon\":\"a2867\", \"text\":\"Hello\!\"} ] } }" http://<ip address="">:8080/api/v2/device/notifications</ip></your>
          

          Ideen dazu, dies Javascript einzusetzen, habe jetzt schon lange probiert und es nicht hinbekommen…

          1 Reply Last reply Reply Quote 0
          • ruhr70
            ruhr70 last edited by

            @mdsv:

            Per CURL geht es so ….

            curl -X POST -u "dev:<your api/key/here="">" -H "Content-Type: application/json" -d " { \"model\": { \"frames\": [ { \"icon\":\"a2867\", \"text\":\"Hello\!\"} ] } }" http://<ip address="">:8080/api/v2/device/notifications</ip></your>
            

            Ideen dazu, dies Javascript einzusetzen, habe jetzt schon lange probiert und es nicht hinbekommen… `

            mmh… es gibt mehrere Arten der Authentifizierung.

            Für die Basisauthentifizierung musst Du das Skript um die Variable auth erweitern und die Options anpassen:

            var auth = 'user:password';
            
            var options = {
                host: deviceIpAdress,
                port: 4343,
                auth: auth,
                path: cmd,
                method: 'POST',
                headers: {
                    'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
                    'Content-Type': 'application/json',
                    'Accept': 'applciation/json',
                    'Content-Length': data.length
                }
            };
            

            Das sieht aber nicht nach Deinem <your api/key/here="">aus.

            Ich schau mal, was ich dazu finde.</your>

            1 Reply Last reply Reply Quote 0
            • M
              mdsv last edited by

              Tja, geht leider nicht so … auf Seite http://lametric-documentation.readthedo … ation.html ist zwar alles beschrieben, funktioniert aber so nicht …ohne oder mit Base64 (https://www.base64encode.org) alles probiert. API_Key habe ich aber.

              Kann mich auch local nicht anmelden, hier kommt interessanterweise aber ein Anmeldefenster.

              ` > In order to send a notification you must do HTTP POST request to http://<lametric_time_ip_address>:8080/api/v2/device/notifications endpoint with headers:

              Authorization: Basic <base64(dev:api_key)>Content-Type: application/json

              and body:</base64(dev:api_key)></lametric_time_ip_address> `

              1 Reply Last reply Reply Quote 0
              • ruhr70
                ruhr70 last edited by

                @mdsv:

                ` > In order to send a notification you must do HTTP POST request to http://<lametric_time_ip_address>:8080/api/v2/device/notifications endpoint with headers:

                Authorization: Basic <base64(dev:api_key)>Content-Type: application/json

                and body:</base64(dev:api_key)></lametric_time_ip_address>

                Und so?

                var apikey = '<your api/key/here="">';
                var auth = 'Basic ' + new Buffer('dev:' + apikey).toString('base64');
                
                var options = {
                    host: deviceIpAdress,
                    port: 4343,
                    auth: auth,
                    path: cmd,
                    method: 'POST',
                    headers: {
                        'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
                        'Content-Type': 'application/json',
                        'Accept': 'applciation/json',
                        'Content-Length': data.length
                    }
                };</your> 
                
                1 Reply Last reply Reply Quote 0
                • ruhr70
                  ruhr70 last edited by

                  Und eine zweite Variante, falls das oben so nicht funktioniert:

                  var apikey = '<your api/key/here="">';
                  var auth = 'Basic ' + new Buffer('dev:' + apikey).toString('base64');
                  
                  var options = {
                      host: deviceIpAdress,
                      port: 4343,
                      path: cmd,
                      method: 'POST',
                      headers: {
                          'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
                          'Content-Type': 'application/json',
                          'Authorization': 'Basic <'+auth+'>',
                          'Accept': 'applciation/json',
                          'Content-Length': data.length
                      }
                  };</your> 
                  

                  Ich würde auf diese zweite Variante tippen… wg. der Beschreibung Authorization: Basic<base64(dev:api_key)></base64(dev:api_key)>

                  1 Reply Last reply Reply Quote 0
                  • M
                    mdsv last edited by

                    Feedback Var. 1

                    Leider nicht … habe sogar mit <> und ohne probiert.

                    ` > 21:50:06.426 [info] javascript.0 Start javascript script.js.LeMetric.notification

                    21:50:06.426 [info] javascript.0 script.js.LeMetric.notification: Data to request body: {"priority":"warning","model":{"cycles":1,"frames":[{"icon":"data:image/png;base64,iVBORw0KGgoAAAANGRkUFBQZ0…....................<snip>...","text":"HELLO!"}],"sound":{"category":"notifications","id":"cat"}}}

                    21:50:06.426 [info] javascript.0 script.js.LeMetric.notification: registered 0 subscriptions and 0 schedules

                    21:50:06.441 [info] javascript.0 script.js.LeMetric.notification: http Status: 401

                    21:50:06.441 [warn] javascript.0 script.js.LeMetric.notification: HEADERS: {"www-authenticate":"Basic realm="global"","content-type":"application/json;charset=UTF8","content-length":"96","connection":"close","date":"Tue, 02 May 2017 19:50:05 GMT","server":"lighttpd/1.4.35"}

                    21:50:06.441 [info] javascript.0 script.js.LeMetric.notification: body: { "errors":[ { "message":"Authorization is required" } ] }</snip> `

                    Feedback Var.2

                    OK, habe den Unterschied gefunden 😄

                    'Authorization': 'Basic <'+auth+'>' im Header

                    -> gleiche Fehlermeldung.

                    1 Reply Last reply Reply Quote 0
                    • ruhr70
                      ruhr70 last edited by

                      @mdsv:

                      Feedback Var.2

                      OK, habe den Unterschied gefunden 😄

                      'Authorization': 'Basic <'+auth+'>' im Header

                      -> gleiche Fehlermeldung. `

                      Und das auth: über den Header weg.

                      Ich glaube die < > gehören auch nicht dazu…

                      Also:

                      'Authorization': 'Basic '+auth,
                      
                      1 Reply Last reply Reply Quote 0
                      • ruhr70
                        ruhr70 last edited by

                        Poste mal bitte, wie dieser Bereich jetzt bei Dir aussieht:

                        var apikey = '<your api/key/here="">';
                        var auth = 'Basic ' + new Buffer('dev:' + apikey).toString('base64');
                        
                        var options = {
                            host: deviceIpAdress,
                            port: 4343,
                            path: cmd,
                            method: 'POST',
                            headers: {
                                'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
                                'Content-Type': 'application/json',
                                'Authorization': 'Basic '+auth,
                                'Accept': 'applciation/json',
                                'Content-Length': data.length
                            }
                        };</your>
                        
                        1 Reply Last reply Reply Quote 0
                        • M
                          mdsv last edited by

                          ungefähr so ….

                          var https = require('http'); 
                          var apikey = '08cc1947<snip>.....</snip>4a28cb900457b9bd';
                          var auth = 'Basic ' + new Buffer('dev:' + apikey).toString('base64');
                          
                          var options = {
                              host: deviceIpAdress,
                              port: 8080,
                              auth: auth,
                              path: cmd,
                              method: 'POST',
                          
                              headers: {
                                  'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
                                  'Content-Type': 'application/json',
                                  'Accept': 'application/json',
                                  'Content-Length': data.length,
                                  'Authorization': 'Basic '+auth
                          
                              }
                          };
                          ....
                          

                          Meldung kommt immer noch.

                          1 Reply Last reply Reply Quote 0
                          • ruhr70
                            ruhr70 last edited by

                            Da sind jetzt ein paar Punkte…

                            Du nutzt http (8080).

                            Wir verwenden aber das Module https

                            Das muss geändert werden.

                            Dann ist das auth in den Options noch drin.

                            [EDIT]: OK, habe ich übersehen… Du hast es in http oben geändert 😉 sorry... 😉

                            1 Reply Last reply Reply Quote 0
                            • ruhr70
                              ruhr70 last edited by

                              @mdsv ich änder das Skript eben und poste es noch einmal.

                              1 Reply Last reply Reply Quote 0
                              • ruhr70
                                ruhr70 last edited by

                                mhh… wenn es nicht das überflüssige auth: auth, in den Options ist, bin ich mit meinem Latein fast am Ende.

                                Muss mir das Teil wohl doch mal bestellen 😉

                                1 Reply Last reply Reply Quote 0
                                • M
                                  mdsv last edited by

                                  Danke für die Mühe … vielleicht solltest dir eine holen 😉 ... man kann Notification einblenden (Tür öffnet), aber auch durch li/re Klick den Status anzeigen lassen (Wetter, ....).

                                  Durch lokale Update wird die Info dann übermittelt. Den Action-Modus habe ich noch nicht probiert und das ganze unscheinbar als nette Uhr.

                                  Per CURL bekomme ich es schon direkt hin, allerdings wäre Javascript besser, um die Parameter zu übermitteln.

                                  PS: die überflüssige auth war es auch nicht ....schade.

                                  1 Reply Last reply Reply Quote 0
                                  • R
                                    ronnyw last edited by

                                    Meine Programmierkenntnisse sind leider recht rudimentär und ich kann auch gerade nicht helfen. Aber als ioBroker Neuling mit einer LaMetric interessiert mich das Thema sehr (auch wenn ich später Richtung Button-App hinaus möchte und schon überlege, wie man wohl einen LaMetric Adapter hinbekommt, der das ermöglicht). Ich hoffe das Thema LaMetric findet hier noch breiteren Anklang 🙂

                                    1 Reply Last reply Reply Quote 0
                                    • B
                                      Ben last edited by

                                      Hier ist meine funktionierende Lösung. :geek:

                                      Dabei habe ich auch ein cooles ioBroker-Icon gebaut.

                                      Hier eine Vorschau:

                                      https://youtu.be/iDtKofIqIh8

                                      Einfach den API-Key und die IP ersetzen. 😉

                                      var http = require('http'); 
                                      var apikey = '1234567891011121314'; // der API-Key von developer.lametric.com
                                      var auth = 'Basic ' + new Buffer('dev:' + apikey).toString('base64');
                                      
                                      var deviceIpAdress = '192.168.178.??'; // IP vom LaMetric
                                      var port = '8080';
                                      var cmd = '/api/v2/device/notifications';
                                      
                                      var dataTemplate = {
                                          model: {
                                              cycles: 1,
                                              frames: [{
                                                  icon: 'i10462',
                                                  text: null
                                              }],
                                              sound: {
                                                  category: 'notifications',
                                                  id: 'positive6'
                                              }
                                          }
                                      }
                                      
                                      var optionsTemplate = {
                                          host: deviceIpAdress,
                                          port: port,
                                          path: cmd,
                                          method: 'POST',
                                          headers: {
                                              'Content-Length': null,
                                              'Authorization': 'Basic ' + new Buffer('dev' + ':' + apikey).toString('base64')
                                          }
                                      };
                                      
                                      function lametricNotification(notificationText) {
                                          var data = dataTemplate;
                                          data.model.frames[0].text = notificationText;
                                          var dataString = JSON.stringify(data);
                                      
                                          var options = optionsTemplate;
                                          options.headers['Content-Length'] = dataString.length;
                                      
                                          var req = http.request(options, function(res) {
                                              log('http Status: ' + res.statusCode);
                                              log('HEADERS: ' + JSON.stringify(res.headers), (res.statusCode != 200 ? 'warn' : 'info'));
                                      
                                              var bodyChunks  = [];
                                              res.on('data', function(chunk) {
                                                  bodyChunks.push(chunk);
                                              }).on('end', function() {
                                                  var body = Buffer.concat(bodyChunks);
                                                  log('body: ' + body);
                                              });
                                          });
                                      
                                          req.on('error', function(e) {
                                              log('ERROR: ' + e.message, 'warn');
                                          });
                                      
                                          (dataString ? req.write(dataString) : log('Daten: keine Daten im Body angegeben'));
                                          req.end();
                                      }
                                      
                                      
                                      1 Reply Last reply Reply Quote 0
                                      • ruhr70
                                        ruhr70 last edited by

                                        Na das ist ja mal ein Einstieg 🙂

                                        1 Reply Last reply Reply Quote 0
                                        • R
                                          ronnyw last edited by

                                          Super, danke für die Hilfestellung.

                                          Ich habe derweil den umgekehrten Weg probiert: eine Button-App auf der LaMetric schaltet ioBroker. Nachdem ich ein paar Grundlagen lernen musste, war das am Ende eigentlich ziemlich simpel zu realisieren über den Node-Red Adapter.

                                          1 Reply Last reply Reply Quote 0
                                          • ldittmar
                                            ldittmar Developer last edited by

                                            Nur ein kurze Info zu LaMetric: LaMetric kann mit IFTTT kommunizieren und die neueste Version vom Cloud-Adapter (ab Version 1.0.2) wird IFTTT unterstützen. 😉

                                            Grüße,

                                            ldittmar

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

                                            Support us

                                            ioBroker
                                            Community Adapters
                                            Donate

                                            817
                                            Online

                                            31.7k
                                            Users

                                            79.9k
                                            Topics

                                            1.3m
                                            Posts

                                            19
                                            69
                                            10252
                                            Loading More Posts
                                            • Oldest to Newest
                                            • Newest to Oldest
                                            • Most Votes
                                            Reply
                                            • Reply as topic
                                            Log in to reply
                                            Community
                                            Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
                                            The ioBroker Community 2014-2023
                                            logo