-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstationsettings.js
467 lines (425 loc) · 19 KB
/
stationsettings.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
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
// Last modified: 2025/02/17 10:54:18
let StashedStationId;
let accessMode;
$(document).ready(function () {
//let layout1 = '<table class="table table-hover"><tr><td id="left"></td><td id="right"></td></tr></table>';
$('form').alpaca({
dataSource: '/api/settings/stationdata.json',
optionsSource: '/json/StationOptions.json',
schemaSource: '/json/StationSchema.json',
ui: 'bootstrap',
view: 'bootstrap-edit-horizontal',
options: {
form: {
buttons: {
// don't use the Submit button because that is disabled on validation errors
validate: {
title: 'Save Settings',
click: function() {
this.refreshValidationState(true);
if (this.isValid(true)) {
let form = $('form').alpaca('get');
let stationIdObj = form.getControlByPath('general/stationtype');
let stationId = stationIdObj.getValue();
if (stationId == -1) {
alert('You have not selected a station type');
return;
}
if (stationId != StashedStationId) {
alert('You have changed the Station type, you must restart Cumulus MX');
StashedStationId = stationId;
}
let json = this.getValue();
$.ajax({
type: 'POST',
url: '/api/setsettings/updatestationconfig.json',
data: {json: JSON.stringify(json)},
dataType: 'text'
})
.done(function () {
alert('Settings updated');
})
.fail(function (jqXHR, textStatus) {
alert('Error: ' + jqXHR.status + '(' + textStatus + ') - ' + jqXHR.responseText);
});
} else {
let firstErr = $('form').find('.has-error:first')
let path = $(firstErr).attr('data-alpaca-field-path');
let msg = $(firstErr).children('.alpaca-message').text();
alert('Invalid value in the form: ' + path + msg);
if ($(firstErr).is(':visible')) {
let entry = $(firstErr).focus();
$(window).scrollTop($(entry).position().top);
}
}
},
styles: 'alpaca-form-button-submit'
}
}
},
fields: {
gw1000: {
fields: {
macaddress: {
validator: function(callback) {
let value = this.getValue();
// check for MAC address format
if (value != '' && !/^[a-fA-F0-9]{2}(:[a-fA-F0-9]{2}){5}$/.test(value)) {
callback({
status: false,
message: 'That is not a valid MAC address!'
});
return;
}
// all OK
callback({
status: true
});
}
}
}
},
ecowittapi: {
fields:{
mac: {
validator: function(callback) {
let value = this.getValue().trim();
if (value != '')
{
// check for IMEI format - 15 or 16 digits
if (Number.isInteger(value)) {
if (value.length == 15 || value.length == 16) {
callback({
status: true
});
} else {
callback({
status: false,
message: 'That is not a valid IMEI!'
});
}
return;
}
// check for MAC address format
if (!/^[a-fA-F0-9]{2}(:[a-fA-F0-9]{2}){5}$/.test(value)) {
callback({
status: false,
message: 'That is not a valid MAC address!'
});
return;
}
}
// all OK
callback({
status: true
});
}
},
applicationkey: {
validator: function(callback) {
let value = this.getValue();
if (value != '' && !/^[A-F0-9]{30,35}$/.test(value)) {
callback({
status: false,
message: 'That is not a valid Application Key!'
});
return;
}
callback({
status: true
});
}
},
userkey: {
validator: function(callback) {
let value = this.getValue();
if (value != '' && !/^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$/.test(value)) {
callback({
status: false,
message: 'That is not a valid API Key!'
});
return;
}
callback({
status: true
});
}
}
}
}
}
},
postRender: function (form) {
// Change in accessibility is enabled
let accessObj = form.childrenByPropertyId['accessible'];
onAccessChange(null, accessObj.getValue());
accessMode = accessObj.getValue();
if (!accessMode) {
setCollapsed(); // sets the class and aria attribute missing on first load by Alpaca
}
// Trigger changes is the accessibility mode is changed
accessObj.on('change', function() {onAccessChange(this)});
let manufacturerObj = form.getControlByPath('general/manufacturer');
// On changing the manufacturer, repopulate the station type list
manufacturerObj.on('change', function () {
let form = $('form').alpaca('get');
let stationIdObj = form.getControlByPath('general/stationtype')
stationIdObj.refresh();
stationIdObj.setValue(-1);
});
let stationIdObj = form.getControlByPath('general/stationtype');
let currId = stationIdObj.getValue();
stationIdObj.options.dataSource = setStationOptions;
stationIdObj.refresh();
stationIdObj.setValue(+currId);
// On changing the station type, propagate down to sub-sections
stationIdObj.on('change', function () {
let form = $('form').alpaca('get');
let manuObj = $('#' + form.getControlByPath('general/manufacturer').id);
let manu = manuObj[0].options[manuObj[0].selectedIndex].innerText;
let stationid = this.getValue();
form.childrenByPropertyId['stationid'].setValue(stationid);
form.getControlByPath('Options/stationid').setValue(stationid);
form.getControlByPath('daviswll/stationid').setValue(stationid);
form.getControlByPath('daviswll/advanced/stationid').setValue(stationid);
form.getControlByPath('ecowittmaps/stationid').setValue(stationid);
form.getControlByPath('ecowittapi/stationid').setValue(stationid);
form.getControlByPath('general/stationmodel').setValue(this.selectOptions.reduce((a, o) => (o.value == stationid && a.push(manu + ' ' + o.text), a), []));
// set the settings name for WLL/Davis cloud
setDavisStationTitle(form.getControlByPath('daviswll'), stationid);
});
// On changing the Davis VP connection type, propagate down to advanced settings
form.getControlByPath('davisvp2/davisconn/conntype').on('change', function () {
let form = $('form').alpaca('get');
let conntype = this.getValue();
form.getControlByPath('davisvp2/advanced/conntype').setValue(conntype);
form.getControlByPath('davisvp2/advanced/conntype').refresh();
});
// Set the initial value of the sub-section station ids
let stationid = form.childrenByPropertyId['stationid'].getValue();
form.getControlByPath('Options/stationid').setValue(stationid);
form.getControlByPath('daviswll/stationid').setValue(stationid);
form.getControlByPath('daviswll/advanced/stationid').setValue(stationid);
form.getControlByPath('ecowittmaps/stationid').setValue(stationid);
form.getControlByPath('ecowittapi/stationid').setValue(stationid);
// Keep a record of the last value
StashedStationId = stationid;
// On changing the JSON Station connection type, propgate to advanced settings
let connType = form.getControlByPath('jsonstation/conntype');
connType.on('change', function () {
let form = $('form').alpaca('get');
let type = this.getValue();
form.getControlByPath('jsonstation/advanced/conntype').setValue(+type);
form.getControlByPath('jsonstation/advanced/conntype').refresh();
});
// Set the initial value of JSON Station connection type in advanced settings
form.getControlByPath('jsonstation/advanced/conntype').setValue(+form.getControlByPath('jsonstation/conntype').getValue());
// Set the initial title of Davis WLL/Cloud
setDavisStationTitle(form.getControlByPath('daviswll'), stationid);
// Set the initial value of Davis advanced conntype
let conntype = form.getControlByPath('davisvp2/davisconn/conntype').getValue();
form.getControlByPath('davisvp2/advanced/conntype').setValue(+conntype);
// Some ecowit api control
let ecowittapi = form.getControlByPath('ecowittapi');
let sdcard = form.getControlByPath('ecowitthttpapi/usesdcard');
// Set the initial value of the ecowitt api
if (stationid == 22 && sdcard.getValue()) {
ecowittapi.hide();
}
// set the ecowitt api to listen to the ecowitt http local api SD card setting
ecowittapi.subscribe(sdcard, function(val) {
if (val) {
this.hide();
} else {
this.show();
}
});
ecowittapi.subscribe(stationid, function(val) {
if (val != 22) {
this.show();
} else {
let form = $('form').alpaca('get');
let sdcard = form.getControlByPath('ecowitthttpapi/usesdcard').getValue();
if (sdcard) {
this.hide();
} else {
this.show();
}
}
});
}
});
});
function setStationOptions(callback) {
let form = $('form').alpaca('get');
if (form === null) {
//return '{}';
callback({'Select Station Type...': -1});
}
let manufacturer = parseInt(form.getControlByPath('general/manufacturer').getValue(), 10);
if (isNaN(manufacturer)) {
manufacturer = -1;
}
switch (manufacturer) {
case 0:
callback(davisStations);
break;
case 1:
callback(oregonStations);
break;
case 2:
callback(ewStations);
break;
case 3:
callback(lacrosseStations);
break;
case 4:
callback(oregonUsbStations);
break;
case 5:
callback(instrometStations);
break;
case 6:
callback(ecowittStations);
break;
case 7:
callback(httpStations);
break;
case 8:
callback(ambientStations);
break;
case 9:
callback(weatherflowStations);
break;
case 10:
callback(simStations);
break;
case 11:
callback(jsonStations);
break;
default:
callback({'Select Station Type...': -1});
break;
}
let cnt = form.getControlByPath('general/stationtype');
cnt.setValue(-1);
}
function addButtons() {
$('form legend').each(function () {
let span = $('span:first',this);
if (span.length === 0)
return;
let butt = $('<button type="button" data-toggle="collapse" data-target="' +
$(span).attr('data-target') +
'" role="treeitem" aria-expanded="false" class="collapsed">' +
$(span).text() +
'</button>');
$(span).remove();
$(this).prepend(butt);
});
}
function removeButtons() {
$('form legend').each(function () {
let butt = $('button:first',this);
if (butt.length === 0)
return;
let span = $('<span data-toggle="collapse" data-target="' +
$(butt).attr('data-target') +
'" role="treeitem" aria-expanded="false" class="collapsed">' +
$(butt).text() +
'</span>');
$(butt).remove();
$(this).prepend(span);
});
}
function setCollapsed() {
$('form div.alpaca-container.collapse').each(function () {
let span = $(this).siblings('legend:first').children('span:first');
if ($(this).hasClass('in')) {
span.attr('role', 'treeitem');
span.attr('aria-expanded', true);
} else {
span.attr('role', 'treeitem');
span.attr('aria-expanded', false);
span.addClass('collapsed')
}
});
}
function getCSSRule(search) {
for (let sheet of document.styleSheets) {
let rules = sheet.cssRules || sheet.rules;
for (let rule of rules) {
if (rule.selectorText && rule.selectorText.lastIndexOf(search) === 0) {
return rule;
}
}
}
return null;
}
function onAccessChange(that, val) {
let mode = val == null ? that.getValue() : val;
if (mode == accessMode) {
return;
}
let expandable = getCSSRule('.alpaca-field > legend > .collapsed::before');
let expanded = getCSSRule('.alpaca-field > legend > span::before');
accessMode = mode;
if (mode) {
expandable.style.setProperty('display','none');
expanded.style.setProperty('display','none');
addButtons();
} else {
expandable.style.removeProperty('display');
expanded.style.removeProperty('display');
removeButtons();
}
}
function setDavisStationTitle(that, val) {
if (val == 11) { // WLL
that.field[0].firstElementChild.firstElementChild.innerText = ' Davis WeatherLink Live';
//that.refresh();
} else if (val == 19) { // Davis cloud WLL
that.field[0].firstElementChild.firstElementChild.innerText = ' Davis WeatherLink Cloud (WLL/WLC)';
//that.refresh();
} else if (val == 20) { // Davis cloud VP2
that.field[0].firstElementChild.firstElementChild.innerText = ' Davis WeatherLink Cloud (VP2)';
//that.refresh();
}
}
let allStations = {
'Select Station Type': -1,
'PWS Simulator': 17,
'Vantage Pro': 0,
'Vantage Pro2/Vue': 1,
'WeatherLink Live': 11,
'WeatherLink Cloud (WLL/WLC)': 19,
'WeatherLink Cloud (VP2)': 20,
'Binary Local API (Legacy)': 12,
'Cloud': 18,
'HTTP Sender': 14,
'HTTP (Wunderground)': 13,
'HTTP (Ambient)': 15,
'Tempest': 16,
'JSON Data Input': 21,
'Fine Offset': 5,
'Fine Offset with Solar Sensor': 7,
'EasyWeather': 4,
'Instromet': 10,
'LaCrosse WS2300': 6,
'WMR100': 8,
'WMR200': 9,
'WM-918': 3,
'WMR-928': 2
};
let davisStations = {'Select Station Type...': -1, 'Vantage Pro': 0, 'Vantage Pro 2': 1, 'WeatherLink Live': 11, 'WeatherLink Cloud (WLL/WLC)': 19,'WeatherLink Cloud (VP2/Vue)': 20};
let ewStations = {'Select Station Type...': -1, 'FineOffset': 5, 'FineOffset with Solar Sensor': 7, 'EasyWeather File': 4};
let oregonStations = {'Select Station Type...': -1, 'WMR-928': 2, 'WMR-918': 3};
let lacrosseStations = {'WS2300': 6};
let oregonUsbStations = {'Select Station Type...': -1 , 'WMR200': 9, 'WMR100': 8};
let instrometStations = {'Instromet': 10};
let ecowittStations = {'Select Station Type...': -1, 'HTTP Local API': 22, 'Binary Local API (Legacy)': 12, 'HTTP Custom Sender': 14, 'Ecowitt.net Cloud': 18};
let httpStations = {'HTTP Sender (WUnderground format)': 13};
let ambientStations = {'HTTP Sender (Ambient format)': 15};
let weatherflowStations = {'Tempest': 16};
let simStations = {'Simulated Station': 17};
let jsonStations = {'JSON data input Station': 21};