Last active
September 18, 2018 14:55
-
-
Save riebschlager/9f938a21cb6e7f53a15ddf7449ef53fd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The lighting controller expects a JSON-formatted array of lights states in the following format: | |
[ | |
{ | |
"lightId": Number, | |
"state": Boolean | |
} | |
]; | |
// Sample usage: Turning on lights 1, 2 and 3. Turning off light 4. | |
[ | |
{ | |
"lightId": 1, | |
"state": true | |
}, | |
{ | |
"lightId": 2, | |
"state": true | |
}, | |
{ | |
"lightId": 3, | |
"state": true | |
}, | |
{ | |
"lightId": 4, | |
"state": false | |
} | |
]; | |
// Note: Any lights not specifically addressed in the message will not change state. | |
// If light 7 was on before the message above was sent, it will still be on afterwards. | |
// Example Server Implementation | |
const app = require('http').createServer(); | |
const io = require('socket.io')(app); | |
app.listen(8000); | |
io.on('connection', function(socket) { | |
// Send random light data every second | |
setInterval(() => { | |
let lightData = []; | |
for (let i = 0; i < 14; i++) { | |
lightData.push({ | |
lightId: i, | |
state: Math.random() > 0.5 ? true : false | |
}); | |
} | |
socket.emit('message', lightData); | |
}, 1000); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment