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