1/**
2 * data service
3 *
4 * @module app/common/services/dataService
5 * @exports dataService
6 * @name dataService
7
8 * @version 0.0.1
9 */
10
11window.angular && (function (angular) {
12    'use strict';
13
14    angular
15        .module('app.common.services')
16        .service('dataService', ['Constants', function (Constants) {
17            this.app_version = "V.0.0.1";
18            this.server_health = Constants.SERVER_HEALTH.unknown;
19            this.server_state = 'Unreachable';
20            this.server_status = -2;
21            this.chassis_state = 'On';
22            this.LED_state = Constants.LED_STATE_TEXT.off;
23            this.last_updated = new Date();
24
25            this.loading = false;
26            this.server_unreachable = false;
27            this.loading_message = "";
28            this.showNavigation = false;
29            this.bodyStyle = {};
30            this.path = '';
31            this.sensorData = [];
32
33            this.hostname = "";
34            this.mac_address = "";
35            this.remote_window_active = false;
36
37            this.displayErrorModal = false;
38            this.errorModalDetails = {};
39
40            this.ignoreHttpError = false;
41            this.getServerId = function(){
42                 return this.host.replace(/^https?\:\/\//ig,"");
43            }
44
45            this.reloadServerId = function(){
46                this.server_id = this.getServerId();
47            }
48
49            this.getHost = function(){
50                if(sessionStorage.getItem(Constants.API_CREDENTIALS.host_storage_key) !== null){
51                    return sessionStorage.getItem(Constants.API_CREDENTIALS.host_storage_key);
52                }else{
53                    return Constants.API_CREDENTIALS.default_protocol + "://" +
54                           window.location.hostname +
55                           (window.location.port ? ":" + window.location.port : "");
56                }
57            }
58
59            this.setHost = function(hostWithPort){
60                hostWithPort = hostWithPort.replace(/^https?\:\/\//ig, '');
61                var hostURL = Constants.API_CREDENTIALS.default_protocol + "://" + hostWithPort;
62                sessionStorage.setItem(Constants.API_CREDENTIALS.host_storage_key, hostURL);
63                this.host = hostURL;
64                this.reloadServerId();
65            }
66
67            this.getUser = function(){
68                return sessionStorage.getItem('LOGIN_ID');
69            }
70
71            this.host = this.getHost();
72            this.server_id = this.getServerId();
73
74            this.setNetworkInfo = function(data){
75                this.hostname = data.hostname;
76                this.mac_address = data.mac_address;
77            }
78
79            this.setPowerOnState = function(){
80                this.server_state = Constants.HOST_STATE_TEXT.on;
81                this.server_status = Constants.HOST_STATE.on;
82            }
83
84            this.setPowerOffState = function(){
85                this.server_state = Constants.HOST_STATE_TEXT.off;
86                this.server_status = Constants.HOST_STATE.off;
87            }
88
89            this.setErrorState = function(){
90                this.server_state = Constants.HOST_STATE_TEXT.error;
91                this.server_status = Constants.HOST_STATE.error;
92            }
93
94            this.setUnreachableState = function(){
95                this.server_state = Constants.HOST_STATE_TEXT.unreachable;
96                this.server_status = Constants.HOST_STATE.unreachable;
97            }
98
99            this.setRemoteWindowActive = function(){
100                this.remote_window_active = true;
101            }
102
103            this.setRemoteWindowInactive = function(){
104                this.remote_window_active = false;
105            }
106
107            this.updateServerHealth = function(logs){
108                var criticals = logs.filter(function(item){
109                    return item.health_flags.critical == true;
110                });
111
112                if(criticals.length){
113                    this.server_health = Constants.SERVER_HEALTH.critical;
114                    return;
115                }
116
117                var warnings = logs.filter(function(item){
118                    return item.health_flags.warning == true;
119                });
120
121                if(warnings.length){
122                    this.server_health = Constants.SERVER_HEALTH.warning;
123                    return;
124                }
125
126                this.server_health = Constants.SERVER_HEALTH.good;
127            }
128
129            this.activateErrorModal = function(data){
130                if(data && data.hasOwnProperty('title')){
131                    this.errorModalDetails.title = data.title;
132                }else{
133                    this.errorModalDetails.title = Constants.MESSAGES.ERROR_MODAL.TITLE;
134                }
135
136                if(data && data.hasOwnProperty('description')){
137                    this.errorModalDetails.description = data.description;
138                }else{
139                    this.errorModalDetails.description = Constants.MESSAGES.ERROR_MODAL.DESCRIPTION;
140                }
141                this.displayErrorModal = true;
142            }
143
144            this.deactivateErrorModal = function(){
145                this.displayErrorModal = false;
146            }
147        }]);
148
149})(window.angular);
150