SurenNeware | 6185909 | 2020-10-01 09:37:32 +0530 | [diff] [blame] | 1 | import api from '@/store/api'; |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 2 | import i18n from '@/i18n'; |
| 3 | |
| 4 | const DateTimeStore = { |
| 5 | namespaced: true, |
| 6 | state: { |
| 7 | ntpServers: [], |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 8 | isNtpProtocolEnabled: null, |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 9 | }, |
| 10 | getters: { |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 11 | ntpServers: (state) => state.ntpServers, |
| 12 | isNtpProtocolEnabled: (state) => state.isNtpProtocolEnabled, |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 13 | }, |
| 14 | mutations: { |
| 15 | setNtpServers: (state, ntpServers) => (state.ntpServers = ntpServers), |
| 16 | setIsNtpProtocolEnabled: (state, isNtpProtocolEnabled) => |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 17 | (state.isNtpProtocolEnabled = isNtpProtocolEnabled), |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 18 | }, |
| 19 | actions: { |
| 20 | async getNtpData({ commit }) { |
| 21 | return await api |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame^] | 22 | .get(`${await this.dispatch('global/getBmcPath')}/NetworkProtocol`) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 23 | .then((response) => { |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 24 | const ntpServers = response.data.NTP.NTPServers; |
| 25 | const isNtpProtocolEnabled = response.data.NTP.ProtocolEnabled; |
| 26 | commit('setNtpServers', ntpServers); |
| 27 | commit('setIsNtpProtocolEnabled', isNtpProtocolEnabled); |
| 28 | }) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 29 | .catch((error) => { |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 30 | console.log(error); |
| 31 | }); |
| 32 | }, |
Sandeepa Singh | f67f769 | 2021-07-19 18:04:18 +0530 | [diff] [blame] | 33 | async updateDateTime({ state }, dateTimeForm) { |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 34 | const ntpData = { |
| 35 | NTP: { |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 36 | ProtocolEnabled: dateTimeForm.ntpProtocolEnabled, |
| 37 | }, |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 38 | }; |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 39 | if (dateTimeForm.ntpProtocolEnabled) { |
| 40 | ntpData.NTP.NTPServers = dateTimeForm.ntpServersArray; |
| 41 | } |
| 42 | return await api |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame^] | 43 | .patch( |
| 44 | `${await this.dispatch('global/getBmcPath')}/NetworkProtocol`, |
| 45 | ntpData, |
| 46 | ) |
Dixsie Wolmers | ecd45a8 | 2020-07-14 17:07:26 -0500 | [diff] [blame] | 47 | .then(async () => { |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 48 | if (!dateTimeForm.ntpProtocolEnabled) { |
| 49 | const dateTimeData = { |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 50 | DateTime: dateTimeForm.updatedDateTime, |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 51 | }; |
Dixsie Wolmers | ecd45a8 | 2020-07-14 17:07:26 -0500 | [diff] [blame] | 52 | /** |
| 53 | * https://github.com/openbmc/phosphor-time-manager/blob/master/README.md#special-note-on-changing-ntp-setting |
| 54 | * When time mode is initially set to Manual from NTP, |
| 55 | * NTP service is disabled and the NTP service is |
| 56 | * stopping but not stopped, setting time will return an error. |
| 57 | * There are no responses from backend to notify when NTP is stopped. |
| 58 | * To work around, a timeout is set to allow NTP to fully stop |
| 59 | * TODO: remove timeout if backend solves |
| 60 | * https://github.com/openbmc/openbmc/issues/3459 |
| 61 | */ |
| 62 | const timeoutVal = state.isNtpProtocolEnabled ? 20000 : 0; |
| 63 | return await new Promise((resolve, reject) => { |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame^] | 64 | setTimeout(async () => { |
Dixsie Wolmers | ecd45a8 | 2020-07-14 17:07:26 -0500 | [diff] [blame] | 65 | return api |
Sean Zhang | 8841b7d | 2024-06-15 08:42:41 +0300 | [diff] [blame^] | 66 | .patch( |
| 67 | `${await this.dispatch('global/getBmcPath')}`, |
| 68 | dateTimeData, |
| 69 | ) |
Dixsie Wolmers | ecd45a8 | 2020-07-14 17:07:26 -0500 | [diff] [blame] | 70 | .then(() => resolve()) |
| 71 | .catch(() => reject()); |
| 72 | }, timeoutVal); |
| 73 | }); |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 74 | } |
| 75 | }) |
| 76 | .then(() => { |
Sandeepa Singh | f67f769 | 2021-07-19 18:04:18 +0530 | [diff] [blame] | 77 | return i18n.t('pageDateTime.toast.successSaveDateTime'); |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 78 | }) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 79 | .catch((error) => { |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 80 | console.log(error); |
Sandeepa Singh | f67f769 | 2021-07-19 18:04:18 +0530 | [diff] [blame] | 81 | throw new Error(i18n.t('pageDateTime.toast.errorSaveDateTime')); |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 82 | }); |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 83 | }, |
| 84 | }, |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | export default DateTimeStore; |