summaryrefslogtreecommitdiffstats
path: root/app/configuration/controllers/date-time-controller.js
blob: 92864db69f0cf89338a5e303988b6d40318d8422 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
 * Controller for date-time
 *
 * @module app/configuration
 * @exports dateTimeController
 * @name dateTimeController
 */

window.angular && (function(angular) {
  'use strict';

  angular.module('app.configuration').controller('dateTimeController', [
    '$scope', 'APIUtils', '$route', '$q', 'toastService', '$timeout',
    function($scope, APIUtils, $route, $q, toastService, $timeout) {
      $scope.bmc = {};
      // Only used when the owner is "Split"
      $scope.host = {};
      $scope.ntp = {servers: []};
      $scope.time = {mode: '', owner: ''};
      // Possible time owners
      // https://github.com/openbmc/phosphor-dbus-interfaces/blob/master/xyz/openbmc_project/Time/Owner.interface.yaml#L13
      $scope.timeOwners = ['BMC', 'Host', 'Both', 'Split'];
      $scope.loading = true;
      var timePath = '/xyz/openbmc_project/time/';

      var getTimePromise = APIUtils.getTime().then(
          function(data) {
            // The time is returned as Epoch microseconds convert to
            // milliseconds.
            if (data.data[timePath + 'bmc'] &&
                data.data[timePath + 'bmc'].hasOwnProperty('Elapsed')) {
              $scope.bmc.date =
                  new Date(data.data[timePath + 'bmc'].Elapsed / 1000);
              // Don't care about milliseconds and don't want them displayed
              $scope.bmc.date.setMilliseconds(0);

              // Examples:
              //   Central Standard Time (UTC-06:00)
              //   Moscow Standard Time (UTC+03:00)
              $scope.bmc.timezone = getUserTimezone($scope.bmc.date) + ' ' +
                  createOffset($scope.bmc.date);
            }
            if (data.data[timePath + 'host'] &&
                data.data[timePath + 'host'].hasOwnProperty('Elapsed')) {
              $scope.host.date =
                  new Date(data.data[timePath + 'host'].Elapsed / 1000);
              $scope.host.date.setMilliseconds(0);
              $scope.host.timezone = getUserTimezone($scope.bmc.date) + ' ' +
                  createOffset($scope.bmc.date);
            }
            if (data.data[timePath + 'owner'] &&
                data.data[timePath + 'owner'].hasOwnProperty('TimeOwner')) {
              $scope.time.owner =
                  data.data[timePath + 'owner'].TimeOwner.split('.').pop();
            }
            if (data.data[timePath + 'sync_method'] &&
                data.data[timePath + 'sync_method'].hasOwnProperty(
                    'TimeSyncMethod')) {
              $scope.time.mode = data.data[timePath + 'sync_method']
                                     .TimeSyncMethod.split('.')
                                     .pop();
            }
          },
          function(error) {
            console.log(JSON.stringify(error));
          });

      var getNTPPromise = APIUtils.getNTPServers().then(
          function(data) {
            $scope.ntp.servers = data.data;
          },
          function(error) {
            console.log(JSON.stringify(error));
          });

      var promises = [getTimePromise, getNTPPromise];

      $q.all(promises).finally(function() {
        $scope.loading = false;
      });

      /**
       * https://github.com/openbmc/phosphor-time-manager/blob/master/README.md#special-note-on-changing-ntp-setting
       * When time mode is initially set to Manual from NTP,
       * NTP service is disabled and the NTP service is
       * stopping but not stopped, setting time will return an error.
       * There are no responses from backend to notify when NTP is stopped.
       * To work around, a timeout is set to allow NTP to fully stop
       * TODO: remove timeout if backend solves
       * https://github.com/openbmc/openbmc/issues/3459
       */
      $scope.saveDateTimeSettings = function() {
        $scope.loading = true;
        if ($scope.time.mode == 'Manual' || $scope.time.owner == 'Split') {
          setTimeMode()
              .then(setTimeOwner)
              .then(setNTPServers)
              .then($timeout(setDateTime, 20000));
        } else {
          setTimeMode()
              .then(setTimeOwner)
              .then(setNTPServers)
              .then(
                  function() {
                    toastService.success('Date and time settings saved');
                  },
                  function(errors) {
                    console.log(JSON.stringify(errors));
                    toastService.error(
                        'Date and time settings could not be saved');
                  })
              .finally(function() {
                $scope.loading = false;
              });
        }
      };

      const setDateTime = function() {
        var manualPromises = [];
        if ($scope.time.mode == 'Manual') {
          // If owner is 'Split' set both.
          // If owner is 'Host' set only it.
          if ($scope.time.owner != 'Host') {
            manualPromises.push(setBMCTime($scope.bmc.date.getTime() * 1000));
          }
          // Even though we are setting Host time, we are setting from
          // the BMC date and time fields labeled "BMC and Host Time"
          // currently.
          if ($scope.time.owner == 'Host') {
            manualPromises.push(setHostTime($scope.bmc.date.getTime() * 1000));
          }
        }
        // Set the Host if Split even if NTP. In split mode, the host has
        // its own date and time field set from it.
        if ($scope.time.owner == 'Split') {
          manualPromises.push(setHostTime($scope.host.date.getTime() * 1000));
        }

        $q.all(manualPromises)
            .then(
                function() {
                  toastService.success('Date and time settings saved');
                },
                function(errors) {
                  console.log(JSON.stringify(errors));
                  toastService.error(
                      'Date and time settings could not be saved');
                })
            .finally(function() {
              $scope.loading = false;
            });
      };

      $scope.refresh = function() {
        $route.reload();
      };

      $scope.addNTPField = function() {
        $scope.ntp.servers.push('');
      };

      $scope.removeNTPField = function(index) {
        $scope.ntp.servers.splice(index, 1);
      };

      function setNTPServers() {
        // Remove any empty strings from the array. If the
        // user doesn't fill out the field, we don't want to add.
        $scope.ntp.servers = $scope.ntp.servers.filter(Boolean);

        return APIUtils.setNTPServers($scope.ntp.servers);
      }

      function setTimeMode() {
        return APIUtils.setTimeMode(
            'xyz.openbmc_project.Time.Synchronization.Method.' +
            $scope.time.mode);
      }

      function setTimeOwner() {
        return APIUtils.setTimeOwner(
            'xyz.openbmc_project.Time.Owner.Owners.' + $scope.time.owner);
      }

      function setBMCTime(time) {
        // Add the separate date and time objects and convert to Epoch time in
        // microseconds.
        return APIUtils.setBMCTime(time);
      }

      function setHostTime(time) {
        // Add the separate date and time objects and convert to Epoch time
        // microseconds.
        return APIUtils.setHostTime(time);
      }
      function createOffset(date) {
        // https://stackoverflow.com/questions/9149556/how-to-get-utc-offset-in-javascript-analog-of-timezoneinfo-getutcoffset-in-c
        var sign = (date.getTimezoneOffset() > 0) ? '-' : '+';
        var offset = Math.abs(date.getTimezoneOffset());
        var hours = pad(Math.floor(offset / 60));
        var minutes = pad(offset % 60);
        return '(UTC' + sign + hours + ':' + minutes + ')';
      }
      function getUserTimezone(date) {
        const ro = Intl.DateTimeFormat().resolvedOptions();
        // A safe, easy way to get the timezone (e.g. Central Standard Time) is
        // to subtract the time string without a timezone from the time string
        // with a timezone.
        // Hardcoded to 'en-US' so all timezones are displayed in English
        // (e.g. Moscow Standard Time).
        var ret = date.toLocaleTimeString('en-US', {timeZoneName: 'long'})
                      .replace(date.toLocaleTimeString('en-US'), '')
                      .trim();
        // Do not return GMT+/-offset.
        if (ret.indexOf('GMT') >= 0) {
          return '';
        }
        return ret;
      }
      function pad(value) {
        return value < 10 ? '0' + value : value;
      }
    }
  ]);
})(angular);
OpenPOWER on IntegriCloud