-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApi.js
More file actions
97 lines (89 loc) · 4.28 KB
/
Copy pathApi.js
File metadata and controls
97 lines (89 loc) · 4.28 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
const axios = require('axios');
const net = require("net");
const ping = require("ping");
exports.address = '10.5.0.45';
exports.effets = {};
exports.palettes = {};
exports.status = {};
exports.ping = async function() {
let result = await ping.promise.probe(this.address, {timeout: 2});
return result.alive;
}
exports.fetchEffects = async function() {
return axios.get("http://" + this.address + "/api?effects=0").then(res => {
for (let effect in res.data)
this.effets[res.data[effect]] = effect;
}).catch(err => { console.log(err.code); });
}
exports.calibrate = async function() {
return axios.get("http://" + this.address + "/calibrate").catch(err => { console.log(err.code); });
}
exports.fetchPalettes = async function() {
return axios.get("http://" + this.address + "/api?palettes=0").then(res => {
for (let palette in res.data)
this.palettes[res.data[palette]] = palette;
}).catch(err => { console.log(err.code); });
}
exports.fetchStatus = async function() {
return axios.get("http://" + this.address + "/api?status=0").then(res => {
for (let status in res.data)
this.status[status] = res.data[status];
}).catch(err => { console.log(err.code); });
}
exports.set = async function(params) {
if (params.primary !== undefined) {
return axios.post("http://" + this.address + "/api", "primary_color=" + parseInt(params.primary, 16)).then(res => {
for (let status in res.data)
this.status[status] = res.data[status];
}).catch(err => { console.log(err.code); });
} if (params.secondary !== undefined) {
return axios.post("http://" + this.address + "/api", "secondary=" + parseInt(params.secondary, 16)).then(res => {
for (let status in res.data)
this.status[status] = res.data[status];
}).catch(err => { console.log(err.code); });
} if (params.tertiary !== undefined) {
return axios.post("http://" + this.address + "/api", "tertiary_color=" + parseInt(params.tertiary, 16)).then(res => {
for (let status in res.data)
this.status[status] = res.data[status];
}).catch(err => { console.log(err.code); });
} if (params.effect !== undefined) {
return axios.post("http://" + this.address + "/api", "effect=" + params.effect).then(res => {
for (let status in res.data)
this.status[status] = res.data[status];
}).catch(err => { console.log(err.code); });
} if (params.speed !== undefined) {
return axios.post("http://" + this.address + "/api", "speed=" + params.speed).then(res => {
for (let status in res.data)
this.status[status] = res.data[status];
}).catch(err => { console.log(err.code); });
} if (params.intensity !== undefined) {
return axios.post("http://" + this.address + "/api", "intensity=" + params.intensity).then(res => {
for (let status in res.data)
this.status[status] = res.data[status];
}).catch(err => { console.log(err.code); });
} if (params.palette !== undefined) {
return axios.post("http://" + this.address + "/api", "palette=" + params.palette).then(res => {
for (let status in res.data)
this.status[status] = res.data[status];
}).catch(err => { console.log(err.code); });
} if (params.brightness !== undefined) {
return axios.post("http://" + this.address + "/api", "brightness=" + params.brightness).then(res => {
for (let status in res.data)
this.status[status] = res.data[status];
}).catch(err => { console.log(err.code); });
}
}
exports.setNetwork = function(ssid, pass, ip, gateway, mask) {
if (net.isIPv4(ip) && net.isIPv4(gateway) && net.isIPv4(mask)) {
let ip_str = ip.split(".");
let gateway_str = gateway.split(".");
let mask_str = mask.split(".");
let data = "net_ssid=" + ssid + "&net_pass=" + pass;
for (let i = 0; i < 4; i++) {
data += "&ip_" + i + "=" + ip_str[i];
data += "&gateway_" + i + "=" + gateway_str[i];
data += "&mask_" + i + "=" + mask_str[i];
}
axios.get("http://" + this.address + "/setup?" + data).catch(err => { console.log(err.code); });
}
}