-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
782 lines (677 loc) · 19.6 KB
/
app.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
angular.module("fishmograph", ["ui.router", "ui-notification"])
// --- FISHMOGRAPH API ---
// Host address (debug)
//.constant("FH_HOST", "http://192.168.0.200")
// Host address (production)
.constant("FH_HOST", "")
// Login/Logout
.constant("FH_API_LOGIN", "/login")
.constant("FH_API_LOGOUT", "/logout")
.constant("FH_API_PASSWORD_UPDATE", "/password/update")
.constant("FH_API_STATUS", "/status")
.constant("FH_API_DATA", "/data")
.constant("FH_API_DATA_D7S", "/data/d7s")
.constant("FH_API_SETTINGS_INITIALIZE", "/settings/initialize")
.constant("FH_API_SETTINGS_SELFTEST", "/settings/selftest")
.constant("FH_API_SETTINGS_CLEAR_D7S", "/settings/clear/d7s")
.constant("FH_API_SETTINGS_CLEAR_DATA", "/settings/clear/data")
.constant("FH_API_RECIPIENT_LIST", "/recipient/list")
.constant("FH_API_RECIPIENT_DELETE", "/recipient/delete")
.constant("FH_API_RECIPIENT_NEW", "/recipient/new")
.config(function ($urlRouterProvider, $locationProvider, $urlMatcherFactoryProvider, $stateProvider, $httpProvider, NotificationProvider) {
// --- ROUTES ---
// ui-router non strict mode and case insensitive
$urlMatcherFactoryProvider.caseInsensitive(true);
$urlMatcherFactoryProvider.strictMode(false);
// Index state (redirect to earthquakes)
$stateProvider.state("index", {
redirectTo: "login",
url: "/"
});
// Login state
$stateProvider.state("login", {
controller: "loginCtrl",
templateUrl: "login.htm",
url: "/login"
});
// Container logged area
$stateProvider.state("page", {
controller: "pageCtrl",
templateUrl: "cntr.htm",
data: {
requireLogin: true // every child state require login
},
//redirectTo: "page.earthquakes"
});
// Earthquakes
$stateProvider.state("page.earthquakes", {
controller: "earthquakesCtrl",
templateUrl: "earthqks.htm",
url: "/earthquakes"
});
// D7S readings
$stateProvider.state("page.d7s", {
controller: "d7sCtrl",
templateUrl: "d7s.htm",
url: "/d7s"
});
// Alerts
// Container
$stateProvider.state("page.alerts", {
controller: "alertsCtrl",
template: "<ui-view></ui-view>",
url: "/alerts"
});
$stateProvider.state("page.alerts.list", {
controller: "alertsListCtrl",
templateUrl: "alerts.htm",
url: "/list"
});
$stateProvider.state("page.alerts.new", {
controller: "alertsNewCtrl",
templateUrl: "alert_nw.htm",
url: "/new"
});
// Settings
$stateProvider.state("page.settings", {
controller: "settingsCtrl",
templateUrl: "settings.htm",
url: "/settings"
});
// Credits and license
$stateProvider.state("page.credits", {
controller: "creditsCtrl",
templateUrl: "credits.htm",
url: "/credits"
});
$urlRouterProvider.when("", function ($state) {
$state.go("index");
});
//error404 main routes
$urlRouterProvider.otherwise(function ($injector) {
//getting $state service from $injector
$state = $injector.get("$state");
//transition to the default page
$state.go("page.earthquakes");
});
// --- HTTP CONFIG ---
$httpProvider.defaults.withCredentials = true;
//$httpProvider.defaults.headers.common["Content-Type"] = "text/plain";
// --- NOTIFICATIONS ---
NotificationProvider.setOptions({
delay: 10000,
startTop: 20,
startRight: 10,
verticalSpacing: 10,
horizontalSpacing: 10,
positionX: 'center',
positionY: 'top'
});
})
.run(function ($state, $transitions, LoginService) {
// If the user is already logged in (session is valid) redirect him to the logged page (only performed once at app startup)
// Check if the user is already logged in
LoginService.login().then(function () {
$state.go("page.earthquakes");
}, function () {
$state.go("login");
}).finally(function () {
// Check if the user is autenticated, otherwise redirect him to the login page
$transitions.onStart({ }, function(transition) {
//if the state require the login and the user is not logged
if (transition.to().name != "login" && transition.to().data !== undefined && transition.to().data.requireLogin && !LoginService.isUserLogged()) {
return transition.router.stateService.target('login');
}
});
})
})
// --- SERVICES ---
// Login service
.factory("LoginService", function ($q, $http, FH_HOST, FH_API_LOGIN, FH_API_LOGOUT) {
// User status
var isLogged = false;
// Function to log in a user
function login(password) {
var deferred = $q.defer();
$http({
method: 'POST',
url: FH_HOST + FH_API_LOGIN,
headers: {
'Content-Type': undefined
},
data: {
password: password
}
// Success
}).then(function () {
// Saving the success login status
isLogged = true;
// Resolving the promise
deferred.resolve();
// Error
}, function () {
// Resolving the promise
deferred.reject();
});
return deferred.promise;
}
// Function to logout a user
function logout(request = true) {
// If the logout need to be sent to the server
if (request) {
var deferred = $q.defer();
$http({
method: 'POST',
url: FH_HOST + FH_API_LOGOUT,
headers: {
'Content-Type': undefined
}
// Success
}).then(function () {
// Saving the success logout status
isLogged = false;
// Resolving the promise
deferred.resolve();
// Error
}, function () {
// Resolving the promise
deferred.reject();
});
return deferred.promise;
} else {
isLogged = false;
}
}
// Function to check if a user is logged
function isUserLogged() {
return isLogged;
}
// Return the service object
return {
login: login, //login function
logout: logout, //logout function
isUserLogged: isUserLogged, //check if the user is logged
};
})
// Notification service
.factory("AlertService", function (Notification) {
// Show a success alert
function success(message) {
Notification({templateUrl: "alert-success.html", message: message});
}
// Show a warning alert
function warning(message) {
Notification({templateUrl: "alert-warning.html", message: message});
}
// Show a danger alert
function danger(message) {
Notification({templateUrl: "alert-danger.html", message: message});
}
// Return the service object
return {
success: success, // Show a success alert
warning: warning, // Show a warning alert
danger: danger // Show a danger alert
};
})
// Authorized http service
// It will send the request using $http service, if the response is 401 (Unauthorized) the user is redirected to login
.factory("AuthorizedHttp", function($http, $state, $q, LoginService, AlertService) {
function request(request) {
var deferred = $q.defer();
$http(request).then(function (response) {
// Resolving the promise
deferred.resolve(response);
// Error
}, function (response) {
// If the user is not autorized
if (response.status == 401) {
// Logout the user and redirect him to the login page
LoginService.logout(false);
$state.go("login");
return;
}
// Resolving the promise
deferred.reject(response);
});
return deferred.promise;
}
return {
request: request
};
})
// --- CONTROLLERS ---
// Earthquakes page controller
.controller("loginCtrl", function ($scope, $state, LoginService, AlertService) {
// If the user is already logged in redirect him to the logged page
// Check if the user is already logged in
if (LoginService.isUserLogged()) {
$state.go("page.earthquakes");
}
// Login the user
$scope.login = function (password) {
// If password is undefined put an empty string
if (password == undefined) {
password = "";
}
LoginService.login(password).then(function () {
$state.go("page.earthquakes");
}, function () {
AlertService.danger("The password is wrong!");
});
}
})
// Menu controller
.controller("pageCtrl", function ($scope, $state, $interval, $filter, LoginService, AlertService, AuthorizedHttp, FH_HOST, FH_API_STATUS) {
var pageTitle = "";
var menuItemHighlighted = -1;
var checkingStatus = false;
// Set the page title
$scope.setPageTitle = function (title) {
pageTitle = title;
};
// Set the menu item to highlight
$scope.setMenuItemHighlighted = function (item) {
menuItemHighlighted = item;
};
// Get the page title
$scope.getPageTitle = function (item) {
return pageTitle;
};
// Check if the current menu item is active
$scope.isMenuItemHighlighted = function (item) {
return item == menuItemHighlighted;
};
// Logout
$scope.logout = function () {
LoginService.logout(true).then(function () {
$state.go("login");
}, function () {
AlertService.danger("The server has encountered some problems. Try again later!");
});
}
//status check every 5 seconds
function checkStatus() {
if (checkingStatus) {
return;
}
checkingStatus = true;
if (LoginService.isUserLogged()) {
AuthorizedHttp.request({
method: 'GET',
url: FH_HOST + FH_API_STATUS,
headers: {
'Content-Type': undefined
}
// Success
}).then(function (response) {
if (response.data.status == 2) {
AlertService.warning("Earthquake started on " + $filter('date')(response.data.timestamp*1000, 'dd/MM/yyyy at HH:mm:ss'));
} else if (response.data.status == 3) {
AlertService.warning("Earthquake ended on " + $filter('date')(response.data.timestamp*1000, 'dd/MM/yyyy at HH:mm:ss'));
} else if (response.data.status == 4) {
AlertService.danger("Shutoff signal generated on " + $filter('date')(response.data.timestamp*1000, 'dd/MM/yyyy at HH:mm:ss'));
} else if (response.data.status == 3) {
AlertService.danger("Collapse signal generated on " + $filter('date')(response.data.timestamp*1000, 'dd/MM/yyyy at HH:mm:ss'));
}
// Error
}, function () {
// Alert that the server encountered some problems
AlertService.danger("The server has encountered some problems. Try again later!");
// Finally
}).finally(function () {
checkingStatus = false;
});
}
}
//register event
$interval(checkStatus, 5000);
})
// Earthquakes page controller
.controller("earthquakesCtrl", function ($scope, AlertService, AuthorizedHttp, FH_HOST, FH_API_DATA) {
// Set page title and highlight the menu item
$scope.setPageTitle("Earthquakes Registered");
$scope.setMenuItemHighlighted(0);
// Show the loader gear
$scope.loader = true;
// Prepare the data
$scope.data = {};
AuthorizedHttp.request({
method: 'GET',
url: FH_HOST + FH_API_DATA,
headers: {
'Content-Type': undefined
}
// Success
}).then(function (response) {
// Clear old data
$scope.data = {};
// If the data are valid
if (angular.isArray(response.data) && response.data.length != 0) {
$scope.data.items = response.data;
}
// Error
}, function () {
// Clear the data
$scope.data = {};
// Alert that the server encountered some problems
AlertService.danger("The server has encountered some problems. Try again later!");
// Finally
}).finally(function () {
// Hide the loader gear
$scope.loader = false;
});
})
// D7S Readings page controller
.controller("d7sCtrl", function ($scope, AlertService, AuthorizedHttp, FH_HOST, FH_API_DATA_D7S) {
// Set page title and highlight the menu item
$scope.setPageTitle("D7S Readings");
$scope.setMenuItemHighlighted(1);
// Show the loader gear
$scope.loader = true;
// Prepare the data
$scope.data = {};
AuthorizedHttp.request({
method: 'GET',
url: FH_HOST + FH_API_DATA_D7S,
headers: {
'Content-Type': undefined
}
// Success
}).then(function (response) {
// Prepare the data
$scope.data = {};
//prepare data
var lastest = [];
var ranked = [];
//lastest data
for (i = 0; i < 5; i++) {
if (response.data[0][i].si != 0 || response.data[0][i].pga != 0) {
lastest.push(response.data[0][i]);
}
}
if (lastest.length != 0) {
$scope.data.lastest = lastest;
}
//ranked data
for (i = 0; i < 5; i++) {
if (response.data[1][i].si != 0 || response.data[1][i].pga != 0) {
ranked.push(response.data[1][i]);
}
}
if (ranked.length != 0) {
$scope.data.ranked = ranked;
}
// Error
}, function () {
// Clear the data
$scope.data = {};
// Alert that the server encountered some problems
AlertService.danger("The server has encountered some problems. Try again later!");
// Finally
}).finally(function () {
// Hide the loader gear
$scope.loader = false;
});
})
// Alerts container page controller
.controller("alertsCtrl", function ($scope) {
// Set page title and highlight the menu item
$scope.setPageTitle("Email Alerts");
$scope.setMenuItemHighlighted(2);
})
// Alerts list page controller
.controller("alertsListCtrl", function ($scope, $state, AlertService, AuthorizedHttp, FH_HOST, FH_API_RECIPIENT_LIST, FH_API_RECIPIENT_DELETE) {
// Show the loader gear
$scope.loader = true;
// Prepare the data
$scope.data = {
items: {}
};
AuthorizedHttp.request({
method: 'GET',
url: FH_HOST + FH_API_RECIPIENT_LIST,
headers: {
'Content-Type': undefined
}
// Success
}).then(function (response) {
// Clear old data
$scope.data = {
items: {}
};
// If the data are valid
if (angular.isArray(response.data) && response.data.length != 0) {
$scope.data.items = response.data;
}
// Error
}, function () {
// Clear the data
$scope.data = {
items: {}
};
// Alert that the server encountered some problems
AlertService.danger("The server has encountered some problems. Try again later!");
// Finally
}).finally(function () {
// Hide the loader gear
$scope.loader = false;
});
$scope.addRecipient = function () {
if ($scope.data.items.length == 5) {
AlertService.warning("You can only set 5 recipients!");
} else {
$state.go("page.alerts.new");
}
}
$scope.deleteRecipient = function (index) {
AuthorizedHttp.request({
method: 'POST',
url: FH_HOST + FH_API_RECIPIENT_DELETE,
headers: {
'Content-Type': undefined
},
data: {
index: index
}
// Success
}).then(function () {
// remove the element from the array
$scope.data.items.splice(index, 1);
// Error
}, function () {
// Alert that the server encountered some problems
AlertService.danger("The server has encountered some problems. Try again later!");
});
}
})
// New alert page controller
.controller("alertsNewCtrl", function ($scope, $state, AlertService, AuthorizedHttp, FH_HOST, FH_API_RECIPIENT_NEW) {
$scope.newRecipient = function (name, email) {
AuthorizedHttp.request({
method: 'POST',
url: FH_HOST + FH_API_RECIPIENT_NEW,
headers: {
'Content-Type': undefined
},
data: {
name: name,
email: email
}
// Success
}).then(function () {
AlertService.success("The new recipient has been added!");
// Transit to the list page
$state.go("page.alerts.list");
// Error
}, function () {
// Alert that the server encountered some problems
AlertService.danger("The server has encountered some problems. Try again later!");
});
}
})
// Settings page controller
.controller("settingsCtrl", function ($scope, AlertService, AuthorizedHttp, FH_HOST, FH_API_SETTINGS_INITIALIZE, FH_API_SETTINGS_SELFTEST, FH_API_SETTINGS_CLEAR_DATA, FH_API_SETTINGS_CLEAR_D7S, FH_API_PASSWORD_UPDATE) {
var activePanel = 0;
// Set page title and highlight the menu item
$scope.setPageTitle("Fishmograph Settings");
$scope.setMenuItemHighlighted(3);
// Set the active panel
$scope.setActivePanel = function (item) {
activePanel = item;
};
// Check if the current panel is active
$scope.isPanelActive = function (item) {
return item == activePanel;
};
//only one request at a time
var requestOutgoing = false;
$scope.initialize = function () {
if (requestOutgoing) {
AlertService.warning("Too many commands! You can send one command at a time!");
return;
}
// We are making a request
requestOutgoing = true;
// Alert the user for the action
AlertService.warning("The sensor will be initialized!");
AuthorizedHttp.request({
method: 'POST',
url: FH_HOST + FH_API_SETTINGS_INITIALIZE,
headers: {
'Content-Type': undefined
}
// Success
}).then(function (response) { //success promises
if (response.data.status == 1) {
AlertService.success("Sensor succesfully initialized!");
} else if (response.data.status == 2) {
AlertService.warning("Sensor cannot be initialized beacuse an earthquake is occuring!");
}
// Error
}, function () {
AlertService.danger("The server has encountered some problems. Try again later!");
// Finally
}).finally(function () {
requestOutgoing = false;
});
};
$scope.selftest = function () {
if (requestOutgoing) {
AlertService.warning("Too many commands! You can send one command at a time.");
return;
}
// We are making a request
requestOutgoing = true;
// Alert the user for the action
AlertService.warning("The selftest procedure is started!");
AuthorizedHttp.request({
method: 'POST',
url: FH_HOST + FH_API_SETTINGS_SELFTEST,
headers: {
'Content-Type': undefined
}
// Success
}).then(function (response) { //success promises
if (response.data.status == 1) {
AlertService.success("Selftest completed successfully!");
} else if (response.data.status == -1) {
AlertService.danger("Selftest ended with errors!");
} else if (response.data.status == 2) {
AlertService.warning("Selftest cannot be performed beacuse an earthquake is occuring!");
}
// Error
}, function () {
AlertService.danger("The server has encountered some problems. Try again later!");
// Finally
}).finally(function () {
requestOutgoing = false;
});
};
$scope.clearMemory = function () {
if (requestOutgoing) {
AlertService.warning("Too many commands! You can send one command at a time.");
return;
}
// We are making a request
requestOutgoing = true;
AuthorizedHttp.request({
method: 'POST',
url: FH_HOST + FH_API_SETTINGS_CLEAR_D7S,
headers: {
'Content-Type': undefined
}
// Success
}).then(function (response) { //success promises
AlertService.success("Sensor memory successfully cleared!");
// Error
}, function () {
AlertService.danger("The server has encountered some problems. Try again later!");
// Finally
}).finally(function () {
requestOutgoing = false;
});
};
$scope.clearData = function () {
if (requestOutgoing) {
AlertService.warning("Too many commands! You can send one command at a time.");
return;
}
// We are making a request
requestOutgoing = true;
AuthorizedHttp.request({
method: 'POST',
url: FH_HOST + FH_API_SETTINGS_CLEAR_DATA,
headers: {
'Content-Type': undefined
}
// Success
}).then(function (response) { //success promises
AlertService.success("Earthquake data has been successfully cleared!");
// Error
}, function () {
AlertService.danger("The server has encountered some problems. Try again later!");
// Finally
}).finally(function () {
requestOutgoing = false;
});
};
$scope.changePassword = function (password, confirmPassword) {
if (password != confirmPassword) {
AlertService.danger("Password mismatch!");
return;
}
AuthorizedHttp.request({
method: 'POST',
url: FH_HOST + FH_API_PASSWORD_UPDATE,
headers: {
'Content-Type': undefined
},
data: {
password: password
}
// Success
}).then(function () { //success promises
AlertService.success("Password succesfully updated!");
// Error
}, function () {
AlertService.danger("The server has encountered some problems. Try again later!");
// Finally
});
};
})
// Credits page controller
.controller("creditsCtrl", function ($scope) {
var activePanel = 0;
// Set page title and highlight the menu item
$scope.setPageTitle("Credits & License");
$scope.setMenuItemHighlighted(4);
// Set the active panel
$scope.setActivePanel = function (item) {
activePanel = item;
};
// Check if the current panel is active
$scope.isPanelActive = function (item) {
return item == activePanel;
};
});