blob: aca4c4c73f6f51530d5c4efe88358754e2d8a880 [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 Mills2f955bd2018-10-13 16:56:10 -050019 $scope.time = {mode: '', owner: ''};
20 // Possible time owners
21 // https://github.com/openbmc/phosphor-dbus-interfaces/blob/master/xyz/openbmc_project/Time/Owner.interface.yaml#L13
22 $scope.timeOwners = ['BMC', 'Host', 'Both', 'Split'];
23 $scope.error = false;
24 $scope.success = false;
Gunnar Mills7de38662018-07-18 13:01:48 -050025 $scope.loading = true;
Gunnar Mills2f955bd2018-10-13 16:56:10 -050026 var timePath = '/xyz/openbmc_project/time/';
Gunnar Mills7de38662018-07-18 13:01:48 -050027
Gunnar Millsc74d4342018-07-18 14:52:02 -050028 var getTimePromise = APIUtils.getTime().then(
Gunnar Mills7de38662018-07-18 13:01:48 -050029 function(data) {
Gunnar Millsb7ea2792018-07-18 13:01:48 -050030 // The time is returned as Epoch microseconds convert to
31 // milliseconds.
Gunnar Mills2f955bd2018-10-13 16:56:10 -050032 if (data.data[timePath + 'bmc'] &&
33 data.data[timePath + 'bmc'].hasOwnProperty('Elapsed')) {
Gunnar Millsb7ea2792018-07-18 13:01:48 -050034 $scope.bmc.date =
Gunnar Mills2f955bd2018-10-13 16:56:10 -050035 new Date(data.data[timePath + 'bmc'].Elapsed / 1000);
Gunnar Millsb7ea2792018-07-18 13:01:48 -050036 // Don't care about milliseconds and don't want them displayed
37 $scope.bmc.date.setMilliseconds(0);
38 // https://stackoverflow.com/questions/1091372/getting-the-clients-timezone-in-javascript
39 // GMT-0400 (EDT)
40 $scope.bmc.timezone =
41 $scope.bmc.date.toString().match(/([A-Z]+[\+-][0-9]+.*)/)[1];
42 }
Gunnar Mills2f955bd2018-10-13 16:56:10 -050043 if (data.data[timePath + 'host'] &&
44 data.data[timePath + 'host'].hasOwnProperty('Elapsed')) {
Gunnar Millsb7ea2792018-07-18 13:01:48 -050045 $scope.host.date =
Gunnar Mills2f955bd2018-10-13 16:56:10 -050046 new Date(data.data[timePath + 'host'].Elapsed / 1000);
Gunnar Millsb7ea2792018-07-18 13:01:48 -050047 $scope.host.date.setMilliseconds(0);
48 $scope.host.timezone =
49 $scope.host.date.toString().match(/([A-Z]+[\+-][0-9]+.*)/)[1];
50 }
Gunnar Mills2f955bd2018-10-13 16:56:10 -050051 if (data.data[timePath + 'owner'] &&
52 data.data[timePath + 'owner'].hasOwnProperty('TimeOwner')) {
53 $scope.time.owner =
54 data.data[timePath + 'owner'].TimeOwner.split('.').pop();
Gunnar Millsb7ea2792018-07-18 13:01:48 -050055 }
Gunnar Mills2f955bd2018-10-13 16:56:10 -050056 if (data.data[timePath + 'sync_method'] &&
57 data.data[timePath + 'sync_method'].hasOwnProperty(
Gunnar Millsb7ea2792018-07-18 13:01:48 -050058 'TimeSyncMethod')) {
Gunnar Mills2f955bd2018-10-13 16:56:10 -050059 $scope.time.mode = data.data[timePath + 'sync_method']
Gunnar Millsb7ea2792018-07-18 13:01:48 -050060 .TimeSyncMethod.split('.')
61 .pop();
62 }
Gunnar Mills7de38662018-07-18 13:01:48 -050063 },
64 function(error) {
65 console.log(JSON.stringify(error));
66 });
67
Gunnar Millsb7ea2792018-07-18 13:01:48 -050068 var getNTPPromise = APIUtils.getNTPServers().then(
69 function(data) {
70 $scope.ntp.servers = data.data;
71 },
72 function(error) {
73 console.log(JSON.stringify(error));
74 });
75
76 var promises = [
77 getTimePromise,
78 getNTPPromise,
79 ];
80
81 $q.all(promises).finally(function() {
Gunnar Mills7de38662018-07-18 13:01:48 -050082 $scope.loading = false;
83 });
Gunnar Millsb7ea2792018-07-18 13:01:48 -050084
85 $scope.setTime = function() {
Gunnar Mills2f955bd2018-10-13 16:56:10 -050086 $scope.error = false;
87 $scope.success = false;
Gunnar Millsb7ea2792018-07-18 13:01:48 -050088 $scope.loading = true;
89 var promises = [setTimeMode(), setTimeOwner(), setNTPServers()];
90
91 $q.all(promises).then(
92 function() {
93 // Have to set the time mode and time owner first to avoid a
94 // insufficient permissions if the time mode or time owner had
95 // changed.
96 var manual_promises = [];
Gunnar Mills2f955bd2018-10-13 16:56:10 -050097 if ($scope.time.mode == 'Manual') {
Gunnar Millsb7ea2792018-07-18 13:01:48 -050098 // If owner is 'Split' set both.
99 // If owner is 'Host' set only it.
100 // Else set BMC only. See:
101 // https://github.com/openbmc/phosphor-time-manager/blob/master/README.md
Gunnar Mills2f955bd2018-10-13 16:56:10 -0500102 if ($scope.time.owner != 'Host') {
Gunnar Millseab32132018-09-27 10:07:17 -0500103 manual_promises.push(
104 setBMCTime($scope.bmc.date.getTime() * 1000));
Gunnar Millsb7ea2792018-07-18 13:01:48 -0500105 }
Gunnar Millseab32132018-09-27 10:07:17 -0500106 // Even though we are setting Host time, we are setting from
107 // the BMC date and time fields labeled "BMC and Host Time"
108 // currently.
Gunnar Mills2f955bd2018-10-13 16:56:10 -0500109 if ($scope.time.owner == 'Host') {
Gunnar Millseab32132018-09-27 10:07:17 -0500110 manual_promises.push(
111 setHostTime($scope.bmc.date.getTime() * 1000));
Gunnar Millsb7ea2792018-07-18 13:01:48 -0500112 }
113 }
Gunnar Millseab32132018-09-27 10:07:17 -0500114 // Set the Host if Split even if NTP. In split mode, the host has
115 // its own date and time field. Set from it.
Gunnar Mills2f955bd2018-10-13 16:56:10 -0500116 if ($scope.time.owner == 'Split') {
Gunnar Millseab32132018-09-27 10:07:17 -0500117 manual_promises.push(
118 setHostTime($scope.host.date.getTime() * 1000));
Gunnar Millsf378c772018-09-27 08:53:07 -0500119 }
120
Gunnar Millsb7ea2792018-07-18 13:01:48 -0500121 $q.all(manual_promises)
122 .then(
123 function() {
Gunnar Mills2f955bd2018-10-13 16:56:10 -0500124 $scope.success = true;
Gunnar Millsb7ea2792018-07-18 13:01:48 -0500125 },
126 function(errors) {
127 console.log(JSON.stringify(errors));
Gunnar Mills2f955bd2018-10-13 16:56:10 -0500128 $scope.error = true;
Gunnar Millsb7ea2792018-07-18 13:01:48 -0500129 })
130 .finally(function() {
131 $scope.loading = false;
132 });
133 },
134 function(errors) {
135 console.log(JSON.stringify(errors));
Gunnar Mills2f955bd2018-10-13 16:56:10 -0500136 $scope.error = true;
Gunnar Millsb7ea2792018-07-18 13:01:48 -0500137 $scope.loading = false;
138 });
139 };
140 $scope.refresh = function() {
141 $route.reload();
142 };
143
144 $scope.addNTPField = function() {
145 $scope.ntp.servers.push('');
146 };
147
Gunnar Mills90121f32018-09-16 21:22:45 -0500148 $scope.removeNTPField = function(index) {
149 $scope.ntp.servers.splice(index, 1);
150 };
151
Gunnar Millsb7ea2792018-07-18 13:01:48 -0500152 function setNTPServers() {
153 // Remove any empty strings from the array. Important because we add an
154 // empty string to the end so the user can add a new NTP server, if the
155 // user doesn't fill out the field, we don't want to add.
156 $scope.ntp.servers = $scope.ntp.servers.filter(Boolean);
157 // NTP servers does not allow an empty array, since we remove all empty
158 // strings above, could have an empty array. TODO: openbmc/openbmc#3240
159 if ($scope.ntp.servers.length == 0) {
160 $scope.ntp.servers.push('');
161 }
162 return APIUtils.setNTPServers($scope.ntp.servers);
163 }
164
165 function setTimeMode() {
166 return APIUtils.setTimeMode(
167 'xyz.openbmc_project.Time.Synchronization.Method.' +
Gunnar Mills2f955bd2018-10-13 16:56:10 -0500168 $scope.time.mode);
Gunnar Millsb7ea2792018-07-18 13:01:48 -0500169 }
170
171 function setTimeOwner() {
172 return APIUtils.setTimeOwner(
Gunnar Mills2f955bd2018-10-13 16:56:10 -0500173 'xyz.openbmc_project.Time.Owner.Owners.' + $scope.time.owner);
Gunnar Millsb7ea2792018-07-18 13:01:48 -0500174 }
175
Gunnar Millseab32132018-09-27 10:07:17 -0500176 function setBMCTime(time) {
Gunnar Millsb7ea2792018-07-18 13:01:48 -0500177 // Add the separate date and time objects and convert to Epoch time in
178 // microseconds.
Gunnar Millseab32132018-09-27 10:07:17 -0500179 return APIUtils.setBMCTime(time);
Gunnar Millsb7ea2792018-07-18 13:01:48 -0500180 }
181
Gunnar Millseab32132018-09-27 10:07:17 -0500182 function setHostTime(time) {
Gunnar Millsb7ea2792018-07-18 13:01:48 -0500183 // Add the separate date and time objects and convert to Epoch time
184 // microseconds.
Gunnar Millseab32132018-09-27 10:07:17 -0500185 return APIUtils.setHostTime(time);
Gunnar Millsb7ea2792018-07-18 13:01:48 -0500186 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700187 }
188 ]);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500189})(angular);