1window.angular && (function(angular) {
2  'use strict';
3
4  angular.module('app.common.directives').directive('appHeader', [
5    'APIUtils',
6    function(APIUtils) {
7      return {
8        'restrict': 'E',
9        'template': require('./app-header.html'),
10        'scope': {'path': '='},
11        'controller': [
12          '$rootScope', '$scope', 'dataService', 'userModel', '$location',
13          '$route',
14          function(
15              $rootScope, $scope, dataService, userModel, $location, $route) {
16            $scope.dataService = dataService;
17
18            // 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, adding ws under $scope etc.. but auto
56                // refresh is not happening.
57                $scope.loadServerStatus();
58              }
59            };
60
61            $scope.loadServerHealth = function() {
62              APIUtils.getLogs().then(function(result) {
63                dataService.updateServerHealth(result.data);
64              });
65            };
66
67            $scope.loadServerStatus = function() {
68              if (!userModel.isLoggedIn()) {
69                return;
70              }
71              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) {
85                    console.log(error);
86                  });
87            };
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');
110                } else {
111                  console.log(error);
112                }
113              });
114            };
115
116            $scope.refresh = function() {
117              // reload current page controllers and header
118              loadData();
119              $route.reload();
120              // Add flash class to header timestamp on click of refresh
121              var myEl =
122                  angular.element(document.querySelector('.header__refresh'));
123              myEl.addClass('flash');
124              setTimeout(function() {
125                myEl.removeClass('flash');
126              }, 2000);
127            };
128
129            var loginListener =
130                $rootScope.$on('user-logged-in', function(event, arg) {
131                  loadData();
132                });
133
134            $scope.$on('$destroy', function() {
135              loginListener();
136            });
137
138            $scope.multiRecent = function() {
139              $scope.multi_server_recent = !$scope.multi_server_recent;
140            };
141          }
142        ]
143      };
144    }
145  ]);
146})(window.angular);
147