- Home
- Deutsch
- ioBroker Allgemein
- Adapter: ioBroker.unifi
Adapter: ioBroker.unifi
-
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: {}
-
@liv-in-sky
@thewhobox
moin, schon alle wieder fleißig...
Welches ist jetzt das komplette Script ? -
@dslraser 3 posts weiter oben
-
So hier nochmal mit Console Ausgaben nur wenn debug true ist.
@dslraser bringt er bei dir auch was nach "getting status"?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; let debug = false; function log(message) { if(debug) console.log(message); } 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) => { log("login: reject"), reject(e) }); if(resp != null) { 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 { log("login: no cookies to set!") } loggedIn = true; resolve(); } else { log("login: rejected") reject("resp = null"); } }); } async function getStatus() { return new Promise(async (resolve, reject) => { 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) => { log("getStatus reject " + e); reject(e) }); log("got response " + JSON.stringify(resp)); if(resp != null && resp.statusCode == 200) { log("Status erfolgreich geholt!"); log(resp); let status = JSON.parse(resp); let wlanOn = status.data[0].enabled; 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) { log("Du bist nun ausgeloggt."); log(resp); resolve(); } else { reject("resp = null"); } }); } async function setWifi(enabled) { return new Promise(async (resolve, reject) => { log("setWifi: start set wifi"); if(!loggedIn) { log("need to login"); await login().catch((e) => reject(e)); } 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) => { log("setWifi: rejected: " + e); reject(e) }); log("setWifi: got response") if(resp != null) { log("setWifi: Wifi wurde erfolgreich " + (enabled ? "eingeschaltet" : "ausgeschaltet")); log(resp); resolve(); } else { log("setWifi: rejetced") log("resp: " + JSON.stringify(resp)); reject("msg: " + JSON.parse(resp.body).meta.msg); } }); } async function test() { log("starting test"); await setWifi(true).catch((e) => log("reject1: " + e)); log("getting status"); let wlan = await getStatus().catch((e) => log("reject2: " + e)); console.log("fin") console.log(wlan); } test()
-
was aber nervt - nicht script - es werden immer alle netzwerke aus und eingeschaltet - ist beim linux script auch - das scheint etwas mit unifi selbst zu tun zu haben
-
ich bin irgendwo nicht mit gekommen...?
das passiert beim letzten Script
javascript.0 2019-08-28 10:07:00.672 error at ContextifyScript.Script.runInContext (vm.js:59:29) javascript.0 2019-08-28 10:07:00.672 error at script.js.Heiko.Unifi.unifi_wifi:1:1 javascript.0 2019-08-28 10:07:00.672 error SyntaxError: Identifier 'log' has already been declared javascript.0 2019-08-28 10:07:00.672 error ^ javascript.0 2019-08-28 10:07:00.672 error const request = require('request-promise-native').defaults({ rejectUnauthorized: false }); javascript.0 2019-08-28 10:07:00.672 error script.js.Heiko.Unifi.unifi_wifi: script.js.Heiko.Unifi.unifi_wifi:1
-
muss leider nochmal fragen - wenn ich nur getStatus() anstatt test() aufrufe, sollte das funktionieren - es kommt kein WLAN ist an oder aus
10:05:22.860 info js2fs.0 Script script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 modified in ioBroker, write to file 10:05:22.864 info javascript.1 Stop script script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 10:05:22.865 info javascript.0 Stop script script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 10:05:22.874 info javascript.2 Stop script script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 10:05:22.886 info javascript.2 Start javascript script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 10:05:22.886 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: starting test 10:05:22.886 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: start set wifi 10:05:22.886 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: need to login 10:05:22.886 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: registered 0 subscriptions and 0 schedules 10:05:23.174 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: Login war erfolgreich! Mit Cookies 10:05:23.174 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: now setting wifi 10:05:23.174 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: got response 10:05:23.174 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: Wifi wurde erfolgreich eingeschaltet 10:05:23.175 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: {} 10:05:23.175 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: getting status 10:05:23.175 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: nur mal so 10:05:23.183 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: got response "{\"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}]}" 10:05:24.876 info js2fs.0 Script file script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 changed, also update in ioBroker 10:05:24.895 info javascript.2 Stop script script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 10:05:24.919 info javascript.2 Start javascript script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2 10:05:24.919 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: starting test 10:05:24.919 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: start set wifi 10:05:24.919 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: need to login 10:05:24.919 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: registered 0 subscriptions and 0 schedules 10:05:25.205 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: login: Login war erfolgreich! Mit Cookies 10:05:25.205 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: now setting wifi 10:05:25.205 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: got response 10:05:25.205 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: setWifi: Wifi wurde erfolgreich eingeschaltet 10:05:25.206 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: {} 10:05:25.206 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: getting status 10:05:25.206 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: nur mal so 10:05:25.214 info javascript.2 script.js.Armin_Schalter.ALLERLEI.UnifiWLAN2: got response "{\"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}]}"
-
@dslraser Nein war mein Fehler gerade, jetzt nochmal:
@liv-in-sky laut Log hast du aber test aufgerufen? Hätte aber trotzdem nicht funktioniert, sollte jetzt aber gefixt sein.
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; let debug = true; function dlog(message) { if(debug) console.log(message); } 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) => { dlog("login: reject"), reject(e) }); if(resp != null) { dlog("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 { dlog("login: no cookies to set!") } loggedIn = true; resolve(); } else { dlog("login: rejected") reject("resp = null"); } }); } async function getStatus() { return new Promise(async (resolve, reject) => { dlog("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) => { dlog("getStatus reject " + e); reject(e) }); dlog("got response " + JSON.stringify(resp)); if(resp != null && resp.meta.rc == "ok") { dlog("Status erfolgreich geholt!"); dlog(resp); let wlanOn = resp.data[0].enabled; dlog("WLAN ist: " + (wlanOn ? "an" : "aus")); resolve(wlanOn); } else { reject(JSON.stringify(resp)); } let wlanOn = resp.data[0].enabled; dlog("WLAN ist: " + (wlanOn ? "an" : "aus")); }); } async function dlogout() { return new Promise(async (resolve, reject) => { let resp = await request.get({ url: unifi_controller + "/dlogout", headers: { Cookie: cookies.join("; ") } }).catch((e) => reject(e)); if(resp != null) { dlog("Du bist nun ausgedloggt."); dlog(resp); resolve(); } else { reject("resp = null"); } }); } async function setWifi(enabled) { return new Promise(async (resolve, reject) => { dlog("setWifi: start set wifi"); if(!loggedIn) { dlog("need to login"); await login().catch((e) => reject(e)); } dlog("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) => { dlog("setWifi: rejected: " + e); reject(e) }); dlog("setWifi: got response") if(resp != null) { dlog("setWifi: Wifi wurde erfolgreich " + (enabled ? "eingeschaltet" : "ausgeschaltet")); dlog(resp); resolve(); } else { dlog("setWifi: rejetced") dlog("resp: " + JSON.stringify(resp)); reject("msg: " + JSON.parse(resp.body).meta.msg); } }); } async function test() { dlog("starting test"); await setWifi(true).catch((e) => dlog("reject1: " + e)); dlog("getting status"); let wlan = await getStatus().catch((e) => dlog("reject2: " + e)); console.log("fin") console.log(wlan); } test()