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