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.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;
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.host = this.getHost();
64            this.server_id = this.getServerId();
65
66            this.setNetworkInfo = function(data){
67                this.hostname = data.hostname;
68                this.mac_address = data.mac_address;
69            }
70
71            this.setPowerOnState = function(){
72                this.server_state = Constants.HOST_STATE_TEXT.on;
73                this.server_status = Constants.HOST_STATE.on;
74            }
75
76            this.setPowerOffState = function(){
77                this.server_state = Constants.HOST_STATE_TEXT.off;
78                this.server_status = Constants.HOST_STATE.off;
79            }
80
81            this.setBootingState = function(){
82                this.server_state = Constants.HOST_STATE_TEXT.booting;
83                this.server_status = Constants.HOST_STATE.booting;
84            }
85
86            this.setUnreachableState = function(){
87                this.server_state = Constants.HOST_STATE_TEXT.unreachable;
88                this.server_status = Constants.HOST_STATE.unreachable;
89            }
90
91            this.setRemoteWindowActive = function(){
92                this.remote_window_active = true;
93            }
94
95            this.setRemoteWindowInactive = function(){
96                this.remote_window_active = false;
97            }
98
99            this.updateServerHealth = function(logs){
100                var criticals = logs.filter(function(item){
101                    return item.health_flags.critical == true;
102                });
103
104                if(criticals.length){
105                    this.server_health = Constants.SERVER_HEALTH.critical;
106                    return;
107                }
108
109                var warnings = logs.filter(function(item){
110                    return item.health_flags.warning == true;
111                });
112
113                if(warnings.length){
114                    this.server_health = Constants.SERVER_HEALTH.warning;
115                    return;
116                }
117
118                this.server_health = Constants.SERVER_HEALTH.good;
119            }
120        }]);
121
122})(window.angular);
123