blob: 9d804a7eee63d7481b5fd1053a9f6867235bd1e3 [file] [log] [blame]
SurenNeware61859092020-10-01 09:37:32 +05301import api from '@/store/api';
Dixsie Wolmers739e4592020-06-05 07:00:06 -05002import i18n from '@/i18n';
3
4const DateTimeStore = {
5 namespaced: true,
6 state: {
7 ntpServers: [],
Derick Montague602e98a2020-10-21 16:20:00 -05008 isNtpProtocolEnabled: null,
Dixsie Wolmers739e4592020-06-05 07:00:06 -05009 },
10 getters: {
Derick Montague602e98a2020-10-21 16:20:00 -050011 ntpServers: (state) => state.ntpServers,
12 isNtpProtocolEnabled: (state) => state.isNtpProtocolEnabled,
Dixsie Wolmers739e4592020-06-05 07:00:06 -050013 },
14 mutations: {
15 setNtpServers: (state, ntpServers) => (state.ntpServers = ntpServers),
16 setIsNtpProtocolEnabled: (state, isNtpProtocolEnabled) =>
Derick Montague602e98a2020-10-21 16:20:00 -050017 (state.isNtpProtocolEnabled = isNtpProtocolEnabled),
Dixsie Wolmers739e4592020-06-05 07:00:06 -050018 },
19 actions: {
20 async getNtpData({ commit }) {
21 return await api
Sean Zhang8841b7d2024-06-15 08:42:41 +030022 .get(`${await this.dispatch('global/getBmcPath')}/NetworkProtocol`)
Derick Montague602e98a2020-10-21 16:20:00 -050023 .then((response) => {
Dixsie Wolmers739e4592020-06-05 07:00:06 -050024 const ntpServers = response.data.NTP.NTPServers;
25 const isNtpProtocolEnabled = response.data.NTP.ProtocolEnabled;
26 commit('setNtpServers', ntpServers);
27 commit('setIsNtpProtocolEnabled', isNtpProtocolEnabled);
28 })
Derick Montague602e98a2020-10-21 16:20:00 -050029 .catch((error) => {
Dixsie Wolmers739e4592020-06-05 07:00:06 -050030 console.log(error);
31 });
32 },
Sandeepa Singhf67f7692021-07-19 18:04:18 +053033 async updateDateTime({ state }, dateTimeForm) {
Dixsie Wolmers739e4592020-06-05 07:00:06 -050034 const ntpData = {
35 NTP: {
Derick Montague602e98a2020-10-21 16:20:00 -050036 ProtocolEnabled: dateTimeForm.ntpProtocolEnabled,
37 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -050038 };
Dixsie Wolmers739e4592020-06-05 07:00:06 -050039 if (dateTimeForm.ntpProtocolEnabled) {
40 ntpData.NTP.NTPServers = dateTimeForm.ntpServersArray;
41 }
42 return await api
Sean Zhang8841b7d2024-06-15 08:42:41 +030043 .patch(
44 `${await this.dispatch('global/getBmcPath')}/NetworkProtocol`,
45 ntpData,
46 )
Dixsie Wolmersecd45a82020-07-14 17:07:26 -050047 .then(async () => {
Dixsie Wolmers739e4592020-06-05 07:00:06 -050048 if (!dateTimeForm.ntpProtocolEnabled) {
49 const dateTimeData = {
Derick Montague602e98a2020-10-21 16:20:00 -050050 DateTime: dateTimeForm.updatedDateTime,
Dixsie Wolmers739e4592020-06-05 07:00:06 -050051 };
Dixsie Wolmersecd45a82020-07-14 17:07:26 -050052 /**
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 Zhang8841b7d2024-06-15 08:42:41 +030064 setTimeout(async () => {
Dixsie Wolmersecd45a82020-07-14 17:07:26 -050065 return api
Sean Zhang8841b7d2024-06-15 08:42:41 +030066 .patch(
67 `${await this.dispatch('global/getBmcPath')}`,
68 dateTimeData,
69 )
Dixsie Wolmersecd45a82020-07-14 17:07:26 -050070 .then(() => resolve())
71 .catch(() => reject());
72 }, timeoutVal);
73 });
Dixsie Wolmers739e4592020-06-05 07:00:06 -050074 }
75 })
76 .then(() => {
Sandeepa Singhf67f7692021-07-19 18:04:18 +053077 return i18n.t('pageDateTime.toast.successSaveDateTime');
Dixsie Wolmers739e4592020-06-05 07:00:06 -050078 })
Derick Montague602e98a2020-10-21 16:20:00 -050079 .catch((error) => {
Dixsie Wolmers739e4592020-06-05 07:00:06 -050080 console.log(error);
Sandeepa Singhf67f7692021-07-19 18:04:18 +053081 throw new Error(i18n.t('pageDateTime.toast.errorSaveDateTime'));
Dixsie Wolmers739e4592020-06-05 07:00:06 -050082 });
Derick Montague602e98a2020-10-21 16:20:00 -050083 },
84 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -050085};
86
87export default DateTimeStore;