xref: /openbmc/webui-vue/src/store/modules/Settings/DateTimeStore.js (revision 75b020396e10c3ecc5ceae8eaf7ce7705445cdf8)
1import api from '@/store/api';
2import i18n from '@/i18n';
3
4const DateTimeStore = {
5  namespaced: true,
6  state: {
7    ntpServers: [],
8    isNtpProtocolEnabled: null,
9  },
10  getters: {
11    ntpServers: (state) => state.ntpServers,
12    isNtpProtocolEnabled: (state) => state.isNtpProtocolEnabled,
13  },
14  mutations: {
15    setNtpServers: (state, ntpServers) => (state.ntpServers = ntpServers),
16    setIsNtpProtocolEnabled: (state, isNtpProtocolEnabled) =>
17      (state.isNtpProtocolEnabled = isNtpProtocolEnabled),
18  },
19  actions: {
20    async getNtpData({ commit }) {
21      return await api
22        .get(`${await this.dispatch('global/getBmcPath')}/NetworkProtocol`)
23        .then((response) => {
24          const ntpServers = response.data.NTP.NTPServers;
25          const isNtpProtocolEnabled = response.data.NTP.ProtocolEnabled;
26          commit('setNtpServers', ntpServers);
27          commit('setIsNtpProtocolEnabled', isNtpProtocolEnabled);
28        })
29        .catch((error) => {
30          console.log(error);
31        });
32    },
33    async updateDateTime({ state }, dateTimeForm) {
34      const ntpData = {
35        NTP: {
36          ProtocolEnabled: dateTimeForm.ntpProtocolEnabled,
37        },
38      };
39      if (dateTimeForm.ntpProtocolEnabled) {
40        ntpData.NTP.NTPServers = dateTimeForm.ntpServersArray;
41      }
42
43      const bmcPath = await this.dispatch('global/getBmcPath');
44
45      // Try to update NTP settings, but continue with DateTime even if it fails
46      return await api
47        .patch(`${bmcPath}/NetworkProtocol`, ntpData)
48        .catch((error) => {
49          console.warn(
50            'NTP settings update failed, continuing with DateTime update:',
51            error,
52          );
53        })
54        .then(async () => {
55          if (!dateTimeForm.ntpProtocolEnabled) {
56            const dateTimeData = {
57              DateTime: dateTimeForm.updatedDateTime,
58            };
59            /**
60             * https://github.com/openbmc/phosphor-time-manager/blob/master/README.md#special-note-on-changing-ntp-setting
61             * When time mode is initially set to Manual from NTP,
62             * NTP service is disabled and the NTP service is
63             * stopping but not stopped, setting time will return an error.
64             * There are no responses from backend to notify when NTP is stopped.
65             * To work around, a timeout is set to allow NTP to fully stop
66             * TODO: remove timeout if backend solves
67             * https://github.com/openbmc/openbmc/issues/3459
68             */
69            const timeoutVal = state.isNtpProtocolEnabled ? 20000 : 0;
70            return await new Promise((resolve, reject) => {
71              setTimeout(async () => {
72                return api
73                  .patch(bmcPath, dateTimeData)
74                  .then(() => resolve())
75                  .catch(() => reject());
76              }, timeoutVal);
77            });
78          }
79        })
80        .then(() => {
81          return i18n.global.t('pageDateTime.toast.successSaveDateTime');
82        })
83        .catch((error) => {
84          console.log(error);
85          throw new Error(
86            i18n.global.t('pageDateTime.toast.errorSaveDateTime'),
87          );
88        });
89    },
90  },
91};
92
93export default DateTimeStore;
94