199d199f3SIftekharul Islamwindow.angular && (function(angular) { 299d199f3SIftekharul Islam 'use strict'; 399d199f3SIftekharul Islam 4d27bb135SAndrew Geissler angular.module('app.common.directives').directive('appHeader', [ 5d27bb135SAndrew Geissler 'APIUtils', 6d27bb135SAndrew Geissler function(APIUtils) { 799d199f3SIftekharul Islam return { 899d199f3SIftekharul Islam 'restrict': 'E', 9bbcf670aSEd Tanous 'template': require('./app-header.html'), 10d27bb135SAndrew Geissler 'scope': {'path': '='}, 11d27bb135SAndrew Geissler 'controller': [ 12d27bb135SAndrew Geissler '$rootScope', '$scope', 'dataService', 'userModel', '$location', 13d27bb135SAndrew Geissler '$route', 14d27bb135SAndrew Geissler function( 15d27bb135SAndrew Geissler $rootScope, $scope, dataService, userModel, $location, $route) { 1699d199f3SIftekharul Islam $scope.dataService = dataService; 1799d199f3SIftekharul Islam 18*0433e005SYoshie Muranaka try { 19a4ec4679SJayashankar Padath // Create a secure websocket with URL as /subscribe 20a4ec4679SJayashankar Padath // TODO: Need to put in a generic APIUtils to avoid duplicate 21a4ec4679SJayashankar Padath // controller 22*0433e005SYoshie Muranaka var ws = new WebSocket( 23*0433e005SYoshie Muranaka 'wss://' + dataService.server_id + '/subscribe'); 24*0433e005SYoshie Muranaka } catch (error) { 25*0433e005SYoshie Muranaka console.log('WebSocket', error); 26*0433e005SYoshie Muranaka } 27a4ec4679SJayashankar Padath 28*0433e005SYoshie Muranaka if (ws !== undefined) { 29a4ec4679SJayashankar Padath // Specify the required event details as JSON dictionary 30a4ec4679SJayashankar Padath var data = JSON.stringify({ 31a4ec4679SJayashankar Padath 'paths': ['/xyz/openbmc_project/state/host0'], 32a4ec4679SJayashankar Padath 'interfaces': ['xyz.openbmc_project.State.Host'] 33a4ec4679SJayashankar Padath }); 34a4ec4679SJayashankar Padath 35a4ec4679SJayashankar Padath // Send the JSON dictionary data to host 36a4ec4679SJayashankar Padath ws.onopen = function() { 37a4ec4679SJayashankar Padath ws.send(data); 38a4ec4679SJayashankar Padath console.log('host0 ws opened'); 39a4ec4679SJayashankar Padath }; 40a4ec4679SJayashankar Padath 41a4ec4679SJayashankar Padath // Close the web socket 42a4ec4679SJayashankar Padath ws.onclose = function() { 43a4ec4679SJayashankar Padath console.log('host0 ws closed'); 44a4ec4679SJayashankar Padath }; 45a4ec4679SJayashankar Padath 46a4ec4679SJayashankar Padath // Websocket event handling function which catches the 47a4ec4679SJayashankar Padath // current host state 48a4ec4679SJayashankar Padath ws.onmessage = function(evt) { 49a4ec4679SJayashankar Padath // Parse the response (JSON dictionary data) 50a4ec4679SJayashankar Padath var content = JSON.parse(evt.data); 51a4ec4679SJayashankar Padath 52a4ec4679SJayashankar Padath // Fetch the current server power state 53a4ec4679SJayashankar Padath if (content.hasOwnProperty('properties') && 54a4ec4679SJayashankar Padath content['properties'].hasOwnProperty('CurrentHostState')) { 55a4ec4679SJayashankar Padath // Refresh the host state and status 56a4ec4679SJayashankar Padath // TODO: As of now not using the current host state 57a4ec4679SJayashankar Padath // value for updating the data Service state rather 58a4ec4679SJayashankar Padath // using it to detect the command line state change. 59a4ec4679SJayashankar Padath // Tried different methods like creating a separate 60ffdef96dSRebecca Shaw // function, adding ws under $scope etc.. but auto 61a4ec4679SJayashankar Padath // refresh is not happening. 62a4ec4679SJayashankar Padath $scope.loadServerStatus(); 63a4ec4679SJayashankar Padath } 64a4ec4679SJayashankar Padath }; 65*0433e005SYoshie Muranaka } 66a4ec4679SJayashankar Padath 67c22425f2SIftekharul Islam $scope.loadServerHealth = function() { 68c22425f2SIftekharul Islam APIUtils.getLogs().then(function(result) { 69c22425f2SIftekharul Islam dataService.updateServerHealth(result.data); 70c22425f2SIftekharul Islam }); 71ba5e3f34SAndrew Geissler }; 72c22425f2SIftekharul Islam 7399d199f3SIftekharul Islam $scope.loadServerStatus = function() { 7499d199f3SIftekharul Islam if (!userModel.isLoggedIn()) { 7599d199f3SIftekharul Islam return; 7699d199f3SIftekharul Islam } 77d27bb135SAndrew Geissler APIUtils.getHostState().then( 78d27bb135SAndrew Geissler function(status) { 79d27bb135SAndrew Geissler if (status == 80d27bb135SAndrew Geissler 'xyz.openbmc_project.State.Host.HostState.Off') { 8199d199f3SIftekharul Islam dataService.setPowerOffState(); 82d27bb135SAndrew Geissler } else if ( 83d27bb135SAndrew Geissler status == 84d27bb135SAndrew Geissler 'xyz.openbmc_project.State.Host.HostState.Running') { 8599d199f3SIftekharul Islam dataService.setPowerOnState(); 86d27bb135SAndrew Geissler } else { 87d80c280bSCamVan Nguyen dataService.setErrorState(); 8899d199f3SIftekharul Islam } 89d27bb135SAndrew Geissler }, 90d27bb135SAndrew Geissler function(error) { 91a38a287cSJayashankar Padath console.log(error); 9299d199f3SIftekharul Islam }); 93ba5e3f34SAndrew Geissler }; 94ba556c31SIftekharul Islam 95ba556c31SIftekharul Islam $scope.loadNetworkInfo = function() { 96ba556c31SIftekharul Islam if (!userModel.isLoggedIn()) { 97ba556c31SIftekharul Islam return; 98ba556c31SIftekharul Islam } 99ba556c31SIftekharul Islam APIUtils.getNetworkInfo().then(function(data) { 100ba556c31SIftekharul Islam dataService.setNetworkInfo(data); 101ba556c31SIftekharul Islam }); 102ba5e3f34SAndrew Geissler }; 103ba556c31SIftekharul Islam 104b1289ec9SAppaRao Puli $scope.loadSystemName = function() { 105b1289ec9SAppaRao Puli // Dynamically get ComputerSystems Name/serial 106b1289ec9SAppaRao Puli // which differs across OEM's 107b1289ec9SAppaRao Puli APIUtils.getRedfishSysName().then( 108b1289ec9SAppaRao Puli function(res) { 109b1289ec9SAppaRao Puli dataService.setSystemName(res); 110b1289ec9SAppaRao Puli }, 111b1289ec9SAppaRao Puli function(error) { 112b1289ec9SAppaRao Puli console.log(JSON.stringify(error)); 113b1289ec9SAppaRao Puli }); 114b1289ec9SAppaRao Puli }; 115b1289ec9SAppaRao Puli 116c22425f2SIftekharul Islam function loadData() { 11799d199f3SIftekharul Islam $scope.loadServerStatus(); 118ba556c31SIftekharul Islam $scope.loadNetworkInfo(); 119c22425f2SIftekharul Islam $scope.loadServerHealth(); 120b1289ec9SAppaRao Puli $scope.loadSystemName(); 121c22425f2SIftekharul Islam } 122c22425f2SIftekharul Islam 123c22425f2SIftekharul Islam loadData(); 12499d199f3SIftekharul Islam 12599d199f3SIftekharul Islam $scope.logout = function() { 12699d199f3SIftekharul Islam userModel.logout(function(status, error) { 12799d199f3SIftekharul Islam if (status) { 12899d199f3SIftekharul Islam $location.path('/logout'); 129d27bb135SAndrew Geissler } else { 13099d199f3SIftekharul Islam console.log(error); 13199d199f3SIftekharul Islam } 13299d199f3SIftekharul Islam }); 133ba5e3f34SAndrew Geissler }; 13499d199f3SIftekharul Islam 13599d199f3SIftekharul Islam $scope.refresh = function() { 136a72686f7SAndrew Geissler // reload current page controllers and header 137c22425f2SIftekharul Islam loadData(); 138a72686f7SAndrew Geissler $route.reload(); 139a5f222a1SMichael Davis // Add flash class to header timestamp on click of refresh 140d27bb135SAndrew Geissler var myEl = 141d27bb135SAndrew Geissler angular.element(document.querySelector('.header__refresh')); 142a5f222a1SMichael Davis myEl.addClass('flash'); 143a5f222a1SMichael Davis setTimeout(function() { 144ba5e3f34SAndrew Geissler myEl.removeClass('flash'); 145a5f222a1SMichael Davis }, 2000); 146ba5e3f34SAndrew Geissler }; 14799d199f3SIftekharul Islam 148d27bb135SAndrew Geissler var loginListener = 149d27bb135SAndrew Geissler $rootScope.$on('user-logged-in', function(event, arg) { 150c22425f2SIftekharul Islam loadData(); 15199d199f3SIftekharul Islam }); 15299d199f3SIftekharul Islam 15399d199f3SIftekharul Islam $scope.$on('$destroy', function() { 15499d199f3SIftekharul Islam loginListener(); 15599d199f3SIftekharul Islam }); 156ba5e3f34SAndrew Geissler } 157ba5e3f34SAndrew Geissler ] 15899d199f3SIftekharul Islam }; 159d27bb135SAndrew Geissler } 160d27bb135SAndrew Geissler ]); 16199d199f3SIftekharul Islam})(window.angular); 162