blob: df39772b13e5ce01b4a5a32843069e2c3cfd11b0 [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': [
12 '$rootScope', '$scope', 'dataService', 'userModel', '$location',
13 '$route',
14 function(
15 $rootScope, $scope, dataService, userModel, $location, $route) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070016 $scope.dataService = dataService;
Yoshie Muranaka4148f2e2020-01-29 13:21:12 -080017 $scope.username = '';
Iftekharul Islam99d199f2017-03-24 15:28:25 -050018
Yoshie Muranaka0433e002019-08-13 16:35:16 -050019 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 }
Jayashankar Padatha4ec4672018-04-27 18:44:13 +053028
Yoshie Muranaka0433e002019-08-13 16:35:16 -050029 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 });
Jayashankar Padatha4ec4672018-04-27 18:44:13 +053035
Yoshie Muranaka0433e002019-08-13 16:35:16 -050036 // Send the JSON dictionary data to host
37 ws.onopen = function() {
38 ws.send(data);
39 console.log('host0 ws opened');
40 };
Jayashankar Padatha4ec4672018-04-27 18:44:13 +053041
Yoshie Muranaka0433e002019-08-13 16:35:16 -050042 // Close the web socket
43 ws.onclose = function() {
44 console.log('host0 ws closed');
45 };
Jayashankar Padatha4ec4672018-04-27 18:44:13 +053046
Yoshie Muranaka0433e002019-08-13 16:35:16 -050047 // 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);
Jayashankar Padatha4ec4672018-04-27 18:44:13 +053052
Yoshie Muranaka0433e002019-08-13 16:35:16 -050053 // 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 }
Jayashankar Padatha4ec4672018-04-27 18:44:13 +053067
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070068 $scope.loadServerHealth = function() {
69 APIUtils.getLogs().then(function(result) {
70 dataService.updateServerHealth(result.data);
71 });
Iftekharul Islam99d199f2017-03-24 15:28:25 -050072 };
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070073
74 $scope.loadServerStatus = function() {
75 if (!userModel.isLoggedIn()) {
76 return;
77 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -070078 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) {
Jayashankar Padatha38a2872018-06-07 11:34:58 +053092 console.log(error);
Andrew Geisslerd27bb132018-05-24 11:07:27 -070093 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070094 };
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
AppaRao Pulib1289ec2018-11-14 20:33:30 +0530105 $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
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700117 function loadData() {
118 $scope.loadServerStatus();
119 $scope.loadNetworkInfo();
120 $scope.loadServerHealth();
AppaRao Pulib1289ec2018-11-14 20:33:30 +0530121 $scope.loadSystemName();
Yoshie Muranaka4148f2e2020-01-29 13:21:12 -0800122 $scope.username = dataService.getUser();
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700123 }
124
125 loadData();
126
127 $scope.logout = function() {
128 userModel.logout(function(status, error) {
129 if (status) {
130 $location.path('/logout');
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700131 } else {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700132 console.log(error);
133 }
134 });
135 };
136
137 $scope.refresh = function() {
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700138 // reload current page controllers and header
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700139 loadData();
140 $route.reload();
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700141 // Add flash class to header timestamp on click of refresh
142 var myEl =
143 angular.element(document.querySelector('.header__refresh'));
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700144 myEl.addClass('flash');
145 setTimeout(function() {
146 myEl.removeClass('flash');
147 }, 2000);
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700148 };
149
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700150 var loginListener =
151 $rootScope.$on('user-logged-in', function(event, arg) {
152 loadData();
153 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700154
155 $scope.$on('$destroy', function() {
156 loginListener();
157 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700158 }
159 ]
160 };
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700161 }
162 ]);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500163})(window.angular);