power.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. include_once("../../../auth.php");
  3. //This php can be called with ajax and ask for waking up wlan0 and wlan1. However, one of them must be on at a time.
  4. function mv($value){
  5. if (isset($_GET[$value]) && $_GET[$value] != ""){
  6. return $_GET[$value];
  7. }else{
  8. return "";
  9. }
  10. }
  11. function checkLinuxWlanUpDown($wlanID){
  12. $result = trim(shell_exec("cat /sys/class/net/wlan" . $wlanID . "/operstate"));
  13. if (trim($result) == ""){
  14. return "not_found";
  15. }else{
  16. return $result;
  17. }
  18. }
  19. if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
  20. //Running on Linux host
  21. if (mv("enable") != ""){
  22. $wlan = mv("enable");
  23. if ($wlan == "wlan0"){
  24. //Enable wlan0 no matter it is enabled or not
  25. system("sudo ifup wlan0");
  26. die("DONE");
  27. }else if ($wlan == "wlan1"){
  28. //Enable wlan1 no matter it is enable or not
  29. system("sudo ifup wlan1");
  30. die("DONE");
  31. }
  32. }
  33. if (mv("disable") != ""){
  34. if (mv("disable") == "wlan0"){
  35. if (checkLinuxWlanUpDown(1) == "down"){
  36. die("ERROR. Cannot disable wlan0 while wlan1 is also disabled.");
  37. }else{
  38. system("sudo ifdown wlan0");
  39. die("DONE");
  40. }
  41. }else if (mv("disable") == "wlan1"){
  42. if (checkLinuxWlanUpDown(0) == "down"){
  43. die("ERROR. Cannot disable wlan1 while wlan0 is also disabled.");
  44. }else{
  45. system("sudo ifdown wlan1");
  46. die("DONE");
  47. }
  48. }
  49. }
  50. if (mv("check") != ""){
  51. die(checkLinuxWlanUpDown(mv("check")));
  52. }
  53. }else{
  54. //Running on a Window host
  55. }
  56. ?>
  57. <html>
  58. <head>
  59. <title>WiFi Power Status</title>
  60. <link href="../../../script/tocas/tocas.css" rel='stylesheet'>
  61. <script src="../../../script/jquery.min.js"></script>
  62. <style>
  63. body{
  64. background-color:#f7f7f7;
  65. }
  66. .selectable{
  67. cursor:pointer;
  68. }
  69. </style>
  70. </head>
  71. <body>
  72. <br><br>
  73. <div class="ts container">
  74. <div class="ts segment">
  75. <div class="ts header">
  76. WiFi Adapter Power Management
  77. <div class="sub header">Shutdown unused wireless hardware to save power</div>
  78. </div>
  79. </div>
  80. <?php
  81. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  82. echo '<div class="ts message">
  83. <div class="header">Not Supported OS</div>
  84. <p>This function is not supported in Windows Host.</p>
  85. </div>';
  86. exit(0);
  87. }
  88. ?>
  89. <div id="errormsg" class="ts primary segment" style="display:none;">
  90. <p>Error Message: </p>
  91. </div>
  92. <div class="ts inverted segment">
  93. <p id="wlan0">wlan0: Loading...</p>
  94. </div>
  95. <div>Action: <a onClick="onwlan(0);" class="selectable">wlan0-up</a> / <a onClick="offwlan(0);" class="selectable">wlan0-down</a></div>
  96. <div class="ts inverted segment">
  97. <p id="wlan1">wlan1: Loading...</p>
  98. </div>
  99. <div>Action: <a onClick="onwlan(1);" class="selectable">wlan1-up</a> / <a onClick="offwlan(1);" class="selectable">wlan1-down</a></div>
  100. <details class="ts accordion">
  101. <summary>
  102. <i class="dropdown icon"></i> What is wlan0 and wlan1?
  103. </summary>
  104. <div class="content">
  105. <p>ArOZ Online System that runs on Raspberry Pi Zero W / Pi 3B+ or above require at least two WiFi Adapter to work.
  106. But in most of the cases, even two wireless hardware is built in, most people only use one of them at the same time.
  107. Hence, switching on of them off will save more power and extend the battery life of the device.</p>
  108. <mark>By default, wlan0 is WiFi Client and wlan1 is Acess Point</mark>
  109. <p>If you are accessing the ArOZ Online System via Local IP Scanner (e.g. lanips.imuslab.com or ArOZ Portable Scanner Apps), do not shutdown wlan0<br>
  110. If you are accessing the ArOZ Online System via directly connect to the devices' WiFi Hotspot, do not turn of wlan1.</p>
  111. <p>If you don't know anything about WiFi or AP, just leave both of them on.</p>
  112. </div>
  113. </details>
  114. </div>
  115. <script>
  116. $(document).ready(function(){
  117. updateBothwlanStatus();
  118. });
  119. function onwlan(id){
  120. if (id == 0){
  121. $.get("power.php?enable=wlan0",function(data){
  122. updateBothwlanStatus();
  123. });
  124. }else if (id == 1){
  125. $.get("power.php?enable=wlan1",function(data){
  126. updateBothwlanStatus();
  127. });
  128. }
  129. }
  130. function offwlan(id){
  131. if (id == 0){
  132. $.get("power.php?disable=wlan0",function(data){
  133. if (data.includes("ERROR")){
  134. $("#errormsg").find("p").text(data);
  135. $("#errormsg").fadeIn().delay(5000).fadeOut();
  136. }else{
  137. updateBothwlanStatus();
  138. }
  139. });
  140. }else if (id == 1){
  141. $.get("power.php?disable=wlan1",function(data){
  142. if (data.includes("ERROR")){
  143. $("#errormsg").find("p").text(data);
  144. $("#errormsg").fadeIn().delay(5000).fadeOut();
  145. }else{
  146. updateBothwlanStatus();
  147. }
  148. });
  149. }
  150. }
  151. function updateBothwlanStatus(){
  152. $("#wlan0").parent().removeClass("positive").removeClass("negative");
  153. $("#wlan1").parent().removeClass("positive").removeClass("negative");
  154. $.get("power.php?check=0",function(data){
  155. if (data == "up"){
  156. $("#wlan0").parent().addClass("positive");
  157. $("#wlan0").html("<i class='checkmark icon'></i>wlan0: " + data);
  158. }else if (data == "down"){
  159. $("#wlan0").parent().addClass("negative");
  160. $("#wlan0").html("<i class='remove icon'></i>wlan0: " + data);
  161. }
  162. });
  163. $.get("power.php?check=1",function(data){
  164. if (data == "up"){
  165. $("#wlan1").html("<i class='checkmark icon'></i>wlan1: " + data);
  166. $("#wlan1").parent().addClass("positive");
  167. }else if (data == "down"){
  168. $("#wlan1").html("<i class='remove icon'></i>wlan1: " + data);
  169. $("#wlan1").parent().addClass("negative");
  170. }
  171. });
  172. }
  173. </script>
  174. </body>
  175. </html>