ModelOfMatter.config.ts
3.08 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { DeviceModelOfMatterAttrs } from '/@/api/device/model/deviceModel';
import { EdgeDeviceItemType } from '/@/api/edgeManage/model/edgeInstance';
import { DataType, Specs } from '/@/api/device/model/modelOfMatterModel';
import { TCPProtocolTypeEnum, TransportTypeEnum } from '/@/enums/deviceEnum';
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;
enum?: Record<string, string>;
}
export function buildTableDataSourceByObjectModel(
models: DeviceModelOfMatterAttrs[],
deviceDetail: EdgeDeviceItemType
): SocketInfoDataSourceItemType[] {
const isTCPTransportType =
deviceDetail.deviceData.transportConfiguration.type === TransportTypeEnum.TCP;
const isModbusDevice =
isTCPTransportType &&
deviceDetail?.deviceProfile?.profileData?.transportConfiguration?.protocol ===
TCPProtocolTypeEnum.MODBUS_RTU;
function getAdditionalInfoByDataType(dataType?: DataType) {
const { specs, specsList, type } = dataType || {};
if (isArray(specs)) return {};
const { unit, boolClose, boolOpen, unitName } = (specs as Partial<Specs>) || {};
const result = { unit, boolClose, boolOpen, unitName };
if ((type == DataTypeEnum.ENUM && specsList && specsList.length) || isModbusDevice) {
Reflect.set(
result,
'enum',
(specsList || []).reduce((prev, next) => ({ ...prev, [next.value!]: next.name }), {})
);
}
if (isModbusDevice) {
result.boolClose = '关';
result.boolOpen = '开';
}
return result;
}
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;
});
}