ModelOfMatter.config.ts
2.2 KB
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
import { DeviceModelOfMatterAttrs } from '/@/api/device/model/deviceModel';
import { DataType, Specs } from '/@/api/device/model/modelOfMatterModel';
import { DataTypeEnum } from '/@/enums/objectModelEnum';
import { isArray } from '/@/utils/is';
export interface StructValueItemType extends BaseAdditionalInfo {
  name: string;
  key: string;
  value?: string | number | boolean;
}
export interface BaseAdditionalInfo {
  unit?: string;
  boolClose?: string;
  boolOpen?: string;
  unitName?: string;
}
export interface SocketInfoDataSourceItemType extends BaseAdditionalInfo {
  accessMode: string;
  key: string;
  name: string;
  type: DataTypeEnum;
  detail: DeviceModelOfMatterAttrs;
  time?: number;
  value?: string | number | boolean | Record<string, StructValueItemType>;
  expand?: boolean;
  showHistoryDataButton?: boolean;
  rawValue?: any;
}
export function buildTableDataSourceByObjectModel(
  models: DeviceModelOfMatterAttrs[]
): SocketInfoDataSourceItemType[] {
  function getAdditionalInfoByDataType(dataType?: DataType) {
    const { specs } = dataType || {};
    if (isArray(specs)) return {};
    const { unit, boolClose, boolOpen, unitName } = (specs as Partial<Specs>) || {};
    return { unit, boolClose, boolOpen, unitName };
  }
  return models.map((item) => {
    const { accessMode, identifier, name, detail } = item;
    const { dataType } = detail;
    const { type, specs } = dataType || {};
    const res = {
      accessMode,
      name,
      key: identifier,
      type: type!,
      detail: item,
    };
    if (isArray(specs) && type === DataTypeEnum.STRUCT) {
      const value: Record<string, StructValueItemType> = specs
        .filter((item) => item.dataType?.type !== DataTypeEnum.STRUCT)
        .reduce((prev, next) => {
          const { identifier, functionName, dataType } = next;
          return {
            ...prev,
            [identifier]: {
              key: identifier!,
              name: functionName!,
              type: dataType?.type,
              ...getAdditionalInfoByDataType(dataType),
            },
          };
        }, {});
      Object.assign(res, { value });
    } else {
      Object.assign(res, getAdditionalInfoByDataType(dataType));
    }
    return res;
  });
}