blob: 98d210fcd15e7755be246ce4c6dd93d9d685b0c7 [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;
Iftekharul Islam99d199f2017-03-24 15:28:25 -050017
Yoshie Muranaka0433e002019-08-13 16:35:16 -050018 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 }
Jayashankar Padatha4ec4672018-04-27 18:44:13 +053027
Yoshie Muranaka0433e002019-08-13 16:35:16 -050028 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 });
Jayashankar Padatha4ec4672018-04-27 18:44:13 +053034
Yoshie Muranaka0433e002019-08-13 16:35:16 -050035 // Send the JSON dictionary data to host
36 ws.onopen = function() {
37 ws.send(data);
38 console.log('host0 ws opened');
39 };
Jayashankar Padatha4ec4672018-04-27 18:44:13 +053040
Yoshie Muranaka0433e002019-08-13 16:35:16 -050041 // Close the web socket
42 ws.onclose = function() {
43 console.log('host0 ws closed');
44 };
Jayashankar Padatha4ec4672018-04-27 18:44:13 +053045
Yoshie Muranaka0433e002019-08-13 16:35:16 -050046 // 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);
Jayashankar Padatha4ec4672018-04-27 18:44:13 +053051
Yoshie Muranaka0433e002019-08-13 16:35:16 -050052 // 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 }
Jayashankar Padatha4ec4672018-04-27 18:44:13 +053066
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070067 $scope.loadServerHealth = function() {
68 APIUtils.getLogs().then(function(result) {
69 dataService.updateServerHealth(result.data);
70 });
Iftekharul Islam99d199f2017-03-24 15:28:25 -050071 };
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070072
73 $scope.loadServerStatus = function() {
74 if (!userModel.isLoggedIn()) {
75 return;
76 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -070077 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) {
Jayashankar Padatha38a2872018-06-07 11:34:58 +053091 console.log(error);
Andrew Geisslerd27bb132018-05-24 11:07:27 -070092 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070093 };
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
AppaRao Pulib1289ec2018-11-14 20:33:30 +0530104 $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
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700116 function loadData() {
117 $scope.loadServerStatus();
118 $scope.loadNetworkInfo();
119 $scope.loadServerHealth();
AppaRao Pulib1289ec2018-11-14 20:33:30 +0530120 $scope.loadSystemName();
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700121 }
122
123 loadData();
124
125 $scope.logout = function() {
126 userModel.logout(function(status, error) {
127 if (status) {
128 $location.path('/logout');
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700129 } else {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700130 console.log(error);
131 }
132 });
133 };
134
135 $scope.refresh = function() {
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700136 // reload current page controllers and header
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700137 loadData();
138 $route.reload();
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700139 // Add flash class to header timestamp on click of refresh
140 var myEl =
141 angular.element(document.querySelector('.header__refresh'));
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700142 myEl.addClass('flash');
143 setTimeout(function() {
144 myEl.removeClass('flash');
145 }, 2000);
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700146 };
147
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700148 var loginListener =
149 $rootScope.$on('user-logged-in', function(event, arg) {
150 loadData();
151 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700152
153 $scope.$on('$destroy', function() {
154 loginListener();
155 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700156 }
157 ]
158 };
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700159 }
160 ]);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500161})(window.angular);