summaryrefslogtreecommitdiffstats
path: root/app/common/directives/app-header.js
blob: 98d210fcd15e7755be246ce4c6dd93d9d685b0c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
window.angular && (function(angular) {
  'use strict';

  angular.module('app.common.directives').directive('appHeader', [
    'APIUtils',
    function(APIUtils) {
      return {
        'restrict': 'E',
        'template': require('./app-header.html'),
        'scope': {'path': '='},
        'controller': [
          '$rootScope', '$scope', 'dataService', 'userModel', '$location',
          '$route',
          function(
              $rootScope, $scope, dataService, userModel, $location, $route) {
            $scope.dataService = dataService;

            try {
              // Create a secure websocket with URL as /subscribe
              // TODO: Need to put in a generic APIUtils to avoid duplicate
              // controller
              var ws = new WebSocket(
                  'wss://' + dataService.server_id + '/subscribe');
            } catch (error) {
              console.log('WebSocket', error);
            }

            if (ws !== undefined) {
              // Specify the required event details as JSON dictionary
              var data = JSON.stringify({
                'paths': ['/xyz/openbmc_project/state/host0'],
                'interfaces': ['xyz.openbmc_project.State.Host']
              });

              // Send the JSON dictionary data to host
              ws.onopen = function() {
                ws.send(data);
                console.log('host0 ws opened');
              };

              // Close the web socket
              ws.onclose = function() {
                console.log('host0 ws closed');
              };

              // Websocket event handling function which catches the
              // current host state
              ws.onmessage = function(evt) {
                // Parse the response (JSON dictionary data)
                var content = JSON.parse(evt.data);

                // Fetch the current server power state
                if (content.hasOwnProperty('properties') &&
                    content['properties'].hasOwnProperty('CurrentHostState')) {
                  // Refresh the host state and status
                  // TODO: As of now not using the current host state
                  // value for updating the data Service state rather
                  // using it to detect the command line state change.
                  // Tried different methods like creating a separate
                  // function, adding ws under $scope etc.. but auto
                  // refresh is not happening.
                  $scope.loadServerStatus();
                }
              };
            }

            $scope.loadServerHealth = function() {
              APIUtils.getLogs().then(function(result) {
                dataService.updateServerHealth(result.data);
              });
            };

            $scope.loadServerStatus = function() {
              if (!userModel.isLoggedIn()) {
                return;
              }
              APIUtils.getHostState().then(
                  function(status) {
                    if (status ==
                        'xyz.openbmc_project.State.Host.HostState.Off') {
                      dataService.setPowerOffState();
                    } else if (
                        status ==
                        'xyz.openbmc_project.State.Host.HostState.Running') {
                      dataService.setPowerOnState();
                    } else {
                      dataService.setErrorState();
                    }
                  },
                  function(error) {
                    console.log(error);
                  });
            };

            $scope.loadNetworkInfo = function() {
              if (!userModel.isLoggedIn()) {
                return;
              }
              APIUtils.getNetworkInfo().then(function(data) {
                dataService.setNetworkInfo(data);
              });
            };

            $scope.loadSystemName = function() {
              // Dynamically get ComputerSystems Name/serial
              // which differs across OEM's
              APIUtils.getRedfishSysName().then(
                  function(res) {
                    dataService.setSystemName(res);
                  },
                  function(error) {
                    console.log(JSON.stringify(error));
                  });
            };

            function loadData() {
              $scope.loadServerStatus();
              $scope.loadNetworkInfo();
              $scope.loadServerHealth();
              $scope.loadSystemName();
            }

            loadData();

            $scope.logout = function() {
              userModel.logout(function(status, error) {
                if (status) {
                  $location.path('/logout');
                } else {
                  console.log(error);
                }
              });
            };

            $scope.refresh = function() {
              // reload current page controllers and header
              loadData();
              $route.reload();
              // Add flash class to header timestamp on click of refresh
              var myEl =
                  angular.element(document.querySelector('.header__refresh'));
              myEl.addClass('flash');
              setTimeout(function() {
                myEl.removeClass('flash');
              }, 2000);
            };

            var loginListener =
                $rootScope.$on('user-logged-in', function(event, arg) {
                  loadData();
                });

            $scope.$on('$destroy', function() {
              loginListener();
            });
          }
        ]
      };
    }
  ]);
})(window.angular);
OpenPOWER on IntegriCloud