-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
554 lines (462 loc) · 14.6 KB
/
renderer.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
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
const electron = require('electron')
const ipc = electron.ipcRenderer
const tabulator = require('tabulator-tables')
const constants = require('./constants.js')
var readButton
var writeButton
var resetModificationButton
var resetZoomButton
var exitButton
var allDotsCountLabel
var badDotsCountLabel
var goodDotsCountLabel
var pathChart
var pathChartController
var pathTable
var originalWeldingPathData = []
var modifiedWeldingPathData = []
var dataSpaceSeparator
var tableSpace
var chartSpace
var draggedDotIndex
window.onload = function() {
ipc.on('set-path-data', (event, arg) => {
originalWeldingPathData = arg;
refreshWeldingPathTable(originalWeldingPathData);
refreshPathChart(originalWeldingPathData);
refreshDotsCountLabels(originalWeldingPathData);
});
ipc.on('get-path-data', () => {
readModifiedWeldingPathData();
ipc.send('get-path-data-reply', modifiedWeldingPathData);
});
ipc.on('open-options-dialog', () => onOpenOptionsDialog());
ipc.on('open-error-dialog', (event, arg) => onOpenErrorDialog(arg));
ipc.on('open-write-success-dialog', () => onOpenWriteSuccessDialog());
readButton = document.getElementById('read-button');
readButton.addEventListener('click', readPathFromSimatic);
writeButton = document.getElementById('write-button');
writeButton.addEventListener('click', onWriteButtonClick);
resetModificationButton = document.getElementById('reset-modification-button');
resetModificationButton.addEventListener('click', () => {
openConfirmationDialog(
'Все изменения будут потеряны. Продолжить?',
resetModifiedPathData
);
});
resetZoomButton = document.getElementById('reset-zoom-button');
resetZoomButton.addEventListener('click', resetZoom);
exitButton = document.getElementById('exit-button');
exitButton.addEventListener('click', () => { window.close(); });
allDotsCountLabel = document.getElementById('all-dots-count-label');
allDotsCountLabel.textContent = '0';
badDotsCountLabel = document.getElementById('bad-dots-count-label');
badDotsCountLabel.textContent = '0';
goodDotsCountLabel = document.getElementById('good-dots-count-label');
goodDotsCountLabel.textContent = '0';
dataSpaceSeparator = document.getElementById('data-space-separator');
tableSpace = document.getElementById('table-space');
chartSpace = document.getElementById('chart-space');
pathChartController = $.jqplot('path-chart-controller', [[,]],
{
seriesColors: [
'#dd0000'
],
seriesDefaults: {
showMarker: true,
shadow: false,
lineWidth: 2.5,
markerOptions: {
show: false
},
fill: true,
fillAndStroke: true
},
cursor: {
show: true,
zoom: true,
tooltipLocation: 'sw'
},
axes: {
xaxis: {
pad: 0,
rendererOptions: {
forceTickAt0: true
}
}
},
grid: {
shadow: false,
background: '#bbeebb',
borderWidth: 0,
gridLineColor: '#77bb77'
},
gridPadding: {
top: 8,
bottom: 24,
left: 80,
right: 20
}
}
);
$.jqplot.config.enablePlugins = true;
pathChart = $.jqplot('path-chart', [[,]],
{
seriesColors: [
'#ffffff'
],
seriesDefaults: {
showMarker: true,
shadow: false,
lineWidth: 1,
markerOptions: {
shadow: false,
size: 7,
color: '#ffaa88'
},
},
cursor: {
show: true,
zoom: true,
tooltipLocation: 'sw'
},
axes: {
xaxis: {
pad: 0,
rendererOptions: {
forceTickAt0: true
}
}
},
grid: {
shadow: false
},
highlighter: {
sizeAdjust: 10,
tooltipLocation: 'n',
tooltipAxes: 'y',
tooltipFormatString: '%.3f',
useAxesFormatters: false,
},
grid: {
shadow: false,
borderWidth: 0,
background: 'black',
gridLineColor: '#555555',
},
gridPadding: {
top: 8,
bottom: 24,
left: 80,
right: 20
}
}
);
pathChart.series[0].plugins.draggable.constrainTo = 'y';
$.jqplot.Cursor.zoomProxy(pathChart, pathChartController);
$('#path-chart').bind('jqplotDragStart', (ev, seriesIndex, pointIndex) => { draggedDotIndex = pointIndex; });
$('#path-chart').bind('jqplotDragStop', () => { onDragStopPathChart(draggedDotIndex); });
pathTable = new tabulator("#path-table", {
autoResize: false,
height: "400px",
columns:[
{
title: "ИНДЕКС",
field: "index",
align: "right",
width: 72,
headerSort: false
},
{
title: "X,мм",
field: "x",
align: "right",
width: 68,
headerSort: false
},
{
title: "Y,мм",
field: "y",
align: "right",
width: 68,
formatter: function(cell, formatterParams, onRendered) {
return parseFloat(cell.getValue()).toFixed(3);
},
editor: "input",
headerSort: false
},
{
title: "СТАТУС",
field: "valid",
align: "center",
width: 72,
formatter:
function(cell, formatterParams, onRendered) {
return this.formatters['tickCross'](cell, formatterParams, onRendered);
},
editor: 'tickCross',
headerSort: false
},
],
dataEdited: onPathTableDataEdited,
});
window.addEventListener('resize', event => onResizeWindow(event));
onResizeWindow()
}
function readPathFromSimatic() {
ipc.on('read-path-reply', (event, arg) => {
originalWeldingPathData = arg;
refreshWeldingPathTable(originalWeldingPathData);
refreshPathChart(originalWeldingPathData);
refreshDotsCountLabels(originalWeldingPathData);
});
ipc.send('read-path', "");
}
function onWriteButtonClick() {
var globalVars = ipc.sendSync('get-global', [
'controllerIp',
'blockNumber',
'yArrayAddress',
'yStatusArrayAddress',
'numberOfDotsAddress'
]);
var idPrefix = 'write-confirmation-modal-dialog-';
var setVar = function(name, value, valuePrefix) {
var el = document.getElementById(idPrefix + name);
el.innerHTML =
'<b>' +
(value ? (valuePrefix ? valuePrefix : '') + value : '?') +
'</b>';
}
setVar('ip', globalVars.controllerIp);
setVar('block-number', globalVars.blockNumber, 'DB');
setVar('number-of-dots-address', globalVars.numberOfDotsAddress);
setVar('y-array-address', globalVars.yArrayAddress);
setVar('y-status-array-address', globalVars.yStatusArrayAddress);
$('#write-confirmation-modal-dialog').modal('show');
}
function writePathToSimatic() {
readModifiedWeldingPathData();
ipc.send('write-path', modifiedWeldingPathData);
}
function refreshWeldingPathTable(pathData) {
var index = 0;
var tableData = [];
for (item of pathData) {
var x = index * 5;
tableData.push(
{index:index, x:x, y:item[0], valid:item[1] != 0}
);
index++;
}
pathTable.replaceData(tableData);
}
function readModifiedWeldingPathData() {
var data = pathTable.getData()
modifiedWeldingPathData = [];
for (item of data) {
modifiedWeldingPathData.push([
item.y,
item.valid ? 1 : 0
]);
}
}
function resetModifiedPathData() {
var newModified = [];
for (var item of originalWeldingPathData) {
newModified.push( [].concat(item) );
}
modifiedWeldingPathData = originalWeldingPathData;
new Promise(() => {
refreshPathChart(originalWeldingPathData);
refreshWeldingPathTable(originalWeldingPathData);
refreshDotsCountLabels(originalWeldingPathData);
});
}
function refreshPathChart(weldingData) {
var seriesData = [];
var count = weldingData.length;
var kX = constants.kX;
for (var i = 0; i < count; i++) {
var item = weldingData[i];
var status = item[1];
if ( status ) {
seriesData.push([kX*i, item[0]]);
}
}
pathChart.series[0].data = seriesData;
pathChartController.series[0].data = seriesData;
new Promise(() => {
pathChart.replot( {resetAxes: true} );
});
new Promise(() => {
pathChartController.replot( {resetAxes: true} );
});
}
function onResizeWindow() {
var gap = chartSpace.getBoundingClientRect().left;
var pathTableOffsets = pathTable.element.getBoundingClientRect();
var pathTableW = pathTableOffsets.right - pathTableOffsets.left;
var tableLeft = window.innerWidth - pathTableW - gap;
var top = dataSpaceSeparator.getBoundingClientRect().bottom;
tableSpace.style.left = tableLeft + 'px';
tableSpace.style.top = top + 'px';
tableSpace.style.width = pathTableW + 'px';
var dataSpaceHeight = window.innerHeight - top - gap;
if ( dataSpaceHeight < 0 ) {
dataSpaceHeight = 0;
}
tableSpace.style.height = dataSpaceHeight + 'px';
var dotsCountTableContainer = document.getElementById('dots-count-table-container');
var dotsCountTableContainerOffsets = dotsCountTableContainer.getBoundingClientRect();
var pathTableTop = dotsCountTableContainerOffsets.bottom - dotsCountTableContainerOffsets.top + gap;
pathTable.element.style.top = pathTableTop + 'px';
pathTableOffsets = pathTable.element.getBoundingClientRect();
var pathTableHeight = dataSpaceHeight - pathTableTop;
if ( pathTableHeight < 0 ) {
pathTableHeight = 0;
}
pathTable.element.style.height = pathTableHeight + 'px';
dotsCountTableContainer.style.width = (pathTableOffsets.right - pathTableOffsets.left) + 'px';
chartSpace.style.height = dataSpaceHeight + 'px';
var chartSpaceW = tableLeft - 2*gap;
if ( chartSpaceW < 0 ) {
chartSpaceW = 0;
}
chartSpace.style.width = chartSpaceW + 'px';
var chartControllerOffsets = document.getElementById('path-chart-controller').getBoundingClientRect();
var chartW = chartSpaceW - 2;
if (chartW < 0) {
chartW = 0;
}
var chartH = dataSpaceHeight - (chartControllerOffsets.bottom - chartControllerOffsets.top) - gap;
if ( chartH < 0 ) {
chartH = 0;
}
var chartControllerTop = chartH + gap;
$('#path-chart-controller').width(chartW);
$('#path-chart-controller').css({top:chartControllerTop + 'px'});
$('#path-chart').width(chartW);
$('#path-chart').height(chartH);
pathChartController.replot();
pathChart.replot();
pathTable.redraw(true);
}
function resetZoom() {
pathChartController.resetZoom();
}
function onDragStopPathChart(dotChartIndex) {
var dot = pathChart.series[0].data[dotChartIndex];
pathChartController.series[0].data[dotChartIndex][1] = dot[1];
new Promise(() => {
pathChartController.replot();
});
var dotTableIndex = Math.round(dot[0]/constants.kX);
var tableData = pathTable.getData();
tableData[dotTableIndex].y = dot[1];
new Promise(() => {
pathTable.replaceData(tableData);
});
}
function onOpenDotsCountDialog() {
var input = document.getElementById('dots-count-modal-input');
readModifiedWeldingPathData();
input.value = modifiedWeldingPathData.length;
$('#dots-count-modal-dialog').modal('show');
}
function onDotsCountDialogOkClick() {
var input = document.getElementById('dots-count-modal-input');
var newSize = parseInt(input.value);
if ( !isNaN(newSize) && newSize >= 0 ) {
if ( newSize > constants.dotsCountMax ) {
newSize = constants.dotsCountMax;
}
if ( newSize != modifiedWeldingPathData.length ) {
allDotsCountLabel.textContent = newSize;
new Promise(() => {
resizeWeldingData(newSize);
});
}
}
$('#dots-count-modal-dialog').modal('hide');
}
function onPathTableDataEdited(data) {
readModifiedWeldingPathData();
refreshPathChart(modifiedWeldingPathData);
refreshDotsCountLabels(modifiedWeldingPathData);
}
function resizeWeldingData(newSize) {
if ( newSize < modifiedWeldingPathData.length ) {
while ( newSize < modifiedWeldingPathData.length ) {
modifiedWeldingPathData.pop();
}
}
else {
while ( modifiedWeldingPathData.length < newSize ) {
modifiedWeldingPathData.push([0.0, 1]);
}
}
refreshWeldingPathTable(modifiedWeldingPathData);
refreshPathChart(modifiedWeldingPathData);
refreshDotsCountLabels(modifiedWeldingPathData);
}
function refreshDotsCountLabels(data) {
allDotsCountLabel.textContent = data.length;
var goodDots = 0;
for (var item of data) {
if ( item[1] ) {
goodDots++;
}
}
goodDotsCountLabel.textContent = goodDots;
var badDots = data.length - goodDots;
badDotsCountLabel.textContent = badDots;
}
function onOpenOptionsDialog() {
var globalVars = ipc.sendSync('get-global', [
'controllerIp',
'blockNumber',
'yArrayAddress',
'yStatusArrayAddress',
'numberOfDotsAddress'
]);
var ipInput = document.getElementById('ip-input');
ipInput.value = globalVars.controllerIp;
var blockNumberInput = document.getElementById('block-number-input');
blockNumberInput.value = globalVars.blockNumber;
var yArrayAddressInput = document.getElementById('y-array-address-input');
yArrayAddressInput.value = globalVars.yArrayAddress;
var yStatusArrayAddressInput = document.getElementById('y-status-array-address-input');
yStatusArrayAddressInput.value = globalVars.yStatusArrayAddress;
var numberOfDotsAddressInput = document.getElementById('number-of-dots-address-input');
numberOfDotsAddressInput.value = globalVars.numberOfDotsAddress;
$('#options-modal-dialog').modal('show');
}
function onOptionsDialogOkClick() {
var ipInput = document.getElementById('ip-input');
var blockNumberInput = document.getElementById('block-number-input');
var yArrayAddressInput = document.getElementById('y-array-address-input');
var yStatusArrayAddressInput = document.getElementById('y-status-array-address-input');
var numberOfDotsAddressInput = document.getElementById('number-of-dots-address-input');
ipc.sendSync('set-global', {
controllerIp: ipInput.value,
blockNumber: blockNumberInput.value,
yArrayAddress: yArrayAddressInput.value,
yStatusArrayAddress: yStatusArrayAddressInput.value,
numberOfDotsAddress: numberOfDotsAddressInput.value,
});
$('#options-modal-dialog').modal('hide');
}
function onOpenErrorDialog(msg) {
var el = document.getElementById('error-modal-body');
el.innerHTML = msg;
$('#error-modal-dialog').modal('show');
}
function onOpenWriteSuccessDialog() {
$('#write-success-modal-dialog').modal('show');
}
function openConfirmationDialog(msg, func) {
document.getElementById('confirmation-modal-body').innerHTML = msg;
document.getElementById('confirmation-modal-dialog-yes-button').onclick = () => { func&&func(); };
$('#confirmation-modal-dialog').modal('show');
}