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