Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Controller for date-time |
| 3 | * |
| 4 | * @module app/configuration |
| 5 | * @exports dateTimeController |
| 6 | * @name dateTimeController |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 9 | window.angular && (function(angular) { |
| 10 | 'use strict'; |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 11 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 12 | angular.module('app.configuration').controller('dateTimeController', [ |
Gunnar Mills | b7ea279 | 2018-07-18 13:01:48 -0500 | [diff] [blame] | 13 | '$scope', '$window', 'APIUtils', '$route', '$q', |
| 14 | function($scope, $window, APIUtils, $route, $q) { |
| 15 | $scope.bmc = {}; |
Gunnar Mills | eab3213 | 2018-09-27 10:07:17 -0500 | [diff] [blame] | 16 | // Only used when the owner is "Split" |
Gunnar Mills | b7ea279 | 2018-07-18 13:01:48 -0500 | [diff] [blame] | 17 | $scope.host = {}; |
| 18 | $scope.ntp = {servers: []}; |
Gunnar Mills | c74d434 | 2018-07-18 14:52:02 -0500 | [diff] [blame] | 19 | $scope.time_mode = ''; |
| 20 | $scope.time_owner = ''; |
Gunnar Mills | b7ea279 | 2018-07-18 13:01:48 -0500 | [diff] [blame] | 21 | $scope.time_owners = ['BMC', 'Host', 'Both', 'Split']; |
| 22 | $scope.set_time_error = false; |
| 23 | $scope.set_time_success = false; |
Gunnar Mills | 7de3866 | 2018-07-18 13:01:48 -0500 | [diff] [blame] | 24 | $scope.loading = true; |
Gunnar Mills | b7ea279 | 2018-07-18 13:01:48 -0500 | [diff] [blame] | 25 | var time_path = '/xyz/openbmc_project/time/'; |
Gunnar Mills | 7de3866 | 2018-07-18 13:01:48 -0500 | [diff] [blame] | 26 | |
Gunnar Mills | c74d434 | 2018-07-18 14:52:02 -0500 | [diff] [blame] | 27 | var getTimePromise = APIUtils.getTime().then( |
Gunnar Mills | 7de3866 | 2018-07-18 13:01:48 -0500 | [diff] [blame] | 28 | function(data) { |
Gunnar Mills | b7ea279 | 2018-07-18 13:01:48 -0500 | [diff] [blame] | 29 | // 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 Mills | 7de3866 | 2018-07-18 13:01:48 -0500 | [diff] [blame] | 62 | }, |
| 63 | function(error) { |
| 64 | console.log(JSON.stringify(error)); |
| 65 | }); |
| 66 | |
Gunnar Mills | b7ea279 | 2018-07-18 13:01:48 -0500 | [diff] [blame] | 67 | 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 Mills | 7de3866 | 2018-07-18 13:01:48 -0500 | [diff] [blame] | 81 | $scope.loading = false; |
| 82 | }); |
Gunnar Mills | b7ea279 | 2018-07-18 13:01:48 -0500 | [diff] [blame] | 83 | |
| 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 Mills | eab3213 | 2018-09-27 10:07:17 -0500 | [diff] [blame] | 102 | manual_promises.push( |
| 103 | setBMCTime($scope.bmc.date.getTime() * 1000)); |
Gunnar Mills | b7ea279 | 2018-07-18 13:01:48 -0500 | [diff] [blame] | 104 | } |
Gunnar Mills | eab3213 | 2018-09-27 10:07:17 -0500 | [diff] [blame] | 105 | // 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 Mills | f378c77 | 2018-09-27 08:53:07 -0500 | [diff] [blame] | 108 | if ($scope.time_owner == 'Host') { |
Gunnar Mills | eab3213 | 2018-09-27 10:07:17 -0500 | [diff] [blame] | 109 | manual_promises.push( |
| 110 | setHostTime($scope.bmc.date.getTime() * 1000)); |
Gunnar Mills | b7ea279 | 2018-07-18 13:01:48 -0500 | [diff] [blame] | 111 | } |
| 112 | } |
Gunnar Mills | eab3213 | 2018-09-27 10:07:17 -0500 | [diff] [blame] | 113 | // 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 Mills | f378c77 | 2018-09-27 08:53:07 -0500 | [diff] [blame] | 115 | if ($scope.time_owner == 'Split') { |
Gunnar Mills | eab3213 | 2018-09-27 10:07:17 -0500 | [diff] [blame] | 116 | manual_promises.push( |
| 117 | setHostTime($scope.host.date.getTime() * 1000)); |
Gunnar Mills | f378c77 | 2018-09-27 08:53:07 -0500 | [diff] [blame] | 118 | } |
| 119 | |
Gunnar Mills | b7ea279 | 2018-07-18 13:01:48 -0500 | [diff] [blame] | 120 | $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 Mills | 90121f3 | 2018-09-16 21:22:45 -0500 | [diff] [blame] | 147 | $scope.removeNTPField = function(index) { |
| 148 | $scope.ntp.servers.splice(index, 1); |
| 149 | }; |
| 150 | |
Gunnar Mills | b7ea279 | 2018-07-18 13:01:48 -0500 | [diff] [blame] | 151 | 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 Mills | eab3213 | 2018-09-27 10:07:17 -0500 | [diff] [blame] | 175 | function setBMCTime(time) { |
Gunnar Mills | b7ea279 | 2018-07-18 13:01:48 -0500 | [diff] [blame] | 176 | // Add the separate date and time objects and convert to Epoch time in |
| 177 | // microseconds. |
Gunnar Mills | eab3213 | 2018-09-27 10:07:17 -0500 | [diff] [blame] | 178 | return APIUtils.setBMCTime(time); |
Gunnar Mills | b7ea279 | 2018-07-18 13:01:48 -0500 | [diff] [blame] | 179 | } |
| 180 | |
Gunnar Mills | eab3213 | 2018-09-27 10:07:17 -0500 | [diff] [blame] | 181 | function setHostTime(time) { |
Gunnar Mills | b7ea279 | 2018-07-18 13:01:48 -0500 | [diff] [blame] | 182 | // Add the separate date and time objects and convert to Epoch time |
| 183 | // microseconds. |
Gunnar Mills | eab3213 | 2018-09-27 10:07:17 -0500 | [diff] [blame] | 184 | return APIUtils.setHostTime(time); |
Gunnar Mills | b7ea279 | 2018-07-18 13:01:48 -0500 | [diff] [blame] | 185 | } |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 186 | } |
| 187 | ]); |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 188 | })(angular); |