-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
170 lines (154 loc) · 4.79 KB
/
client.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
const axios = require("axios");
const base64 = require("base-64");
const env = require("./env");
const Max = require("max-api");
const stopAllMappings = require("./stop-all-mappings");
const BASE_URL = "https://visuals.madzoo.events/api";
let authHeader = "";
const getAuthHeader = () => {
if (authHeader === "") {
if (!env) {
Max.post("Credentials file not found. Please refer to the README");
return;
}
authHeader = `Basic ${base64.encode(`${env.username}:${env.password}`)}`;
}
return authHeader;
};
const fetchEffects = async () => {
const response = await axios.get("https://visuals.madzoo.events/api/presets/all", {
headers: {
authorization: getAuthHeader()
}
});
return response.data;
};
const appendMappings = (presets, effectType, maps) => {
if (!presets) {
return;
}
presets.forEach(({id, midiMappings, displayName}) => {
if (!midiMappings) {
return;
}
midiMappings.forEach(({channel, key, behavior}) => {
switch (behavior) {
case "trigger":
maps.triggerMap.set(`${channel}:${key}`, {
id,
effectType,
displayName
});
break;
case "toggle":
maps.toggleMap.set(`${channel}:${key}`, {
id,
effectType,
displayName
});
break;
case "hold":
maps.holdMap.set(`${channel}:${key}`, {
id,
effectType,
displayName
});
break;
case "clock1Toggle":
case "clock2Toggle":
maps.clockToggleMap.set(`${channel}:${key}`, {
displayName,
payload: {
presetId: id,
effectType,
isRunning: false,
offBeat: behavior === "clock2Toggle" ? true : false
}
});
break;
case "clock1Hold":
case "clock2Hold":
maps.clockHoldMap.set(`${channel}:${key}`, {
displayName,
payload: {
presetId: id,
effectType,
isRunning: false,
offBeat: behavior === "clock2Hold" ? true : false
}
});
break;
}
});
});
};
const getMappings = async () => {
const maps = {
triggerMap: new Map(),
toggleMap: new Map(),
holdMap: new Map(),
clockToggleMap: new Map(),
clockHoldMap: new Map(),
stopAllMap: new Map()
};
const {
commandPresets,
dragonPresets,
laserPresets,
lightningPresets,
particlePresets,
potionPresets,
timeshiftPresets
} = await fetchEffects();
appendMappings(commandPresets, "command", maps);
appendMappings(particlePresets, "particle", maps);
appendMappings(dragonPresets, "dragon", maps);
appendMappings(timeshiftPresets, "timeshift", maps);
appendMappings(potionPresets, "potion", maps);
appendMappings(lightningPresets, "lightning", maps);
appendMappings(laserPresets, "laser", maps);
stopAllMappings.forEach((mapping) => {
const {channel, key, displayName, payload} = mapping;
maps.stopAllMap.set(`${channel}:${key}`, {
displayName,
payload
});
});
return maps;
};
const runEffect = async (effectType, id, action) => {
return await axios.post(`${BASE_URL}/effects/run/${effectType}/${id}/${action}`, null, {
headers: {
authorization: getAuthHeader()
}
});
};
const runStopAll = async (payload) => {
return await axios.post(`${BASE_URL}/effects/stopall`, payload, {
headers: {
authorization: getAuthHeader()
}
});
};
const subscribeToClock = async (payload) => {
return await axios.put(`${BASE_URL}/clock/subscribe`, payload, {
headers: {
authorization: getAuthHeader()
}
});
};
const unsubscribeFromClock = async (payload) => {
return await axios.put(`${BASE_URL}/clock/unsubscribe`, payload, {
headers: {
authorization: getAuthHeader()
}
});
};
module.exports = {
getAuthHeader,
getMappings,
runEffect,
runStopAll,
subscribeToClock,
unsubscribeFromClock
};