- Home
- Deutsch
- ioBroker Allgemein
- Adapter: ioBroker.unifi
Adapter: ioBroker.unifi
-
@liv-in-sky sagte in Adapter: ioBroker.unifi:
login: got response: {"statusCode":200,"body":"{\
Könntest du davon mir die komplette Zeile schicken?
Also momentan funktioniert das speichern des Cookies nicht, deswegen geht getStatus und setWifi nicht.
-
javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: got response: {"statusCode":200,"body":"{\"meta\":{\"rc\":\"ok\"},\"data\":[]}","headers":{"vary":"Origin","access-control-allow-credentials":"true","access-control-expose-headers":"Access-Control-Allow-Origin,Access-Control-Allow-Credentials","set-cookie":["unifises=7Eqk0YywPeNtHf5nKKMdWmNAqRhSyR8Q; Path=/; Secure; HttpOnly","csrf_token=HlhrzcVRNkyp4CpUYN2pRQAvYzwK6p6U; Path=/; Secure"],"x-frame-options":"DENY","content-type":"application/json;charset=UTF-8","content-length":"30","date":"Tue, 27 Aug 2019 15:27:36 GMT","connection":"close"},"request":{"uri":{"protocol":"https:","slashes":true,"auth":null,"host":"192.168.178.157:8443","port":"8443","hostname":"192.168.178.157","hash":null,"search":null,"query":null,"pathname":"/api/login","path":"/api/login","href":"https://192.168.178.157:8443/api/login"},"method":"POST","headers":{"Content-Type":"application/json","content-length":40}}}
alles
17:37:35.460 info javascript.2 Stop script script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 17:37:36.896 info javascript.2 Start javascript script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 17:37:36.896 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: registered 0 subscriptions and 0 schedules 17:37:36.918 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: Du bist nun ausgeloggt. 17:37:36.918 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: <!DOCTYPE html><html lang="en" ng-controller="ManageController as manageCtrl" ng-strict-di><head><meta charset="utf-8"><title ng-bind="manageCtrl.system.getName() || manageCtrl.system.getHostname() || 'UniFi Network'">UniFi Network</title><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" unifi-prevent-focus-zoom><meta name="apple-itunes-app" content="app-id=1057750338"><base href="/manage/"><link rel="apple-touch-icon-precomposed" href="images/favicons/favicon-152.png?v=2"><meta name="msapplication-TileColor" content="#0193d7"><meta name="msapplication-TileImage" content="images/favicons/favicon-144.png?v=2"><link rel="apple-touch-icon-precomposed" sizes="152x152" href="images/favicons/favicon-152.png?v=2"><link rel="apple-touch-icon-precomposed" sizes="144x144" href="images/favicons/favicon-144.png?v=2"><link rel="apple-touch-icon-precomposed" sizes="120x120" href="images/favicons/favicon-120.png?v=2"><link rel="apple-touch-icon-precomposed" sizes="72x72" href="images/favicons/favicon-72.png?v=2"><link rel="apple-touch-icon-precomposed" href="images/favicons/favicon-57.png?v=2"><link rel="icon" href="images/favicons/favicon-32.png?v=2" sizes="32x32"><script src="config/config.v5.11.39.0.js"></script><script type="text/javascript">window.unifiConfig.version = '5.11.39.0'; </script><script src="js/initial.v5.11.39.0.js"></script><script src="js/components.v5.11.39.0.js" defer></script><script src="js/base.v5.11.39.0.js" defer></script><script src="js/main.manage.v5.11.39.0.js" defer></script><style>.whiteout{position:fixed;top:0;right:0;bottom:0;left:0;z-index:30;display:flex;flex-direction:column;justify-content:center;align-items:center}.type-word-break{word-break:break-all}[ng-cloak]{display:none}.app{visibility:hidden}*,*:before,*:after{-webkit-box-sizing:border-box;box-sizing:border-box}html,body{font-size:14px !important}body{font-family:"Lato", "Helvetica Neue", "Helvetica", Helvetica, Arial, sans-serif;font-weight:400;height:100%;background:#ffffff;color:#000000;line-height:1}div{display:block}.mt-3{margin-top:12px !important}.mb-3{margin-bottom:12px !important}.stackCenter{display:flex;flex-direction:column;align-items:center}.ubntStatusBar{background-color:#d6d9e2;display:inline-block;height:2px;width:100%}.ubntStatusBar--success{--status-bar-color: #39cc64}.ubntStatusBar__status{background-color:#006fff;display:block;height:100%}.appSpinner.appSpinner--medium{background-size:cover;width:3.5714285714rem;height:3.5714285714rem} </style><link href="fonts/5.11.39.0/ubnt-icon/style.css" rel="stylesheet" media="none" onload="if(media!=='all') media='all'"><link href="css/styles.bundle.v5.11.39.0.css" rel="stylesheet" media="none" onload="if(media!=='all') media='all'"><link href="css/app.v5.11.39.0.css" rel="stylesheet" media="none" onload="if(media!=='all') media='all'"><link href="https://chrome.google.com/webstore/detail/hmpigflbjeapnknladcfphgkemopofig" rel="chrome-webstore-item"><link href="fonts/5.11.39.0/aura/fonts.css" rel="stylesheet" media="none" onload="if(media!=='all') media='all'"></head><body aura-theme><svg class="pos-absolute" height="0" width="0"><defs><linearGradient id="experienceGraphGradient" x1="0" y1="0" x2="0" y2="100%" patternUnits="userSpaceOnUse"><stop offset="0%" stop-color="#0046cc" stop-opacity="0.3"></stop><stop offset="100%" stop-color="#ffffff" stop-opacity="0"></stop></linearGradient></defs></svg><route-status route-status-fatal-state="manageCtrl.ENV.FATAL_STATE" route-status-immediate="manageCtrl.site.isSiteChanging"><unifi-connection-status></unifi-connection-status></route-status><div class="app" unifi-bowser ng-cloak><unifi-key-events></unifi-key-events><!--[if lte IE 9]><div class="appOutdatedBrowserWarning is-warning-visible ubntToastContainer"><![endif]--><!--[if gt IE 9]><!--><div class="appOutdatedBrowserWarning ubntToastContainer"><!--<![endif]--><div class="ubntToast"><div class="ubntToast__image"><div class="ubntIconContainer ubntIconContainer--round ubntIconContainer--danger"><div class="type-larger ubnt-icon--alert" style="margin-top: -4px;"></div></div></div><div class="ubntToast__body"><div class="ubntToast__title">Outdated Browser</div><div class="ubntToast__details">You are using an <strong>outdated</strong> browser. Please <a href='http://browsehappy.com/' class='ubntLink'>upgrade your browser</a> to improve your experience.</div></div></div></div><div class="ubntToastContainer" hey-toaster unifi-position-alerts><div class="ubntToast" ng-class="{'is-child-dropdown-expanded': dropdownVisible}"><div class="ubntToast__image appAlertIcon" style="display: flex;" ng-if="::toast.message.icon" ng-class="::[toast.message.icon, { 'primary': toast.level === 'primary' || toast.level === 'default', 'success': toast.level === 'success', 'warning': toast.level === 'warning', 'danger': toast.level === 'danger' }]"></div><div class="ubntToast__image" ng-if="::!toast.message.icon"><div class="ubntIconContainer ubntIconContainer--round" ng-class="::{ 'ubntIconContainer--primary': toast.level === 'primary' || toast.level === 'default', 'ubntIconContainer--success': toast.level === 'success', 'ubntIconContainer--warning': toast.level === 'warning', 'ubntIconContainer--danger': toast.level === 'danger' }" ng-init="icon = ((toast.level === 'primary' || toast.level === 'default') && 'bell' || toast.level === 'danger' && 'close' || toast.level)"><unifi-aura-icon unifi-aura-icon="::icon" unifi-aura-icon-class="['ubntIcon']"></unifi-aura-icon></div></div><div class="ubntToast__close"><div class="ubntIcon ubntIcon--actionable ubntIcon--transparent iconText__icon ubnt-icon--x-circle" ng-click="toast.close()"></div></div><div class="ubntToast__body"><div class="ubntToast__title" ng-if="::toast.message.title">{{::toast.message.title}}</div><div class="ubntToast__details">{{::toast.message.text || toast.message | decodeHtml}}</div><div class="ubntToast__actions ubntButtonGroup ubntButtonGroup--cozy" ng-if="::toast.message.primary || toast.message.secondary"><button class="ubntButton ubntButton--link" ng-if="toast.message.secondary" type="button" ng-click="toast.message.secondary.callback();toast.close()"><span class="button__icon icon" ng-class="::toast.message.secondary.icon"></span><span>{{::toast.message.secondary.label}}</span></button><button class="ubntButton ubntButton--link" ng-if="toast.message.primary && !toast.message.primary.actions" type="button" ng-click="toast.message.primary.callback();toast.close()"><span class="button__icon icon" ng-class="::toast.message.primary.icon ? toast.message.primary.icon : 'ubnt-icon--check-2'"></span><span>{{::toast.message.primary.label}}</span></button><button class="ubntSelect ubntSelect--link" ng-if="toast.message.primary && toast.message.primary.actions" type="button" ng-click="$parent.$parent.dropdownVisible = !$parent.$parent.dropdownVisible;"> {{::toast.message.primary.label}}</button></div><div class="ubntToast__dropdown" ng-if="::toast.message.primary && toast.message.primary.actions"><div class="ubntDropdown"><a class="ubntDropdown__item" ng-repeat="item in ::toast.message.primary.actions" ng-click="item.callback(toast)">{{::item.label}}</a></div></div></div></div></div><ui-view></ui-view><unifi-debug-panel></unifi-debug-panel><div class="unifiModalLayoutContainer"><unifi-blackout-modal unifi-blackout-modal-layout="addClientLayout" unifi-blackout-modal-disable-esc="true"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="autoChannelsModalLayout" unifi-blackout-modal-disable-esc="true"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="addDpiCategoryGraphLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="addSiteLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="addSshKeyLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="cloudAccess2faTokenLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="cloudAccessUserLayout" unifi-blackout-modal-reject-on-esc="true"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="cloudKeySdCardCheckLayout" unifi-blackout-modal-disable-esc="true"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="configureMapsLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="confirmLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="dashboardFormLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="deviceUlteSetupLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="discoveryToolChromeRequiredLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="discoveryToolInstallLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="discoveryToolPortAlreadyInUseLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="dpiRestrictionFormLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="dpiRestrictionGroupFormLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="dpiRestrictionNetworkAssignmentFormLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="editAccountLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="adminPasswordChangeSuggestionLayout" unifi-blackout-modal-disable-esc="true"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="editMapLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="exportSiteLayout" unifi-blackout-modal-disable-esc="true"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="eliteDeviceOfflineLayout" unifi-blackout-modal-disable-esc="true"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="firewallGroupsCreateLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="fallbackVlanLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="hotspot2NaiRealmListEntryLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="hotspotPackageLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="importSiteLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="ipsSuppressionFormLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="ipsWhitelistFormLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="instantRmaLayout" unifi-blackout-modal-disable-esc="true"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="ipsThreatDetailLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="ispThroughputLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="preferencesLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="radiusProfileLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="reportClientFingerprintLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="simpleTextEditorLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="sitesOverviewLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="speedTestLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="statisticsDpiFiltersLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="systemConfigLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="reportTermsOfServiceLayout" unifi-blackout-modal-disable-esc="true"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="trainingLoginLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="webrtcErrorLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="webrtcFailedLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="wirelessNetworkMacFilterAddBatchLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="wirelessNetworkMacFilterAddClientsLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="wirelessUplinkDfsWarningLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="workingLayout"></unifi-blackout-modal><unifi-blackout-modal unifi-blackout-modal-layout="webrtcReconnectingLayout" unifi-blackout-modal-disable-esc="true"></unifi-blackout-modal><unifi-app-takeover unifi-app-takeover-layout="whatsNewTakeoverLayout" unifi-app-takeover-show-header="true"></unifi-app-takeover></div></div><!-- Native app loading status--></body></html> 17:37:36.918 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: starting test 17:37:36.918 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: start set wifi 17:37:36.918 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: need to login 17:37:37.194 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: got response: {"statusCode":200,"body":"{\"meta\":{\"rc\":\"ok\"},\"data\":[]}","headers":{"vary":"Origin","access-control-allow-credentials":"true","access-control-expose-headers":"Access-Control-Allow-Origin,Access-Control-Allow-Credentials","set-cookie":["unifises=UxCIewm8sjQjyecfzvUm4dfDNbrsE2d5; Path=/; Secure; HttpOnly","csrf_token=uN8KhbamMmCoPo1GpgnK94JEBQQJRGZf; Path=/; Secure"],"x-frame-options":"DENY","content-type":"application/json;charset=UTF-8","content-length":"30","date":"Tue, 27 Aug 2019 15:37:36 GMT","connection":"close"},"request":{"uri":{"protocol":"https:","slashes":true,"auth":null,"host":"192.168.178.157:8443","port":"8443","hostname":"192.168.178.157","hash":null,"search":null,"query":null,"pathname":"/api/login","path":"/api/login","href":"https://192.168.178.157:8443/api/login"},"method":"POST","headers":{"Content-Type":"application/json","content-length":40}}} 17:37:37.194 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: Login war erfolgreich! 17:37:37.194 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: set_cookies: 17:37:37.194 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: set_cookies: 17:37:37.194 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: Cookies: ["unifises=UxCIewm8sjQjyecfzvUm4dfDNbrsE2d5","csrf_token=uN8KhbamMmCoPo1GpgnK94JEBQQJRGZf"] 17:37:37.194 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: resolved 17:37:37.194 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: now setting wifi 17:37:37.194 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: got response 17:37:37.194 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: rejetced 17:37:37.195 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: resp: {} 17:37:37.200 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: rejected: StatusCodeError: 401 - "{\"meta\":{\"rc\":\"error\",\"msg\":\"api.err.LoginRequired\"},\"data\":[]}" 17:37:37.200 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: reject1 17:37:37.200 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: getting status
-
@thewhobox auch auf die gefahr das dies lächerlich ist
ich habe mal das unifi.js file von nodered geholt - kannst du damit etwas anfangen - damit soll man auch wifi disablen und enablen können - zeile 1171 wird auch wlan ein-ausgeschalten
unifi.js.txt -
@thewhobox
@liv-in-sky
so, jetzt bin ich zu Hause. Jetzt kann ich Euch kaum folgen. Soll ich was helfen/testen ? -
@dslraser
ich glaub im moment ist pausedas ist das aktuelle script: achtung in der ersten zeile hat sich auch etwas verändert - vor dem user setting
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: " + JSON.stringify(resp)) if(resp != null) { // console.log("login: Login war erfolgreich!"); // if(resp.headers && resp.headers.hasOwnProperty("set-cookie")) { // let set_cookies = resp.headers["set-cookie"]; // console.log("set_cookies: ", typeof set_cookies); // console.log("set_cookies: ", set_cookies); // 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)); // } else { // console.log("login: no cookies to set!") // } 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) => { 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' } }).catch((e) => { console.log("setWifi: rejected: " + e); 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") console.log("resp: " + JSON.stringify(resp)); reject("msg: " + JSON.parse(resp.body).meta.msg); } }); } async function test() { await logout(); 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()
-
@thewhobox
würd gern was sinnvolles beitragen, deshalb noch ein amateur versuch etwas beizusteuernhab mal die antworten des bash scriptes und deines scriptes verglichen - schaut irgendwie anders aus
-
@liv-in-sky ja das macht jede Bibliothek beim empfangen der Daten anders.
-
Guten Morgen,
hier nochmal das ganze Skript:
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({ resolveWithFullResponse: true, 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: " + JSON.stringify(resp)) if(resp != null) { console.log("login: Login war erfolgreich!"); if(resp.headers && resp.headers.hasOwnProperty("set-cookie")) { let set_cookies = resp.headers["set-cookie"]; console.log("set_cookies: ", typeof set_cookies); console.log("set_cookies: ", set_cookies); 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)); } else { console.log("login: no cookies to set!") } 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) => { 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: " + e); 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") console.log("resp: " + JSON.stringify(resp)); reject("msg: " + JSON.parse(resp.body).meta.msg); } }); } async function test() { console.log("starting test"); await setWifi(true).catch((e) => console.log("reject1: " + e)); console.log("getting status"); let wlan = await getStatus().catch((e) => console.log("reject2: " + e)); console.log("fin") console.log(wlan); } test()
-
@thewhobox auch einen guten morgen
und hier das log:
09:21:26.481 info javascript.2 Stop script script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 09:21:34.242 info javascript.2 Start javascript script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 09:21:34.242 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: starting test 09:21:34.242 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: start set wifi 09:21:34.242 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: need to login 09:21:34.242 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: registered 0 subscriptions and 0 schedules 09:21:34.536 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: got response: {"statusCode":200,"body":"{\"meta\":{\"rc\":\"ok\"},\"data\":[]}","headers":{"vary":"Origin","access-control-allow-credentials":"true","access-control-expose-headers":"Access-Control-Allow-Origin,Access-Control-Allow-Credentials","set-cookie":["unifises=rUTs0tMWpm4QcxFH1cvV5VuWIYCV4UhY; Path=/; Secure; HttpOnly","csrf_token=lE7GdlIBf0bhQS4qJcaowM1F4iGmc2lk; Path=/; Secure"],"x-frame-options":"DENY","content-type":"application/json;charset=UTF-8","content-length":"30","date":"Wed, 28 Aug 2019 07:21:34 GMT","connection":"close"},"request":{"uri":{"protocol":"https:","slashes":true,"auth":null,"host":"192.168.178.157:8443","port":"8443","hostname":"192.168.178.157","hash":null,"search":null,"query":null,"pathname":"/api/login","path":"/api/login","href":"https://192.168.178.157:8443/api/login"},"method":"POST","headers":{"Content-Type":"application/json","content-length":40}}} 09:21:34.536 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: Login war erfolgreich! 09:21:34.536 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: set_cookies: 09:21:34.536 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: set_cookies: 09:21:34.537 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: Cookies: ["unifises=rUTs0tMWpm4QcxFH1cvV5VuWIYCV4UhY","csrf_token=lE7GdlIBf0bhQS4qJcaowM1F4iGmc2lk"] 09:21:34.537 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: resolved 09:21:34.537 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: now setting wifi 09:21:34.537 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: got response 09:21:34.537 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: rejetced 09:21:34.537 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: resp: {} 09:21:34.549 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: rejected: StatusCodeError: 400 - "{\"meta\":{\"rc\":\"error\",\"msg\":\"api.err.IdInvalid\"},\"data\":[]}" 09:21:34.549 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: reject1: StatusCodeError: 400 - "{\"meta\":{\"rc\":\"error\",\"msg\":\"api.err.IdInvalid\"},\"data\":[]}" 09:21:34.549 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: getting status
-
@liv-in-sky sagte in Adapter: ioBroker.unifi:
api.err.IdInvalid
Da scheint die WLAN ID nicht zu stimmen. Hast du die richtig eingegeben?
-
@thewhobox oje - überprüfe ich gleich
-
@liv-in-sky Könnte aber auch ein Fehler im setWifif gewesen sein.
Zum testen kurz ersetzen.
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: "default", enabled }), headers: { 'Content-Type': 'application/json', Cookie: cookies.join("; ") } }).catch((e) => { console.log("setWifi: rejected: " + e); 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") console.log("resp: " + JSON.stringify(resp)); reject("msg: " + JSON.parse(resp.body).meta.msg); } }); }
-
@thewhobox also mit dem linux script ist die id richtig und wird ein und ausgeschalten -- id im linuxscript ist die selbe wie in deinem
-
09:33:56.981 info javascript.2 Stop script script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 09:33:57.965 info javascript.2 Start javascript script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 09:33:57.965 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: starting test 09:33:57.965 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: start set wifi 09:33:57.965 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: need to login 09:33:57.965 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: registered 0 subscriptions and 0 schedules 09:33:58.196 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: got response: {"statusCode":200,"body":"{\"meta\":{\"rc\":\"ok\"},\"data\":[]}","headers":{"vary":"Origin","access-control-allow-credentials":"true","access-control-expose-headers":"Access-Control-Allow-Origin,Access-Control-Allow-Credentials","set-cookie":["unifises=yrsA7DInl1z2N6wr4IpLjwaR4fuKq1yc; Path=/; Secure; HttpOnly","csrf_token=X1FfCKVPvRUrXuhNffnO4sf9IbtNq5PH; Path=/; Secure"],"x-frame-options":"DENY","content-type":"application/json;charset=UTF-8","content-length":"30","date":"Wed, 28 Aug 2019 07:33:58 GMT","connection":"close"},"request":{"uri":{"protocol":"https:","slashes":true,"auth":null,"host":"192.168.178.157:8443","port":"8443","hostname":"192.168.178.157","hash":null,"search":null,"query":null,"pathname":"/api/login","path":"/api/login","href":"https://192.168.178.157:8443/api/login"},"method":"POST","headers":{"Content-Type":"application/json","content-length":40}}} 09:33:58.196 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: Login war erfolgreich! 09:33:58.197 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: set_cookies: 09:33:58.197 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: set_cookies: 09:33:58.197 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: Cookies: ["unifises=yrsA7DInl1z2N6wr4IpLjwaR4fuKq1yc","csrf_token=X1FfCKVPvRUrXuhNffnO4sf9IbtNq5PH"] 09:33:58.197 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: resolved 09:33:58.197 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: now setting wifi 09:33:58.197 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: got response 09:33:58.197 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: rejetced 09:33:58.197 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: resp: {} 09:33:58.210 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: rejected: StatusCodeError: 400 - "{\"meta\":{\"rc\":\"error\",\"msg\":\"api.err.IdInvalid\"},\"data\":[]}" 09:33:58.210 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: reject1: StatusCodeError: 400 - "{\"meta\":{\"rc\":\"error\",\"msg\":\"api.err.IdInvalid\"},\"data\":[]}" 09:33:58.210 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: getting status
-
@liv-in-sky Okay. Dann ersetze mal bitte die Zeile in setWifi:
body: JSON.stringify({ enabled }),
-
hier der test mit linux
unifi enable {"meta":{"rc":"ok"},"data":[]}Enabling WiFi. {"meta":{"rc":"ok"},"data":[{"_id":"5d6512a897578401d1edd761","enabled":true,"security":"open","wep_idx":1,"wpa_mode":"wpa2","wpa_enc":"ccmp","usergroup_id":"5cadc7f73b6a3967dd7e5376","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":"DragonRootGuest","wlangroup_id":"5cadc7f73b6a3967dd7e5377","fast_roaming_enabled":false,"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":"5cadc7f53b6a3967dd7e5369","x_iapp_key":"cf930ad9a6750bd1d7c2f8676f775668","name_combine_enabled":true}]}root@iobroker59 09:28:00 /usr/local/bin > unifi disable {"meta":{"rc":"ok"},"data":[]}Disabling WiFi. {"meta":{"rc":"ok"},"data":[{"_id":"5d6512a897578401d1edd761","enabled":false,"security":"open","wep_idx":1,"wpa_mode":"wpa2","wpa_enc":"ccmp","usergroup_id":"5cadc7f73b6a3967dd7e5376","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":"DragonRootGuest","wlangroup_id":"5cadc7f73b6a3967dd7e5377","fast_roaming_enabled":false,"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":"5cadc7f53b6a3967dd7e5369","x_iapp_key":"cf930ad9a6750bd1d7c2f8676f775668","name_combine_enabled":true}]}root@iobroker59 09:29:23 /usr/local/bin >
-
@thewhobox sagte in Adapter: ioBroker.unifi:
body: JSON.stringify({ enabled }),
jetzt weiß ich wieder, warum ich mit json auf kriegsfuß stehe
09:36:40.055 info js2fs.0 Script script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 modified in ioBroker, write to file 09:36:40.065 info javascript.1 Stop script script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 09:36:40.065 info javascript.0 Stop script script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 09:36:40.069 info javascript.2 Stop script script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 09:36:40.080 info javascript.2 Start javascript script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 09:36:40.081 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: starting test 09:36:40.081 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: start set wifi 09:36:40.081 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: need to login 09:36:40.081 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: registered 0 subscriptions and 0 schedules 09:36:40.311 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: got response: {"statusCode":200,"body":"{\"meta\":{\"rc\":\"ok\"},\"data\":[]}","headers":{"vary":"Origin","access-control-allow-credentials":"true","access-control-expose-headers":"Access-Control-Allow-Origin,Access-Control-Allow-Credentials","set-cookie":["unifises=4lIovxEqmKXvAF8JehI733XXQ6fSh1Se; Path=/; Secure; HttpOnly","csrf_token=4D3RfUt1GtDD4k6JXdNUcunJw5O0jqvu; Path=/; Secure"],"x-frame-options":"DENY","content-type":"application/json;charset=UTF-8","content-length":"30","date":"Wed, 28 Aug 2019 07:36:40 GMT","connection":"close"},"request":{"uri":{"protocol":"https:","slashes":true,"auth":null,"host":"192.168.178.157:8443","port":"8443","hostname":"192.168.178.157","hash":null,"search":null,"query":null,"pathname":"/api/login","path":"/api/login","href":"https://192.168.178.157:8443/api/login"},"method":"POST","headers":{"Content-Type":"application/json","content-length":40}}} 09:36:40.312 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: Login war erfolgreich! 09:36:40.312 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: set_cookies: 09:36:40.312 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: set_cookies: 09:36:40.312 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: Cookies: ["unifises=4lIovxEqmKXvAF8JehI733XXQ6fSh1Se","csrf_token=4D3RfUt1GtDD4k6JXdNUcunJw5O0jqvu"] 09:36:40.312 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: resolved 09:36:40.312 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: now setting wifi 09:36:40.312 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: got response 09:36:40.312 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: rejetced 09:36:40.312 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: resp: {} 09:36:40.322 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: rejected: StatusCodeError: 400 - "{\"meta\":{\"rc\":\"error\",\"msg\":\"api.err.IdInvalid\"},\"data\":[]}" 09:36:40.322 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: reject1: StatusCodeError: 400 - "{\"meta\":{\"rc\":\"error\",\"msg\":\"api.err.IdInvalid\"},\"data\":[]}" 09:36:40.322 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: getting status 09:36:42.072 info js2fs.0 Script file script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 changed, also update in ioBroker 09:36:42.095 info javascript.2 Stop script script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 09:36:42.117 info javascript.2 Start javascript script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 09:36:42.117 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: starting test 09:36:42.118 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: start set wifi 09:36:42.118 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: need to login 09:36:42.118 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: registered 0 subscriptions and 0 schedules 09:36:42.421 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: got response: {"statusCode":200,"body":"{\"meta\":{\"rc\":\"ok\"},\"data\":[]}","headers":{"vary":"Origin","access-control-allow-credentials":"true","access-control-expose-headers":"Access-Control-Allow-Origin,Access-Control-Allow-Credentials","set-cookie":["unifises=EnoMzZ5FyglDTNPrZYGa2hvP16jewWiU; Path=/; Secure; HttpOnly","csrf_token=fAjfJm99u8hTqyKjEBfv7IRA4lZMILYO; Path=/; Secure"],"x-frame-options":"DENY","content-type":"application/json;charset=UTF-8","content-length":"30","date":"Wed, 28 Aug 2019 07:36:42 GMT","connection":"close"},"request":{"uri":{"protocol":"https:","slashes":true,"auth":null,"host":"192.168.178.157:8443","port":"8443","hostname":"192.168.178.157","hash":null,"search":null,"query":null,"pathname":"/api/login","path":"/api/login","href":"https://192.168.178.157:8443/api/login"},"method":"POST","headers":{"Content-Type":"application/json","content-length":40}}} 09:36:42.421 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: Login war erfolgreich! 09:36:42.421 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: set_cookies: 09:36:42.421 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: set_cookies: 09:36:42.421 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: Cookies: ["unifises=EnoMzZ5FyglDTNPrZYGa2hvP16jewWiU","csrf_token=fAjfJm99u8hTqyKjEBfv7IRA4lZMILYO"] 09:36:42.421 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: resolved 09:36:42.421 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: now setting wifi 09:36:42.421 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: got response 09:36:42.421 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: rejetced 09:36:42.421 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: resp: {} 09:36:42.429 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: rejected: StatusCodeError: 400 - "{\"meta\":{\"rc\":\"error\",\"msg\":\"api.err.IdInvalid\"},\"data\":[]}" 09:36:42.429 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: reject1: StatusCodeError: 400 - "{\"meta\":{\"rc\":\"error\",\"msg\":\"api.err.IdInvalid\"},\"data\":[]}" 09:36:42.429 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: getting status
-
@liv-in-sky Okay, zu dem Fehler finde ich leider auch nix im Netz. Hab es jetzt mal so angepasst wie in deinem verlinkten Skript.
Was mich wundert, dass nach "getting status" nix mehr kommt...
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({ resolveWithFullResponse: true, 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) }); if(resp != null) { console.log("login: Login war erfolgreich! " + ((resp.headers && resp.headers.hasOwnProperty("set-cookie")) ? "Mit Cookies":"Ohne Cookies")); if(resp.headers && resp.headers.hasOwnProperty("set-cookie")) { 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); } } else { console.log("login: no cookies to set!") } loggedIn = true; resolve(); } else { console.log("login: rejected") reject("resp = null"); } }); } async function getStatus() { return new Promise(async (resolve, reject) => { console.log("nur mal so"); 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) => { console.log("getStatus reject " + e); reject(e) }); console.log("got response " + JSON.stringify(resp)); 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) => { 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/upd/wlanconf/" + wifi_id, body: JSON.stringify({ enabled }), headers: { 'Content-Type': 'application/json', Cookie: cookies.join("; ") } }).catch((e) => { console.log("setWifi: rejected: " + e); 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") console.log("resp: " + JSON.stringify(resp)); reject("msg: " + JSON.parse(resp.body).meta.msg); } }); } async function test() { console.log("starting test"); await setWifi(true).catch((e) => console.log("reject1: " + e)); console.log("getting status"); let wlan = await getStatus().catch((e) => console.log("reject2: " + e)); console.log("fin") console.log(wlan); } test()
-
@thewhobox moment noch - jetzt ist was passiert
-
@thewhobox
jeah -
scheint zu klappen - kann es ein und ausschalten09:55:42.324 info js2fs.0 Script script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 modified in ioBroker, write to file 09:55:42.331 info javascript.2 Stop script script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 09:55:42.337 info javascript.0 Stop script script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 09:55:42.341 info javascript.1 Stop script script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 09:55:42.352 info javascript.2 Start javascript script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 09:55:42.353 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: starting test 09:55:42.353 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: start set wifi 09:55:42.353 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: need to login 09:55:42.353 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: registered 0 subscriptions and 0 schedules 09:55:42.672 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: Login war erfolgreich! Mit Cookies 09:55:42.673 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: now setting wifi 09:55:42.673 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: got response 09:55:42.673 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: rejetced 09:55:42.673 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: resp: {} 09:55:44.342 info js2fs.0 Script file script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 changed, also update in ioBroker 09:55:44.360 info javascript.2 Stop script script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 09:55:44.408 info javascript.2 Start javascript script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 09:55:44.408 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: starting test 09:55:44.408 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: start set wifi 09:55:44.408 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: need to login 09:55:44.408 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: registered 0 subscriptions and 0 schedules 09:55:44.730 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: Login war erfolgreich! Mit Cookies 09:55:44.730 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: now setting wifi 09:55:44.730 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: got response 09:55:44.730 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: rejetced 09:55:44.730 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: resp: {}