summaryrefslogtreecommitdiff
path: root/src/store/modules/HardwareStatus/PciStore.js
blob: 9f76e69de2d2ecfc3e354d623dac77ca95cc03b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import api from '@/store/api';

const PciStore = {
  namespaced: true,
  state: {
    pci: [],
  },
  getters: {
    pciDevices: (state) => state.pci,
  },
  mutations: {
    resetState: (state) => {
      state.pci = [];
    },
    setPciDevises: (state, data) => {
      const device = {
        id: data.data.Id,
        device: data.data.Device,
        name: data.data.Name,
        DeviceType: data.data.DeviceType,
        manufacturer: data.data.Manufacturer,
        PCIeFunction: data.data.PCIeFunctions,
      };
      state.pci.push(device);
    },
    setPciFunctions: (state, data) => {
      const index = state.pci.length - 1;
      console.log(index);
      const functions = data.map((item) => {
        return {
          classCode: item.value.data.ClassCode,
          deviceClass: item.value.data.DeviceClass,
          deviceId: item.value.data.DeviceId,
          functionType: item.value.data.FunctionType,
          DeviceName: item.value.data.DeviceName,
        };
      });
      if (data.length > 1) {
        state.pci[index].Functions = functions;
      } else {
        console.log('state.inedex', state.pci[index]);
        state.pci[index].Functions = functions[0];
        state.pci[index].DeviceName = functions[0].DeviceName;
      }
    },
  },
  actions: {
    async getDevices({ commit }) {
      commit('resetState');

      return await api
        .get('/redfish/v1/Systems/system/PCIeDevices')
        // get all pci devices
        .then(({ data: { Members = [] } }) => {
          // get for each device
          const devices = Members.map((item) => async () =>
            api
              .get(item['@odata.id'])
              .then((response) => {
                commit('setPciDevises', response);
                return response;
              })
              .then((res) => {
                // get functions data
                api
                  .get(res.data.PCIeFunctions['@odata.id'])
                  .then((res) => {
                    const responses = res.data.Members.map((item) => {
                      return api.get(item['@odata.id']);
                    });
                    return Promise.allSettled(responses);
                  })
                  .then((res) => commit('setPciFunctions', res))
                  .catch((error) => console.log(error));
              })
          );
          return devices;
        })
        .then(async (devices) => {
          for (let request of devices) {
            await request();
          }
        })
        .catch((error) => console.log(error));
    },
  },
};

export default PciStore;