-
Notifications
You must be signed in to change notification settings - Fork 0
/
led-view.js
926 lines (885 loc) · 30.6 KB
/
led-view.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
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
'use strict';
//
// Useful links, etc.
// https://en.wikipedia.org/wiki/Union_Pacific_/_Northwest_Line - 63 miles (Harvard), 50 miles (McHenry)
// https://en.wikipedia.org/wiki/Union_Pacific_/_West_Line - 44 miles
// https://en.wikipedia.org/wiki/Milwaukee_District_/_West_Line - 40 miles
// https://en.wikipedia.org/wiki/Union_Pacific_/_North_Line - 78 miles?
// https://en.wikipedia.org/wiki/Milwaukee_District_/_North_Line - 50 miles
// https://en.wikipedia.org/wiki/North_Central_Service - 56 miles
// dp3rs6 - geohash of palatine train station
//
// TODO: Log errors when the number of trains ≠ the number of LEDs
// TODO: Previous LEDs and current LEDs
// TODO: Rows of segments – use cursor to update
//
var _ = require('lodash');
var async = require('async');
var bunyan = require('bunyan');
var geohash = require('latlon-geohash');
var geolib = require('geolib');
var readLine = require('readline');
var fs = require('fs');
var ttys = require('ttys');
var request = require('request')
var log = bunyan.createLogger({ name : "led view", level : "error" });
var sharedConstants = require(process.cwd() + '/constants');
let GEOHASH_LENGTH = sharedConstants.GEOHASH_LENGTH;
let linesNamesArray = sharedConstants.linesNamesArray;
// This comes from calculations done outside this program or the spreadsheet
const HARVARD_LED = 182;
const MCHENRY_LED = 21;
const SPUR_JUNCTION_POSITION = 37;
const ELBURN_LED = 127;
// These will be calculated in the init routine
var PALATINE_LED = 0;
var FOX_RIVER_LED = 0;
var WEST_CHICAGO_LED = 0;
var COLLEGE_AVE_LED = 0;
var SCHAUMBURG_LED = 0;
var BARTLET_LED = 0;
var BIG_TIMBER_LED = 0;
// The linesObject object contains all of the LED geohashes for each segment
// of each spur for each line. The format is as follows:
//
// {
// <line_name_1> : [ \
// [ \ |-- line array
// { | | (array of
// segment : <seg_index> |-- spur | spurs)
// ledArray : [ \ | array |
// { |-- led | (array of |
// ledLat : <lat_degrees>, | array | segments) |
// ledLon : <lon_degrees>, | (array of | |
// geohash : <led_geohash> | leds) | |
// geohashNeighbors : { | | |
// n: <neighbor_geohash>, | | |
// ne: ... | | |
// e: ... | | |
// se: ... | | |
// s: ... | | |
// sw: ... | | |
// w: ... | | |
// nw: ... | | |
// } | | |
// }, | | |
// { | | |
// <second_led> | | |
// }, | | |
// ... | | |
// ] / | |
// }, | |
// { | |
// <second_segment_object_properties> | |
// }, | |
// ... | |
// ], / |
// [ |
// <second_spur_array_elements> |
// ], |
// ... |
// [ |
// <nth_spur_array_elements> |
// ] |
// ], /
// <line_name_2> : [
// ...
// ],
// ...
// <line_name_n> : [
// ...
// ]
// }
//
// This structure is an object containing an array for each line, accessed via
// the line name, which is stored in the linesNamesArray array. Each of these
// arrays is made up of an array for each spur that makes up the line. In most cases,
// there is only a single "main" spur for any given line, and in these cases,
// the line array will contain only a single spur array.
//
// Each spur array is an array of segment objects that correspond to the KML
// line segments that were used to create the line's data. Each segment object
// contains a "segment" attribute containing the segment's index and an "ledArray"
// that contains objects describing each LED in the segment.
//
// The LED objects contain an "ledLat" (latitude), "ledLon" (longitude), and
// a "geohash" attribute.
//
var linesObject = {};
// ledsToLight is organized by line, as is the linesObject. It contains an array
// for each line where each element contains a spur index, a segment index, and
// an LED index. In addition, it contains a spurLed property, which is the
// absolute LED index in a spur, which is likely to be a single LED strand.
//
// {
// <line_name_1> : [
// {
// spur : <spur_index>,
// segment : <segment_index>,
// segmentLed : <led_index>,
// spurLed : <spur_led_index>
// },
// <second_led_object>,
// ...
// ],
// <line_name_2> : ...
// ...
// }
//
var ledsToLight = {};
////////////////////////////////////////////////////////////////////////////////
// PREPARE LINES DATA
//
function init(done) {
log.debug("populateLineObjects()");
var linePopulateFunctions = [];
linesNamesArray.forEach((lineName, index, array) => {
linePopulateFunctions.push(_.partial(getLineData, lineName, _));
});
async.series(
linePopulateFunctions,
(error, results) => {
if (error) {
done(error, null);
}
else {
// results[0] is the results from populateLinesObject. results[1] is
// the results of startFetching
var linesData = results;
linesData.forEach((dataForLine, index, array) => {
linesObject[dataForLine.line] = dataForLine.data;
});
initStationPositions();
done(null);
}
}
);
}
function initStationPositions() {
var palatine = {
latitude: 42.113,
longitude: -88.049,
geohash: geohash.encode(42.113, -88.049, GEOHASH_LENGTH)
};
var foxRiver = {
latitude: 42.198,
longitude: -88.219,
geohash: geohash.encode(42.198, -88.219, GEOHASH_LENGTH)
};
var collegeAve = {
latitude: 41.868,
longitude: -88.090,
geohash: geohash.encode(41.868, -88.090, GEOHASH_LENGTH)
};
var westChicago = {
latitude: 41.881,
longitude: -88.199,
geohash: geohash.encode(41.881, -88.199, GEOHASH_LENGTH)
};
var schaumburg = {
latitude: 41.989,
longitude: -88.118,
geohash: geohash.encode(41.989, -88.118, GEOHASH_LENGTH)
}
var bartlet = {
latitude: 41.992,
longitude: -88.183,
geohash: geohash.encode(41.992, -88.183, GEOHASH_LENGTH)
}
var bigTimber = {
latitude: 42.059,
longitude: -88.327,
geohash: geohash.encode(42.059, -88.327, GEOHASH_LENGTH)
}
PALATINE_LED = matchLedWithTrain('UP-NW', palatine).spurLed;
FOX_RIVER_LED = matchLedWithTrain('UP-NW', foxRiver).spurLed;
COLLEGE_AVE_LED = matchLedWithTrain('UP-W', collegeAve).spurLed;
WEST_CHICAGO_LED = matchLedWithTrain('UP-W', westChicago).spurLed;
SCHAUMBURG_LED = matchLedWithTrain('MD-W', schaumburg).spurLed;
BARTLET_LED = matchLedWithTrain('MD-W', bartlet).spurLed;
BIG_TIMBER_LED = matchLedWithTrain('MD-W', bigTimber).spurLed;
}
// TODO: functionalize
function getLineData(name, done) {
var lineReader = readLine.createInterface({
input: fs.createReadStream('RouteFiles/LineData-' + name + '.csv')
});
// The spursArray holds the data from the spreadsheet CSV file
var spursArray = [];
var currentSpursSubArray = -1;
// The spursLEDArray holds the calculated lat/lon and geohash for each LED for
// each spur for each line
var spursLEDArray = [];
lineReader.on('line', (line) => {
var lineArray = line.split(',');
if (lineArray[0] != "") {
// console.log("Good Line - " + JSON.stringify(lineArray));
var index = parseInt(lineArray[0], 10);
if (index == 0) {
currentSpursSubArray++;
spursArray[currentSpursSubArray] = [];
}
var lineObject = {};
lineObject.lon = parseFloat(lineArray[1], 10);
lineObject.lat = parseFloat(lineArray[2], 10);
lineObject.numLEDs = parseInt(lineArray[10], 10);
lineObject.dlonOffset = parseFloat(lineArray[14], 10);
lineObject.dlatOffset = parseFloat(lineArray[15], 10);
lineObject.dlonLED = parseFloat(lineArray[16], 10);
lineObject.dlatLED = parseFloat(lineArray[17], 10);
lineObject.segment = parseFloat(lineArray[18], 10);
spursArray[currentSpursSubArray][index] = lineObject;
}
});
lineReader.on('close', () => {
// console.log("*************************************************************");
spursArray.forEach((segArray, spursIndex, spursArray) => {
var segLEDArray = [];
// console.log("SPUR - " + spursIndex);
segArray.forEach((segment, segIndex, segArray) => {
if (segIndex > 0) {
segLEDArray[segIndex-1] = {};
var ledArray = [];
for (var ledIndex = 0; ledIndex < segment.numLEDs; ledIndex++) {
var ledObject = {};
var ledLon, ledLat;
if (ledIndex == 0) {
ledLon = segArray[segIndex-1].lon + segment.dlonOffset;
ledLat = segArray[segIndex-1].lat + segment.dlatOffset;
}
else {
ledLon = ledArray[ledIndex-1].ledLon + segment.dlonLED;
ledLat = ledArray[ledIndex-1].ledLat + segment.dlatLED;
}
ledObject.ledLon = ledLon;
ledObject.ledLat = ledLat;
ledObject.geohash = geohash.encode(ledLat, ledLon, GEOHASH_LENGTH);
ledObject.geohashNeighbors = geohash.neighbours(ledObject.geohash);
ledArray.push(ledObject);
// console.log("\t\tLED - " + ledIndex + " : " + JSON.stringify(ledObject));
}
// spursArray[spursIndex][segIndex].ledArray = ledArray;
segLEDArray[segIndex-1].ledArray = ledArray;
segLEDArray[segIndex-1].segment = segment.segment;
}
spursLEDArray[spursIndex] = segLEDArray;
});
});
// console.log("******* spursLEDArray");
// console.log(JSON.stringify(spursLEDArray, null, 4));
// process.exit();
done(null, { line : name, data : spursLEDArray });
});
}
function textDisplayUPNW(leds) {
var mainLedArray = [];
var spurLedArray = [];
var mainLedPositions = [];
var spurLedPositions = [];
var mainLedDirections = [];
var spurLedDirections = [];
leds.forEach((ledObject) => {
if (ledObject != null && ledObject != undefined) {
if (ledObject.spur == 0) {
mainLedPositions.push(ledObject.spurLed);
mainLedDirections.push(ledObject.direction);
}
if (ledObject.spur == 1) {
spurLedPositions.push(ledObject.spurLed);
spurLedDirections.push(ledObject.direction);
}
}
});
// Push in the main line LEDs
for (var i = HARVARD_LED; i >= 0 ; i--) {
// Push train first, so it will take precidence
var indexInArray = _.indexOf(mainLedPositions, i);
if (indexInArray >= 0) {
if (mainLedDirections[indexInArray] == 'inbound') {
mainLedArray.push('>');
}
else if (mainLedDirections[indexInArray] == 'outbound') {
mainLedArray.push('<');
}
else {
mainLedArray.push('%');
}
}
else if (i == 0) {
mainLedArray.push('O');
}
else if (i == PALATINE_LED) {
mainLedArray.push('P');
}
else if (i == HARVARD_LED) {
mainLedArray.push('H');
}
else if (i == FOX_RIVER_LED) {
mainLedArray.push('F');
}
else {
mainLedArray.push('=');
}
}
// Push in the offset for the spur junction point
var upnwString = 'UP-NW';
spurLedArray.push(upnwString);
for (var i = 0; i < SPUR_JUNCTION_POSITION-upnwString.length; i++) {
spurLedArray.push(' ');
}
// Push in the spur LEDs (original 0.5" LED spacing was 22 LEDs (.818))
for (var i = MCHENRY_LED; i >= 0; i--) {
var indexInArray = _.indexOf(spurLedPositions, i);
if (indexInArray >= 0) {
if (spurLedDirections[indexInArray] == 'inbound') {
spurLedArray.push('>');
}
else if (spurLedDirections[indexInArray] == 'outbound') {
spurLedArray.push('<');
}
else {
spurLedArray.push('%');
}
}
else if (i == MCHENRY_LED) {
spurLedArray.push('M');
}
// The first led of this spur never "lit up", so I'm removing it
else if (i == 0) {
spurLedArray.push(' ');
}
else {
spurLedArray.push('=');
}
}
console.log(spurLedArray.join(''));
console.log(mainLedArray.join(''));
}
function textDisplayUPW(leds) {
console.log('UP-W');
var mainLedArray = [];
var mainLedPositions = [];
var mainLedDirections = [];
leds.forEach((ledObject) => {
if (ledObject != null && ledObject != undefined) {
mainLedPositions.push(ledObject.spurLed);
mainLedDirections.push(ledObject.direction);
}
});
// Push in the main line LEDs
for (var i = 0; i < HARVARD_LED-ELBURN_LED; i++) {
mainLedArray.push(' ');
}
for (var i = ELBURN_LED; i >= 0 ; i--) {
// Push train first, so it will take precidence
var indexInArray = _.indexOf(mainLedPositions, i);
if (indexInArray >= 0) {
if (mainLedDirections[indexInArray] == 'inbound') {
mainLedArray.push('>');
}
else if (mainLedDirections[indexInArray] == 'outbound') {
mainLedArray.push('<');
}
else {
mainLedArray.push('%');
}
}
else if (i == 0) {
mainLedArray.push('O');
}
else if (i == ELBURN_LED) {
mainLedArray.push('E');
}
else if (i == COLLEGE_AVE_LED) {
mainLedArray.push('C');
}
else if (i == WEST_CHICAGO_LED) {
mainLedArray.push('W');
}
else {
mainLedArray.push('=');
}
}
console.log(mainLedArray.join(''));
}
function textDisplayMDW(leds) {
console.log('MD-W');
var mainLedArray = [];
var mainLedPositions = [];
var mainLedDirections = [];
leds.forEach((ledObject) => {
if (ledObject != null && ledObject != undefined) {
mainLedPositions.push(ledObject.spurLed);
mainLedDirections.push(ledObject.direction);
}
});
// Push in the main line LEDs
for (var i = 0; i < HARVARD_LED-BIG_TIMBER_LED; i++) {
mainLedArray.push(' ');
}
for (var i = BIG_TIMBER_LED; i >= 0 ; i--) {
// Push train first, so it will take precidence
var indexInArray = _.indexOf(mainLedPositions, i);
if (indexInArray >= 0) {
if (mainLedDirections[indexInArray] == 'inbound') {
mainLedArray.push('>');
}
else if (mainLedDirections[indexInArray] == 'outbound') {
mainLedArray.push('<');
}
else {
mainLedArray.push('%');
}
}
else if (i == 0) {
mainLedArray.push('O');
}
else if (i == BIG_TIMBER_LED) {
mainLedArray.push('B');
}
else if (i == BARTLET_LED) {
mainLedArray.push('B');
}
else if (i == SCHAUMBURG_LED) {
mainLedArray.push('S');
}
else {
mainLedArray.push('=');
}
}
console.log(mainLedArray.join(''));
}
// These constants represent the number of displayable locations for each line.
// Some of the lines actually contain more or less than the number of displayable
// locations, so the data will be truncated or the last few LEDs will not be lit
// in some cases.
// For the prototype, the actual number of LEDs is half the number of displayable
// locations.
const UPNW_DISP_LOCS = 120;
const UPW_DISP_LOCS = 120;
// const UPW_MAX_INDEX = UPW_DISP_LOCS - 1;
const MDW_DISP_LOCS = 120;
const MDW_MAX_INDEX = MDW_DISP_LOCS - 1;
function protoDisplayAll(leds) {
var ledLightArray = [];
// The Ogilve LEDs are all at the ends of the strips
var upnwOgilve = 0;
var upwOgilve = 119;
var mdwOgilve = 120;
// The other stations positions are calculated based on their LED number and
// the length of the strips
// Strip 1, UP-NW
var palatine = Math.floor(PALATINE_LED/2);
var foxRiver = Math.floor(FOX_RIVER_LED/2);
// Strip 2, MD-W
var schaumburg = Math.floor(((MDW_MAX_INDEX - SCHAUMBURG_LED) + UPNW_DISP_LOCS)/2);
var bartlet = Math.floor(((MDW_MAX_INDEX - BARTLET_LED) + UPNW_DISP_LOCS)/2)
var bigTimber = Math.floor(((MDW_MAX_INDEX - BIG_TIMBER_LED) + UPNW_DISP_LOCS)/2);
// Strip 3, UP-W
var collegeAve = Math.floor((COLLEGE_AVE_LED + UPNW_DISP_LOCS + MDW_DISP_LOCS)/2);
var westChicago = Math.floor((WEST_CHICAGO_LED + UPNW_DISP_LOCS + MDW_DISP_LOCS)/2);
var elburn = Math.floor((ELBURN_LED + UPNW_DISP_LOCS + MDW_DISP_LOCS)/2);
// Add Ogilve (thrice) and stations as blue or yellow LEDs
var stationColor = 'DarkBlue';
// var stationColor = 'DarkYellow';
var postBodyArray = [
// UP-NW stations and ogilve
{position:upnwOgilve, color:stationColor},
{position:palatine, color:stationColor},
{position:foxRiver, color:stationColor},
// MD-W stations and ogilve
{position:mdwOgilve, color:stationColor},
{position:schaumburg, color:stationColor},
{position:bartlet, color:stationColor},
{position:bigTimber, color:stationColor},
// UP-W stations and ogilve
{position:upwOgilve, color:stationColor},
{position:collegeAve, color:stationColor},
{position:westChicago, color:stationColor}
];
// Strip 1
leds['UP-NW'].forEach((ledObject) => {
if (ledObject != null && ledObject != undefined) {
// FIXME: The UP-NW prototype LED strip is 60 LEDs long. This means
// that LED positions >= 120 will not be displayed once the
// 2:1 poistion to LED mapping is done below. LEDs for positions
// 120 and higher will be used for the UP-W line.
if (ledObject.spur == 0 && ledObject.spurLed < UPNW_DISP_LOCS) {
ledLightArray.push({ledIndex:ledObject.spurLed, direction:ledObject.direction});
}
// TODO: This needs to be added back before any full display, but
// the offset is unknown (based on how UP-W fits into the whole
// continuous string of LEDs)
// If on the McHenry spur, add 220 in order to only need a single
// continuous strand.
// if (ledObject.spur == 1) {
// ledLightArray.push(ledObject.spurLed + 220);
// }
}
});
// Strip 2
// FIXME: Since the UP-NW prototype LED strip is 60 LEDs long, and the MD-W
// LEDs will be attached directly to the end of the UP-NW strip, all
// positions >= 120 (divide by two for prototype LED positions) are
// MD-W positions. So, the MD-W is offset by 120. In addition, the
// positions are "reversed", since the LED strip in our physical layout
// wraps around in a Z-shape. In other words, the farther-out trains
// on the MD-W line are in lower numbered positions, which is the
// opposite to what the UP-NW situation is. For this reason, all
// positions will be given a negative value and offset from 119.
leds['MD-W'].forEach((ledObject) => {
if (ledObject != null && ledObject != undefined) {
if (ledObject.spurLed < MDW_DISP_LOCS) {
ledLightArray.push({ledIndex:(MDW_MAX_INDEX - ledObject.spurLed) + UPNW_DISP_LOCS, direction:ledObject.direction});
}
}
});
// leds['UP-W'].forEach((ledObject) => {
// if (ledObject != null && ledObject != undefined) {
// if (ledObject.spurLed < UPW_DISP_LOCS) {
// ledLightArray.push((UPW_MAX_INDEX - ledObject.spurLed) + UPNW_DISP_LOCS);
// }
// }
// });
// Strip 3
// The UP-W trains are displayed on the "last" strip, which is attached to
// the end of the MD-W strip. These train positions are in-order, in that
// the higher numbered positions are farther out (same as UP-NW). Since
// this is the furthest strip from the start of the chain, a two-strip
// offset (240) must be added.
leds['UP-W'].forEach((ledObject) => {
if (ledObject != null && ledObject != undefined) {
if (ledObject.spurLed < UPW_DISP_LOCS) {
ledLightArray.push({ledIndex:ledObject.spurLed + UPNW_DISP_LOCS + MDW_DISP_LOCS, direction:ledObject.direction});
}
}
});
// leds['MD-W'].forEach((ledObject) => {
// if (ledObject != null && ledObject != undefined) {
// if (ledObject.spurLed < MDW_DISP_LOCS) {
// ledLightArray.push(ledObject.spurLed + UPNW_DISP_LOCS + UPW_DISP_LOCS);
// }
// }
// });
// Squeeze the array in half for the protytype strips
var newPosition, newColor, newDirection;
var processedPositions = [];
ledLightArray.forEach((ledObject) => {
newPosition = Math.floor(ledObject.ledIndex/2);
processedPositions.push(newPosition);
// If there are two instances of this position, then two trains are passing
// each other and the LED should be yellow
if (_.indexOf(processedPositions, newPosition) != _.lastIndexOf(processedPositions, newPosition)) {
newColor = "yellow";
// If they are passing in a station, paint it white
if (newPosition == upnwOgilve ||
newPosition == palatine ||
newPosition == foxRiver ||
newPosition == upwOgilve ||
newPosition == collegeAve ||
newPosition == westChicago ||
newPosition == mdwOgilve ||
newPosition == schaumburg ||
newPosition == bartlet ||
newPosition == bigTimber)
{
newColor = "white";
}
}
// ogilve and palatine get lit a mixed color if a train is present, as does fox river
else if (newPosition == upnwOgilve || newPosition == palatine || newPosition == foxRiver) {
if (ledObject.direction == 'outbound') {
newColor = "SpringGreen";
}
else if (ledObject.direction == 'inbound') {
newColor = "SpringViolet"; // SpringViolet is not in bibliopixel palate, so I check for it directly in the python server
}
else {
newColor = "Orange";
log.error('UP-NW station color assignment - direction value not recognized - %s', ledObject.direction);
}
}
// ogilve for the UP-W line gets lit as a mixed color, as do the stations
else if (newPosition == upwOgilve || newPosition == collegeAve || newPosition == westChicago) {
if (ledObject.direction == 'outbound') {
newColor = "SpringGreen";
}
else if (ledObject.direction == 'inbound') {
newColor = "SpringViolet"; // SpringViolet is not in bibliopixel palate, so I check for it directly in the python server
}
else {
newColor = "Orange";
log.error('UP-w station color assignment - direction value not recognized - %s', ledObject.direction);
}
}
// same for MD-W
else if (newPosition == mdwOgilve || newPosition == schaumburg || newPosition == bartlet || newPosition == bigTimber) {
if (ledObject.direction == 'outbound') {
newColor = "SpringGreen";
}
else if (ledObject.direction == 'inbound') {
newColor = "SpringViolet"; // SpringViolet is not in bibliopixel palate, so I check for it directly in the python server
}
else {
newColor = "Orange";
log.error('MD-W station color assignment - direction value not recognized - %s', ledObject.direction);
}
}
// all trains at LED positions 120 and above are UP-W trains, and are painted UP-W color
else if (newPosition > 119) {
if (ledObject.direction == 'outbound') {
newColor = "green";
}
else if (ledObject.direction == 'inbound') {
newColor = "red";
}
else {
newColor = "Orange";
log.error('UP-W train color assignment - direction value not recognized - %s', ledObject.direction);
}
}
// all trains at LED position 60 and above are MD-W, and are painted the MD-W color
else if (newPosition > 59) {
if (ledObject.direction == 'outbound') {
newColor = "green";
}
else if (ledObject.direction == 'inbound') {
newColor = "red";
}
else {
newColor = "Orange";
log.error('MD-W train color assignment - direction value not recognized - %s', ledObject.direction);
}
}
// all other trains are UP-NW color
else {
if (ledObject.direction == 'outbound') {
newColor = "green";
}
else if (ledObject.direction == 'inbound') {
newColor = "red";
}
else {
newColor = "Orange";
log.error('UP-NW train color assignment - direction value not recognized - %s', ledObject.direction);
}
}
postBodyArray.push({position:newPosition, color:newColor});
});
// POST new array to LED server
request.post(
{
headers: {'Content-Type' : 'application/json'},
uri: 'http://127.0.0.1:8675',
body: JSON.stringify(postBodyArray)
},
function(error, response, body) {
if (error) {
log.error('POST to LED server resulted in an error - %j', error);
}
else if (response.statusCode != 201) {
log.warn('POST to LED server returned an error code (%d)', response.statusCode);
}
else {
log.debug('POST to LED server succeeded');
}
}
);
}
//
// NO WIFI - .....**.*...**.**.**......*.**.**...*.*...*.*.**.*...*.*....
// 555555555544444444443333333333222222222211111111119876543210
// 98765432109876543210987654321098765432109876543210
//
function protoDisplayError() {
var postBodyArray = [
{position:4, color:"red"},
{position:6, color:"red"},
{position:10, color:"red"},
{position:12, color:"red"},
{position:13, color:"red"},
{position:15, color:"red"},
{position:17, color:"red"},
{position:21, color:"red"},
{position:23, color:"red"},
{position:27, color:"red"},
{position:28, color:"red"},
{position:30, color:"red"},
{position:31, color:"red"},
{position:33, color:"red"},
{position:40, color:"red"},
{position:41, color:"red"},
{position:43, color:"red"},
{position:44, color:"red"},
{position:46, color:"red"},
{position:47, color:"red"},
{position:51, color:"red"},
{position:53, color:"red"},
{position:54, color:"red"}
];
// POST new array to LED server
request.post(
{
headers: {'Content-Type' : 'application/json'},
uri: 'http://127.0.0.1:8675',
body: JSON.stringify(postBodyArray)
},
function(error, response, body) {
if (error) {
log.error('POST to LED server resulted in an error - %j', error);
}
else if (response.statusCode != 201) {
log.warn('POST to LED server returned an error code (%d)', response.statusCode);
}
else {
log.debug('POST to LED server succeeded');
}
}
);
}
/******************************************************************************/
// ENTRY POINT
function displayData(sortedPositions, done) {
if (sortedPositions.error) {
processFetchedError(done);
}
else {
processFetchedData(sortedPositions, done);
}
}
/******************************************************************************/
// Call function that creates a "no wifi" error message
function processFetchedError(done) {
log.error("NO WIFI error display processedFetchedError()");
protoDisplayError();
done(null);
}
// Process each line fetched
function processFetchedData(sortedPositions, done) {
log.debug("processFetchedData()");
linesNamesArray.forEach((name) => {
ledsToLight[name] = processFetchedLineData(name, sortedPositions[name]);
});
log.debug("%s", JSON.stringify(ledsToLight, null, 4));
// textDisplayUPNW(ledsToLight['UP-NW']);
// textDisplayMDW(ledsToLight['MD-W']);
// textDisplayUPW(ledsToLight['UP-W']);
protoDisplayAll(ledsToLight);
done(null);
}
// Process each train in the line
function processFetchedLineData(lineName, trainPositionArray) {
log.debug("processFetchedLineData()");
// log.debug("%s - found %d trains", lineName, trainPositionArray.length);
var ledArray = [];
if (trainPositionArray != null && trainPositionArray != undefined) {
trainPositionArray.forEach((trainPosition) => {
// log.debug("Processing %s train - %j", lineName, element);
ledArray.push(matchLedWithTrain(lineName, trainPosition));
});
}
return ledArray;
}
// Pick the LED to light for one train
function matchLedWithTrain(lineName, trainPosition) {
log.debug("matchLedWithTrain(%s, %j)", lineName, trainPosition);
var lineArray = linesObject[lineName];
var possibleMatches = [];
lineArray.forEach((spurArray, spurIndex) => {
spurArray.forEach((segmentObject, segmentIndex) => {
var ledArray = segmentObject.ledArray;
ledArray.forEach((led, ledIndex) => {
var trainGeohash = trainPosition.geohash;
if (isInLEDNineBox(trainGeohash, led)) {
possibleMatches.push({
led : led,
spurIndex : spurIndex,
segmentIndex : segmentIndex,
ledIndex : ledIndex,
spurLed : getSpurLedCount(lineName, spurIndex, segmentIndex, ledIndex)
});
}
});
});
});
var exactMatch = pickExactMatch(trainPosition, possibleMatches);
if (exactMatch != null && exactMatch != undefined) {
return {
spur : exactMatch.spurIndex,
segment : exactMatch.segmentIndex,
segmentLed : exactMatch.ledIndex,
spurLed : exactMatch.spurLed,
direction : trainPosition.direction
};
}
else {
return null;
}
}
// Prune the array of possible matches down to just one
function pickExactMatch(trainPosition, possibleMatches) {
// First, check for exact matches
var prunedMatches = [];
possibleMatches.forEach((possibleMatch) => {
if (possibleMatch.led.geohash == trainPosition.geohash) {
prunedMatches.push(possibleMatch);
}
});
if (prunedMatches.length == 1) {
return prunedMatches[0];
}
else if (prunedMatches.length == 0) {
prunedMatches = possibleMatches;
}
var exactMatch;
var closestDistance = null;
prunedMatches.forEach((possibleMatch) => {
var distance = geolib.getDistance({
latitude : trainPosition.latitude,
longitude : trainPosition.longitude
},
{
latitude : possibleMatch.led.ledLat,
longitude : possibleMatch.led.ledLon
});
if (closestDistance == null) {
closestDistance = distance;
exactMatch = possibleMatch;
}
else {
if (distance < closestDistance) {
closestDistance = distance;
exactMatch = possibleMatch;
}
}
});
return exactMatch;
}
// Is the train in one of the neighbor blocks?
function isInLEDNineBox(trainGeohash, ledObject) {
if (trainGeohash == ledObject.geohash) {
return true;
}
else {
var retVal = false;
_.values(ledObject.geohashNeighbors).forEach((neighborGeohash) => {
if (trainGeohash == neighborGeohash) {
retVal = true;
}
});
return retVal;
}
}
// Calculate the led index for the entire spur
function getSpurLedCount(name, spur, segment, led) {
// log.debug("getSpurLedCount()");
var segmentsArray = linesObject[name][spur];
var totalLedIndex = 0;
for (var i = 0; i < segment; i++) {
totalLedIndex += segmentsArray[i].ledArray.length;
}
totalLedIndex += (led + 1);
return totalLedIndex;
}
module.exports.init = init;
module.exports.displayData = displayData;