| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- var host = window.location.href
- console.log("当前页面地址:" + host)
- function show_heat_img(oHeatsw, status){
- console.log('status:', status)
- // 必须要 == 1,否则会导致无法关闭
- // 可能是因为status是字符串,所以 if(status) 这种方式必定为真
- if (status == 1) {
- oHeatsw.src = "img/switch_on.png";
- } else {
- oHeatsw.src = "img/switch_off.png";
- }
- }
- function show_temp(oTemp, value, name){
- oTemp.innerHTML = name + "温度:" + value.toFixed(2); + '℃';
- }
- function show_time(oRuntime, run_time){
- console.log("time:", run_time)
- oRuntime.innerHTML = '运行时间:' + PrefixInteger(parseInt(run_time/3600), 2) + ':' + PrefixInteger(parseInt(run_time/60) % 60, 2) + ':' + PrefixInteger(parseInt(run_time % 60),2)
- }
- function toggle_heat_switch(oHeatsw){
- var request = new XMLHttpRequest();
- if (oHeatsw.src.indexOf('switch_off') != -1) {
- // false 是不采用异步方式,也就是说必须等到 request 返回,程序才执行下一步
- request.open('GET', 'heat/on', false);
- } else if (oHeatsw.src.indexOf('switch_on')){
- request.open('GET', 'heat/off', false);
- }
- var heat_response
- request.onreadystatechange = function(){
- if (request.readyState == 4 && request.status == 200){
- heat_response = parseInt(request.responseText);
- if (heat_response in [0,1]){
- show_heat_img(oHeatsw, heat_response);
- }
- }
- }
- request.send(null)
- return heat_response
- }
- function get_status(url_staus){
- var res_status = new Object;
- var request = new XMLHttpRequest();
- console.log("url_staus :" + url_staus)
- request.open('GET', url_staus, false);
- // 等待获取成功 onreadystatechange
- request.onreadystatechange = function(){
- if (request.readyState == 4 && request.status == 200){
- res_status = JSON.parse(request.responseText)
- console.log('res_status:', res_status)
- }
- }
- request.send(null)
- return res_status
- }
- var temp_cnt = 5
- function reflash_dat(owireTemp, ocenterTemp, oPower, board_status){
- temp_cnt += 1
- if (temp_cnt > 3)
- {
- temp_cnt = 0
- if (board_status == null){
- board_status = get_status(host + 'status')
- }
- ntc = board_status['ntc']
- show_temp(owireTemp, ntc['wire_temp'], '铁丝')
- show_temp(ocenterTemp, ntc['center_temp'], '中心')
- oPower.innerHTML = '功率:' + board_status['heat']['pwr_percent'].toFixed(2) * 100 + '%'
- }
- }
- document.write("javascript")
- function my_function() {
- oHeatsw = document.getElementById("heatsw");
- owireTemp = document.getElementById("r_temp");
- ocenterTemp = document.getElementById("c_temp");
- oRunTime = document.getElementById("run_time");
- oPower = document.getElementById("pwr");
-
- var board_status
- function show_dat(){
- board_status = get_status(host + 'status')
- show_heat_img(oHeatsw, board_status['heat']['status']);
- reflash_dat(owireTemp, ocenterTemp, oPower, board_status)
- show_time(oRunTime, board_status['time']['run_time'], 0)
- }
- show_dat()
- var run_time = board_status['time']['run_time']
- var stop_reflash = 0
- function reflash(){
- // 不论开启或关闭加热,温度都会一直获取
- reflash_dat(owireTemp, ocenterTemp, oPower, null)
- if (stop_reflash == 0){
- show_time(oRunTime, run_time, 1)
- run_time += 1
- }
- }
- oHeatsw.onclick = function() {
- res = toggle_heat_switch(oHeatsw)
- if (res == 0){
- show_time(oRunTime, 0, 0)
- stop_reflash = 1
- console.log('stop:interval')
- }
- else if(res ==1){
- stop_reflash = 0
- cnt = 6
- run_time = 0
- }
-
- };
- setInterval(reflash,1000)
- };
- window.onload = my_function
- // num传入的数字,n需要的字符长度
- function PrefixInteger(num, n) {
- return (Array(n).join(0) + num).slice(-n);
- }
|