Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1 | window.angular && (function(angular) { |
| 2 | 'use strict'; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 3 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 4 | angular.module('app.common.directives').directive('appHeader', [ |
| 5 | 'APIUtils', |
| 6 | function(APIUtils) { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 7 | return { |
| 8 | 'restrict': 'E', |
| 9 | 'template': require('./app-header.html'), |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 10 | 'scope': {'path': '='}, |
| 11 | 'controller': [ |
James Feist | 4a16a02 | 2020-04-24 13:11:31 -0700 | [diff] [blame] | 12 | '$rootScope', '$cookies', '$scope', 'dataService', 'userModel', |
| 13 | '$location', '$route', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 14 | function( |
James Feist | 4a16a02 | 2020-04-24 13:11:31 -0700 | [diff] [blame] | 15 | $rootScope, $cookies, $scope, dataService, userModel, $location, |
| 16 | $route) { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 17 | $scope.dataService = dataService; |
Yoshie Muranaka | 4148f2e | 2020-01-29 13:21:12 -0800 | [diff] [blame] | 18 | $scope.username = ''; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 19 | |
Yoshie Muranaka | 0433e00 | 2019-08-13 16:35:16 -0500 | [diff] [blame] | 20 | try { |
| 21 | // Create a secure websocket with URL as /subscribe |
| 22 | // TODO: Need to put in a generic APIUtils to avoid duplicate |
| 23 | // controller |
James Feist | 4a16a02 | 2020-04-24 13:11:31 -0700 | [diff] [blame] | 24 | var token = $cookies.get('XSRF-TOKEN'); |
Yoshie Muranaka | 0433e00 | 2019-08-13 16:35:16 -0500 | [diff] [blame] | 25 | var ws = new WebSocket( |
James Feist | 4a16a02 | 2020-04-24 13:11:31 -0700 | [diff] [blame] | 26 | 'wss://' + dataService.server_id + '/subscribe', [token]); |
Yoshie Muranaka | 0433e00 | 2019-08-13 16:35:16 -0500 | [diff] [blame] | 27 | } catch (error) { |
| 28 | console.log('WebSocket', error); |
| 29 | } |
Jayashankar Padath | a4ec467 | 2018-04-27 18:44:13 +0530 | [diff] [blame] | 30 | |
Yoshie Muranaka | 0433e00 | 2019-08-13 16:35:16 -0500 | [diff] [blame] | 31 | if (ws !== undefined) { |
| 32 | // Specify the required event details as JSON dictionary |
| 33 | var data = JSON.stringify({ |
| 34 | 'paths': ['/xyz/openbmc_project/state/host0'], |
| 35 | 'interfaces': ['xyz.openbmc_project.State.Host'] |
| 36 | }); |
Jayashankar Padath | a4ec467 | 2018-04-27 18:44:13 +0530 | [diff] [blame] | 37 | |
Yoshie Muranaka | 0433e00 | 2019-08-13 16:35:16 -0500 | [diff] [blame] | 38 | // Send the JSON dictionary data to host |
| 39 | ws.onopen = function() { |
| 40 | ws.send(data); |
| 41 | console.log('host0 ws opened'); |
| 42 | }; |
Jayashankar Padath | a4ec467 | 2018-04-27 18:44:13 +0530 | [diff] [blame] | 43 | |
Yoshie Muranaka | 0433e00 | 2019-08-13 16:35:16 -0500 | [diff] [blame] | 44 | // Close the web socket |
| 45 | ws.onclose = function() { |
| 46 | console.log('host0 ws closed'); |
| 47 | }; |
Jayashankar Padath | a4ec467 | 2018-04-27 18:44:13 +0530 | [diff] [blame] | 48 | |
Yoshie Muranaka | 0433e00 | 2019-08-13 16:35:16 -0500 | [diff] [blame] | 49 | // Websocket event handling function which catches the |
| 50 | // current host state |
| 51 | ws.onmessage = function(evt) { |
| 52 | // Parse the response (JSON dictionary data) |
| 53 | var content = JSON.parse(evt.data); |
Jayashankar Padath | a4ec467 | 2018-04-27 18:44:13 +0530 | [diff] [blame] | 54 | |
Yoshie Muranaka | 0433e00 | 2019-08-13 16:35:16 -0500 | [diff] [blame] | 55 | // Fetch the current server power state |
| 56 | if (content.hasOwnProperty('properties') && |
| 57 | content['properties'].hasOwnProperty('CurrentHostState')) { |
| 58 | // Refresh the host state and status |
| 59 | // TODO: As of now not using the current host state |
| 60 | // value for updating the data Service state rather |
| 61 | // using it to detect the command line state change. |
| 62 | // Tried different methods like creating a separate |
| 63 | // function, adding ws under $scope etc.. but auto |
| 64 | // refresh is not happening. |
| 65 | $scope.loadServerStatus(); |
| 66 | } |
| 67 | }; |
| 68 | } |
Jayashankar Padath | a4ec467 | 2018-04-27 18:44:13 +0530 | [diff] [blame] | 69 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 70 | $scope.loadServerHealth = function() { |
| 71 | APIUtils.getLogs().then(function(result) { |
| 72 | dataService.updateServerHealth(result.data); |
| 73 | }); |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 74 | }; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 75 | |
| 76 | $scope.loadServerStatus = function() { |
| 77 | if (!userModel.isLoggedIn()) { |
| 78 | return; |
| 79 | } |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 80 | APIUtils.getHostState().then( |
| 81 | function(status) { |
| 82 | if (status == |
| 83 | 'xyz.openbmc_project.State.Host.HostState.Off') { |
| 84 | dataService.setPowerOffState(); |
| 85 | } else if ( |
| 86 | status == |
| 87 | 'xyz.openbmc_project.State.Host.HostState.Running') { |
| 88 | dataService.setPowerOnState(); |
| 89 | } else { |
| 90 | dataService.setErrorState(); |
| 91 | } |
| 92 | }, |
| 93 | function(error) { |
Jayashankar Padath | a38a287 | 2018-06-07 11:34:58 +0530 | [diff] [blame] | 94 | console.log(error); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 95 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | $scope.loadNetworkInfo = function() { |
| 99 | if (!userModel.isLoggedIn()) { |
| 100 | return; |
| 101 | } |
| 102 | APIUtils.getNetworkInfo().then(function(data) { |
| 103 | dataService.setNetworkInfo(data); |
| 104 | }); |
| 105 | }; |
| 106 | |
AppaRao Puli | b1289ec | 2018-11-14 20:33:30 +0530 | [diff] [blame] | 107 | $scope.loadSystemName = function() { |
| 108 | // Dynamically get ComputerSystems Name/serial |
| 109 | // which differs across OEM's |
| 110 | APIUtils.getRedfishSysName().then( |
| 111 | function(res) { |
| 112 | dataService.setSystemName(res); |
| 113 | }, |
| 114 | function(error) { |
| 115 | console.log(JSON.stringify(error)); |
| 116 | }); |
| 117 | }; |
| 118 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 119 | function loadData() { |
| 120 | $scope.loadServerStatus(); |
| 121 | $scope.loadNetworkInfo(); |
| 122 | $scope.loadServerHealth(); |
AppaRao Puli | b1289ec | 2018-11-14 20:33:30 +0530 | [diff] [blame] | 123 | $scope.loadSystemName(); |
Yoshie Muranaka | 4148f2e | 2020-01-29 13:21:12 -0800 | [diff] [blame] | 124 | $scope.username = dataService.getUser(); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | loadData(); |
| 128 | |
| 129 | $scope.logout = function() { |
| 130 | userModel.logout(function(status, error) { |
| 131 | if (status) { |
| 132 | $location.path('/logout'); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 133 | } else { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 134 | console.log(error); |
| 135 | } |
| 136 | }); |
| 137 | }; |
| 138 | |
| 139 | $scope.refresh = function() { |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 140 | // reload current page controllers and header |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 141 | loadData(); |
| 142 | $route.reload(); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 143 | // Add flash class to header timestamp on click of refresh |
| 144 | var myEl = |
| 145 | angular.element(document.querySelector('.header__refresh')); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 146 | myEl.addClass('flash'); |
| 147 | setTimeout(function() { |
| 148 | myEl.removeClass('flash'); |
| 149 | }, 2000); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 150 | }; |
| 151 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 152 | var loginListener = |
| 153 | $rootScope.$on('user-logged-in', function(event, arg) { |
| 154 | loadData(); |
| 155 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 156 | |
| 157 | $scope.$on('$destroy', function() { |
| 158 | loginListener(); |
| 159 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 160 | } |
| 161 | ] |
| 162 | }; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 163 | } |
| 164 | ]); |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 165 | })(window.angular); |