-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResourceServer.js
81 lines (64 loc) · 2.72 KB
/
ResourceServer.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
const { default: axios } = require("axios");
const { RESOURCE_SERVER_URL } = require("./config");
module.exports = class ResourceServer {
constructor(token) {
this._token = token;
axios.defaults.headers['Authorization'] = token;
axios.interceptors.request.use(config => {
console.log(`Making a request to ${config.url}`)
return config;
}, error => Promise.reject(error));
}
async isTokenValid() {
try {
console.log(`Making a request to ${RESOURCE_SERVER_URL}/auth/verify`);
await axios.post(`${RESOURCE_SERVER_URL}/auth/verify`);
return true;
} catch (e) {
console.log("Error => ", e);
return false;
}
}
async getGroups() {
const { data } = await axios.get(`${RESOURCE_SERVER_URL}/api/group`);
return data.groups;
}
async getDevices() {
const { data } = await axios.get(`${RESOURCE_SERVER_URL}/api/device`);
return data.devices;
}
async getGroup(groupID) {
const { data } = await axios.get(`${RESOURCE_SERVER_URL}/api/group/${groupID}`);
return data.group;
}
async getDevice(deviceID) {
const { data } = await axios.get(`${RESOURCE_SERVER_URL}/api/device/${deviceID}`);
return data.device;
}
didGroupHasAController(group, controller) {
return group.devices.map(device => device.type === controller).indexOf(true) >= 0;
}
getDeviceFromGroup(group, controller) {
return group.devices.find(device => device.type === controller);
}
async triggerDoorLock(deviceID) {
await axios.post(`${RESOURCE_SERVER_URL}/api/device/${deviceID}/lock`);
}
async triggerDoorUnlock(deviceID) {
await axios.post(`${RESOURCE_SERVER_URL}/api/device/${deviceID}/unlock`);
}
async triggerTurnOn(deviceID) {
await axios.post(`${RESOURCE_SERVER_URL}/api/device/${deviceID}/turnON`);
}
async triggerTurnOff(deviceID) {
await axios.post(`${RESOURCE_SERVER_URL}/api/device/${deviceID}/turnOFF`);
}
async setTemperature(deviceID, temperature) {
await axios.post(`${RESOURCE_SERVER_URL}/api/device/${deviceID}/setTemperature`, {
temperature
});
}
get token() {
return this._token;
}
}