1cd789508SIftekharul Islam/** 2cd789508SIftekharul Islam * Controller for date-time 3cd789508SIftekharul Islam * 4cd789508SIftekharul Islam * @module app/configuration 5cd789508SIftekharul Islam * @exports dateTimeController 6cd789508SIftekharul Islam * @name dateTimeController 7cd789508SIftekharul Islam */ 8cd789508SIftekharul Islam 9cd789508SIftekharul Islamwindow.angular && (function(angular) { 10cd789508SIftekharul Islam 'use strict'; 11cd789508SIftekharul Islam 12d27bb135SAndrew Geissler angular.module('app.configuration').controller('dateTimeController', [ 13*b7ea2790SGunnar Mills '$scope', '$window', 'APIUtils', '$route', '$q', 14*b7ea2790SGunnar Mills function($scope, $window, APIUtils, $route, $q) { 15*b7ea2790SGunnar Mills $scope.bmc = {}; 16*b7ea2790SGunnar Mills $scope.host = {}; 17*b7ea2790SGunnar Mills $scope.ntp = {servers: []}; 18c74d434cSGunnar Mills $scope.time_mode = ''; 19c74d434cSGunnar Mills $scope.time_owner = ''; 20*b7ea2790SGunnar Mills $scope.time_owners = ['BMC', 'Host', 'Both', 'Split']; 21*b7ea2790SGunnar Mills $scope.set_time_error = false; 22*b7ea2790SGunnar Mills $scope.set_time_success = false; 237de38662SGunnar Mills $scope.loading = true; 24*b7ea2790SGunnar Mills var time_path = '/xyz/openbmc_project/time/'; 257de38662SGunnar Mills 26c74d434cSGunnar Mills var getTimePromise = APIUtils.getTime().then( 277de38662SGunnar Mills function(data) { 28*b7ea2790SGunnar Mills // The time is returned as Epoch microseconds convert to 29*b7ea2790SGunnar Mills // milliseconds. 30*b7ea2790SGunnar Mills if (data.data[time_path + 'bmc'] && 31*b7ea2790SGunnar Mills data.data[time_path + 'bmc'].hasOwnProperty('Elapsed')) { 32*b7ea2790SGunnar Mills $scope.bmc.date = 33*b7ea2790SGunnar Mills new Date(data.data[time_path + 'bmc'].Elapsed / 1000); 34*b7ea2790SGunnar Mills // Don't care about milliseconds and don't want them displayed 35*b7ea2790SGunnar Mills $scope.bmc.date.setMilliseconds(0); 36*b7ea2790SGunnar Mills // https://stackoverflow.com/questions/1091372/getting-the-clients-timezone-in-javascript 37*b7ea2790SGunnar Mills // GMT-0400 (EDT) 38*b7ea2790SGunnar Mills $scope.bmc.timezone = 39*b7ea2790SGunnar Mills $scope.bmc.date.toString().match(/([A-Z]+[\+-][0-9]+.*)/)[1]; 40*b7ea2790SGunnar Mills } 41*b7ea2790SGunnar Mills if (data.data[time_path + 'host'] && 42*b7ea2790SGunnar Mills data.data[time_path + 'host'].hasOwnProperty('Elapsed')) { 43*b7ea2790SGunnar Mills $scope.host.date = 44*b7ea2790SGunnar Mills new Date(data.data[time_path + 'host'].Elapsed / 1000); 45*b7ea2790SGunnar Mills $scope.host.date.setMilliseconds(0); 46*b7ea2790SGunnar Mills $scope.host.timezone = 47*b7ea2790SGunnar Mills $scope.host.date.toString().match(/([A-Z]+[\+-][0-9]+.*)/)[1]; 48*b7ea2790SGunnar Mills } 49*b7ea2790SGunnar Mills if (data.data[time_path + 'owner'] && 50*b7ea2790SGunnar Mills data.data[time_path + 'owner'].hasOwnProperty('TimeOwner')) { 51*b7ea2790SGunnar Mills $scope.time_owner = 52*b7ea2790SGunnar Mills data.data[time_path + 'owner'].TimeOwner.split('.').pop(); 53*b7ea2790SGunnar Mills } 54*b7ea2790SGunnar Mills if (data.data[time_path + 'sync_method'] && 55*b7ea2790SGunnar Mills data.data[time_path + 'sync_method'].hasOwnProperty( 56*b7ea2790SGunnar Mills 'TimeSyncMethod')) { 57*b7ea2790SGunnar Mills $scope.time_mode = data.data[time_path + 'sync_method'] 58c74d434cSGunnar Mills .TimeSyncMethod.split('.') 59c74d434cSGunnar Mills .pop(); 60*b7ea2790SGunnar Mills } 617de38662SGunnar Mills }, 627de38662SGunnar Mills function(error) { 637de38662SGunnar Mills console.log(JSON.stringify(error)); 647de38662SGunnar Mills }); 657de38662SGunnar Mills 66*b7ea2790SGunnar Mills var getNTPPromise = APIUtils.getNTPServers().then( 67*b7ea2790SGunnar Mills function(data) { 68*b7ea2790SGunnar Mills $scope.ntp.servers = data.data; 69*b7ea2790SGunnar Mills }, 70*b7ea2790SGunnar Mills function(error) { 71*b7ea2790SGunnar Mills console.log(JSON.stringify(error)); 72*b7ea2790SGunnar Mills }); 73*b7ea2790SGunnar Mills 74*b7ea2790SGunnar Mills var promises = [ 75*b7ea2790SGunnar Mills getTimePromise, 76*b7ea2790SGunnar Mills getNTPPromise, 77*b7ea2790SGunnar Mills ]; 78*b7ea2790SGunnar Mills 79*b7ea2790SGunnar Mills $q.all(promises).finally(function() { 807de38662SGunnar Mills $scope.loading = false; 817de38662SGunnar Mills }); 82*b7ea2790SGunnar Mills 83*b7ea2790SGunnar Mills $scope.setTime = function() { 84*b7ea2790SGunnar Mills $scope.set_time_error = false; 85*b7ea2790SGunnar Mills $scope.set_time_success = false; 86*b7ea2790SGunnar Mills $scope.loading = true; 87*b7ea2790SGunnar Mills var promises = [setTimeMode(), setTimeOwner(), setNTPServers()]; 88*b7ea2790SGunnar Mills 89*b7ea2790SGunnar Mills $q.all(promises).then( 90*b7ea2790SGunnar Mills function() { 91*b7ea2790SGunnar Mills // Have to set the time mode and time owner first to avoid a 92*b7ea2790SGunnar Mills // insufficient permissions if the time mode or time owner had 93*b7ea2790SGunnar Mills // changed. 94*b7ea2790SGunnar Mills var manual_promises = []; 95*b7ea2790SGunnar Mills if ($scope.time_mode == 'Manual') { 96*b7ea2790SGunnar Mills // If owner is 'Split' set both. 97*b7ea2790SGunnar Mills // If owner is 'Host' set only it. 98*b7ea2790SGunnar Mills // Else set BMC only. See: 99*b7ea2790SGunnar Mills // https://github.com/openbmc/phosphor-time-manager/blob/master/README.md 100*b7ea2790SGunnar Mills if ($scope.time_owner != 'Host') { 101*b7ea2790SGunnar Mills manual_promises.push(setBMCTime()); 102*b7ea2790SGunnar Mills } 103*b7ea2790SGunnar Mills 104*b7ea2790SGunnar Mills if ($scope.time_owner == 'Host' || 105*b7ea2790SGunnar Mills $scope.time_owner == 'Split') { 106*b7ea2790SGunnar Mills manual_promises.push(setHostTime()); 107*b7ea2790SGunnar Mills } 108*b7ea2790SGunnar Mills } 109*b7ea2790SGunnar Mills $q.all(manual_promises) 110*b7ea2790SGunnar Mills .then( 111*b7ea2790SGunnar Mills function() { 112*b7ea2790SGunnar Mills $scope.set_time_success = true; 113*b7ea2790SGunnar Mills }, 114*b7ea2790SGunnar Mills function(errors) { 115*b7ea2790SGunnar Mills console.log(JSON.stringify(errors)); 116*b7ea2790SGunnar Mills $scope.set_time_error = true; 117*b7ea2790SGunnar Mills }) 118*b7ea2790SGunnar Mills .finally(function() { 119*b7ea2790SGunnar Mills $scope.loading = false; 120*b7ea2790SGunnar Mills }); 121*b7ea2790SGunnar Mills }, 122*b7ea2790SGunnar Mills function(errors) { 123*b7ea2790SGunnar Mills console.log(JSON.stringify(errors)); 124*b7ea2790SGunnar Mills $scope.set_time_error = true; 125*b7ea2790SGunnar Mills $scope.loading = false; 126*b7ea2790SGunnar Mills }); 127*b7ea2790SGunnar Mills }; 128*b7ea2790SGunnar Mills $scope.refresh = function() { 129*b7ea2790SGunnar Mills $route.reload(); 130*b7ea2790SGunnar Mills }; 131*b7ea2790SGunnar Mills 132*b7ea2790SGunnar Mills $scope.addNTPField = function() { 133*b7ea2790SGunnar Mills $scope.ntp.servers.push(''); 134*b7ea2790SGunnar Mills }; 135*b7ea2790SGunnar Mills 136*b7ea2790SGunnar Mills function setNTPServers() { 137*b7ea2790SGunnar Mills // Remove any empty strings from the array. Important because we add an 138*b7ea2790SGunnar Mills // empty string to the end so the user can add a new NTP server, if the 139*b7ea2790SGunnar Mills // user doesn't fill out the field, we don't want to add. 140*b7ea2790SGunnar Mills $scope.ntp.servers = $scope.ntp.servers.filter(Boolean); 141*b7ea2790SGunnar Mills // NTP servers does not allow an empty array, since we remove all empty 142*b7ea2790SGunnar Mills // strings above, could have an empty array. TODO: openbmc/openbmc#3240 143*b7ea2790SGunnar Mills if ($scope.ntp.servers.length == 0) { 144*b7ea2790SGunnar Mills $scope.ntp.servers.push(''); 145*b7ea2790SGunnar Mills } 146*b7ea2790SGunnar Mills return APIUtils.setNTPServers($scope.ntp.servers); 147*b7ea2790SGunnar Mills } 148*b7ea2790SGunnar Mills 149*b7ea2790SGunnar Mills function setTimeMode() { 150*b7ea2790SGunnar Mills return APIUtils.setTimeMode( 151*b7ea2790SGunnar Mills 'xyz.openbmc_project.Time.Synchronization.Method.' + 152*b7ea2790SGunnar Mills $scope.time_mode); 153*b7ea2790SGunnar Mills } 154*b7ea2790SGunnar Mills 155*b7ea2790SGunnar Mills function setTimeOwner() { 156*b7ea2790SGunnar Mills return APIUtils.setTimeOwner( 157*b7ea2790SGunnar Mills 'xyz.openbmc_project.Time.Owner.Owners.' + $scope.time_owner); 158*b7ea2790SGunnar Mills } 159*b7ea2790SGunnar Mills 160*b7ea2790SGunnar Mills function setBMCTime() { 161*b7ea2790SGunnar Mills // Add the separate date and time objects and convert to Epoch time in 162*b7ea2790SGunnar Mills // microseconds. 163*b7ea2790SGunnar Mills return APIUtils.setBMCTime($scope.bmc.date.getTime() * 1000); 164*b7ea2790SGunnar Mills } 165*b7ea2790SGunnar Mills 166*b7ea2790SGunnar Mills function setHostTime() { 167*b7ea2790SGunnar Mills // Add the separate date and time objects and convert to Epoch time 168*b7ea2790SGunnar Mills // microseconds. 169*b7ea2790SGunnar Mills return APIUtils.setHostTime($scope.host.date.getTime() * 1000); 170*b7ea2790SGunnar Mills } 171cd789508SIftekharul Islam } 172ba5e3f34SAndrew Geissler ]); 173cd789508SIftekharul Islam})(angular); 174