-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathSettings.svelte
587 lines (543 loc) · 23.5 KB
/
Settings.svelte
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
<script>
import { onMount, onDestroy } from 'svelte';
import { writable } from 'svelte/store';
import { _, number } from 'svelte-i18n';
import { PageHeader, BreadcrumbItem } from '@keenmate/svelte-adminlte';
import { createForm } from 'felte';
import { Config } from '@keenmate/svelte-adminlte';
import { setCustomPageTitle, customPageTitleUsed } from '../stores/page-title';
import { successNotification, errorNotification } from '../providers/notification-provider';
import { autoDarkMode } from '../helpers/color-helpers';
import { formToJSON, invalid_form_fields } from '../helpers/form-helpers';
import { languageFlag } from '../helpers/string-helpers';
import { fetchSystemSettings, updateSystemSettings, uploadFile } from '../providers/api';
import { changeLang, languages, currencies, currency } from '../locale/i18n';
import { isDay, isDarkDesktop } from '../stores/terrariumpi';
import Card from '../user-controls/Card.svelte';
import CardSettingsTools from '../components/common/CardSettingsTools.svelte';
import Field from '../components/form/Field.svelte';
import FileUpload from '../components/form/FileUpload.svelte';
import Select from '../components/form/Select.svelte';
import Switch from '../components/form/Switch.svelte';
import Helper from '../components/form/Helper.svelte';
let formData = writable({});
let editForm = null;
const units = {
temperature: [
{ value: 'celsius', text: $_('settings.units.celsius', { default: 'Celsius' }) },
{ value: 'fahrenheit', text: $_('settings.units.fahrenheit', { default: 'Fahrenheit' }) },
{ value: 'kelvin', text: $_('settings.units.kelvin', { default: 'Kelvin' }) },
],
distance: [
{ value: 'cm', text: $_('settings.units.cm', { default: 'Centimeter' }) },
{ value: 'inch', text: $_('settings.units.inch', { default: 'Inch' }) },
],
liquid_volume: [
{ value: 'l', text: $_('settings.units.l', { default: 'Liter' }) },
{ value: 'ukgall', text: $_('settings.units.ukgall', { default: 'UK Gallons' }) },
{ value: 'usgall', text: $_('settings.units.usgall', { default: 'US Gallons' }) },
],
wind_speed: [
{ value: 'm/s', text: $_('settings.units.m_s', { default: 'Meter per second' }) },
{ value: 'km/h', text: $_('settings.units.km_h', { default: 'Kilometer per hour' }) },
{ value: 'm/h', text: $_('settings.units.m_h', { default: 'Miles per hour' }) },
{ value: 'beaufort', text: $_('settings.units.beaufort', { default: 'Beaufort' }) },
],
};
const { form, setFields, isSubmitting, reset } = createForm({
onSubmit: async (values, context) => {
// Extra check on the password
if (values.password !== '' && values.password2 !== '' && values.password !== values.password2) {
context.form.elements['password2'].setCustomValidity('dummy');
} else {
// Reset error
context.form.elements['password2'].setCustomValidity('');
}
validated = true;
if (context.form.checkValidity()) {
loading = true;
values = formToJSON(context.form);
if (values.file_profile_image) {
// Upload first image to make sure it is valid
try {
values.profile_image = await uploadFile(context.form.file_profile_image);
values.delete_image = false;
} catch (error) {
errorNotification(error.message, $_('notification.form.upload.error.title', { default: 'Upload error' }));
loading = false;
validated = false;
// Return to current form
return;
}
}
delete values.file_profile_image;
// TODO: Fix this. We need to convert to string, so that backend settings will work.... not handy or nice
Object.keys(values).forEach((key) => {
if (key === 'exclude_ids') {
values[key] = values[key].join(',');
} else {
values[key] = values[key] + '';
}
});
try {
await updateSystemSettings(values);
successNotification(
$_('system.settings.save.ok.message', { default: 'Settings are updated.' }),
$_('notification.form.save.ok.title', { default: 'Update OK' }),
);
changeLang(values.language);
currency.set(values.currency);
Config.set(formToJSON(context.form));
autoDarkMode($isDay, $isDarkDesktop);
} catch (error) {
errorNotification(error.message, $_('notification.form.save.error.title', { default: 'Save Error' }));
}
loading = false;
validated = false;
} else {
let error_message = $_('webcams.settings.save.error.required_fields', {
default: 'Not all required fields are entered correctly.',
});
error_message += "\n'" + invalid_form_fields(editForm).join("'\n'") + "'";
errorNotification(error_message, $_('notification.form.save.error.title', { default: 'Save Error' }));
}
},
});
//let languages = []
let loading = true;
let validated = false;
let excluded_ids = [];
onMount(() => {
(async () => {
await fetchSystemSettings(null, (settings) => {
for (let field of settings) {
$formData[field.id] = field.value;
// TODO: Fix json data to real true and false values
$formData[field.id] =
$formData[field.id] === 'true' ? true : $formData[field.id] === 'false' ? false : $formData[field.id];
if (field.id === 'exclude_ids') {
excluded_ids = $formData[field.id].map((item) => {
return { value: item.id, text: item.name };
});
$formData[field.id] = $formData[field.id].map((item) => {
return item.id;
});
} else if (field.id === 'language') {
$formData[field.id] = $formData[field.id].replace(/_/gm, '-');
} else if (
field.id === 'always_authenticate' &&
($formData[field.id] === true || $formData[field.id] === false)
) {
$formData[field.id] = $formData[field.id] ? 1 : 0;
}
}
setFields($formData);
loading = false;
});
})();
// Reset form validation
reset();
$formData = formToJSON(editForm);
validated = false;
// Toggle loading div
loading = true;
setCustomPageTitle($_('system.settings.page.title', { default: 'System settings' }));
editForm.setAttribute('novalidate', 'novalidate');
});
onDestroy(() => {
customPageTitleUsed.set(false);
});
</script>
<PageHeader>
{$_('system.settings.page.title', { default: 'System settings' })}
<svelte:fragment slot="breadcrumbs">
<BreadcrumbItem>
<Helper button={false} showMessage={true} />
</BreadcrumbItem>
</svelte:fragment>
</PageHeader>
<div class="container-fluid">
<form class="form-horizontal needs-validation" class:was-validated={validated} use:form bind:this={editForm}>
<div class="row">
<div class="col">
<Card {loading}>
<svelte:fragment slot="header">
<i class="fas fa-cog mr-2"></i>{$_('system.settings.header.system', { default: 'System' })}
</svelte:fragment>
<svelte:fragment slot="tools">
<CardSettingsTools />
</svelte:fragment>
<Field
type="number"
name="pi_wattage"
required={true}
min="0.00"
step="0.01"
horizontal={true}
label={$_('system.settings.pi-wattage.label', { default: 'Pi power usage' })}
help={$_('system.settings.pi-wattage.help', {
default: 'Enter the total power usage of the Pi with all its USB devices connected.',
})}
invalid={$_('system.settings.pi-wattage.invalid', {
values: { value: 0 },
default: 'Please enter a minimum value of {value}',
})}
/>
<Field
type="text"
name="host"
required={true}
horizontal={true}
label={$_('system.settings.host.label', { default: 'IP number' })}
help={$_('system.settings.host.help', {
default: 'Enter the IP to listen for connections. Default 0.0.0.0',
})}
invalid={$_('system.settings.host.invalid', { default: 'Please enter a valid IP address' })}
/>
<Field
type="number"
name="port"
required={true}
min="1024"
step="1"
horizontal={true}
label={$_('system.settings.port.label', { default: 'Port number' })}
help={$_('system.settings.port.help', {
default: 'Enter the port number to listen for connections. Default 8090',
})}
invalid={$_('system.settings.port.invalid', {
values: { value: 1024 },
default: 'Please enter a minimum value of {value}',
})}
/>
<Select
name="always_authenticate"
value={$formData.always_authenticate}
required={true}
horizontal={true}
options={[
{ value: 1, text: $_('system.settings.authenticate.options.full', { default: 'Full authentication' }) },
{ value: 0, text: $_('system.settings.authenticate.options.changes', { default: 'Only for changes' }) },
{
value: -1,
text: $_('system.settings.authenticate.options.no_authentication', { default: 'No authentication' }),
},
]}
label={$_('system.settings.authenticate.label', { default: 'Authentication mode' })}
help={$_('system.settings.authenticate.help', {
default: 'Always authenticate or only when changes are being made.',
})}
invalid={$_('system.settings.authenticate.invalid', { default: 'Please make a choice' })}
/>
<Field
type="text"
name="username"
required={true}
horizontal={true}
label={$_('system.settings.username.label', { default: 'Username' })}
help={$_('system.settings.username.help', { default: 'Enter the username for authentication.' })}
invalid={$_('system.settings.username.invalid', { default: 'The username cannot be empty.' })}
/>
<Field
type="password"
name="password"
horizontal={true}
label={$_('system.settings.password.label', { default: 'New password' })}
help={$_('system.settings.password.help', { default: 'Enter a password for authentication.' })}
invalid={$_('system.settings.password.invalid', { default: 'The password cannot be empty.' })}
/>
<Field
type="password"
name="password2"
horizontal={true}
label={$_('system.settings.password2.label', { default: 'Confirm new password' })}
help={$_('system.settings.password2.help', { default: 'Enter the new password again.' })}
invalid={$_('system.settings.password2.invalid', { default: 'The new password do not match.' })}
/>
<Select
name="exclude_ids"
multiple={true}
options={excluded_ids}
value={$formData.exclude_ids}
horizontal={true}
label={$_('system.settings.exclude-ids.label', { default: 'Excluded ids' })}
help={$_('system.settings.exclude-ids.help', { default: 'IDs that are excluded.' })}
/>
</Card>
</div>
<div class="col">
<Card {loading}>
<svelte:fragment slot="header">
<i class="fas fa-language mr-2"></i>{$_('system.settings.header.locale', { default: 'Locale' })}
</svelte:fragment>
<svelte:fragment slot="tools">
<CardSettingsTools />
</svelte:fragment>
<Select
name="language"
value={$formData.language}
required={true}
options={languages.map((item) => {
return { value: item.code, text: languageFlag(item.code) + ' ' + item.title };
})}
horizontal={true}
label={$_('system.settings.language.label', { default: 'Language' })}
help={$_('system.settings.language.help', { default: 'Select the interface language.' })}
invalid={$_('system.settings.language.invalid', { default: 'Please make a choice.' })}
/>
<Select
name="currency"
value={$formData.currency}
required={true}
options={currencies.map((item) => {
return { value: item, text: $number(1000, { style: 'currency', currency: item }) };
})}
horizontal={true}
label={$_('system.settings.currency.label', { default: 'Currency' })}
help={$_('system.settings.currency.help', { default: 'Select the interface currency.' })}
invalid={$_('system.settings.currency.invalid', { default: 'Please make a choice.' })}
/>
<Select
name="temperature_indicator"
value={$formData.temperature_indicator}
required={true}
options={units.temperature}
horizontal={true}
label={$_('system.settings.temperature_indicator.label', { default: 'Temperature type' })}
help={$_('system.settings.temperature_indicator.help', {
default: 'Select the temperature indicator. Only affects the current values.',
})}
invalid={$_('system.settings.temperature_indicator.invalid', { default: 'Please make a choice.' })}
/>
<Select
name="distance_indicator"
value={$formData.distance_indicator}
required={true}
options={units.distance}
horizontal={true}
label={$_('system.settings.distance_indicator.label', { default: 'Distance type' })}
help={$_('system.settings.distance_indicator.help', {
default: 'Select the distance indicator. Only affects the current values.',
})}
invalid={$_('system.settings.distance_indicator.invalid', { default: 'Please make a choice.' })}
/>
<Select
name="water_volume_indicator"
value={$formData.water_volume_indicator}
required={true}
options={units.liquid_volume}
horizontal={true}
label={$_('system.settings.water_volume_indicator.label', { default: 'Liquid volume type' })}
help={$_('system.settings.water_volume_indicator.help', {
default: 'Select the liquid volume indicator. Only affects the current values.',
})}
invalid={$_('system.settings.water_volume_indicator.invalid', { default: 'Please make a choice.' })}
/>
<Select
name="wind_speed_indicator"
value={$formData.wind_speed_indicator}
required={true}
options={units.wind_speed}
horizontal={true}
label={$_('system.settings.wind_speed_indicator.label', { default: 'Wind speed type' })}
help={$_('system.settings.wind_speed_indicator.help', {
default: 'Select the wind speed indicator. Only affects the current values.',
})}
invalid={$_('system.settings.wind_speed_indicator.invalid', { default: 'Please make a choice.' })}
/>
<Field
type="number"
name="power_price"
required={true}
min="0.0000"
step="0.0001"
horizontal={true}
label={$_('system.settings.power_price.label', { default: 'Power price' })}
help={$_('system.settings.power_price.help', { default: 'Enter the price in euro/dollar/pound per kWh.' })}
invalid={$_('system.settings.power_price.invalid', {
values: { value: 0 },
default: 'Please enter a minimum value of {value}.',
})}
/>
<Field
type="number"
name="water_price"
required={true}
min="0.0000"
step="0.0001"
horizontal={true}
label={$_('system.settings.water_price.label', { default: 'Water price' })}
help={$_('system.settings.water_price.help', {
default: 'Enter the price in euro/dollar/pound per L/Gallon.',
})}
invalid={$_('system.settings.water_price.invalid', {
values: { value: 0 },
default: 'Please enter a minimum value of {value}.',
})}
/>
</Card>
</div>
</div>
<div class="row">
<div class="col">
<Card {loading}>
<svelte:fragment slot="header">
<i class="fas fa-marker mr-2"></i>{$_('system.settings.header.gui', { default: 'GUI' })}
</svelte:fragment>
<svelte:fragment slot="tools">
<CardSettingsTools />
</svelte:fragment>
<Field
type="text"
name="title"
required={true}
horizontal={true}
label={$_('system.settings.title.label', { default: 'Title' })}
help={$_('system.settings.title.help', { default: 'Enter a custom title.' })}
invalid={$_('system.settings.title.invalid', { default: 'The title cannot be empty.' })}
/>
<FileUpload
name="profile_image"
value={$formData.profile_image}
horizontal={true}
accept="image/*"
label={$_('system.settings.profile_image.label', { default: 'Profile image' })}
help={$_('system.settings.profile_image.help', { default: 'Update a custom image used in the menu.' })}
invalid={$_('system.settings.profile_image.invalid', { default: 'Invalid profile image format.' })}
/>
<Field
type="number"
name="graph_smooth_value"
required={true}
min="0"
step="1"
horizontal={true}
label={$_('system.settings.graph_smooth_value.label', { default: 'Graph smoothing' })}
help={$_('system.settings.graph_smooth_value.help', {
default: 'Enter the amount of data points to average. 0 will disable this feature.',
})}
invalid={$_('system.settings.graph_smooth_value.invalid', {
values: { value: 0 },
default: 'Please enter a minimum value of {value}.',
})}
/>
<Select
name="dashboard_mode"
value={$formData.dashboard_mode}
required={false}
options={[
{
value: 0,
text: $_('system.settings.dashboard_mode.options.normal', { default: 'Graphs and enclosures' }),
},
{ value: 1, text: $_('system.settings.dashboard_mode.options.only_graphs', { default: 'Only graphs' }) },
{
value: 2,
text: $_('system.settings.dashboard_mode.options.only_enclosures', { default: 'Only enclosures' }),
},
]}
horizontal={true}
label={$_('system.settings.dashboard_mode.label', { default: 'Dashboard mode' })}
help={$_('system.settings.dashboard_mode.help', { default: 'Select how the dashboard looks like.' })}
invalid={$_('system.settings.dashboard_mode.invalid', { default: 'Please make a choice.' })}
/>
<Select
name="auto_dark_mode"
value={$formData.auto_dark_mode}
required={false}
options={[
{ value: 'off', text: $_('system.settings.auto_dark_mode.options.off', { default: 'No dark mode' }) },
{ value: 'on', text: $_('system.settings.auto_dark_mode.options.on', { default: 'Based on day/night' }) },
{
value: 'desktop',
text: $_('system.settings.auto_dark_mode.options.desktop', { default: 'Based on desktop' }),
},
{
value: 'always',
text: $_('system.settings.auto_dark_mode.options.always', { default: 'Always dark mode' }),
},
]}
horizontal={true}
label={$_('system.settings.auto_dark_mode.label', { default: 'Auto dark mode' })}
help={$_('system.settings.auto_dark_mode.help', { default: 'Set dark mode.' })}
invalid={$_('system.settings.auto_dark_mode.invalid', { default: 'Please make a choice.' })}
/>
<Switch
name="show_min_max_gauge"
value={$formData.show_min_max_gauge}
horizontal={true}
label={$_('system.settings.show_min_max_gauge.label', {
default: 'Show min and max values in gauge graphs',
})}
help={$_('system.settings.show_min_max_gauge.help', {
default: 'Show the minimum and maximum value on the gauges.',
})}
/>
<Switch
name="graph_limit_min_max"
value={$formData.graph_limit_min_max}
horizontal={true}
label={$_('system.settings.graph_limit_min_max.label', {
default: 'Limit graph values',
})}
help={$_('system.settings.graph_limit_min_max.help', {
default: 'Limit graph values to 20% of the alarm values.',
})}
/>
<Switch
name="all_gauges_on_single_page"
value={$formData.all_gauges_on_single_page}
horizontal={true}
label={$_('system.settings.all_gauges_on_single_page.label', { default: 'All gauges on a single page' })}
help={$_('system.settings.all_gauges_on_single_page.help', {
default: 'Add extra menu item for all sensors on a single page.',
})}
/>
</Card>
</div>
<div class="col">
<Card {loading}>
<svelte:fragment slot="header">
<i class="fas fa-cloud-download-alt mr-2"></i>{$_('system.settings.header.cloud', { default: 'Cloud' })}
</svelte:fragment>
<svelte:fragment slot="tools">
<CardSettingsTools />
</svelte:fragment>
<Field
type="text"
name="meross_cloud_username"
horizontal={true}
label={$_('system.settings.meross_cloud_username.label', { default: 'Meross username' })}
help={$_('system.settings.meross_cloud_username.help', {
default: 'Enter the username to login into Meross cloud.',
})}
/>
<Field
type="password"
name="meross_cloud_password"
horizontal={true}
label={$_('system.settings.meross_cloud_password.label', { default: 'Meross password' })}
help={$_('system.settings.meross_cloud_password.help', {
default: 'Enter the password to login into Meross cloud.',
})}
/>
</Card>
</div>
</div>
<div class="row pb-2">
<div class="col">
<div class="d-flex justify-content-center">
<button type="submit" class="btn btn-primary" disabled={loading || $isSubmitting}>
<span
class="spinner-border spinner-border-sm"
role="status"
aria-hidden="true"
class:d-none={!$isSubmitting}
></span>
{$_('modal.general.save', { default: 'Save' })}
</button>
</div>
</div>
</div>
</form>
</div>