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