sky1909
大概是这样,填上你家里的ssid,用于切换DNS的模块名,以及http api参数。然后创建一个event 脚本。
test = type=event,event-name=network-changed,script-path=test.js
const ssid = "home_ssid";
const dns_module = "DNS";
const http_api = "localhost:6166";
const http_api_key = "mieq";
let request = {
url: `http://${http_api}/v1/modules`,
headers: {"X-Key": http_api_key},
body: {},
}
getModuleStatus(dns_module).then(main)
function main(enable) {
let home = ($network.wifi.ssid == ssid);
if (home && !enable) {
//家里,未开启模块 => 开启
$notification.post("开启dns模块","","")
enableModule(true);
} else if (!home && enable){
//不是家里,开启了模块 => 关闭
$notification.post("关闭dns模块","","")
enableModule(false);
} else {
//重复触发 => 结束
//$notification.post("重复触发","","")
$done();
}
}
function getModuleStatus(dns_module) {
return new Promise(resolve => {
$httpClient.get(request, (error, response, data) => {
let enabled = JSON.parse(data).enabled;
resolve(enabled.includes(dns_module));
});
});
}
function enableModule(enable) {
request.body[dns_module] = enable;
$httpClient.post(request, () => $done());
}