-
Notifications
You must be signed in to change notification settings - Fork 0
/
treadmill.js
859 lines (727 loc) · 27.6 KB
/
treadmill.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
// Robert Cudmore
// 20180601
// Factory
// statusFactory
// Controller
// configFormController
// has CallParentMethod to be called when other controlers (e.g. arduinoFormController) modify config
// arduinoFormController
// treadmill
var app = angular.module('demo', ['uiSwitch', 'ngRoute']);
///
///
/*
var socket = io.connect('http://192.168.1.15:5010');
socket.on('connect', function() {
// we emit a connected message to let knwo the client that we are connected.
console.log('socket.on connect')
socket.emit('client_connected', {data: 'New client!'});
console.log('2 socket.on connect')
});
socket.on('my_response', function(msg) {
console.log('socket.on my_response msg:', msg)
//console.log('msg.status:', msg.status)
$scope.status = msg.status;
// for armed checkbox, it needs a model
$scope.isArmed = $scope.isState('armed') || $scope.isState('armedrecording')
//for streaming
var tmpWidth = parseInt($scope.status.trial.config.video.streamResolution.split(',')[0],10)
var tmpHeight = parseInt($scope.status.trial.config.video.streamResolution.split(',')[1],10)
$scope.streamWidth = tmpWidth + (tmpWidth * 0.04)
$scope.streamHeight = tmpHeight + (tmpWidth * 0.04)
// for recording
$scope.lastImage = $scope.myUrl + 'api/lastimage' + '?' + new Date().getTime()
//console.log('$scope.lastImage:', $scope.lastImage)
//$rootScope.$emit("CallParentMethod", {});
//console.log('$scope.status:', $scope.status)
//$rootScope.$emit("CallParentMethod_SetConfigData", response.data);
//if ( $scope.status.runtime.xxxChange) {
//}
//console.log('xxx $scope.configData:', $scope.configData)
$rootScope.$emit("CallParentMethod_SetConfigData", msg.status);
$route.reload();
});
*/
///
///
//////////////////////////////////////////////////////////////////////////////
app.factory('statusFactory', function($http, $location, $interval) {
var myUrl = $location.absUrl(); //with port :5000
var getStatus = function () {
var url = myUrl + 'status'
return $http.get(url).
then(function(response) {
//console.log('statusFactory status=', response.data)
return response.data
});
};
return {getStatus: getStatus}
});
//////////////////////////////////////////////////////////////////////////////
// change parameters in config json file
app.controller('configFormController', function($scope, $rootScope, $http, $interval, $route, statusFactory) {
// call this from other controllers to update new values from server
//removed
//removed this and now updating at an interval from main controller
/*
$rootScope.$on("CallParentMethod", function() {
//console.log('in CallParentMethod:')
$scope.getPromise();
});
*/
//console.log('angular.version:', angular.version)
//called from main controller at an interval
$rootScope.$on("CallParentMethod_SetConfigData", function(event, thisConfig) {
//console.log('CallParentMethod_SetConfigData thisConfig:', thisConfig)
$scope.configData = thisConfig
if ($scope.configData.trial.runtime.cameraState == 'armed' || $scope.configData.trial.runtime.cameraState == 'armedrecording') {
//20180718, get this working
//console.log('CallParentMethod_SetConfigData() is calling $route.reload()')
// this gets called at an interval (very expensive for Pi to be serving this)
//console.log('$rootScope.$on("CallParentMethod_SetConfigData" is calling $route.reload')
$route.reload();
}
});
//
// save and load config files
$scope.saveConfig = function() {
//console.log('saveConfig()', $scope.configData, $scope.configData.$valid);
url = $scope.myUrl + 'api/submit/saveconfig'
//console.log('$scope.configData:', $scope.configData.trial.config)
$http.get(url).
then(function(response) {
//console.log('response.data:', response.data)
});
};
$scope.loadConfig = function(loadThis) {
console.log('loadConfig()', loadThis);
url = $scope.myUrl + 'api/submit/loadconfig/' + loadThis
//console.log('$scope.configData:', $scope.configData.trial.config)
$http.get(url).
then(function(response) {
//console.log('response.data:', response.data)
// update values in table
//$scope.configData = result
//$scope.getPromise()
//removed
//$rootScope.$emit("CallParentMethod", {}); //CallParentMethod calls getPromise()
},
function(data) {
// Handle error here
console.log('loadConfig http error')
});
};
//
// submit changes to backend server
$scope.submitConfigForm = function() {
console.log('submitConfigForm()', $scope.configData, $scope.configData.$valid);
url = $scope.myUrl + 'api/submit/configparams'
//console.log('$scope.configData:', $scope.configData.trial.config)
$http.post(url, JSON.stringify($scope.configData.trial.config)).
then(function(response) {
//console.log('response.data:', response.data)
//$scope.getPromise();
});
};
$scope.submitPinForm = function() {
console.log('submitPinForm()', $scope.configData, $scope.configData.$valid);
url = $scope.myUrl + 'api/submit/pinparams'
//console.log('$scope.configData:', $scope.configData.trial.config)
$http.post(url, JSON.stringify($scope.configData.trial.config)).
then(function(response) {
//console.log('response.data:', response.data)
//$scope.getPromise();
});
};
$scope.submitAnimalForm = function() {
console.log('submitAnimalForm()', $scope.configData, $scope.configData.$valid);
url = $scope.myUrl + 'api/submit/animalparams'
//console.log('$scope.configData:', $scope.configData.trial.config)
$http.post(url, JSON.stringify($scope.configData.trial.config)).
then(function(response) {
//console.log('response.data:', response.data)
//$scope.configData = response.data
});
};
//
// roll each of these into one function, e.g. merge animalParamChange and submitAnimalForm
$scope.animalParamChange = function(isValid) {
// if form fields don't pass validation, they will be 'undefined'
//console.log('animalParamChange()', 'isValid:', isValid, $scope.configData)
if (isValid) {
$scope.submitAnimalForm()
}
}
// called on each change of a config parameter (keystroke, tab, enter)
$scope.configParamChange = function(isValid) {
// if form fields don't pass validation, they will be 'undefined'
//console.log('configParamChange()', 'isValid:', isValid, $scope.configData)
if (isValid) {
$scope.submitConfigForm()
} else {
//console.log('no action taken')
}
}
$scope.pinParamChange = function(isValid) {
// if form fields don't pass validation, they will be 'undefined'
//console.log('configParamChange()', 'isValid:', isValid, $scope.configData)
if (isValid) {
$scope.submitPinForm()
}
}
//
// get data from the server
// not used -->> remove
$scope.getPromise = function() {
//console.log('getPromise')
var myPromise = statusFactory.getStatus()
myPromise.then(function(result) {
//console.log('getPromise() then clause')
$scope.configData = result
//$route.reload();
//$rootScope.$emit("CallParentMethod", {});
//console.log('getPromise() got $scope.configData')
//console.log($scope.configData)
},
function(data) {
// Handle error here
console.log('configFormController error in myPromise')
}); // mypromise.then
}
//$scope.getPromise()
//$interval($scope.getPromise, 400); // no () in function call !!!
}); // configFormController
//////////////////////////////////////////////////////////////////////////////
// controller for motor
app.controller('arduinoFormController', function($scope, $rootScope, $http, statusFactory) {
// take current params and submit
$scope.submitForm = function() {
console.log("posting data....", $scope.data, $scope.data.$valid);
url = $scope.myUrl + 'api/submit/motorparams'
$http.post(url, JSON.stringify($scope.data)).
then(function(response) {
// call the other controller with this
//This updates configFormController configData from server again
//removed
//$rootScope.$emit("CallParentMethod", {});
// html code has ng-form arduinoForm
$scope.arduinoForm.$setPristine();
});
};
// 20181220
//$scope.$on('someEvent', function(event, args) {
/*
$scope.$on('someEvent', function(event, data) {
//args is secondsElapsedStr
console.log('arduinoFormController received someEvent')
//$scope.refreshPlotly(args);
});
*/
//
//
//
//
//
//
//
//
// THIS IS FUCKING IMPOSSIBLE TO GET WORKING
// I CAN NOT DEAL WITH THESE JAVASCRIPT FRAMEWORKS !!!!!!!!!!!!!!!!!
// THEY MAKE MY CODING STALL FOR DAYS AND WEEKS
// COME ON GOOD AND FACEBOOK, THIS IS FUCKING BULLSHIT
//
//
//
//
//
//
//
//
//
/*
$rootScope.$on("rootEvent", function(event, thisConfig) {
console.log('arduinoFormController received rootEvent')
})
$scope.$on("rootEvent", function(event, thisConfig) {
console.log('arduinoFormController received rootEvent')
})
*/
$scope.$on('rootEvent2', function (event, secondsElapsedStr) {
console.log('I am from motor controller', secondsElapsedStr);
var msElapsed = parseFloat(secondsElapsedStr) * 1000;
Plotly.restyle('test_plotly', 'x', [[msElapsed, msElapsed]]);
});
/*
$scope.$on('rootEvent', function(event, data){
console.log('arduinoFormController received rootEvent')
})
$rootScope.$on('rootEvent', function(event, data){
console.log('arduinoFormController received rootEvent')
})
*/
//rebuild plotly but do not submit
$scope.motorParamChange = function() {
// if form fields don't pass validation, they will be 'undefined'
console.log('motorParamChange()', $scope.data)
buildPlotly($scope.data)
}
// respond to new secondsElapsedStr
/*
$scope.updateRuntime - function() {
// Plotly.react(gd, data, layout, config)
//buildPlotly($scope.data) //$scope.data is motorParams
$scope.refreshPlotly($scope.status)
}
*/
var myPromise = statusFactory.getStatus()
myPromise.then(function(result) {
var status = result
console.log('arduinoFormController myPromise:', status)
// motorParams
// remember, repeatDuration is in seconds, repeatduration is in ms
// todo: clean up numberofrepeats versus numberOfRepeats
// this is coming from config.json file.
// we want to be very careful NOT to modify its field names
$scope.data = status.trial.config.motor;
buildPlotly($scope.data)
}); // mypromise.then
// respond to new secondsElapsedStr
$scope.refreshPlotly = function(status) {
console.log('refreshPlotly()')
}
var buildPlotly = function(motorParams){
var trialMS = motorParams.motorNumEpochs * motorParams.motorRepeatDuration
//console.log('buildPlotly() trialMS:', trialMS)
//console.log('buildPlotly() motorParams:', motorParams)
//
// plotly
var d3 = Plotly.d3;
var WIDTH_IN_PERCENT_OF_PARENT = 100
var HEIGHT_IN_PERCENT_OF_PARENT = 12;
var gd3 = d3.select("#test_plotly")
.style({
width: WIDTH_IN_PERCENT_OF_PARENT + '%',
//'margin-left': (100 - WIDTH_IN_PERCENT_OF_PARENT) / 2 + '%',
height: HEIGHT_IN_PERCENT_OF_PARENT + 'vh',
//'margin-top': (100 - HEIGHT_IN_PERCENT_OF_PARENT) / 2 + 'vh'
});
var gd = gd3.node();
var data = [
{
x: [100,100],
y: [0, 1],
type: 'scatter'
}
];
var lineColor = 'rgba(100,100,100,1)'
// make a list of rect shapes, one per epoch/repeat
var i
var shapeList = []
for (i=0; i<motorParams.motorNumEpochs; i++) {
thisStart = motorParams.motorRepeatDuration * i + motorParams.motorDel
thisStop = thisStart + motorParams.motorDur
thisRect = {
'type': 'rect',
'x0': thisStart,
'y0': 0,
'x1': thisStop,
'y1': 1,
'line': {
'color': lineColor,
},
'fillcolor': lineColor,
opacity: 0.5,
};
shapeList.push(thisRect)
}
var layout = {
showlegend: false,
margin: {
l: 25,
t: 25,
r: 25,
b: 40,
pad: 4,
},
'xaxis': {
'range': [0, trialMS],
'zeroline': false,
'title': 'ms'
},
'yaxis': {
'showticklabels': false,
//'zeroline': false,
'showline': false,
'showgrid': false,
'ticks': '',
'showticklabels': false,
'range': [0, 1],
},
//paper_bgcolor: '#7f7f7f',
//plot_bgcolor: '#c7c7c7',
'shapes': shapeList,
};
var config = {
'displayModeBar': false
}
Plotly.plot(gd, data, layout, config)
// todo: seperate new lpot from update plot
// this is for when we update
Plotly.react(gd, data, layout, config)
window.onresize = function() {
Plotly.Plots.resize('test_plotly');
};
}; // $scope.buildPlotly
}); // arduinoFormController
//////////////////////////////////////////////////////////////////////////////
app.controller('exitController', function($scope, $window) {
$scope.onExit = function() {
console.log('user is closing tab')
sleep(5000)
//if ($scope.isstate('streaming')) {
console.log('user is closing tab and stream is running -->> stop it')
url = $scope.myUrl + 'api/action/' + 'stopStream'
console.log(url)
sleep(5000)
$http.get(url).
then(function(response) {
// window/tab has been closed, do nothing
//$scope.status = response.data;
});
//}
return ('bye bye');
};
$window.onbeforeunload = $scope.onExit;
var leavingPageText = "xxx You'll lose your changes if you leave xxx";
window.onbeforeunload = function(){
console.log('in window.onbeforeunload')
return leavingPageText;
}
$scope.$on('$locationChangeStart', function(event, next, current) {
if(!confirm(leavingPageText + "\n\nxxx Are you sure you want to leave this page? xxx")) {
event.preventDefault();
}
});
});
//////////////////////////////////////////////////////////////////////////////
app.controller('treadmill', function($scope, $rootScope, $window, $http, $location, $interval, $route, $sce, $timeout, $document) {
//console.log('angular.version:', angular.version)
$scope.myUrl = $location.absUrl(); //with port :5000
console.log('$scope.myUrl:', $scope.myUrl)
console.log('$location.host():', $location.host())
document.title = 'PiE ' + $location.host()
myStreamUrl = 'http://' + $location.host() + ':8080/stream';
$scope.myStreamUrl0 = myStreamUrl
$scope.myStreamUrl = $sce.trustAsResourceUrl(myStreamUrl);
$scope.showConfig = false;
$scope.showPins = false;
$scope.showMotor = false;
$scope.showDebug = true;
$scope.showConfigTable = false;
$scope.showLastImage = false;
//$scope.allowArming = false;
//$scope.isArmed = false;
//read the state from homecage backend, do this at an interval
$scope.getStatus = function () {
$http.get($scope.myUrl + 'status').
then(function(response) {
$scope.status = response.data;
// for armed checkbox, it needs a model
//$scope.isArmed = $scope.isState('armed') || $scope.isState('armedrecording')
//console.log('after status $scope.isArmed:', $scope.isArmed)
//console.log('$scope.status.trial.runtime.cameraState:', $scope.status.trial.runtime.cameraState)
//for streaming
var tmpWidth = parseInt($scope.status.trial.config.video.streamResolution.split(',')[0],10)
var tmpHeight = parseInt($scope.status.trial.config.video.streamResolution.split(',')[1],10)
$scope.streamWidth = tmpWidth + (tmpWidth * 0.04)
$scope.streamHeight = tmpHeight + (tmpWidth * 0.04)
// for recording
$scope.lastImage = $scope.myUrl + 'api/lastimage' + '?' + new Date().getTime()
//console.log('$scope.lastImage:', $scope.lastImage)
//$rootScope.$emit("CallParentMethod", {});
//console.log('$scope.status:', $scope.status)
//$rootScope.$emit("CallParentMethod_SetConfigData", response.data);
//if ( $scope.status.runtime.xxxChange) {
//}
//console.log('xxx $scope.configData:', $scope.configData)
$rootScope.$emit("CallParentMethod_SetConfigData", response.data);
// turn of interval updates that call REST
$scope.setInterval($scope.status.trial.runtime.cameraState)
// set tab title based on host
document.title = $scope.status.trial.systemInfo.hostname + ' PiE'
});
};
// mixing controllers, this submits main $scope.config
$scope.submitLEDForm = function() {
//console.log('submitLEDForm() $scope.status.trial.config:', $scope.status.trial.config.hardware.eventOut);
url = $scope.myUrl + 'api/submit/ledparams'
$http.post(url, JSON.stringify($scope.status.trial.config)).
then(function(response) {
$scope.status = response.data
//console.log('$scope.status:', $scope.status)
});
};
// davis
// mixing controllers, this submits main $scope.config
$scope.submitFanForm = function() {
//console.log('submitLEDForm() $scope.status.trial.config:', $scope.status.trial.config.hardware.eventOut);
url = $scope.myUrl + 'api/submit/fanparams'
$http.post(url, JSON.stringify($scope.status.trial.config)).
then(function(response) {
$scope.status = response.data
//console.log('$scope.status:', $scope.status)
});
};
// new 20181225
$scope.hardCloseStream = 0
function callStreamAtTimeout() {
console.log("callStreamAtTimeout()");
if ($scope.hardCloseStream) {
console.log('hardCloseStream')
$scope.myStreamUrl = ''
} else {
myStreamUrl = 'http://' + $location.host() + ':8080/stream' + '?' + new Date().getTime()
$scope.myStreamUrl = $sce.trustAsResourceUrl(myStreamUrl);
}
}
// one button callback
$scope.buttonCallback = function(buttonID) {
console.log('buttonCallback() buttonID=', buttonID)
switch (buttonID) {
case 'startRecord':
case 'stopRecord':
case 'startStream':
case 'stopStream':
case 'startArmVideo':
case 'stopArmVideo':
// new 20181225
// try and get stream to always show on first click
if (buttonID == 'startStream') {
$scope.hardCloseStream = 0;
}
if (buttonID == 'stopStream') {
$scope.hardCloseStream = 1;
callStreamAtTimeout();
}
// was this 20181225
url = $scope.myUrl + 'api/action/' + buttonID
$http.get(url).
then(function(response) {
$scope.status = response.data;
//$scope.isArmed = $scope.isState('armed') || $scope.isState('armedrecording')
//$rootScope.$emit("CallParentMethod_SetConfigData", response.data);
//$route.reload();
//console.log('$scope.isArmed:', $scope.isArmed)
$scope.setInterval($scope.status.trial.runtime.cameraState)
});
// new 20181225
//refresh stream
if (buttonID == 'startStream') {
//callAtTimeout()
// give strem some time to refresh after rest call???
$timeout(callStreamAtTimeout, 3000);
}
break;
case 'toggleArm':
if ($scope.isState('armed')) {
console.log('button callback toggleArm armed')
url = $scope.myUrl + 'api/action/stopArm'
$http.get(url).
then(function(response) {
$scope.status = response.data;
//$scope.isArmed = $scope.isState('armed') || $scope.isState('armedrecording')
//$rootScope.$emit("CallParentMethod_SetConfigData", response.data);
//$route.reload();
//console.log('after stopArm $scope.isArmed:', $scope.isArmed)
//console.log('$scope.status.trial.runtime.cameraState:', $scope.status.trial.runtime.cameraState)
// start javascript counter
//setTimeout(myTimeoutFunction, 5000);
//interval = setInterval(myIntervalFunction, 5000);
//$scope.setInterval($scope.status.trial.runtime.cameraState)
});
} else if ($scope.isState('idle')) { //safety check, index interface should handle
console.log('button callback toggleArm idle')
url = $scope.myUrl + 'api/action/startArm'
$http.get(url).
then(function(response) {
console.log('response.data.trial.runtime.cameraState:', response.data.trial.runtime.cameraState)
$scope.status = response.data;
//$scope.isArmed = $scope.isState('armed') || $scope.isState('armedrecording')
//$rootScope.$emit("CallParentMethod_SetConfigData", response.data);
//$route.reload();
//console.log('after startArm $scope.isArmed:', $scope.isArmed)
//console.log('$scope.status.trial.runtime.cameraState:', $scope.status.trial.runtime.cameraState)
// start javascript counter
//setTimeout(myTimeoutFunction, 800);
//interval = setInterval(myIntervalFunction, 800);
//$scope.setInterval('startArm')
//removed
//$scope.setInterval($scope.status.trial.runtime.cameraState)
});
}
break;
default:
console.log('buttonCallback() case not taken, buttonID=',buttonID);
break;
}
}
$scope.isState = function(state) {
if ($scope.status) {
return $scope.status.trial.runtime.cameraState == state
} else {
return ''
}
}
$scope.allowParamEdit = function() {
return $scope.isState('idle')
}
/*
$scope.getLastImage2 = function () {
$scope.lastImage2 = $scope.myUrl + 'api/lastimage' + '?' + new Date().getTime()
}
*/
// reduce this to improve GPIO frame in????
//$interval($scope.getStatus, 400); // no () in function call !!!
//$interval($scope.getStatus, 800); // no () in function call !!!
$scope.getStatus()
///////////////////////////////////////////////////////////////////////////
// counter to display elapsed recording time rather than hitting REST with http.get
// this is used while we are acquiring data, VERY IMPORTANT
var defaultInterval = 800; // 800
var counter = 0;
$scope.myIntervalFunction = function(){
clearInterval(interval);
//console.log('myIntervalFunction counter:', counter)
//interval = setInterval(myIntervalFunction, counter);
counter += 0.4
counter = Math.round(counter * 100) / 100
$scope.status.trial.runtime.secondsElapsedStr = counter
//
//
//
//
//
//
//
//
// THIS IS FUCKING IMPOSSIBLE TO GET WORKING
// I CAN NOT DEAL WITH THESE JAVASCRIPT FRAMEWORKS !!!!!!!!!!!!!!!!!
// THEY MAKE MY CODING STALL FOR DAYS AND WEEKS
// COME ON GOOD AND FACEBOOK, THIS IS FUCKING BULLSHIT
//
//
//
//
//
//
//
//
//
// see: https://stackoverflow.com/questions/9293423/can-one-angularjs-controller-call-another
// 20181220, update plot of motor !!!
//$scope.refreshPlotly($scope.status)
// emit an update (for motor controller)
//$rootScope.$broadcast('someEvent', $scope.status.trial.runtime.secondsElapsedStr);
//broadcast from rootScope and receive on $scope, this is fucking confusing
//console.log('emit someEvent', $scope.status.trial.runtime.secondsElapsedStr)
//$rootScope.$broadcast('someEvent', $scope.status.trial.runtime.secondsElapsedStr);
console.log('emit rootEvent', $scope.status.trial.runtime.secondsElapsedStr)
//$rootScope.$broadcast('rootEvent', 'Hello from the rootScope!');
//$rootScope.$emit("rootEvent", 'Hello from the rootScope!');
$rootScope.$broadcast("rootEvent2", $scope.status.trial.runtime.secondsElapsedStr);
//$scope.$broadcast('rootEvent', 'Hello from the rootScope!');
//$scope.$emit('rootEvent', 'Hello from the rootScope!');
//$scope.$emit('someEvent', $scope.status.trial.runtime.secondsElapsedStr);
// this will be captured/responded in motor controller with
// motor controller repsonds with
//$scope.$on('someEvent', function(event, args) {});
}
$scope.setInterval = function(state) {
//console.log('setInterval()', state)
$interval.cancel(interval);
if (state == 'armedrecording') {
counter = 0
interval = $interval($scope.myIntervalFunction, 400);
} else {
interval = $interval($scope.getStatus, defaultInterval);
}
}
var interval = $interval($scope.getStatus, defaultInterval);
///////////////////////////////////////////////////////////////////////////
// SOCKETIO
//var socket = io.connect($scope.myUrl);
///
///
angular.element(document).ready(function () {
//var socket = io.connect('http://192.168.1.15:5010');
var socket = io.connect($scope.myUrl);
socket.on('connect', function() {
// we emit a connected message to let knwo the client that we are connected.
//console.log('socket.on connect aaa')
socket.emit('client_connected', {data: 'New client!', clientUrl: $scope.myUrl});
//console.log('socket.on connect bbb')
});
socket.on('my_response', function(msg) {
console.log('socket.on my_response msg:') //, msg)
/*
//console.log('msg.status:', msg.status)
$scope.status = msg.status;
// for armed checkbox, it needs a model
$scope.isArmed = $scope.isState('armed') || $scope.isState('armedrecording')
//for streaming
var tmpWidth = parseInt($scope.status.trial.config.video.streamResolution.split(',')[0],10)
var tmpHeight = parseInt($scope.status.trial.config.video.streamResolution.split(',')[1],10)
$scope.streamWidth = tmpWidth + (tmpWidth * 0.04)
$scope.streamHeight = tmpHeight + (tmpWidth * 0.04)
// for recording
$scope.lastImage = $scope.myUrl + 'api/lastimage' + '?' + new Date().getTime()
//console.log('$scope.lastImage:', $scope.lastImage)
//$rootScope.$emit("CallParentMethod", {});
//console.log('$scope.status:', $scope.status)
//$rootScope.$emit("CallParentMethod_SetConfigData", response.data);
//if ( $scope.status.runtime.xxxChange) {
//}
//console.log('xxx $scope.configData:', $scope.configData)
$rootScope.$emit("CallParentMethod_SetConfigData", msg.status);
$route.reload();
*/
});
socket.on('socket_startTrial', function(msg) {
console.log('START socket_startTrial() msg:', msg)
$scope.status = msg.status
// todo: this is going to cause problems !!!!
$scope.isArmed = true //$scope.isState('armed') || $scope.isState('armedrecording')
//$scope.setInterval($scope.status.trial.runtime.cameraState)
$scope.setInterval('armedrecording')
console.log($scope.status.trial.runtime.cameraState)
});
socket.on('socket_stopTrial', function(msg) {
console.log('STOP socket_stopTrial() msg:', msg)
$scope.status = msg.status
// todo: this is going to cause problems !!!!
$scope.isArmed = false //$scope.isState('armed') || $scope.isState('armedrecording')
//$scope.setInterval($scope.status.trial.runtime.cameraState)
$scope.setInterval('armed')
console.log($scope.status.trial.runtime.cameraState)
});
});
///////////////////////////////////////////////////////////////////////
// does nothing for ~/pie/pie run
$scope.restartpieserver = function() {
restartOK = $window.confirm('Are you sure you want to restart the PiE server?');
if (restartOK) {
url = $scope.myUrl + 'api/restartpieserver'
console.log('restartpieserver() url: ' + url)
$http.get(url).
then(function(response) {
// do nothing
//$scope.status = response.data;
});
}
}
///////////////////////////////////////////////////////////////////////
// if streaming then stop streaming on window/tab close
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
}); // treadmill controller