//自动化操作
import win.ui;
/*DSG{{*/
var winform = win.form(text="使用webform调用网页上的对象、函数";right=848;bottom=585)
winform.add(
edit={cls="edit";text="f0:8f:49:8f:f1:3f";left=671;top=496;right=819;bottom=526;edge=1;z=2};
edit2={cls="edit";text="mosquitto_pub -h 192.168.1.100 -p 1883 -t cmnd/tasmota_95EA2A/POWER -m on";left=125;top=539;right=825;bottom=569;edge=1;z=3};
static={cls="static";text="指定mac存在则触发mqtt";left=493;top=502;right=664;bottom=527;transparent=1;z=1};
static2={cls="static";text="mqtt指令";left=50;top=544;right=107;bottom=565;transparent=1;z=4}
)
/*}}*/

//创建web窗体
import web.form;
import process;
var wb = web.form( winform );
wb.noScriptErr = true;
var randomi=0
//打开光猫管理网页
wb.go("http://192.168.1.1/cgi-bin/luci#login_form") 

//显示窗口
winform.show();
//登录光猫
wb.waitEle("username")
ele = wb.getEle( "login_username" )
ele2= wb.getEle( "login_password" )

ele.value="useradmin";
ele2.value="ypwxu";
var ele3 = wb.queryEles( 
innerHTML="确认登录";
tagName="BUTTON";
className="btn"
 );
 
ele3.click()
//////////////////////点击设备管理
wb.waitEle("first_menu_device")
ele = wb.getEle("first_menu_device")  
ele.click()
//点击无线终端设备
ele=wb.waitEle("sub_second_menu_device_wifi")  
ele.click()


//无限循环检测指定mac是否存在，mac存在则发出mqtt指令，让home assistant开灯。
do{
//每隔3秒检测网页文本，是否存在指定mac
//mac存在（主人回家）则开灯，mac不存在（主人离家）则关灯
for(i=1;3;1){

url= "http://192.168.1.1/cgi-bin/luci/admin/device/wifi?"++randomi;
randomi=randomi+1 
//wb.go(url)
win.delay(3000)
wb.waitEle("device_iframe")
ele = wb.queryEles( 
innerHTML='@\n\t    <div id="wifi_lists">\n\t\t\t<form id="wired_for';
parent="device_iframe";
tagName="BODY";
className="main_config_body"
 );

//win.msgbox(ele.outerText)
htmltxt=ele.outerText
if(string.find(htmltxt,"@"++winform.edit.text)){
	//win.msgbox("y")
	cmds=winform.edit2.text
	var prcs,err = process(cmds); 
prcs.wait() 
//assert(prcs,err);
}else {
	cmds=`mosquitto_pub -h 192.168.1.100 -p 1883 -t cmnd/tasmota_95EA2A/POWER -m off`
	var prcs,err = process(cmds); 
prcs.wait() 
}

	

}
//防超时无操作被光猫踢，每隔9秒点击一次网页上的“无线终端设备”
win.delay(300)
ele=wb.waitEle("sub_second_menu_device_wifi")  
ele.click()
win.delay(500)

}while(true)



win.loopMessage();