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.module('app.common.services').service('dataService', [
14    'Constants',
15    function(Constants) {
16      this.server_health = Constants.SERVER_HEALTH.unknown;
17      this.server_state = 'Unreachable';
18      this.LED_state = Constants.LED_STATE_TEXT.off;
19      this.last_updated = new Date();
20
21      this.loading = false;
22      this.server_unreachable = false;
23      this.showNavigation = false;
24      this.bodyStyle = {};
25      this.path = '';
26
27      this.hostname = '';
28      this.mac_address = '';
29      this.defaultgateway = '';
30
31      this.ignoreHttpError = false;
32      this.systemName = '';
33
34      this.configJson = require('../../../config.json');
35
36      this.getServerId = function() {
37        return this.host.replace(/^https?\:\/\//ig, '');
38      };
39
40      this.reloadServerId = function() {
41        this.server_id = this.getServerId();
42      };
43
44      this.getHost = function() {
45        if (sessionStorage.getItem(
46                Constants.API_CREDENTIALS.host_storage_key) !== null) {
47          return sessionStorage.getItem(
48              Constants.API_CREDENTIALS.host_storage_key);
49        } else {
50          return Constants.API_CREDENTIALS.default_protocol + '://' +
51              window.location.hostname +
52              (window.location.port ? ':' + window.location.port : '');
53        }
54      };
55
56      this.setHost = function(hostWithPort) {
57        hostWithPort = hostWithPort.replace(/^https?\:\/\//ig, '');
58        var hostURL =
59            Constants.API_CREDENTIALS.default_protocol + '://' + hostWithPort;
60        sessionStorage.setItem(
61            Constants.API_CREDENTIALS.host_storage_key, hostURL);
62        this.host = hostURL;
63        this.reloadServerId();
64      };
65
66      this.getUser = function() {
67        return sessionStorage.getItem('LOGIN_ID');
68      };
69
70      this.host = this.getHost();
71      this.server_id = this.getServerId();
72
73      this.setNetworkInfo = function(data) {
74        this.hostname = data.hostname;
75        this.defaultgateway = data.defaultgateway;
76        this.mac_address = data.mac_address;
77      };
78
79      this.setPowerOnState = function() {
80        this.server_state = Constants.HOST_STATE_TEXT.on;
81      };
82
83      this.setPowerOffState = function() {
84        this.server_state = Constants.HOST_STATE_TEXT.off;
85      };
86
87      this.setErrorState = function() {
88        this.server_state = Constants.HOST_STATE_TEXT.error;
89      };
90
91      this.setUnreachableState = function() {
92        this.server_state = Constants.HOST_STATE_TEXT.unreachable;
93      };
94
95      this.updateServerHealth = function(logs) {
96        // If any unresolved severity high logs are present, set server health
97        // to critical. Else if any unresolved severity medium logs are present
98        // set server health to warning.
99        this.server_health = Constants.SERVER_HEALTH.good;
100        for (var log of logs) {
101          if (log.priority == 'High' && !log.Resolved) {
102            this.server_health = Constants.SERVER_HEALTH.critical;
103            return;
104          } else if (log.priority == 'Medium' && !log.Resolved) {
105            this.server_health = Constants.SERVER_HEALTH.warning;
106          }
107        }
108      };
109
110      this.setSystemName = function(sysName) {
111        this.systemName = sysName;
112      };
113    }
114  ]);
115})(window.angular);
116