20 Mar 2020, 11:09
hier sehr "quick and dirty" zusammenkopiert - bei mir geht es
macht unter javascript.x datenpunkte, die geschalten werden können - ansonsten musst du oben im script deine daten eingeben
const unifi_username = "username";
const unifi_password = "password";
const unifi_controller = "https://192.168.178.157:8443";
let siteName ="default"; //Site name
let cookies = [];
const dpPrefix = "javascript."+ instance +".";
const request = require('request-promise-native').defaults({ rejectUnauthorized: false });
let loggedIn = false;
let wifiDPs = [];
let testerrws;
let debug = false;
// DEFINITION der zu anzeigenden Netzwerke am besten bis auf id und smart alle gleich setzen
const wifis = {
"WLAN_DragonRoot1": { name: "WLAN_DragonRoot1", id: "5caxxxxxxxd7e5381", desc: "WLAN_DragonRoot1" } ,
"WLAN_DragonRootGuest": { name: "WLAN_DragonRootGuest", id: "5d65xxxxxedd761", desc: "WLAN_DragonRootGuest" }
}
for(let wifi_name in wifis) {
wifiDPs.push(dpPrefix + "WLANSchalten." + wifis[wifi_name].name);
createState(dpPrefix + "WLANSchalten."+ wifi_name, { name: wifis[wifi_name].desc, role: 'switch', read: true, write: true, type: "boolean" });}
async function setWifi(enabled, wifi) {
return new Promise(async (resolve, reject) => {
//log("setWifi: start set Wifi_haupt");
if (!loggedIn) { log("need to login"); await login().catch((e) => reject(e)); }
// dlog("setWifi: now setting Wifi_haupt");
let resp = request.post({
url: unifi_controller + "/api/s/"+siteName+"/upd/wlanconf/" + wifi.id,
body: JSON.stringify({ enabled }),
headers: { 'Content-Type': 'application/json', Cookie: cookies.join("; ") }
}).catch((e) => { log("setWifi: rejected: " + e); /*reject(e)*/ return testerrws=true; });
// dlog("setWifi: got response")
if (resp != null) {
// dlog("setWifi: Wifi wurde erfolgreich " + (enabled ? "eingeschaltet" : "ausgeschaltet"));
// dlog(resp);
// getclient() Pause für umschalten
setState(dpPrefix + "WLANSchalten." + wifi.name, enabled, enabled);
resolve();
} else {
// dlog("setWifi: rejetced")
// dlog("resp: " + JSON.stringify(resp));
reject("msg: " + JSON.parse(resp.body).meta.msg);}
});
}
async function login() {
return new Promise(async (resolve, reject) => {
cookies=[];
let respl = 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); loggedIn =false;return respl=[];});
if(respl != null) {
log("login: login war erfolgreich! " + ((respl.headers && respl.headers.hasOwnProperty("set-cookie")) ? "Mit Cookies":"Ohne Cookies"));
if(respl.headers && respl.headers.hasOwnProperty("set-cookie")) {
let set_cookies = respl.headers["set-cookie"];
for(i = 0; i < set_cookies.length; i++) {
let cookie = set_cookies[i];
//log(set_cookies[i]);
cookie = cookie.split(";")[0];
cookies.push(cookie);
}
} else {
log("login: no cookies to set!")
}
loggedIn = true;
resolve();
} else {
log("login: rejected")
loggedIn = false;
reject("respl = null");
}
});
}
on({id: wifiDPs, ack: false, change: "ne"}, function (obj) {
var value = obj.state.val;
var dp2 = obj.name
setWifi(value, wifis[dp2]);
// dlog(wifis[dp2])
//if (firstTime<wifiLength+1) firstTime++;
});
function getStatus(wifi) {
// mylog("BIN IN STATUS");
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/"+siteName+"/rest/wlanconf/" + wifi.id,
headers: { Cookie: cookies.join("; ") }
}).catch((e) => { dlog("getStatus reject " + e); /*reject(e)*/ return testerrws=true; });
if (!testerrws) {
// dlog("got response " + JSON.stringify(resp));
resp = JSON.parse(resp);
let wlanOn = resp.data[0].enabled;
// dlog("WLAN " + wifi.name + " ist: " + (wlanOn ? "an" : "aus"));
if (resp != null && resp.meta && resp.meta.rc == "ok") {
dlog("Status erfolgreich geholt!");
dlog(resp);
let wlanOn = resp.data[0].enabled;
dlog("WLAN ist: " + (wlanOn ? "an" : "aus"));
setStateDelayed(dpPrefix + "WLANSchalten." + wifi.name, wlanOn, 200);
resolve(wlanOn);
} else {
log("nicht ok... getStatusWifi")
reject(JSON.stringify(resp));
}
} else {dlog("reject weil respslogin ist 00"); dlog("BIN raus aus LOGOUT"); reject();}
// mylog("BIN aus STATUS raus");
});
}
function dlog(message) {
if(debug)
console.log(message);
}
for(let wifi_name in wifis) {
// if ( firstTime == 3 && getState(dpPrefix + "WLANUnifi."+wifi_name).val !== null ) firstTime = 3;
getStatus(wifis[wifi_name])}