Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. [gelöst] Skript auf AXIOS/httpGet umbauen

    NEWS

    • 15. 05. Wartungsarbeiten am ioBroker Forum

    • Monatsrückblick - April 2025

    • Minor js-controller 7.0.7 Update in latest repo

    [gelöst] Skript auf AXIOS/httpGet umbauen

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

      Tag,
      heutiges Projekt soll sein mich mal ein wenig mit Javaskript zu beschäftigen.
      Ich muss gestehen dass ich damit wenig bis eigentlich nix gemacht habe und mir die wenigen Skripte welche bei mir laufen auch nur "geklaut" habe.

      Ziel ist es folgende JS-Funktion in Blockly auf Axios umzubauen:

      var request =require('request');
      let result;
      const options = {
          url: 'https://nominatim.openstreetmap.org/reverse.php?format=json&lat=' + lat + '&lon=' + lon + '&zoom=18',
          method: 'GET',
          headers: {
              'Accept': 'application/json',
              'Accept-Charset': 'utf-8',
              'User-Agent': 'iobroker script'
          }
      }
      
      request(options, function(err,response,body){
          result = JSON.parse(body);
      })
      await wait(1000);
      return result;
      

      Die Worte "request" muss ich ja durch "axios" ersetzen - richtig?

      Aber dann verliessen sie ihn auch schon.

      Für Hilfe wäre ich dankbar.
      Merci

      M L 2 Replies Last reply Reply Quote 0
      • M
        MCU @DJMarc75 last edited by

        @djmarc75

        const axios = require('axios');
        
        const getReverseGeoCoding = async (lat, lon) => {
            try {
                const response = await axios.get(`https://nominatim.openstreetmap.org/reverse.php`, {
                    params: {
                        format: 'json',
                        lat: lat,
                        lon: lon,
                        zoom: 18
                    },
                    headers: {
                        'Accept': 'application/json',
                        'Accept-Charset': 'utf-8',
                        'User-Agent': 'iobroker script'
                    }
                });
                return response.data;
            } catch (error) {
                console.error('Error fetching reverse geocoding data:', error);
                return null;
            }
        };
        
        
        const lat = 52.52; 
        const lon = 13.405; 
        const result = await getReverseGeoCoding(lat, lon);
        console.log(result);
        
        DJMarc75 1 Reply Last reply Reply Quote 0
        • L
          Lucky_ESA Developer Most Active @DJMarc75 last edited by

          @djmarc75

          Mit Javascript >= 7.8.0

          httpGet('http://', { timeout: 2000, responseType: 'arraybuffer oder text' }, async (err, response) => {
                  if(err) {
                      console.log('Error: ' + err);
                      return;
                  }
                  if (response.data) {
                      // etwas tun
                  } else {
                      // keine Daten
                  }
              });
          
          1 Reply Last reply Reply Quote 1
          • DJMarc75
            DJMarc75 @MCU last edited by

            @mcu Danke.
            Das wäre dann ein reines Javaskript mit fixen Koordinaten.

            Diese fixen Koordinaten lat und lon müssen aber in meinem Fall durch Datenpunkte ersetzt werden und zusätzlich sollte das Skript als Funktion in Blockly implementiert werden, was so leider einen Fehler auswirft:

            script.js.common.GPS.Adresse_Iris compile failed: at script.js.common.GPS.Adresse_Iris:28
            
            Codierknecht M 2 Replies Last reply Reply Quote 0
            • Codierknecht
              Codierknecht Developer Most Active @DJMarc75 last edited by

              @djmarc75

              quick & dirty:

              async function updateLocation(latitude, longitude) {
                  const url = `https://nominatim.openstreetmap.org/reverse?lat=${latitude}&lon=${longitude}&format=json`;
                  const response = await axios.get(url);
                  if (response && response.data.display_name) {
                    return(response.data.display_name);
                  }
              }
              
              1 Reply Last reply Reply Quote 0
              • M
                MCU @DJMarc75 last edited by MCU

                @djmarc75 Wenn du Blockly nutzt kannst du auch direkt den Block verwenden?
                2ae1f768-c9ce-4adf-b57d-acd251e9e75b-image.png

                09f8663b-e449-4ebd-8352-3ca5b6745fef-image.png

                <xml xmlns="https://developers.google.com/blockly/xml">
                  <variables>
                    <variable id="`eps4Xu;/O9P-h|FWJ-E">lat</variable>
                    <variable id="qICBRrCi%-}_(mq;vWM^">lon</variable>
                    <variable id=")AQ%}HdiHDw^ZDxO0d[?">response</variable>
                  </variables>
                  <block type="variables_set" id="2DF6oAFi1G}uOwa_3?)w" x="88" y="113">
                    <field name="VAR" id="`eps4Xu;/O9P-h|FWJ-E">lat</field>
                    <value name="VALUE">
                      <block type="math_number" id="FduAcw*36HRQdJJcnT(M">
                        <field name="NUM">52.52</field>
                      </block>
                    </value>
                    <next>
                      <block type="variables_set" id="flw^g!3aB+q_~hAO83[g">
                        <field name="VAR" id="qICBRrCi%-}_(mq;vWM^">lon</field>
                        <value name="VALUE">
                          <block type="math_number" id=":CT|HqW-VISJ=XBdNMzY">
                            <field name="NUM">13.405</field>
                          </block>
                        </value>
                        <next>
                          <block type="http_get" id="er#Datp2ZE*VyGUa%*Kv">
                            <field name="TIMEOUT">2000</field>
                            <field name="UNIT">ms</field>
                            <field name="TYPE">text</field>
                            <value name="URL">
                              <shadow type="text" id="Q_WE@|3:]0*%[l-.!;qf">
                                <field name="TEXT"></field>
                              </shadow>
                              <block type="text_join" id="-ys2STS:FUWU@%[8~o[=">
                                <mutation items="5"></mutation>
                                <value name="ADD0">
                                  <block type="text" id="B25~4=^wMHf(vUb3ay~3">
                                    <field name="TEXT">https://nominatim.openstreetmap.org/reverse.php?format=json&amp;lat=</field>
                                  </block>
                                </value>
                                <value name="ADD1">
                                  <block type="variables_get" id="3Y8[4Y4CR#bn(@$tYkc+">
                                    <field name="VAR" id="`eps4Xu;/O9P-h|FWJ-E">lat</field>
                                  </block>
                                </value>
                                <value name="ADD2">
                                  <block type="text" id="==#V5Ah1PyKx_!-*~JfY">
                                    <field name="TEXT">&amp;lon=</field>
                                  </block>
                                </value>
                                <value name="ADD3">
                                  <block type="variables_get" id="ya.!JwNXi/PO.!GZE@tX">
                                    <field name="VAR" id="qICBRrCi%-}_(mq;vWM^">lon</field>
                                  </block>
                                </value>
                                <value name="ADD4">
                                  <block type="text" id="[JnnacT#;5U-rwb.89lc">
                                    <field name="TEXT">&amp;zoom=18</field>
                                  </block>
                                </value>
                              </block>
                            </value>
                            <statement name="STATEMENT">
                              <block type="debug" id="Z/p^=2Hb40.14a$HJF{b">
                                <field name="Severity">log</field>
                                <value name="TEXT">
                                  <shadow type="text" id="X7.E)1NBlIOrpnS^9XwS">
                                    <field name="TEXT">test</field>
                                  </shadow>
                                  <block type="variables_get" id="Jgr?*Epm}01s%F=YGUa+">
                                    <field name="VAR" id=")AQ%}HdiHDw^ZDxO0d[?">response</field>
                                  </block>
                                </value>
                              </block>
                            </statement>
                          </block>
                        </next>
                      </block>
                    </next>
                  </block>
                </xml>
                

                DJMarc75 1 Reply Last reply Reply Quote 3
                • DJMarc75
                  DJMarc75 @MCU last edited by

                  @mcu Jou, so (ähnlich) habe ich es mir eben auch zusammen gebastelt 😉

                  Funktioniert !

                  Merci auch an @Codierknecht & @Lucky_ESA

                  Muss noch viel was JS angeht lernen.

                  haus-automatisierung 1 Reply Last reply Reply Quote 0
                  • haus-automatisierung
                    haus-automatisierung Developer Most Active @DJMarc75 last edited by

                    @djmarc75 Die Idee mit den neuen Versionen war ja eigentlich, nicht wieder in das gleiche Problem zu laufen wie aktuell mit request. Also am besten kommt axios gar nicht in deinem Code vor, sondern Du nutzt httpGet (oder die Blockly-Bausteine dafür).

                    Dann können wir in Zukunft die Library darunter austauschen und alle Scripts laufen weiter.

                    D 1 Reply Last reply Reply Quote 7
                    • D
                      darkiop Most Active @haus-automatisierung last edited by

                      @haus-automatisierung

                      Der httpGet Block hat aktuell keine Option um Zertifikatsfehler zu ignorieren oder?

                      haus-automatisierung 1 Reply Last reply Reply Quote 0
                      • haus-automatisierung
                        haus-automatisierung Developer Most Active @darkiop last edited by

                        @darkiop sagte in [gelöst] Skript auf AXIOS umbauen:

                        Der httpGet Block hat aktuell keine Option um Zertifikatsfehler zu ignorieren

                        Ja, richtig. Weiß auch noch nicht ob ich das anbieten soll. Immerhin ist die Validierung ein Sicherheitsfeature.

                        D 1 Reply Last reply Reply Quote 1
                        • D
                          darkiop Most Active @haus-automatisierung last edited by

                          @haus-automatisierung

                          Stimmt - allerdings gerade im häuslichen Umfeld oft "normal".

                          Am Ende ist ist es ein abwägen von Risiko und Nutzen 😉

                          haus-automatisierung 1 Reply Last reply Reply Quote 0
                          • haus-automatisierung
                            haus-automatisierung Developer Most Active @darkiop last edited by

                            @darkiop Ja, und was ist der Nutzen von einer https Verbindung im LAN?

                            D 1 Reply Last reply Reply Quote 0
                            • D
                              darkiop Most Active @haus-automatisierung last edited by

                              @haus-automatisierung

                              Lässt sich manchmal ja nicht ohne zusätzlichen Aufwand (z.B. per ReverseProxy) vermeiden.

                              Unser SMA WR liefert das WebUi per https aus, eine Möglichkeit ein eigenes Zertifikat zu hinterlegen gibt es imho nicht.

                              Hier läuft alles über einen internen ReverseProxy, mir ist nur aufgefallen das die Option SSL Prüfungen abzuschalten, noch nicht implementiert ist.

                              1 Reply Last reply Reply Quote 0
                              • bahnuhr
                                bahnuhr Forum Testing Most Active last edited by

                                Guten Morgen,

                                bin auch gerade an dem Thema dran:

                                mit request lasse ich mir Bilder der cam an telegram senden, wie folgt:

                                var ip = 159; var vUser = "Dieter"; var cam = "Test";
                                
                                var request = require('request');
                                var fs      = require('fs');
                                    request.get({url: 'http://192.168.243.' + ip + ':88//cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=xx&pwd=xx', encoding: 'binary'}, function (err, response, body) {
                                        fs.writeFile('/opt/iobroker/iobroker-data/tmp/snap' + ip + '.jpg', body, 'binary', function(err) {
                                        if (err) {
                                            console.error(err);
                                        } else {
                                            log('Snapshot sent '+ ip);
                                            sendTo('telegram.0', {user: vUser, text: '/opt/iobroker/iobroker-data/tmp/snap' + ip + '.jpg', caption: cam});
                                        }
                                      }); 
                                    });
                                

                                Wenn ich dies nun mit axios versuche, klappt dies nicht:

                                var ip = 159; var vUser = "Dieter"; var cam = "Test";
                                
                                const axios = require('axios');
                                var fs      = require('fs');
                                    axios.get({url: 'http://192.168.243.' + ip + ':88//cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=xx&pwd=xx', encoding: 'binary'}, function (err, response, body) {
                                        fs.writeFile('/opt/iobroker/iobroker-data/tmp/snap' + ip + '.jpg', body, 'binary', function(err) {
                                        if (err) {
                                            console.error(err);
                                        } else {
                                            log('Snapshot sent '+ ip);
                                            sendTo('telegram.0', {user: vUser, text: '/opt/iobroker/iobroker-data/tmp/snap' + ip + '.jpg', caption: cam});
                                        }
                                      }); 
                                    });
                                

                                Kann der axios diesen "binary" nicht ?
                                Oder liegt es an etwas anderes ?

                                Vielleicht auch ne Info wie der Befehl mit httpget aussehen müsste.
                                Danke.

                                haus-automatisierung 1 Reply Last reply Reply Quote 0
                                • haus-automatisierung
                                  haus-automatisierung Developer Most Active @bahnuhr last edited by

                                  @bahnuhr sagte in [gelöst] Skript auf AXIOS umbauen:

                                  Kann der axios diesen "binary" nicht ?

                                  Du kannst nicht einfach nur 1:1 die Funktion austauschen. Dokumentation gibts hier: https://github.com/axios/axios

                                  Das Ganze heißt responseEncoding: 'binary', im Config-Objekt. Aber warum nutzt Du nicht httpGet ?

                                  https://github.com/ioBroker/ioBroker.javascript/blob/master/docs/en/javascript.md#httpget

                                  haus-automatisierung bahnuhr 2 Replies Last reply Reply Quote 1
                                  • haus-automatisierung
                                    haus-automatisierung Developer Most Active @haus-automatisierung last edited by haus-automatisierung

                                    nicht getestet, nur im Forum programmiert

                                    const fs = require('node:fs');
                                    
                                    const ip = 159;
                                    const vUser = 'Dieter';
                                    const cam = 'Test';
                                    
                                    httpGet(`http://192.168.243.${ip}:88/cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=xx&pwd=xx`, { responseType: 'arraybuffer' }, (err, response) => {
                                        if (err) {
                                            console.error(err);
                                        } else {
                                            const filePath = `/opt/iobroker/iobroker-data/tmp/snap${ip}.jpg`;
                                    
                                            fs.writeFile(filePath, response.data, (err) => {
                                                if (err) {
                                                    console.error(err);
                                                } else {
                                                    log(`Snapshot sent: ${ip}`);
                                    
                                                    sendTo('telegram.0', {
                                                        user: vUser,
                                                        text: filePath,
                                                        caption: cam
                                                    });
                                                }
                                            });
                                        }
                                    });
                                    
                                    1 Reply Last reply Reply Quote 1
                                    • bahnuhr
                                      bahnuhr Forum Testing Most Active @haus-automatisierung last edited by

                                      @haus-automatisierung sagte in [gelöst] Skript auf AXIOS umbauen:

                                      Aber warum nutzt Du nicht httpGet ?

                                      weil es mit der 7.8.0 noch nicht funktioniert.
                                      und die 7.9.0 ist noch nicht stable.

                                      Muss ich wohl mal die beta installieren.

                                      haus-automatisierung 1 Reply Last reply Reply Quote 0
                                      • haus-automatisierung
                                        haus-automatisierung Developer Most Active @bahnuhr last edited by haus-automatisierung

                                        @bahnuhr sagte in [gelöst] Skript auf AXIOS umbauen:

                                        Muss ich wohl mal die beta installieren.

                                        In 8.3.0 habe ich heute noch eine weitere Funktion eingefügt. Damit wird das noch kürzer:

                                        const ip = 159;
                                        const user = 'Dieter';
                                        const caption = 'Test';
                                         
                                        httpGet(`http://192.168.243.${ip}:88/cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=xx&pwd=xx`, { responseType: 'arraybuffer' }, (err, response) => {
                                            if (err) {
                                                console.error(err);
                                            } else {
                                                sendTo('telegram.0', {
                                                    user,
                                                    caption,
                                                    text: createTempFile(`snap${ip}.jpg`, response.data),
                                                });
                                            }
                                        });
                                        
                                        
                                        bahnuhr 1 Reply Last reply Reply Quote 1
                                        • bahnuhr
                                          bahnuhr Forum Testing Most Active @haus-automatisierung last edited by

                                          @haus-automatisierung
                                          so, habs ausprobiert (bin jetzt auf 8.3.0).

                                          Wenn ich folgenden kleinen code ausführe:

                                          httpGet(`http://192.168.243.203/YamahaExtendedControl/v1/main/setPower?power=standby`, { responseType: 'text' }, (err, response) => {
                                              if (err) { console.error(err); } else { log (""); }
                                          });
                                          

                                          kommt trotzdem im log:

                                          	script.js.Scripte.test6: request package is deprecated - please use httpGet (or a stable lib like axios) instead!
                                          

                                          Warum kommt dies im log ?
                                          (httpGet ist in JS auch rot unterstrichen)

                                          Benutze doch httpGet.

                                          haus-automatisierung liv-in-sky 2 Replies Last reply Reply Quote 0
                                          • haus-automatisierung
                                            haus-automatisierung Developer Most Active @bahnuhr last edited by

                                            @bahnuhr sagte in [gelöst] Skript auf AXIOS umbauen:

                                            Warum kommt dies im log ?
                                            (httpGet ist in JS auch rot unterstrichen)

                                            Kann ich nicht reproduzieren, die Meldung kommt auch nur, wenn require aufgerufen wird. Und das passiert bei Dir im Script ja nicht.

                                            Du hast aber schon über npm installiert (und nicht über GitHub), oder?

                                            bahnuhr 2 Replies Last reply Reply Quote 0
                                            • First post
                                              Last post

                                            Support us

                                            ioBroker
                                            Community Adapters
                                            Donate

                                            888
                                            Online

                                            31.6k
                                            Users

                                            79.4k
                                            Topics

                                            1.3m
                                            Posts

                                            17
                                            56
                                            6015
                                            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