Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. ioBroker Allgemein
    4. Adapter: ioBroker.unifi

    NEWS

    • [erledigt] 15. 05. Wartungsarbeiten am ioBroker Forum

    • ioBroker goes Matter ... Matter Adapter in Stable

    • Monatsrückblick - April 2025

    Adapter: ioBroker.unifi

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

      @thewhobox danke dir - werd das mal testen

      1 Reply Last reply Reply Quote 0
      • thewhobox
        thewhobox @liv-in-sky last edited by thewhobox

        @liv-in-sky So, hier auch noch mit ein und ausschalten. Änderungen sind:

        • Bei Aufruf von getStatus oder setWifi wird automatisch vorher eingeloggt.
        • Es gibt nun auch ein logout
        • Über setWifi(true/false) kann das WLAN ein/aus geschaltet werden.
        const request = require('request-promise-native').defaults({ rejectUnauthorized: false });
        const unifi_username = "user";
        const unifi_password = "pass";
        const unifi_controller = "https://192.168.x.xx:xxxx";
        const wifi_id = "dsa4fsda6g46fdsg984";
        let cookies = [];
        let loggedIn = false;
        
        async function login() {
            return new Promise(async (resolve, reject) => {
                let resp = await request.post({
                    url: unifi_controller + "/api/login",
                    body: JSON.stringify({ username: unifi_username, password: unifi_password }),
                    headers: { 'Content-Type': 'application/json' }
                }).catch((e) => reject(e));
        
                if(resp != null) {
                    console.log("Login war erfolgreich!");
                    let set_cookies = resp.headers["set-cookie"];
                    for(i = 0; i < set_cookies.length; i++) {
                        let cookie = set_cookies[i];
                        cookie = cookie.split(";")[0];
                        cookies.push(cookie);
                    }
                    console.log("Cookies: " + JSON.stringify(cookies));
                    loggedIn = true;
                    resolve();
                } else {
                    reject("resp = null");
                }
            });
        }
        
        async function getStatus() {
            return new Promise(async (resolve, reject) => {
                if(!loggedIn) await login().catch((e) => reject(e));
                let resp = await request.get({
                    url: unifi_controller + "/api/s/default/rest/wlanconf/" + wifi_id,
                    headers: { Cookie: cookies.join("; ") }
                }).catch((e) => reject(e));
            
                if(resp != null && resp.statusCode == 200) {
                    console.log("Status erfolgreich geholt!");
                    console.log(resp);
                    let status = JSON.parse(resp);
                    let wlanOn = status.data[0].enabled;
                    console.log("WLAN ist: " + (wlanOn ? "an" : "aus"));
                    resolve(wlanOn);
                } else {
                    reject(JSON.parse(resp.body).meta.msg);
                }
            });
        }
        
        async function logout() {
            return new Promise(async (resolve, reject) => {
                if(!loggedIn) await login().catch((e) => reject(e));
                let resp = await request.get({
                    url: unifi_controller + "/logout",
                    headers: { Cookie: cookies.join("; ") }
                }).catch((e) => reject(e));
                if(resp != null) {
                    console.log("Du bist nun ausgeloggt.");
                    console.log(resp);
                    resolve();
                } else {
                    reject("resp = null");
                }
            });
        }
        
        async function setWifi(enabled) {
            return new Promise(async (resolve, reject) => {
                if(!loggedIn) await login().catch((e) => reject(e));
                let resp = request.post({
                    url: unifi_controller + "/api/s/default/rest/wlanconf/" + wifi_id,
                    body: JSON.stringify({ _id: "", enabled }),
                    headers: { 'Content-Type': 'application/json', Cookie: cookies.join("; ") }
                }).catch((e) => reject(e));
                
                if(resp != null && resp.statusCode == 200) {
                    console.log("Wifi wurde erfolgreich " + (enabled ? "eingeschaltet" : "ausgeschaltet"));
                    console.log(resp);
                    resolve();
                } else {
                    reject(JSON.parse(resp.body).meta.msg);
                }
            });
        }
        

        Aufruf über:

        async function test() {
            await setWifi(true).catch((e) => console.log("Es trat ein Fehler auf: " + e));
            let isWlanOn = await getStatus();
        }
        test();
        
        liv-in-sky 1 Reply Last reply Reply Quote 1
        • liv-in-sky
          liv-in-sky @thewhobox last edited by

          @thewhobox also wenn ich dich richtig verstehe

          kann ich mit setWifi(true) einschalten bzw setWifi(false) ausschalten - dass funktioniert aber nicht

          Image 11.png

          Image 10.png

          es wird nicht geschalten obwohl die meldung es sagt

          oder mach ich was falsch ?

          thewhobox 1 Reply Last reply Reply Quote 0
          • thewhobox
            thewhobox @liv-in-sky last edited by thewhobox

            @liv-in-sky Ah okay. Nein liegt nicht an dir. ich hab vergessen, dass das alles ja gar nicht synchron abläuft.
            Ich hab den Code oben angepasst.
            Achtung! Jetzt wird das Modul request-promise-native benötigt.
            Beachte! Die Funktionen sind nun asynchron! Heißt um danach den Status abfragen zu können musst du es in eine ebenfalls asynchrone Funktion machen und mit await auf die beendigung warten.

            asynch function test() {
                await setWifi(true);
                let isWlanOn = await getStatus();
            }
            
            liv-in-sky 1 Reply Last reply Reply Quote 0
            • liv-in-sky
              liv-in-sky @thewhobox last edited by

              @thewhobox da stimmt was nicht mit dem upgedaten file

              thewhobox 1 Reply Last reply Reply Quote 0
              • thewhobox
                thewhobox @liv-in-sky last edited by

                @liv-in-sky Habs korrigiert.

                liv-in-sky 1 Reply Last reply Reply Quote 0
                • liv-in-sky
                  liv-in-sky @thewhobox last edited by

                  @thewhobox compiler fehler bei der

                  
                  asynch function test() {
                      await setWifi(true);
                      let isWlanOn = await getStatus();
                  }
                  

                  sollte woh async heißen

                  liv-in-sky 1 Reply Last reply Reply Quote 0
                  • liv-in-sky
                    liv-in-sky @liv-in-sky last edited by

                    @liv-in-sky wenn ich test() aufrufe passiert nix

                    Image 12.png

                    thewhobox 1 Reply Last reply Reply Quote 0
                    • thewhobox
                      thewhobox @liv-in-sky last edited by

                      @liv-in-sky Jap, das liegt daran, dass ich zuwenig geschlafen habe^^
                      Ich korrigiere das gleich.

                      liv-in-sky 1 Reply Last reply Reply Quote 0
                      • liv-in-sky
                        liv-in-sky @thewhobox last edited by

                        @thewhobox mach dir keinen stress @dslraser und ich sind froh, dass du dich darum kümmerst - wir testen solange, bis es läuft

                        thewhobox 1 Reply Last reply Reply Quote 0
                        • thewhobox
                          thewhobox @liv-in-sky last edited by

                          @liv-in-sky Okay, dankeschön 🙂 Wenn du magst kannst es testen. Hab es oben aktualisiert.

                          liv-in-sky 1 Reply Last reply Reply Quote 0
                          • liv-in-sky
                            liv-in-sky @thewhobox last edited by

                            @thewhobox

                            compiler fehler - await irgendwas
                            Image 13.png

                            thewhobox 1 Reply Last reply Reply Quote 0
                            • thewhobox
                              thewhobox @liv-in-sky last edited by

                              @liv-in-sky Na gut. Jetzt müsste es aber funktionieren

                              dslraser liv-in-sky 2 Replies Last reply Reply Quote 0
                              • dslraser
                                dslraser Forum Testing Most Active @thewhobox last edited by

                                @thewhobox
                                ich bin noch unterwegs, teste aber gern heute Abend mit.
                                Mein Ziel ist eigentlich Blockly, da ich das einigermaßen kann. Das heißt, ich würde gern den Status in einen eigenen Datenpunkt schreiben (reicht beim Abruf) und die an und ausschalten Funktion in Blockly haben.
                                Also müsste man in Blockly mehrere (3) Funktionen hernehmen und da dann Deinen Code reinpacken ?
                                Von da aus kann ich es dann ja weiter verarbeiten.

                                thewhobox 1 Reply Last reply Reply Quote 0
                                • thewhobox
                                  thewhobox @dslraser last edited by

                                  @dslraser In Blockly wird das schwer bis unmöglich, da es dort keine möglichkeit für asynchrone Funktionen gibt.
                                  Du kannst den Code aber ganz leicht anpassen wie zum Beispiel:

                                  on("javascript.0.turnWifiOn", (state) => {
                                    setWifi(true);
                                  });
                                  
                                  1 Reply Last reply Reply Quote 1
                                  • liv-in-sky
                                    liv-in-sky @thewhobox last edited by

                                    @thewhobox

                                    bin wiedr aktiv - compiler fehler ist weg aber es wird nichts geschalten - auch der status allein wird nicht wiedergegeben

                                    es kommt nur noch login war erfolgreich - bei allen drei möglichkeiten
                                    bei aufruf der test function kommen dann 2 logins

                                    thewhobox 1 Reply Last reply Reply Quote 0
                                    • thewhobox
                                      thewhobox @liv-in-sky last edited by thewhobox

                                      @liv-in-sky Ist leider blöd, wenn ich das hier selbst nicht testen kann. Kannst du das mal kurz testen? Hab mal mehr debug logs eingebaut.

                                      const request = require('request-promise-native').defaults({ rejectUnauthorized: false });
                                      const unifi_username = "user";
                                      const unifi_password = "pass";
                                      const unifi_controller = "https://192.168.x.xx:xxxx";
                                      const wifi_id = "dsa4fsda6g46fdsg984";
                                      let cookies = [];
                                      let loggedIn = false;
                                      
                                      async function login() {
                                          return new Promise(async (resolve, reject) => {
                                              let resp = await request.post({
                                                  url: unifi_controller + "/api/login",
                                                  body: JSON.stringify({ username: unifi_username, password: unifi_password }),
                                                  headers: { 'Content-Type': 'application/json' }
                                              }).catch((e) => { console.log("login: reject"), reject(e) });
                                              console.log("login: got response")
                                              if(resp != null) {
                                                  console.log("login: Login war erfolgreich!");
                                                  let set_cookies = resp.headers["set-cookie"];
                                                  for(i = 0; i < set_cookies.length; i++) {
                                                      let cookie = set_cookies[i];
                                                      cookie = cookie.split(";")[0];
                                                      cookies.push(cookie);
                                                  }
                                                  console.log("login: Cookies: " + JSON.stringify(cookies));
                                                  loggedIn = true;
                                                  console.log("login: resolved")
                                                  resolve();
                                              } else {
                                                  console.log("login: rejected")
                                                  reject("resp = null");
                                              }
                                          });
                                      }
                                      
                                      async function getStatus() {
                                          return new Promise(async (resolve, reject) => {
                                              if(!loggedIn) await login().catch((e) => reject(e));
                                              let resp = await request.get({
                                                  url: unifi_controller + "/api/s/default/rest/wlanconf/" + wifi_id,
                                                  headers: { Cookie: cookies.join("; ") }
                                              }).catch((e) => reject(e));
                                          
                                              if(resp != null && resp.statusCode == 200) {
                                                  console.log("Status erfolgreich geholt!");
                                                  console.log(resp);
                                                  let status = JSON.parse(resp);
                                                  let wlanOn = status.data[0].enabled;
                                                  console.log("WLAN ist: " + (wlanOn ? "an" : "aus"));
                                                  resolve(wlanOn);
                                              } else {
                                                  reject(JSON.parse(resp.body).meta.msg);
                                              }
                                          });
                                      }
                                      
                                      async function logout() {
                                          return new Promise(async (resolve, reject) => {
                                              if(!loggedIn) await login().catch((e) => reject(e));
                                              let resp = await request.get({
                                                  url: unifi_controller + "/logout",
                                                  headers: { Cookie: cookies.join("; ") }
                                              }).catch((e) => reject(e));
                                              if(resp != null) {
                                                  console.log("Du bist nun ausgeloggt.");
                                                  console.log(resp);
                                                  resolve();
                                              } else {
                                                  reject("resp = null");
                                              }
                                          });
                                      }
                                      
                                      async function setWifi(enabled) {
                                          return new Promise(async (resolve, reject) => {
                                              console.log("setWifi: start set wifi");
                                              if(!loggedIn) { console.log("need to login"); await login().catch((e) => reject(e)); }
                                              console.log("setWifi: now setting wifi");
                                              let resp = request.post({
                                                  url: unifi_controller + "/api/s/default/rest/wlanconf/" + wifi_id,
                                                  body: JSON.stringify({ _id: "", enabled }),
                                                  headers: { 'Content-Type': 'application/json', Cookie: cookies.join("; ") }
                                              }).catch((e) => { console.log("setWifi: rejected"); reject(e) });
                                              console.log("setWifi: got response")
                                              if(resp != null && resp.statusCode == 200) {
                                                  console.log("setWifi: Wifi wurde erfolgreich " + (enabled ? "eingeschaltet" : "ausgeschaltet"));
                                                  console.log(resp);
                                                  resolve();
                                              } else {
                                                  console.log("setWifi: rejetced")
                                                  reject(JSON.parse(resp.body).meta.msg);
                                              }
                                          });
                                      }
                                      
                                      async function test() {
                                          console.log("starting test");
                                          await setWifi(true).catch((e) => console.log("reject1"));
                                          console.log("getting status");
                                          let wlan = await getStatus().catch((e) => console.log("reject2"));
                                          console.log("fin")
                                          console.log(wlan);
                                      }
                                      
                                      test()
                                      

                                      liv-in-sky 1 Reply Last reply Reply Quote 0
                                      • liv-in-sky
                                        liv-in-sky @thewhobox last edited by liv-in-sky

                                        @thewhobox das glaub ich
                                        Image 15.png

                                        mehr kommt nicht

                                        thewhobox 1 Reply Last reply Reply Quote 0
                                        • thewhobox
                                          thewhobox @liv-in-sky last edited by

                                          @liv-in-sky Ist das alles im log? Eig müsste er nach "Login erfolgreich" noch die Cookies anzeigen.

                                          Kannst du die zwei Zeilen mal unter let set_cookies = resp.headers["set-cookie"] machen?
                                          (Zeile 19 müsste das sein)

                                          console.log("set_cookies: ", typeof set_cookies);
                                          console.log("set_cookies: ", set_cookies);
                                          
                                          liv-in-sky 1 Reply Last reply Reply Quote 0
                                          • liv-in-sky
                                            liv-in-sky @thewhobox last edited by

                                            @thewhobox sagte in Adapter: ioBroker.unifi:

                                            console.log("set_cookies: ", typeof set_cookies); console.log("set_cookies: ", set_cookies);

                                            soweit kommt er nicht - da gibt es keinen neuen log eintrag

                                            javascript.2	2019-08-27 16:49:56.566	info	script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: Login war erfolgreich!
                                            javascript.2	2019-08-27 16:49:56.566	info	script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: got response
                                            javascript.2	2019-08-27 16:49:56.259	info	script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: registered 0 subscriptions and 0 schedules
                                            javascript.2	2019-08-27 16:49:56.259	info	script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: need to login
                                            javascript.2	2019-08-27 16:49:56.259	info	script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: start set wifi
                                            javascript.2	2019-08-27 16:49:56.259	info	script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: starting test
                                            javascript.2	2019-08-27 16:49:56.259	info	Start javascript script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2
                                            javascript.2	2019-08-27 16:49:56.209	info	Stop script script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2
                                            js2fs.0	2019-08-27 16:49:56.191	info	Script file script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 changed, also update in ioBroker
                                            javascript.2	2019-08-27 16:49:54.480	info	script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: Login war erfolgreich!
                                            javascript.2	2019-08-27 16:49:54.480	info	script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: got response
                                            javascript.2	2019-08-27 16:49:54.186	info	script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: registered 0 subscriptions and 0 schedules
                                            javascript.2	2019-08-27 16:49:54.186	info	script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: need to login
                                            javascript.2	2019-08-27 16:49:54.186	info	script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: start set wifi
                                            javascript.2	2019-08-27 16:49:54.186	info	script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: starting test
                                            javascript.2	2019-08-27 16:49:54.186	info	Start javascript script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2
                                            js2fs.0	2019-08-27 16:49:54.175	info	Script script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 modified in ioBroker, write to file
                                            javascript.2	2019-08-27 16:49:54.173	info	Stop script script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2
                                            javascript.0	2019-08-27 16:49:54.172	info	Stop script script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2
                                            javascript.1	2019-08-27 16:49:54.172	info	Stop script script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2
                                            
                                            thewhobox 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post

                                            Support us

                                            ioBroker
                                            Community Adapters
                                            Donate
                                            FAQ Cloud / IOT
                                            HowTo: Node.js-Update
                                            HowTo: Backup/Restore
                                            Downloads
                                            BLOG

                                            745
                                            Online

                                            31.6k
                                            Users

                                            79.5k
                                            Topics

                                            1.3m
                                            Posts

                                            unifi
                                            170
                                            991
                                            280896
                                            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