Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 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 | |
| 11 | window.angular && (function (angular) { |
| 12 | 'use strict'; |
| 13 | |
| 14 | angular |
| 15 | .module('app.common.services') |
| 16 | .service('dataService', ['Constants', function (Constants) { |
Michael Davis | 7f89fad | 2017-07-31 18:36:45 -0500 | [diff] [blame] | 17 | this.app_version = "V.0.0.1"; |
Iftekharul Islam | 3471409 | 2017-09-06 10:45:27 -0500 | [diff] [blame] | 18 | this.server_health = Constants.SERVER_HEALTH.unknown; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 19 | this.server_state = 'Unreachable'; |
| 20 | this.server_status = -2; |
| 21 | this.chassis_state = 'On'; |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 22 | this.LED_state = Constants.LED_STATE_TEXT.off; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 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 = ''; |
Iftekharul Islam | d2269e2 | 2017-05-02 09:32:45 -0500 | [diff] [blame] | 31 | this.sensorData = []; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 32 | |
Iftekharul Islam | ba556c3 | 2017-08-11 08:37:12 -0500 | [diff] [blame] | 33 | this.hostname = ""; |
| 34 | this.mac_address = ""; |
Iftekharul Islam | 3471409 | 2017-09-06 10:45:27 -0500 | [diff] [blame] | 35 | this.remote_window_active = false; |
Iftekharul Islam | ba556c3 | 2017-08-11 08:37:12 -0500 | [diff] [blame] | 36 | |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 37 | this.getServerId = function(){ |
Sivas SRR | bb9092a | 2018-01-27 08:40:58 -0600 | [diff] [blame] | 38 | return this.host.replace(/^https?\:\/\//ig,""); |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 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 | |
Gunnar Mills | 1a60f6e | 2018-03-14 13:42:08 -0500 | [diff] [blame^] | 63 | this.getUser = function(){ |
| 64 | return sessionStorage.getItem('LOGIN_ID'); |
| 65 | } |
| 66 | |
Iftekharul Islam | 1acb412 | 2017-11-02 13:20:32 -0500 | [diff] [blame] | 67 | this.host = this.getHost(); |
| 68 | this.server_id = this.getServerId(); |
| 69 | |
Iftekharul Islam | ba556c3 | 2017-08-11 08:37:12 -0500 | [diff] [blame] | 70 | this.setNetworkInfo = function(data){ |
| 71 | this.hostname = data.hostname; |
| 72 | this.mac_address = data.mac_address; |
| 73 | } |
| 74 | |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 75 | this.setPowerOnState = function(){ |
| 76 | this.server_state = Constants.HOST_STATE_TEXT.on; |
| 77 | this.server_status = Constants.HOST_STATE.on; |
Iftekharul Islam | ba556c3 | 2017-08-11 08:37:12 -0500 | [diff] [blame] | 78 | } |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 79 | |
| 80 | this.setPowerOffState = function(){ |
| 81 | this.server_state = Constants.HOST_STATE_TEXT.off; |
| 82 | this.server_status = Constants.HOST_STATE.off; |
Iftekharul Islam | ba556c3 | 2017-08-11 08:37:12 -0500 | [diff] [blame] | 83 | } |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 84 | |
| 85 | this.setBootingState = function(){ |
| 86 | this.server_state = Constants.HOST_STATE_TEXT.booting; |
| 87 | this.server_status = Constants.HOST_STATE.booting; |
Iftekharul Islam | ba556c3 | 2017-08-11 08:37:12 -0500 | [diff] [blame] | 88 | } |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 89 | |
| 90 | this.setUnreachableState = function(){ |
| 91 | this.server_state = Constants.HOST_STATE_TEXT.unreachable; |
| 92 | this.server_status = Constants.HOST_STATE.unreachable; |
| 93 | } |
Iftekharul Islam | 3471409 | 2017-09-06 10:45:27 -0500 | [diff] [blame] | 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 | } |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 124 | }]); |
| 125 | |
Sivas SRR | bb9092a | 2018-01-27 08:40:58 -0600 | [diff] [blame] | 126 | })(window.angular); |