Skip to content

Commit 20aafee

Browse files
committed
adding brightness adjustments to websockets
1 parent 35c4512 commit 20aafee

9 files changed

Lines changed: 184 additions & 118 deletions

File tree

app/brightness.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
const _ = require("lodash");
2+
const {updateBrightness, getCurrentBrightness} = require("../db/controllers/brightness");
3+
const {discoverPixelBlazes, sendCommand} = require("./pixelBlazeUtils");
4+
5+
let currentBrightness
6+
let pixelBlazeData = []
7+
let pixelBlazeIds = []
8+
init = async () => {
9+
getCurrentBrightness()
10+
.then((brightness) => {
11+
try {
12+
currentBrightness = brightness[0].value
13+
} catch (err) {
14+
console.warn(`Error: ${err}`)
15+
}
16+
})
17+
pixelBlazeData = discoverPixelBlazes()
18+
pixelBlazeIds = _.map(pixelBlazeData, 'id')
19+
}
20+
21+
initInterval = setInterval(init, 100)
22+
23+
class Brightness {
24+
constructor(utils) {
25+
this.utils = utils ? utils : null
26+
}
27+
adjustBrightness = async (brightness) => {
28+
await new Promise((resolve) => {
29+
this.delayedSaveBrightness(resolve, brightness)
30+
})
31+
}
32+
delayedSaveBrightness = _.debounce(async (resolve, brightness) => {
33+
sendCommand(pixelBlazeIds, null, brightness)
34+
await this.storeBrightness(brightness);
35+
currentBrightness = brightness
36+
await this.utils.broadcastMessage({ currentBrightness: currentBrightness})
37+
}, 1000)
38+
getBrightness = async () =>{
39+
console.log(currentBrightness)
40+
await this.utils.broadcastMessage({ currentBrightness: currentBrightness})
41+
}
42+
storeBrightness = async (brightness) => {
43+
const body = {
44+
value: brightness
45+
}
46+
await updateBrightness(body)
47+
}
48+
49+
}
50+
51+
module.exports.BrightnessWebsocket = function (utils) {
52+
const brightness = new Brightness(utils)
53+
this.utils = utils
54+
this.receiveMessage = async function (data) {
55+
let message
56+
try {
57+
message = JSON.parse(data);
58+
} catch (err) {
59+
this.utils.sendError(err)
60+
return
61+
}
62+
if (message.type === 'ADJUST_BRIGHTNESS') {
63+
console.log('received adjust brightness message!')
64+
await brightness.adjustBrightness(parseFloat(message.brightness))
65+
}
66+
if (message.type === 'GET_CURRENT_BRIGHTNESS') {
67+
console.log('received get current brightness message!')
68+
await brightness.getBrightness()
69+
}
70+
}
71+
}

app/firestormWebsocket.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const WebSocketServer = require('ws').Server
22
const {PlaylistWebSocket} = require('./playlist')
33
const {Utils} = require('./utils')
4+
const {BrightnessWebsocket} = require("./brightness");
45

56
// start playlist server
67
const address = '0.0.0.0';
@@ -9,6 +10,7 @@ const firestormServer = new WebSocketServer({host: address , port: port});
910
console.log(`Firestorm server is running on ${address}:${port}`);
1011
firestormServer.on('connection', function (connection) {
1112
const utils = new Utils(connection)
13+
const brightnessWebsocket = new BrightnessWebsocket(utils)
1214
const playlistWebSocket = new PlaylistWebSocket(utils)
1315
if(utils.addFirestormClient(connection)) {
1416
return
@@ -19,6 +21,9 @@ firestormServer.on('connection', function (connection) {
1921
if (await playlistWebSocket.receiveMessage(message)) {
2022
return
2123
}
24+
if (await brightnessWebsocket.receiveMessage(message)) {
25+
return
26+
}
2227
})
2328
connection.on('close', function() {
2429
console.log('closed connection')

app/pixelBlazeUtils.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const _ = require("lodash");
2+
const {discoveries} = require("./discovery");
3+
4+
module.exports.discoverPixelBlazes = () => {
5+
return _.map(discoveries, function (v, k) {
6+
let res = _.pick(v, ['lastSeen', 'address']);
7+
_.assign(res, v.controller.props);
8+
return res;
9+
})
10+
}
11+
12+
module.exports.sendCommand = (pixelBlazeIds, name, brightness) => {
13+
_.each(pixelBlazeIds, async id => {
14+
id = String(id);
15+
let controller = discoveries[id] && discoveries[id].controller;
16+
if (controller) {
17+
let command = null
18+
if(name) {
19+
command = {
20+
programName: name
21+
}
22+
}
23+
if(brightness){
24+
command = {
25+
brightness: brightness
26+
}
27+
}
28+
if (command) {
29+
await controller.setCommand(command);
30+
} else {
31+
console.log(`No command sent to Pixelblazes command is ${command}`)
32+
}
33+
}
34+
})
35+
}

app/playlist.js

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const _ = require("lodash");
2-
const {discoveries} = require("./discovery");
32
const {getPlaylistFromDB, addPatternToPlaylist, removeAllPatterns} = require("../db/controllers/playlist");
4-
3+
const {discoverPixelBlazes, sendCommand} = require("./pixelBlazeUtils");
54
let currentPlaylistData = []
65
let currentPlaylist = []
76
let initInterval
@@ -27,13 +26,7 @@ init = async () => {
2726
pixelBlazeData = discoverPixelBlazes()
2827
pixelBlazeIds = _.map(pixelBlazeData, 'id')
2928
}
30-
discoverPixelBlazes = () => {
31-
return _.map(discoveries, function (v, k) {
32-
let res = _.pick(v, ['lastSeen', 'address']);
33-
_.assign(res, v.controller.props);
34-
return res;
35-
})
36-
}
29+
3730
initInterval = setInterval(init, 100)
3831

3932
class Playlist {
@@ -48,7 +41,6 @@ class Playlist {
4841
await new Promise(resolve => {
4942
this.playlistLoopTimeout = setTimeout(resolve, 100)
5043
});
51-
console.log({currentPlaylist})
5244
if(pixelBlazeIds.length) {
5345
await this.iterateOnPlaylist()
5446
}
@@ -126,23 +118,13 @@ class Playlist {
126118
}
127119
sendPattern = (pattern) => {
128120
const name = pattern.name
129-
_.each(pixelBlazeIds, async id => {
130-
id = String(id);
131-
let controller = discoveries[id] && discoveries[id].controller;
132-
if (controller) {
133-
const command = {
134-
programName: pattern.name
135-
}
136-
await controller.setCommand(command);
137-
}
138-
})
121+
sendCommand(pixelBlazeIds, name)
139122
// skipping this if utils is not initialized due to no websocket connections
123+
let message = {
124+
currentRunningPattern: name,
125+
currentPlaylist: currentPlaylist
126+
}
140127
if(this.utils) {
141-
let message = {
142-
currentRunningPattern: name,
143-
currentPlaylist: currentPlaylist
144-
}
145-
console.log(message)
146128
this.utils.broadcastMessage(message)
147129
}
148130
}

app/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const {v4: uuidv4} = require("uuid");
2+
const _ = require("lodash");
23
module.exports.Utils = function (connection) {
34
this.connection = connection
45
this.firestormClients = new Array()

db/api/router.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const brightnessDbRoutes = require('../controllers/brightness')
21
const playlistDbRoutes = require('../controllers/playlist')
32

43
// Create router
@@ -7,7 +6,4 @@ module.exports = function (app) {
76
app.post('/playlist/addPattern', playlistDbRoutes.addPatternToPlaylist)
87
app.put('/playlist/removePattern', playlistDbRoutes.removePatternFromPlaylist)
98
app.put('/playlist/newPlaylist', playlistDbRoutes.newPlaylist)
10-
app.put('/playlist/newPlaylist', playlistDbRoutes.newPlaylist)
11-
app.get('/brightness/current', brightnessDbRoutes.currentBrightness)
12-
app.post('/brightness/update', brightnessDbRoutes.updateBrightness)
139
}

db/controllers/brightness.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,30 @@ exports.doesBrightnessExistInTable = async (req) => {
1212
})
1313
}
1414

15-
exports.currentBrightness = async (req, res) => {
15+
exports.getCurrentBrightness = async (req, res) => {
1616
return await knex
1717
.select('*')
1818
.from(brightness_table)
1919
.then((data) => {
20-
res.status(200)
21-
.json(data);
20+
if(res) {
21+
res.status(200)
22+
.json(data);
23+
} else {
24+
return data
25+
}
2226
})
2327
.catch(err => {
2428
console.log(`There was an error retrieving brightness: ${err}`)
2529
})
2630
}
2731

2832
exports.updateBrightness = async (req, res) => {
33+
let brightnessValue = ''
34+
try {
35+
brightnessValue = req.body.value
36+
} catch(err) {
37+
brightnessValue = req.value
38+
}
2939
await knex.transaction(async trx => {
3040
//clear table first
3141
await knex
@@ -36,20 +46,30 @@ exports.updateBrightness = async (req, res) => {
3646
// insert new pattern
3747
await knex
3848
.insert({
39-
value: req.body.value,
49+
value: brightnessValue,
4050
})
4151
.into(brightness_table)
4252
.transacting(trx);
4353
})
44-
.then( () => {
45-
res.status(200)
46-
.json({ message: `Creating a new brightness with level '${req.body.value}'.`});
47-
}
48-
)
49-
.catch(err => {
54+
.then( () => {
55+
if(res) {
56+
res.status(200)
57+
.json({message: `Creating a new brightness with level '${brightnessValue}'.`})
58+
} else {
59+
return JSON.stringify({message: 'ok'})
60+
}
61+
})
62+
.catch(err => {
63+
if(res) {
5064
res.status(500)
5165
.json({
52-
message: `There was an error creating a new brightness with level '${req.body.value}', error: ${err}`
66+
message: `There was an error creating a new brightness with level '${brightnessValue}', error: ${err}`
5367
})
54-
})
68+
} else {
69+
return JSON.stringify({
70+
code: 500,
71+
message: `There was an error creating a new brightness with level '${brightnessValue}', error: ${err}`
72+
})
73+
}
74+
})
5575
}

db/db.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,10 @@ knex.schema
2727
})
2828
}
2929
})
30-
.then(() => {
31-
console.log('done')
32-
})
3330
.catch((error) => {
3431
console.error(`There was an error setting up the database: ${error}`)
3532
})
3633

37-
knex.select('*').from('playlist')
38-
.then(data => console.log('data:', data))
39-
.catch(err => console.log(err))
40-
41-
4234
knex.schema
4335
.hasTable('brightness')
4436
.then((exists) => {
@@ -61,16 +53,8 @@ knex.schema
6153
})
6254
}
6355
})
64-
.then(() => {
65-
console.log('done')
66-
})
6756
.catch((error) => {
6857
console.error(`There was an error setting up the database: ${error}`)
6958
})
7059

71-
knex.select('*').from('brightness')
72-
.then(data => console.log('data:', data))
73-
.catch(err => console.log(err))
74-
75-
7660
module.exports = knex

0 commit comments

Comments
 (0)