useGetModbusCommand.ts
5.29 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { ExtensionDesc } from '/@/api/device/model/modelOfMatterModel';
import { genModbusCommand } from '/@/api/task';
import { GenModbusCommandType } from '/@/api/task/model';
import { ModbusCRCEnum, RegisterActionTypeEnum } from '/@/enums/objectModelEnum';
import { useMessage } from '/@/hooks/web/useMessage';
export const InsertString = (t: any, c: any, n: any) => {
const r: string | number[] = [];
for (let i = 0; i * 2 < t.length; i++) r.push(t.substr(i * 2, n));
return r.join(c);
};
export const FillString = (t: any, c: any, n: any, b: any) => {
if (t === '' || c.length !== 1 || n <= t.length) return t;
const l = t.length;
for (let i = 0; i < n - l; i++) {
if (b === true) t = c + t;
else t += c;
}
return t;
};
export const SingleToHex = (t: any) => {
if (t === '') return '';
t = parseFloat(t);
if (isNaN(t) === true) return 'Error';
if (t === 0) return '00000000';
let s, e, m;
if (t > 0) {
s = 0;
} else {
s = 1;
t = 0 - t;
}
m = t.toString(2);
if (m >= 1) {
if (m.indexOf('.') === -1) m = `${m}.0`;
e = m.indexOf('.') - 1;
} else {
e = 1 - m.indexOf('1');
}
if (e >= 0) m = m.replace('.', '');
else m = m.substring(m.indexOf('1'));
if (m.length > 24) m = m.substr(0, 24);
else m = FillString(m, '0', 24, false);
m = m.substring(1);
e = (e + 127).toString(2);
e = FillString(e, '0', 8, true);
let r = parseInt(s + e + m, 2).toString(16);
r = FillString(r, '0', 8, true);
return InsertString(r, ' ', 2).toUpperCase();
};
export const FormatHex = (t: any, n: any, ie: any) => {
const r: string[] = [];
let s = '';
let c = 0;
for (let i = 0; i < t.length; i++) {
if (t.charAt(i) !== ' ') {
s += t.charAt(i);
c += 1;
if (c === n) {
r.push(s);
s = '';
c = 0;
}
}
if (ie === false) {
if (i === t.length - 1 && s !== '') r.push(s);
}
}
return r.join('\n');
};
export const FormatHexBatch = (t: any, n: any, ie: any) => {
const a = t.split('\n');
const r: string[] = [];
for (let i = 0; i < a.length; i++) r[i] = FormatHex(a[i], n, ie);
return r.join('\n');
};
export const SingleToHexBatch = (t: any) => {
const a = t.split('\n');
const r: string[] = [];
for (let i = 0; i < a.length; i++) r[i] = SingleToHex(a[i]);
return r.join('\r\n');
};
const getArray = (values: any) => {
const str = values.replace(/\s+/g, '');
const array: any = [];
for (let i = 0; i < str.length; i += 4) {
const chunk = parseInt(str.substring(i, i + 4), 16);
array.push(chunk);
}
return array;
};
const getFloatPart = (number: string | number) => {
const isLessZero = Number(number) < 0;
number = number.toString();
const floatPartStartIndex = number.indexOf('.');
const value = ~floatPartStartIndex
? `${isLessZero ? '-' : ''}0.${number.substring(floatPartStartIndex + 1)}`
: '0';
return Number(value);
};
const REGISTER_MAX_VALUE = Number(0xffff);
export function useGetModbusCommand() {
const { createMessage } = useMessage();
const getModbusCommand = async (
value: number,
extensionDesc: ExtensionDesc,
deviceAddressCode: string
) => {
const { registerAddress, actionType, zoomFactor } = extensionDesc as Required<ExtensionDesc>;
const params: GenModbusCommandType = {
crc: ModbusCRCEnum.CRC_16_LOWER,
registerNumber: 1,
deviceCode: deviceAddressCode,
registerAddress,
method: actionType,
registerValues: [value],
};
if (actionType === RegisterActionTypeEnum.INT) {
const newValue = Math.trunc(value) * zoomFactor + getFloatPart(value) * zoomFactor;
if (newValue % 1 !== 0) {
createMessage.warning(`属性下发类型必须是整数,缩放因子为${zoomFactor}`);
return;
}
if (value * zoomFactor > REGISTER_MAX_VALUE) {
createMessage.warning(`属性下发值不能超过${REGISTER_MAX_VALUE},缩放因子为${zoomFactor}`);
return;
}
params.registerValues = [newValue];
} else if (actionType === RegisterActionTypeEnum.DOUBLE) {
const regex = /^-?\d+(\.\d{0,2})?$/;
const values = Math.trunc(value) * zoomFactor + getFloatPart(value) * zoomFactor;
if (!regex.test(values.toString())) {
createMessage.warning(`属性下发值精确到两位小数,缩放因子为${zoomFactor}`);
return;
}
const newValue = values === 0 ? [0, 0] : getArray(SingleToHex(values));
params.registerValues = newValue;
params.registerNumber = 2;
params.method = '10';
}
return await genModbusCommand(params);
};
/**
*
* @param extensionDesc 物模型拓展描述符
* @param deviceAddressCode 设备地址码
*/
const validateCanGetCommand = (
extensionDesc?: ExtensionDesc,
deviceAddressCode?: string,
createValidateMessage = true
) => {
const result = { flag: true, message: '' };
if (!extensionDesc) {
result.flag = false;
result.message = '当前物模型扩展描述没有填写';
}
if (!deviceAddressCode) {
result.flag = false;
result.message = '当前设备未绑定设备地址码';
}
if (result.message && createValidateMessage) createMessage.warning(result.message);
return result;
};
return {
getModbusCommand,
validateCanGetCommand,
};
}