-
Notifications
You must be signed in to change notification settings - Fork 30
/
18-hue_disco.js
106 lines (88 loc) · 2.55 KB
/
18-hue_disco.js
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
99
100
101
102
103
104
105
106
//
// Copyright (c) 2020 Cisco Systems
// Licensed under the MIT License
//
/*
* This macro will enlighten your meeting room,
* no complementary In-Room control needed:
*
* pre-req:
* - configure the macro with your Hue Bridge info and you're good to go.
* - configure your device for HttpClient
*
*/
const xapi = require('xapi');
xapi.on('ready', () => {
disco1(1000, false);
//disco2(0, 1000, false);
});
// Blinks at a regular delay, with changing color
// example: disco1(1000, false);
function disco1(delay, black) {
let color = COLOR_BLACK;
if (!black) {
color = {
hue: Math.round(Math.random() * 65535),
on: true,
sat: 255
};
}
// comment this line to remove blinking effect
black = !black;
changeColor(color);
const MIN_DELAY = 300;
setTimeout(function () {
disco1(delay + MIN_DELAY, black);
}, delay);
}
// Blinks at random delays and random colors
// example: disco2(300, 1000, false);
function disco2(delay, maxDelay, black) {
// Randomize color
let color = COLOR_BLACK;
if (delay < 500 || !black) {
color = {
hue: Math.round(Math.random() * 65535),
on: true,
sat: 255
};
}
// comment this line to remove blinking effect
black = !black;
changeColor(color);
// Randomize delay
const MIN_DELAY = 300;
let nextDelay = Math.round(Math.random() * maxDelay) + MIN_DELAY;
setTimeout(function () {
disco2(nextDelay, maxDelay + MIN_DELAY, black);
}, delay);
}
//
// Hue Library
//
// ACTION: Configure for your Philips Hue deployment
const HUE_BRIDGE = '192.168.1.33';
const HUE_USERNAME = 'EM2Vg2GtNUqAASukv47wm1pWY0FayFe48D03f6Cb';
const HUE_LIGHT = 1; // number of the light in your deployment
console.info(`Hue Bridge with ip: ${HUE_BRIDGE}, bulb: ${HUE_LIGHT}`);
const hue_url = `http://${HUE_BRIDGE}/api/${HUE_USERNAME}/lights/${HUE_LIGHT}/state`;
const COLOR_RED = { on: true, hue: 65535, sat: 255 };
const COLOR_BLUE = { on: true, hue: 46920, sat: 255 };
const COLOR_GREEN = { on: true, hue: 25500, sat: 255 };
const COLOR_WHITE = { on: true, hue: 0, sat: 0 };
const COLOR_BLACK = { on: false };
function changeColor(color) {
console.debug(`posting to hue bulb: ${JSON.stringify(color)}`)
// Post message
xapi.command(
'HttpClient Put',
{
Header: ["Content-Type: application/json"],
Url: hue_url,
AllowInsecureHTTPS: "True"
},
JSON.stringify(color))
.catch((err) => {
console.error('could not contact the Hue Bridge');
});
}