@@ -5,15 +5,19 @@ const socketIo = require("socket.io");
5
5
require ( 'dotenv' ) . config ( ) ;
6
6
const port = process . env [ 'PORT' ] ;
7
7
const local = process . env [ 'CORSLOCAL' ] ;
8
+
8
9
const index = require ( "./routes/index" ) ;
9
10
const app = express ( ) ;
10
11
12
+ const events = require ( 'events' ) ;
13
+ const eventEmitter = new events . EventEmitter ( ) ;
14
+
11
15
app . use ( index ) ;
12
16
13
17
const server = http . createServer ( app ) ;
14
18
const io = socketIo ( server , {
15
19
cors : {
16
- // you may need to change this if the client starts on a different port
20
+ // You may need to change this in .ENV if the client starts on a different port.
17
21
origin : local ,
18
22
methods : [ "GET" , "POST" ]
19
23
} ,
@@ -34,16 +38,24 @@ io.on("connection", (socket) => {
34
38
35
39
interval = setInterval ( ( ) => getApiAndEmit ( socket ) , 1000 ) ;
36
40
41
+ socket . on ( "handleLED" , ( arg ) => {
42
+ console . log ( "toggleLED" , arg ) ;
43
+
44
+ // Fire the 'toggleLED' event.
45
+ eventEmitter . emit ( 'toggleLED' ) ;
46
+ } ) ;
47
+
37
48
socket . on ( "disconnect" , ( ) => {
38
49
console . log ( "Client disconnected" ) ;
39
50
clearInterval ( interval ) ;
40
- socket . offAny ( ) ;
51
+ socket . off ( ) ;
41
52
} ) ;
42
53
} ) ;
43
54
44
55
// https://stackabuse.com/using-global-variables-in-node-js/
45
56
global . lightStatus = { } ;
46
57
global . ledStatus = { } ;
58
+ global . tempStatus = { } ;
47
59
48
60
function setLight ( item , msg ) {
49
61
global . lightStatus = {
@@ -100,7 +112,7 @@ board.on("ready", function () {
100
112
freq : 5000
101
113
} ) ;
102
114
103
- //// "data" get the current reading from the photoresistor
115
+ // "data" is the current reading from the photoresistor.
104
116
photoresistor . on ( "data" , ( ) => {
105
117
setLight ( 'light' , photoresistor . value )
106
118
} ) ;
@@ -132,11 +144,10 @@ board.on("ready", function () {
132
144
}
133
145
134
146
/**
135
- *
136
147
* @param {* } colour
137
148
* usage example: pulseLed(green);
138
149
*/
139
- const pulseLed = function ( colour ) {
150
+ const pulseLed = function ( colour = array ) {
140
151
colour . pulse ( ) ;
141
152
setLED ( 'led' , true ) ;
142
153
@@ -146,19 +157,14 @@ board.on("ready", function () {
146
157
} , 5000 ) ;
147
158
}
148
159
149
- // Turn on Rainbow.
150
- // To turn off: array.stop().off();
160
+ // Turn on Rainbow at start.
151
161
rainbox ( ) ;
152
162
163
+ // Turn on Array of LEDs at start.
153
164
pulseLed ( array ) ;
154
165
155
- board . on ( "exit" , ( ) => {
156
- // Turn off the rgb LED.
157
- rgbOn . off ( ) ;
158
- // Turn off each led in the array of individual leds.
159
- array . stop ( ) . off ( ) ;
160
- } ) ;
161
-
166
+ //Assign the event handler to an event:
167
+ eventEmitter . on ( 'toggleLED' , pulseLed ) ;
162
168
163
169
const thermometer = new five . Thermometer ( {
164
170
controller : "LM35" ,
@@ -170,4 +176,10 @@ board.on("ready", function () {
170
176
setTEMP ( celsius / 10 , fahrenheit / 10 ) ;
171
177
} ) ;
172
178
179
+ board . on ( "exit" , ( ) => {
180
+ // Turn off the rgb LED.
181
+ rgbOn . off ( ) ;
182
+ // Turn off each led in the array of individual leds.
183
+ array . stop ( ) . off ( ) ;
184
+ } ) ;
173
185
} ) ;
0 commit comments