blob: 6be458a64d215c0f2a278b15af0d1f3508d8b0e6 [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
55 // function, addding ws under $scope etc.. but auto
56 // 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
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 Geisslerd27bb132018-05-24 11:07:27 -0700110 } else {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700111 console.log(error);
112 }
113 });
114 };
115
116 $scope.refresh = function() {
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700117 // reload current page controllers and header
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700118 loadData();
119 $route.reload();
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700120 // Add flash class to header timestamp on click of refresh
121 var myEl =
122 angular.element(document.querySelector('.header__refresh'));
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700123 myEl.addClass('flash');
124 setTimeout(function() {
125 myEl.removeClass('flash');
126 }, 2000);
127
128 };
129
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700130 var loginListener =
131 $rootScope.$on('user-logged-in', function(event, arg) {
132 loadData();
133 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700134
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 Geisslerd27bb132018-05-24 11:07:27 -0700145 }
146 ]);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500147})(window.angular);