forked from iNavFlight/inav-configurator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefaults_dialog.js
349 lines (284 loc) · 12.2 KB
/
defaults_dialog.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
'use strict';
const { GUI } = require('./../js/gui');
const FC = require('./fc');
const MSP = require('./msp');
const MSPCodes = require('./../js/msp/MSPCodes');
const mspHelper = require('./msp/MSPHelper');
const MSPChainerClass = require('./msp/MSPchainer');
const features = require('./feature_framework');
const periodicStatusUpdater = require('./periodicStatusUpdater');
const { mixer } = require('./model');
const jBox = require('./libraries/jBox/jBox.min');
const i18n = require('./localization');
const defaultsDialogData = require('./defaults_dialog_entries.js');
const Settings = require('./settings.js');
const wizardUiBindings = require('./wizard_ui_bindings');
const wizardSaveFramework = require('./wizard_save_framework');
var savingDefaultsModal;
var defaultsDialog = (function () {
let publicScope = {},
privateScope = {};
let $container;
privateScope.wizardSettings = [];
publicScope.init = function () {
mspHelper.getSetting("applied_defaults").then(privateScope.onInitSettingReturned);
$container = $("#defaults-wrapper");
};
privateScope.setFeaturesBits = function (selectedDefaultPreset) {
if (selectedDefaultPreset.features && selectedDefaultPreset.features.length > 0) {
features.reset();
for (const feature of selectedDefaultPreset.features) {
if (feature.state) {
features.set(feature.bit);
} else {
features.unset(feature.bit);
}
}
features.execute(function () {
privateScope.setSettings(selectedDefaultPreset);
});
} else {
privateScope.setSettings(selectedDefaultPreset);
}
};
privateScope.saveWizardStep = function (selectedDefaultPreset, wizardStep) {
const steps = selectedDefaultPreset.wizardPages;
const stepName = steps[wizardStep];
if (stepName == "receiver") {
let $receiverPort = $container.find('#wizard-receiver-port');
let receiverPort = $receiverPort.val();
if (receiverPort != "-1") {
privateScope.wizardSettings.push({
name: "receiverPort",
value: receiverPort
});
}
privateScope.wizardSettings.push({
name: "receiverProtocol",
value: $container.find('#wizard-receiver-protocol option:selected').text()
});
} else if (stepName == "gps") {
let port = $container.find('#wizard-gps-port').val();
let baud = $container.find('#wizard-gps-baud').val();
let protocol = $container.find('#wizard-gps-protocol option:selected').text();
privateScope.wizardSettings.push({
name: "gpsPort",
value: {
port: port,
baud: baud
}
});
privateScope.wizardSettings.push({
name: "gpsProtocol",
value: protocol
});
}
privateScope.wizard(selectedDefaultPreset, wizardStep + 1);
};
privateScope.wizard = function (selectedDefaultPreset, wizardStep) {
const steps = selectedDefaultPreset.wizardPages;
const stepsCount = selectedDefaultPreset.wizardPages.length;
const stepName = steps[wizardStep];
if (wizardStep >= stepsCount) {
//This is the last step, time to finalize
$container.hide();
wizardSaveFramework.persist(privateScope.wizardSettings, function () {
mspHelper.saveToEeprom(function () {
//noinspection JSUnresolvedVariable
GUI.log(i18n.getMessage('configurationEepromSaved'));
if (selectedDefaultPreset.reboot) {
privateScope.reboot();
}
});
});
} else {
const $content = $container.find('.defaults-dialog__wizard');
$content.unbind();
$.get("./wizard/" + stepName + ".html", function (data) {
$content.html("");
$(data).appendTo($content);
$.get("./wizard/buttons.html", function (data) {
$(data).appendTo($content);
$content.on('click', '#wizard-next', function () {
privateScope.saveWizardStep(selectedDefaultPreset, wizardStep);
});
$content.on('click', '#wizard-skip', function () {
privateScope.wizard(selectedDefaultPreset, wizardStep + 1);
});
if (stepName == "receiver") {
/**
* Bindings executed when the receiver wizard tab is loaded
*/
wizardUiBindings.receiver($content);
} else if (stepName == "gps") {
/**
* Bindings executed when the GPS wizard tab is loaded
*
*/
wizardUiBindings.gps($content);
}
Settings.configureInputs().then(
function () {
console.log('configure done');
$container.find('.defaults-dialog__content').hide();
$container.find('.defaults-dialog__wizard').show();
savingDefaultsModal.close();
$container.show();
}
);
});
});
}
};
privateScope.reboot = function () {
periodicStatusUpdater.resume();
GUI.tab_switch_cleanup(function () {
MSP.send_message(MSPCodes.MSP_SET_REBOOT, false, false, function () {
//noinspection JSUnresolvedVariable
if (typeof savingDefaultsModal !== 'undefined') {
savingDefaultsModal.close();
}
GUI.log(i18n.getMessage('deviceRebooting'));
GUI.handleReconnect();
});
});
};
privateScope.finalize = function (selectedDefaultPreset) {
if (selectedDefaultPreset.wizardPages) {
privateScope.wizard(selectedDefaultPreset, 0);
} else {
mspHelper.saveToEeprom(function () {
//noinspection JSUnresolvedVariable
GUI.log(i18n.getMessage('configurationEepromSaved'));
if (selectedDefaultPreset.reboot) {
privateScope.reboot();
}
});
}
};
privateScope.setSettings = function (selectedDefaultPreset) {
periodicStatusUpdater.stop();
var currentControlProfile = parseInt($("#profilechange").val());
var currentBatteryProfile = parseInt($("#batteryprofilechange").val());
var controlProfileSettings = [];
var batterySettings = [];
var miscSettings = [];
selectedDefaultPreset.settings.forEach(input => {
if (FC.isControlProfileParameter(input.key)) {
controlProfileSettings.push(input);
} else if (FC.isBatteryProfileParameter(input.key)) {
batterySettings.push(input);
} else {
miscSettings.push(input);
}
});
var settingsChainer = MSPChainerClass();
var chain = [];
miscSettings.forEach(input => {
chain.push(function (callback) {
mspHelper.setSetting(input.key, input.value, callback);
});
});
// Set control and battery parameters on all 3 profiles
for (let profileIdx = 0; profileIdx < 3; profileIdx++){
chain.push(function (callback) {
MSP.send_message(MSPCodes.MSP_SELECT_SETTING, [profileIdx], false, callback);
});
controlProfileSettings.forEach(input => {
chain.push(function (callback) {
mspHelper.setSetting(input.key, input.value, callback);
});
});
chain.push(function (callback) {
MSP.send_message(MSPCodes.MSP2_INAV_SELECT_BATTERY_PROFILE, [profileIdx], false, callback);
});
batterySettings.forEach(input => {
chain.push(function (callback) {
mspHelper.setSetting(input.key, input.value, callback);
});
});
}
// Set Mixers
if (selectedDefaultPreset.mixerToApply) {
let currentMixerPreset = mixer.getById(selectedDefaultPreset.mixerToApply);
mixer.loadServoRules(FC, currentMixerPreset);
mixer.loadMotorRules(FC, currentMixerPreset);
FC.MIXER_CONFIG.platformType = currentMixerPreset.platform;
FC.MIXER_CONFIG.appliedMixerPreset = selectedDefaultPreset.mixerToApply;
FC.MIXER_CONFIG.motorStopOnLow = (currentMixerPreset.motorStopOnLow === true) ? true : false;
FC.MIXER_CONFIG.hasFlaps = (currentMixerPreset.hasFlaps === true) ? true : false;
FC.SERVO_RULES.cleanup();
FC.SERVO_RULES.inflate();
FC.MOTOR_RULES.cleanup();
FC.MOTOR_RULES.inflate();
chain = chain.concat([
mspHelper.saveMixerConfig,
mspHelper.sendServoMixer,
mspHelper.sendMotorMixer
]);
}
chain.push(function (callback) {
MSP.send_message(MSPCodes.MSP_SELECT_SETTING, [currentControlProfile], false, callback);
});
chain.push(function (callback) {
MSP.send_message(MSPCodes.MSP2_INAV_SELECT_BATTERY_PROFILE, [currentBatteryProfile], false, callback);
});
settingsChainer.setChain(chain);
settingsChainer.setExitPoint(function () {
privateScope.finalize(selectedDefaultPreset);
});
settingsChainer.execute();
}
privateScope.onPresetClick = function (event) {
savingDefaultsModal = new jBox('Modal', {
width: 400,
height: 120,
animation: false,
closeOnClick: false,
closeOnEsc: false,
content: $('#modal-saving-defaults')
}).open();
$container.hide();
let selectedDefaultPreset = defaultsDialogData[$(event.currentTarget).data("index")];
if (selectedDefaultPreset && selectedDefaultPreset.settings) {
if (selectedDefaultPreset.id == 0) {
// Close applying preset dialog if keeping current settings.
savingDefaultsModal.close();
}
mspHelper.loadFeatures(function () {
privateScope.setFeaturesBits(selectedDefaultPreset)
});
} else {
savingDefaultsModal.close();
}
};
privateScope.render = function () {
$container.find('.defaults-dialog__content').show();
$container.find('.defaults-dialog__wizard').hide();
let $place = $container.find('.defaults-dialog__options');
$place.html("");
for (let i in defaultsDialogData) {
if (defaultsDialogData.hasOwnProperty(i)) {
let preset = defaultsDialogData[i];
let $element = $("<div class='default_btn defaults_btn'>\
<a class='confirm' href='#'></a>\
</div>")
if (preset.notRecommended) {
$element.addClass("defaults_btn--not-recommended");
}
$element.find("a").html(preset.title);
$element.data("index", i).on('click', privateScope.onPresetClick)
$element.appendTo($place);
}
}
}
privateScope.onInitSettingReturned = function (promise) {
if (promise.value > 0) {
return; //Defaults were applied, we can just ignore
}
privateScope.render();
$container.show();
}
return publicScope;
})();
module.exports = defaultsDialog;