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