Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JSON Tabelle - Clients aus Fritzbox auslesen

    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

    JSON Tabelle - Clients aus Fritzbox auslesen

    This topic has been deleted. Only users with topic management privileges can see it.
    • liv-in-sky
      liv-in-sky last edited by liv-in-sky

      danke an @MCU für die vorlage - kommt von: https://forum.iobroker.net/post/949240

      das script erzeugt eine json tabelle (z.b json inventwo widget) für die vis aus der Fritzbox ausgelesen (active Clients)

      • im javascript setting muss xml-js als zusätzliche node angegeben werden
      • es muss der tr064 adapter für die fritzbbox installiert sein
      • datenpunkt(zeichenkette) muss selber angelegt und im script eingetragen werden

      Image 217.png

      
      //@liv-in-sky 18.02.2023-17:13
      //dank an @MCU für die Vorlage
      var convert = require('xml-js');
      let wlan = '1'; // 1 -> 2.4GHz ( für diesen Fall missverständlich )
      let fritzCommandDP = 'tr-064.0.states.command';
      let fritzResultDP = 'tr-064.0.states.commandResult';
      let ipFritzBox='192.168.178.1'
      let dataPoint='0_userdata.0.FritzBoxDevices' 
      let cmdHostListPath     = {'service': 'urn:dslforum-org:service:Hosts:'+wlan,'action': 'X_AVM-DE_GetHostListPath','params': {}};
      
      sendCMD(JSON.stringify(cmdHostListPath));
      
      schedule("*/5 * * * *", async function () {
                                                sendCMD(JSON.stringify(cmdHostListPath));
                                                 });
      
      
      async function sendCMD(cmd) {
         setState(fritzCommandDP, cmd);
         const obj = await once({ id: fritzResultDP });
         // log("Erg: " + obj.state.val);
         let erg = JSON.parse(obj.state.val);
         // log(erg["NewX_AVM-DE_HostListPath"]);
         const request = require('request');
      
         request('http://'+ipFritzBox+':49000'+erg["NewX_AVM-DE_HostListPath"], { json: true }, (err, res, body) => {
         if (err) { return console.log(err); } else{}
             //console.log(body.url);
             //console.log(body.explanation);
             //console.log(res);
            // console.log(body);
             var result2 = JSON.parse(convert.xml2json(body, {compact: true, spaces: 2, ignoreAttributes:true}));
               //  console.log(result2); 
             let myJsonResult=[]
             let myJsonBest=result2.List ;
             let theNameDate="NAME (Last Update: "+formatDate(getDateObject((new Date().getTime())), "SS:mm - TT.MM.")+")";
             for (let i=0;i<myJsonBest.Item.length;i++){
                 if(myJsonBest.Item[i].Active["_text"]==1){
                 myJsonResult.push({
                     [theNameDate]:myJsonBest.Item[i].HostName["_text"],
                     "IP":myJsonBest.Item[i].IPAddress["_text"],
                     "MAC":myJsonBest.Item[i].MACAddress["_text"] })
                 }
             }
              //log(JSON.stringify(myJsonResult))
                 setState(dataPoint,JSON.stringify(myJsonResult))
         });  
      }
      
      
      
      
      
      
      
      
      

      1 Reply Last reply Reply Quote 4
      • falke69
        falke69 last edited by

        Hallo @liv-in-sky, ich weiß nicht ,ob Du oder jemand anders noch das Script nutzt.
        Wäre es vielleicht möglich, (falls es Deine Zeit erlaubt) dass Du es auf "httpGet" umstellen kannst?
        Ich habe alles versucht, leider ohne Erfolg.

        Vielen Dank

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

          Das Script ist ja wild. Hier eine überarbeitete Version (nicht getestet mangels Hardware):

          // @liv-in-sky 18.02.2023-17:13
          // @haus-automatisierung.com 07.11.2024-12:12
          // dank an @MCU für die Vorlage
          const convert = require('xml-js');
          
          const wlan = '1'; // 1 -> 2.4GHz (für diesen Fall missverständlich)
          const fritzCommandDP = 'tr-064.0.states.command';
          const fritzResultDP = 'tr-064.0.states.commandResult';
          const ipFritzBox = '192.168.178.1';
          const dataPoint = '0_userdata.0.FritzBoxDevices';
          const cmdHostListPath = {
              service: `urn:dslforum-org:service:Hosts:${wlan}`,
              action: 'X_AVM-DE_GetHostListPath',
              params: {}
          };
           
          async function sendCMD() {
              setState(fritzCommandDP, JSON.stringify(cmdHostListPath));
          
              const obj = await once({ id: fritzResultDP });
              const erg = JSON.parse(obj.state.val);
          
              httpGet(`http://${ipFritzBox}:49000${erg['NewX_AVM-DE_HostListPath']}`, { timeout: 3000 }, (err, response) => {
                  if (!err) {
                      const theNameDate = `NAME (Last Update: ${formatDate(Date.now(), 'SS:mm - TT.MM.')})`;
          
                      const result = JSON.parse(convert.xml2json(response.data, { compact: true, spaces: 2, ignoreAttributes:true }));
          
                      const tableData = [];
                      const resultList = result.List;
          
                      for (let i = 0; i < resultList.Item.length; i++) {
                          if (resultList.Item[i].Active['_text'] == 1) {
                              tableData.push({
                                  [theNameDate]: resultList.Item[i].HostName['_text'],
                                  IP: resultList.Item[i].IPAddress['_text'],
                                  MAC: resultList.Item[i].MACAddress['_text'],
                              });
                          }
                      }
          
                      setState(dataPoint, { val: JSON.stringify(tableData), ack: true });
                  } else {
                      console.log(err);
                  }
              });
          }
          
          schedule('*/5 * * * *', sendCMD);
          sendCMD();
          
          falke69 1 Reply Last reply Reply Quote 1
          • falke69
            falke69 @haus-automatisierung last edited by

            @haus-automatisierung

            Vielen herzlichen Dank für Deine Bemühungen.
            Es scheint aktuell zu laufen!

            Danke!

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

            Support us

            ioBroker
            Community Adapters
            Donate

            480
            Online

            31.7k
            Users

            79.9k
            Topics

            1.3m
            Posts

            3
            4
            426
            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