xref: /openbmc/phosphor-webui/app/common/directives/app-header.js (revision b1289ec99faa6345bb402a6887df82bb7dd9a7c0)
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': [
12d27bb135SAndrew Geissler          '$rootScope', '$scope', 'dataService', 'userModel', '$location',
13d27bb135SAndrew Geissler          '$route',
14d27bb135SAndrew Geissler          function(
15d27bb135SAndrew Geissler              $rootScope, $scope, dataService, userModel, $location, $route) {
1699d199f3SIftekharul Islam            $scope.dataService = dataService;
1799d199f3SIftekharul Islam
18a4ec4679SJayashankar Padath            // Create a secure websocket with URL as /subscribe
19a4ec4679SJayashankar Padath            // TODO: Need to put in a generic APIUtils to avoid duplicate
20a4ec4679SJayashankar Padath            // controller
21a4ec4679SJayashankar Padath            var ws =
22a4ec4679SJayashankar Padath                new WebSocket('wss://' + dataService.server_id + '/subscribe');
23a4ec4679SJayashankar Padath
24a4ec4679SJayashankar Padath            // Specify the required event details as JSON dictionary
25a4ec4679SJayashankar Padath            var data = JSON.stringify({
26a4ec4679SJayashankar Padath              'paths': ['/xyz/openbmc_project/state/host0'],
27a4ec4679SJayashankar Padath              'interfaces': ['xyz.openbmc_project.State.Host']
28a4ec4679SJayashankar Padath            });
29a4ec4679SJayashankar Padath
30a4ec4679SJayashankar Padath            // Send the JSON dictionary data to host
31a4ec4679SJayashankar Padath            ws.onopen = function() {
32a4ec4679SJayashankar Padath              ws.send(data);
33a4ec4679SJayashankar Padath              console.log('host0 ws opened');
34a4ec4679SJayashankar Padath            };
35a4ec4679SJayashankar Padath
36a4ec4679SJayashankar Padath            // Close the web socket
37a4ec4679SJayashankar Padath            ws.onclose = function() {
38a4ec4679SJayashankar Padath              console.log('host0 ws closed');
39a4ec4679SJayashankar Padath            };
40a4ec4679SJayashankar Padath
41a4ec4679SJayashankar Padath            // Websocket event handling function which catches the
42a4ec4679SJayashankar Padath            // current host state
43a4ec4679SJayashankar Padath            ws.onmessage = function(evt) {
44a4ec4679SJayashankar Padath              // Parse the response (JSON dictionary data)
45a4ec4679SJayashankar Padath              var content = JSON.parse(evt.data);
46a4ec4679SJayashankar Padath
47a4ec4679SJayashankar Padath              // Fetch the current server power state
48a4ec4679SJayashankar Padath              if (content.hasOwnProperty('properties') &&
49a4ec4679SJayashankar Padath                  content['properties'].hasOwnProperty('CurrentHostState')) {
50a4ec4679SJayashankar Padath                // Refresh the host state and status
51a4ec4679SJayashankar Padath                // TODO: As of now not using the current host state
52a4ec4679SJayashankar Padath                // value for updating the data Service state rather
53a4ec4679SJayashankar Padath                // using it to detect the command line state change.
54a4ec4679SJayashankar Padath                // Tried different methods like creating a separate
55ffdef96dSRebecca Shaw                // function, adding ws under $scope etc.. but auto
56a4ec4679SJayashankar Padath                // refresh is not happening.
57a4ec4679SJayashankar Padath                $scope.loadServerStatus();
58a4ec4679SJayashankar Padath              }
59a4ec4679SJayashankar Padath            };
60a4ec4679SJayashankar Padath
61c22425f2SIftekharul Islam            $scope.loadServerHealth = function() {
62c22425f2SIftekharul Islam              APIUtils.getLogs().then(function(result) {
63c22425f2SIftekharul Islam                dataService.updateServerHealth(result.data);
64c22425f2SIftekharul Islam              });
65ba5e3f34SAndrew Geissler            };
66c22425f2SIftekharul Islam
6799d199f3SIftekharul Islam            $scope.loadServerStatus = function() {
6899d199f3SIftekharul Islam              if (!userModel.isLoggedIn()) {
6999d199f3SIftekharul Islam                return;
7099d199f3SIftekharul Islam              }
71d27bb135SAndrew Geissler              APIUtils.getHostState().then(
72d27bb135SAndrew Geissler                  function(status) {
73d27bb135SAndrew Geissler                    if (status ==
74d27bb135SAndrew Geissler                        'xyz.openbmc_project.State.Host.HostState.Off') {
7599d199f3SIftekharul Islam                      dataService.setPowerOffState();
76d27bb135SAndrew Geissler                    } else if (
77d27bb135SAndrew Geissler                        status ==
78d27bb135SAndrew Geissler                        'xyz.openbmc_project.State.Host.HostState.Running') {
7999d199f3SIftekharul Islam                      dataService.setPowerOnState();
80d27bb135SAndrew Geissler                    } else {
81d80c280bSCamVan Nguyen                      dataService.setErrorState();
8299d199f3SIftekharul Islam                    }
83d27bb135SAndrew Geissler                  },
84d27bb135SAndrew Geissler                  function(error) {
85a38a287cSJayashankar Padath                    console.log(error);
8699d199f3SIftekharul Islam                  });
87ba5e3f34SAndrew Geissler            };
88ba556c31SIftekharul Islam
89ba556c31SIftekharul Islam            $scope.loadNetworkInfo = function() {
90ba556c31SIftekharul Islam              if (!userModel.isLoggedIn()) {
91ba556c31SIftekharul Islam                return;
92ba556c31SIftekharul Islam              }
93ba556c31SIftekharul Islam              APIUtils.getNetworkInfo().then(function(data) {
94ba556c31SIftekharul Islam                dataService.setNetworkInfo(data);
95ba556c31SIftekharul Islam              });
96ba5e3f34SAndrew Geissler            };
97ba556c31SIftekharul Islam
98*b1289ec9SAppaRao Puli            $scope.loadSystemName = function() {
99*b1289ec9SAppaRao Puli              // Dynamically get ComputerSystems Name/serial
100*b1289ec9SAppaRao Puli              // which differs across OEM's
101*b1289ec9SAppaRao Puli              APIUtils.getRedfishSysName().then(
102*b1289ec9SAppaRao Puli                  function(res) {
103*b1289ec9SAppaRao Puli                    dataService.setSystemName(res);
104*b1289ec9SAppaRao Puli                  },
105*b1289ec9SAppaRao Puli                  function(error) {
106*b1289ec9SAppaRao Puli                    console.log(JSON.stringify(error));
107*b1289ec9SAppaRao Puli                  });
108*b1289ec9SAppaRao Puli            };
109*b1289ec9SAppaRao Puli
110c22425f2SIftekharul Islam            function loadData() {
11199d199f3SIftekharul Islam              $scope.loadServerStatus();
112ba556c31SIftekharul Islam              $scope.loadNetworkInfo();
113c22425f2SIftekharul Islam              $scope.loadServerHealth();
114*b1289ec9SAppaRao Puli              $scope.loadSystemName();
115c22425f2SIftekharul Islam            }
116c22425f2SIftekharul Islam
117c22425f2SIftekharul Islam            loadData();
11899d199f3SIftekharul Islam
11999d199f3SIftekharul Islam            $scope.logout = function() {
12099d199f3SIftekharul Islam              userModel.logout(function(status, error) {
12199d199f3SIftekharul Islam                if (status) {
12299d199f3SIftekharul Islam                  $location.path('/logout');
123d27bb135SAndrew Geissler                } else {
12499d199f3SIftekharul Islam                  console.log(error);
12599d199f3SIftekharul Islam                }
12699d199f3SIftekharul Islam              });
127ba5e3f34SAndrew Geissler            };
12899d199f3SIftekharul Islam
12999d199f3SIftekharul Islam            $scope.refresh = function() {
130a72686f7SAndrew Geissler              // reload current page controllers and header
131c22425f2SIftekharul Islam              loadData();
132a72686f7SAndrew Geissler              $route.reload();
133a5f222a1SMichael Davis              // Add flash class to header timestamp on click of refresh
134d27bb135SAndrew Geissler              var myEl =
135d27bb135SAndrew Geissler                  angular.element(document.querySelector('.header__refresh'));
136a5f222a1SMichael Davis              myEl.addClass('flash');
137a5f222a1SMichael Davis              setTimeout(function() {
138ba5e3f34SAndrew Geissler                myEl.removeClass('flash');
139a5f222a1SMichael Davis              }, 2000);
140ba5e3f34SAndrew Geissler            };
14199d199f3SIftekharul Islam
142d27bb135SAndrew Geissler            var loginListener =
143d27bb135SAndrew Geissler                $rootScope.$on('user-logged-in', function(event, arg) {
144c22425f2SIftekharul Islam                  loadData();
14599d199f3SIftekharul Islam                });
14699d199f3SIftekharul Islam
14799d199f3SIftekharul Islam            $scope.$on('$destroy', function() {
14899d199f3SIftekharul Islam              loginListener();
14999d199f3SIftekharul Islam            });
150ba5e3f34SAndrew Geissler          }
151ba5e3f34SAndrew Geissler        ]
15299d199f3SIftekharul Islam      };
153d27bb135SAndrew Geissler    }
154d27bb135SAndrew Geissler  ]);
15599d199f3SIftekharul Islam})(window.angular);
156