blob: 0f6ecf97c7336505adf6d2c39157350a9d6c1526 [file] [log] [blame]
Iftekharul Islamcd789502017-04-19 14:37:55 -05001/**
2 * Controller for date-time
3 *
4 * @module app/configuration
5 * @exports dateTimeController
6 * @name dateTimeController
Iftekharul Islamcd789502017-04-19 14:37:55 -05007 */
8
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07009window.angular && (function(angular) {
10 'use strict';
Iftekharul Islamcd789502017-04-19 14:37:55 -050011
Andrew Geisslerd27bb132018-05-24 11:07:27 -070012 angular.module('app.configuration').controller('dateTimeController', [
Gunnar Millsb7ea2792018-07-18 13:01:48 -050013 '$scope', '$window', 'APIUtils', '$route', '$q',
14 function($scope, $window, APIUtils, $route, $q) {
15 $scope.bmc = {};
Gunnar Millseab32132018-09-27 10:07:17 -050016 // Only used when the owner is "Split"
Gunnar Millsb7ea2792018-07-18 13:01:48 -050017 $scope.host = {};
18 $scope.ntp = {servers: []};
Gunnar Millsc74d4342018-07-18 14:52:02 -050019 $scope.time_mode = '';
20 $scope.time_owner = '';
Gunnar Millsb7ea2792018-07-18 13:01:48 -050021 $scope.time_owners = ['BMC', 'Host', 'Both', 'Split'];
22 $scope.set_time_error = false;
23 $scope.set_time_success = false;
Gunnar Mills7de38662018-07-18 13:01:48 -050024 $scope.loading = true;
Gunnar Millsb7ea2792018-07-18 13:01:48 -050025 var time_path = '/xyz/openbmc_project/time/';
Gunnar Mills7de38662018-07-18 13:01:48 -050026
Gunnar Millsc74d4342018-07-18 14:52:02 -050027 var getTimePromise = APIUtils.getTime().then(
Gunnar Mills7de38662018-07-18 13:01:48 -050028 function(data) {
Gunnar Millsb7ea2792018-07-18 13:01:48 -050029 // The time is returned as Epoch microseconds convert to
30 // milliseconds.
31 if (data.data[time_path + 'bmc'] &&
32 data.data[time_path + 'bmc'].hasOwnProperty('Elapsed')) {
33 $scope.bmc.date =
34 new Date(data.data[time_path + 'bmc'].Elapsed / 1000);
35 // Don't care about milliseconds and don't want them displayed
36 $scope.bmc.date.setMilliseconds(0);
37 // https://stackoverflow.com/questions/1091372/getting-the-clients-timezone-in-javascript
38 // GMT-0400 (EDT)
39 $scope.bmc.timezone =
40 $scope.bmc.date.toString().match(/([A-Z]+[\+-][0-9]+.*)/)[1];
41 }
42 if (data.data[time_path + 'host'] &&
43 data.data[time_path + 'host'].hasOwnProperty('Elapsed')) {
44 $scope.host.date =
45 new Date(data.data[time_path + 'host'].Elapsed / 1000);
46 $scope.host.date.setMilliseconds(0);
47 $scope.host.timezone =
48 $scope.host.date.toString().match(/([A-Z]+[\+-][0-9]+.*)/)[1];
49 }
50 if (data.data[time_path + 'owner'] &&
51 data.data[time_path + 'owner'].hasOwnProperty('TimeOwner')) {
52 $scope.time_owner =
53 data.data[time_path + 'owner'].TimeOwner.split('.').pop();
54 }
55 if (data.data[time_path + 'sync_method'] &&
56 data.data[time_path + 'sync_method'].hasOwnProperty(
57 'TimeSyncMethod')) {
58 $scope.time_mode = data.data[time_path + 'sync_method']
59 .TimeSyncMethod.split('.')
60 .pop();
61 }
Gunnar Mills7de38662018-07-18 13:01:48 -050062 },
63 function(error) {
64 console.log(JSON.stringify(error));
65 });
66
Gunnar Millsb7ea2792018-07-18 13:01:48 -050067 var getNTPPromise = APIUtils.getNTPServers().then(
68 function(data) {
69 $scope.ntp.servers = data.data;
70 },
71 function(error) {
72 console.log(JSON.stringify(error));
73 });
74
75 var promises = [
76 getTimePromise,
77 getNTPPromise,
78 ];
79
80 $q.all(promises).finally(function() {
Gunnar Mills7de38662018-07-18 13:01:48 -050081 $scope.loading = false;
82 });
Gunnar Millsb7ea2792018-07-18 13:01:48 -050083
84 $scope.setTime = function() {
85 $scope.set_time_error = false;
86 $scope.set_time_success = false;
87 $scope.loading = true;
88 var promises = [setTimeMode(), setTimeOwner(), setNTPServers()];
89
90 $q.all(promises).then(
91 function() {
92 // Have to set the time mode and time owner first to avoid a
93 // insufficient permissions if the time mode or time owner had
94 // changed.
95 var manual_promises = [];
96 if ($scope.time_mode == 'Manual') {
97 // If owner is 'Split' set both.
98 // If owner is 'Host' set only it.
99 // Else set BMC only. See:
100 // https://github.com/openbmc/phosphor-time-manager/blob/master/README.md
101 if ($scope.time_owner != 'Host') {
Gunnar Millseab32132018-09-27 10:07:17 -0500102 manual_promises.push(
103 setBMCTime($scope.bmc.date.getTime() * 1000));
Gunnar Millsb7ea2792018-07-18 13:01:48 -0500104 }
Gunnar Millseab32132018-09-27 10:07:17 -0500105 // Even though we are setting Host time, we are setting from
106 // the BMC date and time fields labeled "BMC and Host Time"
107 // currently.
Gunnar Millsf378c772018-09-27 08:53:07 -0500108 if ($scope.time_owner == 'Host') {
Gunnar Millseab32132018-09-27 10:07:17 -0500109 manual_promises.push(
110 setHostTime($scope.bmc.date.getTime() * 1000));
Gunnar Millsb7ea2792018-07-18 13:01:48 -0500111 }
112 }
Gunnar Millseab32132018-09-27 10:07:17 -0500113 // Set the Host if Split even if NTP. In split mode, the host has
114 // its own date and time field. Set from it.
Gunnar Millsf378c772018-09-27 08:53:07 -0500115 if ($scope.time_owner == 'Split') {
Gunnar Millseab32132018-09-27 10:07:17 -0500116 manual_promises.push(
117 setHostTime($scope.host.date.getTime() * 1000));
Gunnar Millsf378c772018-09-27 08:53:07 -0500118 }
119
Gunnar Millsb7ea2792018-07-18 13:01:48 -0500120 $q.all(manual_promises)
121 .then(
122 function() {
123 $scope.set_time_success = true;
124 },
125 function(errors) {
126 console.log(JSON.stringify(errors));
127 $scope.set_time_error = true;
128 })
129 .finally(function() {
130 $scope.loading = false;
131 });
132 },
133 function(errors) {
134 console.log(JSON.stringify(errors));
135 $scope.set_time_error = true;
136 $scope.loading = false;
137 });
138 };
139 $scope.refresh = function() {
140 $route.reload();
141 };
142
143 $scope.addNTPField = function() {
144 $scope.ntp.servers.push('');
145 };
146
Gunnar Mills90121f32018-09-16 21:22:45 -0500147 $scope.removeNTPField = function(index) {
148 $scope.ntp.servers.splice(index, 1);
149 };
150
Gunnar Millsb7ea2792018-07-18 13:01:48 -0500151 function setNTPServers() {
152 // Remove any empty strings from the array. Important because we add an
153 // empty string to the end so the user can add a new NTP server, if the
154 // user doesn't fill out the field, we don't want to add.
155 $scope.ntp.servers = $scope.ntp.servers.filter(Boolean);
156 // NTP servers does not allow an empty array, since we remove all empty
157 // strings above, could have an empty array. TODO: openbmc/openbmc#3240
158 if ($scope.ntp.servers.length == 0) {
159 $scope.ntp.servers.push('');
160 }
161 return APIUtils.setNTPServers($scope.ntp.servers);
162 }
163
164 function setTimeMode() {
165 return APIUtils.setTimeMode(
166 'xyz.openbmc_project.Time.Synchronization.Method.' +
167 $scope.time_mode);
168 }
169
170 function setTimeOwner() {
171 return APIUtils.setTimeOwner(
172 'xyz.openbmc_project.Time.Owner.Owners.' + $scope.time_owner);
173 }
174
Gunnar Millseab32132018-09-27 10:07:17 -0500175 function setBMCTime(time) {
Gunnar Millsb7ea2792018-07-18 13:01:48 -0500176 // Add the separate date and time objects and convert to Epoch time in
177 // microseconds.
Gunnar Millseab32132018-09-27 10:07:17 -0500178 return APIUtils.setBMCTime(time);
Gunnar Millsb7ea2792018-07-18 13:01:48 -0500179 }
180
Gunnar Millseab32132018-09-27 10:07:17 -0500181 function setHostTime(time) {
Gunnar Millsb7ea2792018-07-18 13:01:48 -0500182 // Add the separate date and time objects and convert to Epoch time
183 // microseconds.
Gunnar Millseab32132018-09-27 10:07:17 -0500184 return APIUtils.setHostTime(time);
Gunnar Millsb7ea2792018-07-18 13:01:48 -0500185 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700186 }
187 ]);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500188})(angular);