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 $scope.username = ''; 18 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 } 28 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 }); 35 36 // Send the JSON dictionary data to host 37 ws.onopen = function() { 38 ws.send(data); 39 console.log('host0 ws opened'); 40 }; 41 42 // Close the web socket 43 ws.onclose = function() { 44 console.log('host0 ws closed'); 45 }; 46 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); 52 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 } 67 68 $scope.loadServerHealth = function() { 69 APIUtils.getLogs().then(function(result) { 70 dataService.updateServerHealth(result.data); 71 }); 72 }; 73 74 $scope.loadServerStatus = function() { 75 if (!userModel.isLoggedIn()) { 76 return; 77 } 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) { 92 console.log(error); 93 }); 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 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 117 function loadData() { 118 $scope.loadServerStatus(); 119 $scope.loadNetworkInfo(); 120 $scope.loadServerHealth(); 121 $scope.loadSystemName(); 122 $scope.username = dataService.getUser(); 123 } 124 125 loadData(); 126 127 $scope.logout = function() { 128 userModel.logout(function(status, error) { 129 if (status) { 130 $location.path('/logout'); 131 } else { 132 console.log(error); 133 } 134 }); 135 }; 136 137 $scope.refresh = function() { 138 // reload current page controllers and header 139 loadData(); 140 $route.reload(); 141 // Add flash class to header timestamp on click of refresh 142 var myEl = 143 angular.element(document.querySelector('.header__refresh')); 144 myEl.addClass('flash'); 145 setTimeout(function() { 146 myEl.removeClass('flash'); 147 }, 2000); 148 }; 149 150 var loginListener = 151 $rootScope.$on('user-logged-in', function(event, arg) { 152 loadData(); 153 }); 154 155 $scope.$on('$destroy', function() { 156 loginListener(); 157 }); 158 } 159 ] 160 }; 161 } 162 ]); 163})(window.angular); 164