Repository navigation
Expand file tree
/
Copy pathcloud.js
More file actions
98 lines (85 loc) · 2.56 KB
/
Copy pathcloud.js
File metadata and controls
98 lines (85 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Wrapper for http
var request = require('request');
var APIId = '30e271fb6eab239f791c67f44da98659';
// Options for the Phillips Hue http server where the API requests will be hit
var options = {
host: 'http://10.10.33.105',
port: 80,
path: '/api/' + APIId + '/lights/1/state',
method: 'PUT'
};
console.log(options);
// Sound, rain and thunder!
//var player = require('player');
// var rain = new player('./thunder/background-rain.mp3');
// var thunderMP3s = [
// // './thunder/148277450.mp3',
// // './thunder/315681025.mp3',
// // './thunder/587966950.mp3',
// // './thunder/1913038465.mp3',
// './thunder/thunder-clap.mp3'
// ]
// var thunder;
// Config for flash intervals
var flashInterval = 0;
var numFlashesPerEpisode = 10;
var maxTimeBetweenFlashes = 5 * 1000;
// Start the rain...
// rain.play();
// Generate a random number between 1 sec
function generateFlashTime () {
return Math.floor(Math.random() * 1000);
}
// Hit the API and make the Phillips Hue light flash
function makeLightning (turnBulbOn, bulbBrightness) {
// console.log('⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡');
request(
{
url: options.host + ':' + options.port + options.path,
method: options.method,
json: {
on: turnBulbOn,
bri: bulbBrightness
}
},
function (error, response, body) {
if (response.statusCode == 201) {
// console.log(response);
} else {
// console.log('error: '+ response.statusCode);
// console.log(body);
}
}
);
}
// Action loop
(function loop() {
var timeBetweenFlashes = generateFlashTime();
var bulbBrightness = Math.floor(Math.random() * 255);
setTimeout(function() {
// console.log(flashInterval, numFlashesPerEpisode);
if (flashInterval < numFlashesPerEpisode) {
// Play now and callback when playend
if (flashInterval === 0) {
// var randomThunderClap = thunderMP3s[Math.floor(Math.random() * thunderMP3s.length)];
// thunder = new player(randomThunderClap);
// thunder.play();
// thunder.play(function(err, player) {
// console.log(err, player);
// });
}
makeLightning(true, bulbBrightness);
flashInterval++;
// Loop through the flash cycle
loop();
}
else {
// Stop noise and flashing
// thunder.stop();
// makeLightning(false, 0);
// Wait between the loop starts again
setTimeout(loop, maxTimeBetweenFlashes);
flashInterval = 0;
}
}, timeBetweenFlashes);
}());