-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
173 lines (148 loc) · 4.01 KB
/
index.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const axios = require('axios');
class GoveeLED {
constructor(config) {
this.apiKey = config.apiKey
this.mac = config.mac
this.model = config.model
this.basePath = "https://developer-api.govee.com/v1/devices"
}
request(endpoint, reqData, method) {
return new Promise( ( resolve, reject ) => {
if (this.mac === "") return reject(new Error("No MAC Address provided."));
if (this.model === "") return reject(new Error("No Model provided."));
let reqURL = this.basePath + endpoint;
var data = JSON.stringify(reqData);
var config = {
method: method,
url: reqURL,
headers: {
'Govee-API-Key': this.apiKey,
'Content-Type': 'application/json'
},
data: data
};
axios(config)
.then(function (response) {
resolve(response.data);
})
.catch(function (error) {
reject(error);
});
});
}
setColor(hexCode) {
var regex = /^#([0-9A-F]{3}){1,2}$/i;
if (!regex.test(hexCode)) throw new Error("Invalid Hex Color Code");
function hex2rgb(hex) {
var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, function(m, r, g, b) {
return r + r + g + g + b + b;
});
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
var RGBconv = hex2rgb(hexCode);
var reqData = {
"device": this.mac,
"model": this.model,
"cmd": {
"name": "color",
"value": {
"r": RGBconv.r,
"g": RGBconv.g,
"b": RGBconv.b
}
}
};
var endpoint = '/control';
return this.request(endpoint, reqData, "put");
}
setBrightness(brightnessLevel) {
if (!Number.isInteger(brightnessLevel)) throw new Error("Brightness Level Provided is Not A Number");
if (brightnessLevel > 100) throw new Error("Brightness Level Provided is Not From 1-100");
if (brightnessLevel < 1) throw new Error("Brightness Level Provided is Not From 1-100");
var reqData = {
"device": this.mac,
"model": this.model,
"cmd": {
"name": "brightness",
"value": brightnessLevel
}
};
var endpoint = '/control';
return this.request(endpoint, reqData, "put");
}
setColorTemperature(temperatureLevel) {
if (!Number.isInteger(temperatureLevel)) throw new Error("Temperature Level Provided is Not A Number");
if (temperatureLevel > 9000) throw new Error("Temperature Level Provided is Not From 2000-9000");
if (temperatureLevel < 2000) throw new Error("Temperature Level Provided is Not From 2000-9000");
var reqData = {
"device": this.mac,
"model": this.model,
"cmd": {
"name": "colorTem",
"value": temperatureLevel
}
};
var endpoint = '/control';
return this.request(endpoint, reqData, "put");
}
turnOn() {
var reqData = {
"device": this.mac,
"model": this.model,
"cmd": {
"name": "turn",
"value": "on"
}
};
var endpoint = '/control';
return this.request(endpoint, reqData, "put");
}
turnOff() {
var reqData = {
"device": this.mac,
"model": this.model,
"cmd": {
"name": "turn",
"value": "off"
}
};
var endpoint = '/control';
return this.request(endpoint, reqData, "put");
}
getState() {
var reqData = {};
var endpoint = `/state?device=${this.mac}&model=${this.model}`;
return this.request(endpoint, reqData, "get");
}
async getDevices() {
var reqData = {};
var endpoint = ``;
let reqURL = this.basePath + endpoint;
var data = JSON.stringify(reqData);
var config = {
method: "get",
url: reqURL,
headers: {
'Govee-API-Key': this.apiKey,
'Content-Type': 'application/json'
},
data: data
};
let resData;
await axios(config)
.then(async function (response) {
resData = response.data.data;
})
.catch(function (error) {
throw new Error(error);
});
return resData;
}
};
module.exports = GoveeLED;