heat.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. var host = window.location.href
  2. console.log("当前页面地址:" + host)
  3. function show_heat_img(oHeatsw, status){
  4. console.log('status:', status)
  5. // 必须要 == 1,否则会导致无法关闭
  6. // 可能是因为status是字符串,所以 if(status) 这种方式必定为真
  7. if (status == 1) {
  8. oHeatsw.src = "img/switch_on.png";
  9. } else {
  10. oHeatsw.src = "img/switch_off.png";
  11. }
  12. }
  13. function show_temp(oTemp, value, name){
  14. oTemp.innerHTML = name + "温度:" + value.toFixed(2); + '℃';
  15. }
  16. function show_time(oRuntime, run_time){
  17. console.log("time:", run_time)
  18. oRuntime.innerHTML = '运行时间:' + PrefixInteger(parseInt(run_time/3600), 2) + ':' + PrefixInteger(parseInt(run_time/60) % 60, 2) + ':' + PrefixInteger(parseInt(run_time % 60),2)
  19. }
  20. function toggle_heat_switch(oHeatsw){
  21. var request = new XMLHttpRequest();
  22. if (oHeatsw.src.indexOf('switch_off') != -1) {
  23. // false 是不采用异步方式,也就是说必须等到 request 返回,程序才执行下一步
  24. request.open('GET', 'heat/on', false);
  25. } else if (oHeatsw.src.indexOf('switch_on')){
  26. request.open('GET', 'heat/off', false);
  27. }
  28. var heat_response
  29. request.onreadystatechange = function(){
  30. if (request.readyState == 4 && request.status == 200){
  31. heat_response = parseInt(request.responseText);
  32. if (heat_response in [0,1]){
  33. show_heat_img(oHeatsw, heat_response);
  34. }
  35. }
  36. }
  37. request.send(null)
  38. return heat_response
  39. }
  40. function get_status(url_staus){
  41. var res_status = new Object;
  42. var request = new XMLHttpRequest();
  43. console.log("url_staus :" + url_staus)
  44. request.open('GET', url_staus, false);
  45. // 等待获取成功 onreadystatechange
  46. request.onreadystatechange = function(){
  47. if (request.readyState == 4 && request.status == 200){
  48. res_status = JSON.parse(request.responseText)
  49. console.log('res_status:', res_status)
  50. }
  51. }
  52. request.send(null)
  53. return res_status
  54. }
  55. var temp_cnt = 5
  56. function reflash_dat(owireTemp, ocenterTemp, oPower, board_status){
  57. temp_cnt += 1
  58. if (temp_cnt > 3)
  59. {
  60. temp_cnt = 0
  61. if (board_status == null){
  62. board_status = get_status(host + 'status')
  63. }
  64. ntc = board_status['ntc']
  65. show_temp(owireTemp, ntc['wire_temp'], '铁丝')
  66. show_temp(ocenterTemp, ntc['center_temp'], '中心')
  67. oPower.innerHTML = '功率:' + board_status['heat']['pwr_percent'].toFixed(2) * 100 + '%'
  68. }
  69. }
  70. document.write("javascript")
  71. function my_function() {
  72. oHeatsw = document.getElementById("heatsw");
  73. owireTemp = document.getElementById("r_temp");
  74. ocenterTemp = document.getElementById("c_temp");
  75. oRunTime = document.getElementById("run_time");
  76. oPower = document.getElementById("pwr");
  77. var board_status
  78. function show_dat(){
  79. board_status = get_status(host + 'status')
  80. show_heat_img(oHeatsw, board_status['heat']['status']);
  81. reflash_dat(owireTemp, ocenterTemp, oPower, board_status)
  82. show_time(oRunTime, board_status['time']['run_time'], 0)
  83. }
  84. show_dat()
  85. var run_time = board_status['time']['run_time']
  86. var stop_reflash = 0
  87. function reflash(){
  88. // 不论开启或关闭加热,温度都会一直获取
  89. reflash_dat(owireTemp, ocenterTemp, oPower, null)
  90. if (stop_reflash == 0){
  91. show_time(oRunTime, run_time, 1)
  92. run_time += 1
  93. }
  94. }
  95. oHeatsw.onclick = function() {
  96. res = toggle_heat_switch(oHeatsw)
  97. if (res == 0){
  98. show_time(oRunTime, 0, 0)
  99. stop_reflash = 1
  100. console.log('stop:interval')
  101. }
  102. else if(res ==1){
  103. stop_reflash = 0
  104. cnt = 6
  105. run_time = 0
  106. }
  107. };
  108. setInterval(reflash,1000)
  109. };
  110. window.onload = my_function
  111. // num传入的数字,n需要的字符长度
  112. function PrefixInteger(num, n) {
  113. return (Array(n).join(0) + num).slice(-n);
  114. }