blob: 7f8459f9b267869e495be94c8d8e9c3faea31d52 [file] [log] [blame]
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001window.angular && (function(angular) {
2 'use strict';
Iftekharul Islam99d199f2017-03-24 15:28:25 -05003
Andrew Geisslerd27bb132018-05-24 11:07:27 -07004 angular.module('app.common.directives').directive('appHeader', [
5 'APIUtils',
6 function(APIUtils) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07007 return {
8 'restrict': 'E',
9 'template': require('./app-header.html'),
Andrew Geisslerd27bb132018-05-24 11:07:27 -070010 'scope': {'path': '='},
11 'controller': [
James Feist4a16a022020-04-24 13:11:31 -070012 '$rootScope', '$cookies', '$scope', 'dataService', 'userModel',
13 '$location', '$route',
Andrew Geisslerd27bb132018-05-24 11:07:27 -070014 function(
James Feist4a16a022020-04-24 13:11:31 -070015 $rootScope, $cookies, $scope, dataService, userModel, $location,
16 $route) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070017 $scope.dataService = dataService;
Yoshie Muranaka4148f2e2020-01-29 13:21:12 -080018 $scope.username = '';
Iftekharul Islam99d199f2017-03-24 15:28:25 -050019
Yoshie Muranaka0433e002019-08-13 16:35:16 -050020 try {
21 // Create a secure websocket with URL as /subscribe
22 // TODO: Need to put in a generic APIUtils to avoid duplicate
23 // controller
James Feist4a16a022020-04-24 13:11:31 -070024 var token = $cookies.get('XSRF-TOKEN');
Yoshie Muranaka0433e002019-08-13 16:35:16 -050025 var ws = new WebSocket(
James Feist4a16a022020-04-24 13:11:31 -070026 'wss://' + dataService.server_id + '/subscribe', [token]);
Yoshie Muranaka0433e002019-08-13 16:35:16 -050027 } catch (error) {
28 console.log('WebSocket', error);
29 }
Jayashankar Padatha4ec4672018-04-27 18:44:13 +053030
Yoshie Muranaka0433e002019-08-13 16:35:16 -050031 if (ws !== undefined) {
32 // Specify the required event details as JSON dictionary
33 var data = JSON.stringify({
34 'paths': ['/xyz/openbmc_project/state/host0'],
35 'interfaces': ['xyz.openbmc_project.State.Host']
36 });
Jayashankar Padatha4ec4672018-04-27 18:44:13 +053037
Yoshie Muranaka0433e002019-08-13 16:35:16 -050038 // Send the JSON dictionary data to host
39 ws.onopen = function() {
40 ws.send(data);
41 console.log('host0 ws opened');
42 };
Jayashankar Padatha4ec4672018-04-27 18:44:13 +053043
Yoshie Muranaka0433e002019-08-13 16:35:16 -050044 // Close the web socket
45 ws.onclose = function() {
46 console.log('host0 ws closed');
47 };
Jayashankar Padatha4ec4672018-04-27 18:44:13 +053048
Yoshie Muranaka0433e002019-08-13 16:35:16 -050049 // Websocket event handling function which catches the
50 // current host state
51 ws.onmessage = function(evt) {
52 // Parse the response (JSON dictionary data)
53 var content = JSON.parse(evt.data);
Jayashankar Padatha4ec4672018-04-27 18:44:13 +053054
Yoshie Muranaka0433e002019-08-13 16:35:16 -050055 // Fetch the current server power state
56 if (content.hasOwnProperty('properties') &&
57 content['properties'].hasOwnProperty('CurrentHostState')) {
58 // Refresh the host state and status
59 // TODO: As of now not using the current host state
60 // value for updating the data Service state rather
61 // using it to detect the command line state change.
62 // Tried different methods like creating a separate
63 // function, adding ws under $scope etc.. but auto
64 // refresh is not happening.
65 $scope.loadServerStatus();
66 }
67 };
68 }
Jayashankar Padatha4ec4672018-04-27 18:44:13 +053069
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070070 $scope.loadServerHealth = function() {
71 APIUtils.getLogs().then(function(result) {
72 dataService.updateServerHealth(result.data);
73 });
Iftekharul Islam99d199f2017-03-24 15:28:25 -050074 };
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070075
76 $scope.loadServerStatus = function() {
77 if (!userModel.isLoggedIn()) {
78 return;
79 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -070080 APIUtils.getHostState().then(
81 function(status) {
82 if (status ==
83 'xyz.openbmc_project.State.Host.HostState.Off') {
84 dataService.setPowerOffState();
85 } else if (
86 status ==
87 'xyz.openbmc_project.State.Host.HostState.Running') {
88 dataService.setPowerOnState();
89 } else {
90 dataService.setErrorState();
91 }
92 },
93 function(error) {
Jayashankar Padatha38a2872018-06-07 11:34:58 +053094 console.log(error);
Andrew Geisslerd27bb132018-05-24 11:07:27 -070095 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070096 };
97
98 $scope.loadNetworkInfo = function() {
99 if (!userModel.isLoggedIn()) {
100 return;
101 }
102 APIUtils.getNetworkInfo().then(function(data) {
103 dataService.setNetworkInfo(data);
104 });
105 };
106
AppaRao Pulib1289ec2018-11-14 20:33:30 +0530107 $scope.loadSystemName = function() {
108 // Dynamically get ComputerSystems Name/serial
109 // which differs across OEM's
110 APIUtils.getRedfishSysName().then(
111 function(res) {
112 dataService.setSystemName(res);
113 },
114 function(error) {
115 console.log(JSON.stringify(error));
116 });
117 };
118
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700119 function loadData() {
120 $scope.loadServerStatus();
121 $scope.loadNetworkInfo();
122 $scope.loadServerHealth();
AppaRao Pulib1289ec2018-11-14 20:33:30 +0530123 $scope.loadSystemName();
Yoshie Muranaka4148f2e2020-01-29 13:21:12 -0800124 $scope.username = dataService.getUser();
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700125 }
126
127 loadData();
128
129 $scope.logout = function() {
130 userModel.logout(function(status, error) {
131 if (status) {
132 $location.path('/logout');
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700133 } else {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700134 console.log(error);
135 }
136 });
137 };
138
139 $scope.refresh = function() {
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700140 // reload current page controllers and header
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700141 loadData();
142 $route.reload();
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700143 // Add flash class to header timestamp on click of refresh
144 var myEl =
145 angular.element(document.querySelector('.header__refresh'));
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700146 myEl.addClass('flash');
147 setTimeout(function() {
148 myEl.removeClass('flash');
149 }, 2000);
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700150 };
151
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700152 var loginListener =
153 $rootScope.$on('user-logged-in', function(event, arg) {
154 loadData();
155 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700156
157 $scope.$on('$destroy', function() {
158 loginListener();
159 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700160 }
161 ]
162 };
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700163 }
164 ]);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500165})(window.angular);