-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
222 lines (193 loc) · 5.82 KB
/
main.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
const { InstanceBase, runEntrypoint, InstanceStatus } = require('@companion-module/base')
const Axios = require('axios')
const Options = require('./options')
const UpgradeScripts = require('./upgrades')
const UpdatePresets = require('./presets')
const UpdateActions = require('./actions')
const UpdateFeedbacks = require('./feedbacks')
const UpdateVariableDefinitions = require('./variables')
class ModuleInstance extends InstanceBase {
fastTimer = 0;
slowTimer = 0;
deviceData = {
firstLoad: true,
fetchedCueType: '',
state: {},
settings: {}
};
constructor(_internal) {
super(_internal);
}
async init(_config) {
this.config = _config;
this.updateActions();
this.updateFeedbacks();
this.updateVariableDefinitions();
this.updatePresets();
this.checkCues();
this.checkSettings();
this.updateStatus(InstanceStatus.Ok); // Updates Connection Status
}
// When module gets deleted
async destroy() {
console.log('Destroy');
}
async configUpdated(_config) {
this.config = _config;
}
// Return config fields for web config
getConfigFields() {
return [Options.UnitIP, Options.UnitID];
}
updatePresets() {
UpdatePresets(this);
}
updateActions() {
UpdateActions(this);
}
updateFeedbacks() {
UpdateFeedbacks(this);
}
updateVariableDefinitions() {
UpdateVariableDefinitions(this);
}
// Server responded with a status code outside the 2xx range
filterErrorLog(_error) {
if (_error.response) {
console.error(`HTTP error! status: ${_error.response.status}`);
if (_error.response.status === 404) {
console.error('V7 Not found: Check Connections config');
} else if (_error.response.status === 500) {
console.error('Server error');
} else {
console.error(`Unexpected status code: ${_error.response.status}`);
}
} else if (_error.request) {
// No response received, something went wrong with the request
if (_error.code === 'ECONNABORTED') {
console.error('Connection aborted: Check hardware setup');
} else if (_error.code === 'ERR_INVALID_URL') {
console.error('Invalid IP: Check Connections config');
} else {
console.error(`Network error: ${_error.message}`);
}
} else {
// Something else happened
console.error(`Error: ${_error.message}`);
}
}
checkCues() {
clearTimeout(this.fastTimer);
this.fetchCues();
this.fastTimer = setTimeout(() => { this.checkCues(); }, 500);
}
checkSettings() {
clearTimeout(this.slowTimer);
this.fetchSettings();
this.slowTimer = setTimeout(() => { this.checkSettings(); }, 2000);
}
// Updated when a new Cue Type has been received by the V7
// Triggers On variable change Event
UpdateFetchedCueVariable() {
this.setVariableValues({
'LastCueType': this.deviceData.fetchedCueType
});
}
immediateCheckSettings() {
clearTimeout(this.slowTimer);
this.slowTimer = setTimeout(() => { this.checkSettings(); }, 50);
}
getOutputMask(_portMask) {
let _sum = 0;
for (const _portNum of _portMask) {
_sum += parseInt(_portNum, 10);
}
return _sum;
}
updateHandsetData(_handsetData, _id, _label, _outputMask) {
// Resolves findIndex error when param is undefined
if (_id !== undefined) return;
const _index = _handsetData.findIndex(_item => _item.id === _id);
if (_index !== -1) {
// Only overwrite label if Configure Action has label with content not (undefined/null/empty)
if (_label) {
_handsetData[_index].label = _label;
}
_handsetData[_index].outputMask = _outputMask;
} else {
console.error(`Handset with id ${_id} is not registered to V7`);
}
}
async sendCommand(_body) {
let _url = `http://${this.config.unitIP}/command/${this.config.unitId}`;
console.log(`Attempting to send ${_url} this:`, _body);
try {
const _commandResponse = await Axios.post(_url, _body, {
headers: {
'Content-Type': 'application/json'
}
});
// Axios returns a response with a status field.
if (_commandResponse.status === 200) {
this.deviceData.fetchedCueType = _body['cueType'];
}
} catch (_error) {
this.filterErrorLog(_error);
}
this.checkFeedbacks('ack_cue_feedback');
}
async fetchCues(_body) {
this.deviceData.fetchedCueType = '';
let _url = `http://${this.config.unitIP}/cues/${this.config.unitId}`;
console.log(`Attempting to fetch cues from ${_url}`);
try {
const _cueResponse = await Axios.get(_url, {
headers: {
'Content-Type': 'application/json',
}
});
// Axios automatically throws for non-2xx status codes, so this is only reached for 2xx
if (_cueResponse.status === 200) {
const jsonResponse = _cueResponse.data;
let _cueAge = jsonResponse.now - jsonResponse.at;
// We've had a cue recently
if (_cueAge < 1500) {
if (jsonResponse.at != this.lastCueAt) {
this.lastCueAt = jsonResponse.at;
this.deviceData.fetchedCueType = jsonResponse.type;
this.UpdateFetchedCueVariable();
}
}
}
} catch (_error) {
this.filterErrorLog(_error);
}
this.checkFeedbacks('ack_cue_feedback');
}
async fetchSettings(_body) {
let _url = `http://${this.config.unitIP}/settings/${this.config.unitId}`;
console.log(`Attempting to fetch settings from ${_url}`);
try {
const _settingsResponse = await Axios.get(_url, {
headers: {
'Content-Type': 'application/json',
}
});
// Axios automatically throws for non-2xx status codes, so this is only reached for 2xx
if (_settingsResponse.status === 200) {
const _jsonResponse = _settingsResponse.data;
this.deviceData.state = _jsonResponse.state;
this.deviceData.settings = _jsonResponse.settings;
this.deviceData.firstLoad = false;
}
} catch (_error) {
this.filterErrorLog(_error);
}
this.checkFeedbacks('output_channel_feedback',
'next_feedback',
'back_feedback',
'blackout_feedback',
'technician_feedback');
}
}
runEntrypoint(ModuleInstance, UpgradeScripts)