blob: 9e10619dc887189a0561fc54108180777a1de0ed [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
Jayashankar Padatha4ec4672018-04-27 18:44:13 +053018 // 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
Rebecca Shawffdef962018-07-19 13:37:37 -050055 // function, adding ws under $scope etc.. but auto
Jayashankar Padatha4ec4672018-04-27 18:44:13 +053056 // refresh is not happening.
57 $scope.loadServerStatus();
58 }
59 };
60
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070061 $scope.loadServerHealth = function() {
62 APIUtils.getLogs().then(function(result) {
63 dataService.updateServerHealth(result.data);
64 });
Iftekharul Islam99d199f2017-03-24 15:28:25 -050065 };
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070066
67 $scope.loadServerStatus = function() {
68 if (!userModel.isLoggedIn()) {
69 return;
70 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -070071 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 Padatha38a2872018-06-07 11:34:58 +053085 console.log(error);
Andrew Geisslerd27bb132018-05-24 11:07:27 -070086 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070087 };
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
AppaRao Pulib1289ec2018-11-14 20:33:30 +053098 $scope.loadSystemName = function() {
99 // Dynamically get ComputerSystems Name/serial
100 // which differs across OEM's
101 APIUtils.getRedfishSysName().then(
102 function(res) {
103 dataService.setSystemName(res);
104 },
105 function(error) {
106 console.log(JSON.stringify(error));
107 });
108 };
109
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700110 function loadData() {
111 $scope.loadServerStatus();
112 $scope.loadNetworkInfo();
113 $scope.loadServerHealth();
AppaRao Pulib1289ec2018-11-14 20:33:30 +0530114 $scope.loadSystemName();
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700115 }
116
117 loadData();
118
119 $scope.logout = function() {
120 userModel.logout(function(status, error) {
121 if (status) {
122 $location.path('/logout');
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700123 } else {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700124 console.log(error);
125 }
126 });
127 };
128
129 $scope.refresh = function() {
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700130 // reload current page controllers and header
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700131 loadData();
132 $route.reload();
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700133 // Add flash class to header timestamp on click of refresh
134 var myEl =
135 angular.element(document.querySelector('.header__refresh'));
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700136 myEl.addClass('flash');
137 setTimeout(function() {
138 myEl.removeClass('flash');
139 }, 2000);
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700140 };
141
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700142 var loginListener =
143 $rootScope.$on('user-logged-in', function(event, arg) {
144 loadData();
145 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700146
147 $scope.$on('$destroy', function() {
148 loginListener();
149 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700150 }
151 ]
152 };
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700153 }
154 ]);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500155})(window.angular);