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            this.ignoreHttpError = false;
37            this.getServerId = function(){
38                 return this.host.replace(/^https?\:\/\//ig,"");
39            }
40
41            this.reloadServerId = function(){
42                this.server_id = this.getServerId();
43            }
44
45            this.getHost = function(){
46                if(sessionStorage.getItem(Constants.API_CREDENTIALS.host_storage_key) !== null){
47                    return sessionStorage.getItem(Constants.API_CREDENTIALS.host_storage_key);
48                }else{
49                    return Constants.API_CREDENTIALS.default_protocol + "://" +
50                           window.location.hostname +
51                           (window.location.port ? ":" + window.location.port : "");
52                }
53            }
54
55            this.setHost = function(hostWithPort){
56                hostWithPort = hostWithPort.replace(/^https?\:\/\//ig, '');
57                var hostURL = Constants.API_CREDENTIALS.default_protocol + "://" + hostWithPort;
58                sessionStorage.setItem(Constants.API_CREDENTIALS.host_storage_key, hostURL);
59                this.host = hostURL;
60                this.reloadServerId();
61            }
62
63            this.getUser = function(){
64                return sessionStorage.getItem('LOGIN_ID');
65            }
66
67            this.host = this.getHost();
68            this.server_id = this.getServerId();
69
70            this.setNetworkInfo = function(data){
71                this.hostname = data.hostname;
72                this.mac_address = data.mac_address;
73            }
74
75            this.setPowerOnState = function(){
76                this.server_state = Constants.HOST_STATE_TEXT.on;
77                this.server_status = Constants.HOST_STATE.on;
78            }
79
80            this.setPowerOffState = function(){
81                this.server_state = Constants.HOST_STATE_TEXT.off;
82                this.server_status = Constants.HOST_STATE.off;
83            }
84
85            this.setBootingState = function(){
86                this.server_state = Constants.HOST_STATE_TEXT.booting;
87                this.server_status = Constants.HOST_STATE.booting;
88            }
89
90            this.setUnreachableState = function(){
91                this.server_state = Constants.HOST_STATE_TEXT.unreachable;
92                this.server_status = Constants.HOST_STATE.unreachable;
93            }
94
95            this.setRemoteWindowActive = function(){
96                this.remote_window_active = true;
97            }
98
99            this.setRemoteWindowInactive = function(){
100                this.remote_window_active = false;
101            }
102
103            this.updateServerHealth = function(logs){
104                var criticals = logs.filter(function(item){
105                    return item.health_flags.critical == true;
106                });
107
108                if(criticals.length){
109                    this.server_health = Constants.SERVER_HEALTH.critical;
110                    return;
111                }
112
113                var warnings = logs.filter(function(item){
114                    return item.health_flags.warning == true;
115                });
116
117                if(warnings.length){
118                    this.server_health = Constants.SERVER_HEALTH.warning;
119                    return;
120                }
121
122                this.server_health = Constants.SERVER_HEALTH.good;
123            }
124        }]);
125
126})(window.angular);
127