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 | |
Jayashankar Padath | a4ec467 | 2018-04-27 18:44:13 +0530 | [diff] [blame^] | 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, addding ws under $scope etc.. but auto |
| 56 | // refresh is not happening. |
| 57 | $scope.loadServerStatus(); |
| 58 | } |
| 59 | }; |
| 60 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 61 | $scope.loadServerHealth = function() { |
| 62 | APIUtils.getLogs().then(function(result) { |
| 63 | dataService.updateServerHealth(result.data); |
| 64 | }); |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 65 | }; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 66 | |
| 67 | $scope.loadServerStatus = function() { |
| 68 | if (!userModel.isLoggedIn()) { |
| 69 | return; |
| 70 | } |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 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) { |
Jayashankar Padath | a38a287 | 2018-06-07 11:34:58 +0530 | [diff] [blame] | 85 | console.log(error); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 86 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 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 | function loadData() { |
| 99 | $scope.loadServerStatus(); |
| 100 | $scope.loadNetworkInfo(); |
| 101 | $scope.loadServerHealth(); |
| 102 | } |
| 103 | |
| 104 | loadData(); |
| 105 | |
| 106 | $scope.logout = function() { |
| 107 | userModel.logout(function(status, error) { |
| 108 | if (status) { |
| 109 | $location.path('/logout'); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 110 | } else { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 111 | console.log(error); |
| 112 | } |
| 113 | }); |
| 114 | }; |
| 115 | |
| 116 | $scope.refresh = function() { |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 117 | // reload current page controllers and header |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 118 | loadData(); |
| 119 | $route.reload(); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 120 | // Add flash class to header timestamp on click of refresh |
| 121 | var myEl = |
| 122 | angular.element(document.querySelector('.header__refresh')); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 123 | myEl.addClass('flash'); |
| 124 | setTimeout(function() { |
| 125 | myEl.removeClass('flash'); |
| 126 | }, 2000); |
| 127 | |
| 128 | }; |
| 129 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 130 | var loginListener = |
| 131 | $rootScope.$on('user-logged-in', function(event, arg) { |
| 132 | loadData(); |
| 133 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 134 | |
| 135 | $scope.$on('$destroy', function() { |
| 136 | loginListener(); |
| 137 | }); |
| 138 | |
| 139 | $scope.multiRecent = function() { |
| 140 | $scope.multi_server_recent = !$scope.multi_server_recent; |
| 141 | }; |
| 142 | } |
| 143 | ] |
| 144 | }; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 145 | } |
| 146 | ]); |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 147 | })(window.angular); |