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

    • ioBroker goes Matter ... Matter Adapter in Stable

    • Monatsrückblick - April 2025

    • Minor js-controller 7.0.7 Update in latest repo

    Adapter: ioBroker.unifi

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

      @liv-in-sky Ja hast recht. Hab den teil vergessen zu ändern.
      Hab das Skript oben angepasst. einfach mal das JSON.parse weg lassen.

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

        @thewhobox
        @liv-in-sky

        Unexpected token o in JSON at position 1
        
        

        Ich habe keine Ahnung davon, aber wenn ich google dazu befrage, dann kommt Sinngemäß oft die Antwort, das es schon ein Objekt ist und man es direkt verwenden kann und nicht parsen braucht ?

        Danke für die Mühe.

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

          @dslraser Ja das stimmt. Hab das Skript oben angepasst.

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

            @thewhobox cool - läuft

            könntest du das ein und ausschalten auch noch für uns machen 🙂 🙂

            	javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN1: WLAN ist: an
            
            thewhobox 2 Replies Last reply Reply Quote 0
            • thewhobox
              thewhobox @liv-in-sky last edited by

              @liv-in-sky okay super 🙂 Kann ich später machen.

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

                @thewhobox
                klappt 👍 👍 👍

                10:33:43.645	info	javascript.0 script.js.Heiko.Unifi.unifi_wifi: Login war erfolgreich!
                10:33:43.645	info	javascript.0 script.js.Heiko.Unifi.unifi_wifi: Cookies: ["unifises=JA7z7GVM4cnOB6sZnqkcjBFBFpFzJw5S","csrf_token=GcAjwdvcY6hc58t6E4gLi4X3X7g490mn"]
                10:33:43.689	info	javascript.0 script.js.Heiko.Unifi.unifi_wifi: Status erfolgreich geholt!
                10:33:43.690	info	javascript.0 script.js.Heiko.Unifi.unifi_wifi: {'statusCode':200,'body':'{\'meta\':{\'rc\':\'ok\'},\'data\':[{\'_id\':\'5d3c5be0bc96040011365fe5\',\'enabled\':true,\'security\':\'open\',\'wep_idx\':1,\'wpa_mode\':\'wpa2\',\'wpa_enc\':\'ccmp\',\'usergroup_id\':\'5d3b2323bc9604001132d2c5\',\'dtim_mode\':\'default\',\'dtim_ng\':1,\'dtim_na\':1,\'minrate_ng_enabled\':false,\'minrate_ng_advertising_rates\':false,\'minrate_ng_data_rate_kbps\':1000,\'minrate_ng_cck_rates_enabled\':true,\'minrate_na_enabled\':false,\'minrate_na_advertising_rates\':false,\'minrate_na_data_rate_kbps\':6000,\'mac_filter_enabled\':false,\'mac_filter_policy\':\'allow\',\'mac_filter_list\':[],\'bc_filter_enabled\':false,\'bc_filter_list\':[],\'group_rekey\':3600,\'name\':\'Hübis Gäste\',\'is_guest\':true,\'x_passphrase\':\'dashieristprivat\',\'wlangroup_id\':\'5d3b2323bc9604001132d2c6\',\'schedule\':[],\'minrate_ng_mgmt_rate_kbps\':1000,\'minrate_na_mgmt_rate_kbps\':6000,\'minrate_ng_beacon_rate_kbps\':1000,\'minrate_na_beacon_rate_kbps\':6000,\'site_id\':\'5d3b231abc9604001132d2b8\',\'x_iapp_key\':\'0613db2263d118bc9e21936a202a964e\',\'fast_roaming_enabled\':false,\'no2ghz_oui\':true}]}','headers':{'cache-control':'private','expires':'Thu, 01 Jan 1970 00:00:00 GMT','vary':'Origin,Accept-Encoding','access-control-allow-credentials':'true','access-control-expose-headers':'Access-Control-Allow-Origin,Access-Control-Allow-Credentials','x-frame-options':'DENY','content-type':'application/json;charset=UTF-8','content-length':'1000','date':'Tue, 27 Aug 2019 08:33:43 GMT','connection':'close'},'request':{'uri':{'protocol':'https:','slashes':true,'auth':null,'host':'192.168.180.110:8443','port':'8443','hostname':'192.168.180.110','hash':null,'search':null,'query':null,'pathname':'/api/s/default/rest/wlanconf/5d3c5be0bc96040011365fe5','path':'/api/s/default/rest/wlanconf/5d3c5be0bc96040011365fe5','href':'https://192.168.180.110:8443/api/s/default/rest/wlanconf/5d3c5be0bc96040011365fe5'},'method':'GET','headers':{'Cookie':'unifises=JA7z7GVM4cnOB6sZnqkcjBFBFpFzJw5S; csrf_token=GcAjwdvcY6hc58t6E4gLi4X3X7g490mn'}}}
                10:33:43.690	info	javascript.0 script.js.Heiko.Unifi.unifi_wifi: WLAN ist: an
                
                1 Reply Last reply Reply Quote 0
                • liv-in-sky
                  liv-in-sky @thewhobox last edited by

                  @thewhobox noch ne frage:
                  kann ich die function getStatus auch dafür benutzen (ohne header) , um mit dem simpleapi adapter eines anderen iobroker zu kommunizieren ?

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

                    @liv-in-sky Jain, da die Funktion nur auf die spezielle zurückgabe vom Unifi Controller angepasst ist.
                    Du musst das im Callback anpassen und die Header weglassen.
                    Vereinfacht geht:

                    request.get("http://192.168.0.xxx/get/system.adapter.admin.0.alive", (err, resp) => {
                        if(!err) {
                            let state = JSON.parse(resp);
                            console.log("State hat den Wert: " + state.val);
                        } else {
                            console.log("Es trat ein Fehler auf: " + err);
                        }
                    })
                    
                    liv-in-sky 1 Reply Last reply Reply Quote 1
                    • 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
                                            • First post
                                              Last post

                                            Support us

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

                                            472
                                            Online

                                            31.6k
                                            Users

                                            79.5k
                                            Topics

                                            1.3m
                                            Posts

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