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