@@ -3,16 +3,29 @@ const app = express()
3
3
const port = 4000
4
4
//Tell Express to serve static html files from the 'public' folder.
5
5
app . use ( express . static ( 'public' ) )
6
+ //Create a variable to store the state of the light (on or off) at any given time,
7
+ //assume off (false) to start with
8
+ var light_on = false ;
6
9
7
-
8
- //Configure express to listen for HTTP GET requests at "/"
9
- //(i.e. http://localhost:port"/") <= No need to include the "/" at the end here.
10
- app . get ( "/" , ( req , res ) => {
10
+ //Configure express to listen for HTTP GET requests at "/toggle"
11
+ //(i.e. http://localhost:port"/toggle")
12
+ app . get ( "/toggle" , ( req , res ) => {
11
13
12
14
//Let the const 'spawn' hold the child_process module.
13
15
const { spawn } = require ( "child_process" ) ;
14
- //Call the child_process to run the Python script - store the output in pyProg.
15
- const pyProg = spawn ( "python" , [ "./code/my_script.py" ] ) ;
16
+ var pyProg ;
17
+
18
+ //Toggle the light to the opposite value of whatever it is right now
19
+ light_on = ! light_on ;
20
+
21
+ //Call the child_process to run the Python script to turn on or off the light
22
+ //appropriately, depending on the current value of light_on - store the output in pyProg.
23
+ if ( light_on ) {
24
+ pyProg = spawn ( "python" , [ "./code/script_turn_on.py" ] ) ;
25
+ }
26
+ else {
27
+ pyProg = spawn ( "python" , [ "./code/script_turn_off.py" ] ) ;
28
+ }
16
29
17
30
//When output data (i.e. standard output) has been detected in 'pyProg', run
18
31
//this function to handle that output - the variable 'data' holds the output.
0 commit comments