@@ -14,36 +14,31 @@ <h2>STORINATOR</h2>
1414 < div id ="powerLed " class ="led-off "> </ div >
1515 < p > Power</ p >
1616 </ div >
17- < div class ="led-box ">
18- < div id ="hddLed " class ="led-off "> </ div >
19- < p > HDD</ p >
20- </ div >
2117 </ div >
2218
2319 < div class ="operationContainer ">
24- < button type ="button " class ="powerOnButton " onclick ="powerOn( ) ">
20+ < button type ="button " class ="powerOnButton " onclick ="sendCommand(CMD_POWER_ON ) ">
2521 ON
2622 </ button >
27- < button type ="button " class ="standbyButton " onclick ="standBy( ) ">
23+ < button type ="button " class ="standbyButton " onclick ="sendCommand(CMD_STAND_BY ) ">
2824 STANDBY
2925 </ button >
30- < button type ="button " class ="resetButton " onclick ="reset() "> RESET</ button >
26+ < button type ="button " class ="resetButton " onclick ="sendCommand(CMD_RESET) ">
27+ RESET
28+ </ button >
29+ < button type ="button " class ="killButton " onclick ="sendCommand(CMD_KILL) ">
30+ KILL
31+ </ button >
3132 </ div >
3233
3334 < script >
3435 var state = false
36+ const CMD_POWER_ON = "powerOn" , CMD_STAND_BY = "standBy" , CMD_RESET = "reset" , CMD_KILL = "kill"
37+ const REQ_POWER_STATUS = "powerStatus"
3538
36- function powerOn ( ) {
37- RequestMachine ( 'powerOn' )
38- }
39-
40- function standBy ( ) {
41- RequestMachine ( 'standBy' )
42- }
43-
44- function reset ( ) {
45- RequestMachine ( 'reset' )
46- }
39+ setInterval ( ( ) => {
40+ requestStatus ( REQ_POWER_STATUS )
41+ } , 3000 )
4742
4843 function togglePowerLed ( state ) {
4944 const powerLed = document . getElementById ( 'powerLed' )
@@ -56,28 +51,34 @@ <h2>STORINATOR</h2>
5651 }
5752 }
5853
59- function toggleHddLed ( state ) {
60- const hddLed = document . getElementById ( 'hddLed' )
61- if ( state ) {
62- hddLed . classList . remove ( 'led-off' )
63- hddLed . classList . add ( 'led-blue' )
64- } else {
65- hddLed . classList . remove ( 'led-blue' )
66- hddLed . classList . add ( 'led-off' )
54+ function sendCommand ( request ) {
55+ if ( ! confirm ( request . toUpperCase ( ) ) ) return
56+
57+ var xhttp = new XMLHttpRequest ( )
58+ xhttp . onreadystatechange = function ( ) {
59+ if ( this . readyState == 4 && this . status == 200 ) {
60+ console . debug ( `sendCommand:${ request } :${ this . responseText } ` )
61+ alert ( 'Action confirmed.' )
62+ }
6763 }
64+ xhttp . open ( 'GET' , request , true )
65+ xhttp . send ( )
6866 }
6967
70- function RequestMachine ( type ) {
71- if ( ! confirm ( type . toUpperCase ( ) ) ) return
72-
68+ function requestStatus ( request ) {
7369 var xhttp = new XMLHttpRequest ( )
7470 xhttp . onreadystatechange = function ( ) {
7571 if ( this . readyState == 4 && this . status == 200 ) {
76- alert ( 'Action confirmed.' )
77- console . log ( this . responseText )
72+ console . debug ( `requestStatus:${ request } :${ this . responseText } ` )
73+ if ( request == REQ_POWER_STATUS && typeof this . response === "string" ) {
74+ if ( this . response . toString ( ) . includes ( REQ_POWER_STATUS + ":on" ) )
75+ togglePowerLed ( true )
76+ else
77+ togglePowerLed ( false )
78+ }
7879 }
7980 }
80- xhttp . open ( 'GET' , type , true )
81+ xhttp . open ( 'GET' , request , true )
8182 xhttp . send ( )
8283 }
8384 </ script >
0 commit comments