xref: /openbmc/phosphor-webui/app/common/directives/app-header.js (revision 4a16a026b60a3bf54161ed4e4bc34885b4e3e6c3)
199d199f3SIftekharul Islamwindow.angular && (function(angular) {
299d199f3SIftekharul Islam  'use strict';
399d199f3SIftekharul Islam
4d27bb135SAndrew Geissler  angular.module('app.common.directives').directive('appHeader', [
5d27bb135SAndrew Geissler    'APIUtils',
6d27bb135SAndrew Geissler    function(APIUtils) {
799d199f3SIftekharul Islam      return {
899d199f3SIftekharul Islam        'restrict': 'E',
9bbcf670aSEd Tanous        'template': require('./app-header.html'),
10d27bb135SAndrew Geissler        'scope': {'path': '='},
11d27bb135SAndrew Geissler        'controller': [
12*4a16a026SJames Feist          '$rootScope', '$cookies', '$scope', 'dataService', 'userModel',
13*4a16a026SJames Feist          '$location', '$route',
14d27bb135SAndrew Geissler          function(
15*4a16a026SJames Feist              $rootScope, $cookies, $scope, dataService, userModel, $location,
16*4a16a026SJames Feist              $route) {
1799d199f3SIftekharul Islam            $scope.dataService = dataService;
184148f2eeSYoshie Muranaka            $scope.username = '';
1999d199f3SIftekharul Islam
200433e005SYoshie Muranaka            try {
21a4ec4679SJayashankar Padath              // Create a secure websocket with URL as /subscribe
22a4ec4679SJayashankar Padath              // TODO: Need to put in a generic APIUtils to avoid duplicate
23a4ec4679SJayashankar Padath              // controller
24*4a16a026SJames Feist              var token = $cookies.get('XSRF-TOKEN');
250433e005SYoshie Muranaka              var ws = new WebSocket(
26*4a16a026SJames Feist                  'wss://' + dataService.server_id + '/subscribe', [token]);
270433e005SYoshie Muranaka            } catch (error) {
280433e005SYoshie Muranaka              console.log('WebSocket', error);
290433e005SYoshie Muranaka            }
30a4ec4679SJayashankar Padath
310433e005SYoshie Muranaka            if (ws !== undefined) {
32a4ec4679SJayashankar Padath              // Specify the required event details as JSON dictionary
33a4ec4679SJayashankar Padath              var data = JSON.stringify({
34a4ec4679SJayashankar Padath                'paths': ['/xyz/openbmc_project/state/host0'],
35a4ec4679SJayashankar Padath                'interfaces': ['xyz.openbmc_project.State.Host']
36a4ec4679SJayashankar Padath              });
37a4ec4679SJayashankar Padath
38a4ec4679SJayashankar Padath              // Send the JSON dictionary data to host
39a4ec4679SJayashankar Padath              ws.onopen = function() {
40a4ec4679SJayashankar Padath                ws.send(data);
41a4ec4679SJayashankar Padath                console.log('host0 ws opened');
42a4ec4679SJayashankar Padath              };
43a4ec4679SJayashankar Padath
44a4ec4679SJayashankar Padath              // Close the web socket
45a4ec4679SJayashankar Padath              ws.onclose = function() {
46a4ec4679SJayashankar Padath                console.log('host0 ws closed');
47a4ec4679SJayashankar Padath              };
48a4ec4679SJayashankar Padath
49a4ec4679SJayashankar Padath              // Websocket event handling function which catches the
50a4ec4679SJayashankar Padath              // current host state
51a4ec4679SJayashankar Padath              ws.onmessage = function(evt) {
52a4ec4679SJayashankar Padath                // Parse the response (JSON dictionary data)
53a4ec4679SJayashankar Padath                var content = JSON.parse(evt.data);
54a4ec4679SJayashankar Padath
55a4ec4679SJayashankar Padath                // Fetch the current server power state
56a4ec4679SJayashankar Padath                if (content.hasOwnProperty('properties') &&
57a4ec4679SJayashankar Padath                    content['properties'].hasOwnProperty('CurrentHostState')) {
58a4ec4679SJayashankar Padath                  // Refresh the host state and status
59a4ec4679SJayashankar Padath                  // TODO: As of now not using the current host state
60a4ec4679SJayashankar Padath                  // value for updating the data Service state rather
61a4ec4679SJayashankar Padath                  // using it to detect the command line state change.
62a4ec4679SJayashankar Padath                  // Tried different methods like creating a separate
63ffdef96dSRebecca Shaw                  // function, adding ws under $scope etc.. but auto
64a4ec4679SJayashankar Padath                  // refresh is not happening.
65a4ec4679SJayashankar Padath                  $scope.loadServerStatus();
66a4ec4679SJayashankar Padath                }
67a4ec4679SJayashankar Padath              };
680433e005SYoshie Muranaka            }
69a4ec4679SJayashankar Padath
70c22425f2SIftekharul Islam            $scope.loadServerHealth = function() {
71c22425f2SIftekharul Islam              APIUtils.getLogs().then(function(result) {
72c22425f2SIftekharul Islam                dataService.updateServerHealth(result.data);
73c22425f2SIftekharul Islam              });
74ba5e3f34SAndrew Geissler            };
75c22425f2SIftekharul Islam
7699d199f3SIftekharul Islam            $scope.loadServerStatus = function() {
7799d199f3SIftekharul Islam              if (!userModel.isLoggedIn()) {
7899d199f3SIftekharul Islam                return;
7999d199f3SIftekharul Islam              }
80d27bb135SAndrew Geissler              APIUtils.getHostState().then(
81d27bb135SAndrew Geissler                  function(status) {
82d27bb135SAndrew Geissler                    if (status ==
83d27bb135SAndrew Geissler                        'xyz.openbmc_project.State.Host.HostState.Off') {
8499d199f3SIftekharul Islam                      dataService.setPowerOffState();
85d27bb135SAndrew Geissler                    } else if (
86d27bb135SAndrew Geissler                        status ==
87d27bb135SAndrew Geissler                        'xyz.openbmc_project.State.Host.HostState.Running') {
8899d199f3SIftekharul Islam                      dataService.setPowerOnState();
89d27bb135SAndrew Geissler                    } else {
90d80c280bSCamVan Nguyen                      dataService.setErrorState();
9199d199f3SIftekharul Islam                    }
92d27bb135SAndrew Geissler                  },
93d27bb135SAndrew Geissler                  function(error) {
94a38a287cSJayashankar Padath                    console.log(error);
9599d199f3SIftekharul Islam                  });
96ba5e3f34SAndrew Geissler            };
97ba556c31SIftekharul Islam
98ba556c31SIftekharul Islam            $scope.loadNetworkInfo = function() {
99ba556c31SIftekharul Islam              if (!userModel.isLoggedIn()) {
100ba556c31SIftekharul Islam                return;
101ba556c31SIftekharul Islam              }
102ba556c31SIftekharul Islam              APIUtils.getNetworkInfo().then(function(data) {
103ba556c31SIftekharul Islam                dataService.setNetworkInfo(data);
104ba556c31SIftekharul Islam              });
105ba5e3f34SAndrew Geissler            };
106ba556c31SIftekharul Islam
107b1289ec9SAppaRao Puli            $scope.loadSystemName = function() {
108b1289ec9SAppaRao Puli              // Dynamically get ComputerSystems Name/serial
109b1289ec9SAppaRao Puli              // which differs across OEM's
110b1289ec9SAppaRao Puli              APIUtils.getRedfishSysName().then(
111b1289ec9SAppaRao Puli                  function(res) {
112b1289ec9SAppaRao Puli                    dataService.setSystemName(res);
113b1289ec9SAppaRao Puli                  },
114b1289ec9SAppaRao Puli                  function(error) {
115b1289ec9SAppaRao Puli                    console.log(JSON.stringify(error));
116b1289ec9SAppaRao Puli                  });
117b1289ec9SAppaRao Puli            };
118b1289ec9SAppaRao Puli
119c22425f2SIftekharul Islam            function loadData() {
12099d199f3SIftekharul Islam              $scope.loadServerStatus();
121ba556c31SIftekharul Islam              $scope.loadNetworkInfo();
122c22425f2SIftekharul Islam              $scope.loadServerHealth();
123b1289ec9SAppaRao Puli              $scope.loadSystemName();
1244148f2eeSYoshie Muranaka              $scope.username = dataService.getUser();
125c22425f2SIftekharul Islam            }
126c22425f2SIftekharul Islam
127c22425f2SIftekharul Islam            loadData();
12899d199f3SIftekharul Islam
12999d199f3SIftekharul Islam            $scope.logout = function() {
13099d199f3SIftekharul Islam              userModel.logout(function(status, error) {
13199d199f3SIftekharul Islam                if (status) {
13299d199f3SIftekharul Islam                  $location.path('/logout');
133d27bb135SAndrew Geissler                } else {
13499d199f3SIftekharul Islam                  console.log(error);
13599d199f3SIftekharul Islam                }
13699d199f3SIftekharul Islam              });
137ba5e3f34SAndrew Geissler            };
13899d199f3SIftekharul Islam
13999d199f3SIftekharul Islam            $scope.refresh = function() {
140a72686f7SAndrew Geissler              // reload current page controllers and header
141c22425f2SIftekharul Islam              loadData();
142a72686f7SAndrew Geissler              $route.reload();
143a5f222a1SMichael Davis              // Add flash class to header timestamp on click of refresh
144d27bb135SAndrew Geissler              var myEl =
145d27bb135SAndrew Geissler                  angular.element(document.querySelector('.header__refresh'));
146a5f222a1SMichael Davis              myEl.addClass('flash');
147a5f222a1SMichael Davis              setTimeout(function() {
148ba5e3f34SAndrew Geissler                myEl.removeClass('flash');
149a5f222a1SMichael Davis              }, 2000);
150ba5e3f34SAndrew Geissler            };
15199d199f3SIftekharul Islam
152d27bb135SAndrew Geissler            var loginListener =
153d27bb135SAndrew Geissler                $rootScope.$on('user-logged-in', function(event, arg) {
154c22425f2SIftekharul Islam                  loadData();
15599d199f3SIftekharul Islam                });
15699d199f3SIftekharul Islam
15799d199f3SIftekharul Islam            $scope.$on('$destroy', function() {
15899d199f3SIftekharul Islam              loginListener();
15999d199f3SIftekharul Islam            });
160ba5e3f34SAndrew Geissler          }
161ba5e3f34SAndrew Geissler        ]
16299d199f3SIftekharul Islam      };
163d27bb135SAndrew Geissler    }
164d27bb135SAndrew Geissler  ]);
16599d199f3SIftekharul Islam})(window.angular);
166