agentworkshop 0.9.0 → 0.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.output/nitro.json +1 -1
- package/.output/public/_nuxt/builds/latest.json +1 -1
- package/.output/public/_nuxt/builds/meta/582a3c6f-cd1b-4e1c-b4ee-249fd3efd658.json +1 -0
- package/.output/server/chunks/_/nitro.mjs +1262 -991
- package/.output/server/chunks/_/nitro.mjs.map +1 -1
- package/.output/server/chunks/routes/api/workshop/process-model/experiments.mjs +110 -17
- package/.output/server/chunks/routes/api/workshop/process-model/experiments.mjs.map +1 -1
- package/.output/server/package.json +1 -1
- package/README-zh.md +3 -3
- package/README.md +3 -3
- package/config.yml +1 -1
- package/docs/cli.en.md +2 -2
- package/docs/cli.md +2 -2
- package/docs/plugins.en.md +1 -1
- package/docs/plugins.md +1 -1
- package/docs/sdk.en.md +1 -1
- package/docs/sdk.md +1 -1
- package/package.json +2 -1
- package/scripts/acceptance-scientific-fullchain.ts +391 -0
- package/scripts/acceptance-scientific-loop.ts +16 -0
- package/scripts/demo/build-provision-config.mjs +48 -0
- package/scripts/demo/create-demo-templates.mjs +84 -0
- package/scripts/demo/e2e-optimization-demo.mjs +259 -0
- package/scripts/e2e-model-loop-live.mjs +827 -821
- package/server/services/workshop/agents/industrial/optimization-tools.ts +16 -0
- package/server/services/workshop/aml/intervention-ledger.ts +12 -4
- package/server/services/workshop/aml/twin/cross-check-service.ts +114 -0
- package/server/services/workshop/aml/twin/cross-check.ts +1 -1
- package/server/services/workshop/dcw/dcw-controller/write.ts +4 -2
- package/server/services/workshop/dcw/dcw-node.ts +307 -295
- package/server/services/workshop/process-model/experiment-runner.ts +81 -5
- package/server/services/workshop/process-model/experiment.ts +6 -0
- package/server/services/workshop/process-model/plan-safety.ts +74 -0
- package/shared/dcw-protocol/line.ts +3 -1
- package/.output/public/_nuxt/builds/meta/f507c33b-4ecc-4e32-af88-2e3736b443e9.json +0 -1
|
@@ -1,295 +1,307 @@
|
|
|
1
|
-
import { maskDriverConfigSecrets } from '../../../../shared/daq-protocol'
|
|
2
|
-
/**
|
|
3
|
-
* DcwNode —— 数据写控制节点领域对象(与 DaqNode 对称)。
|
|
4
|
-
*
|
|
5
|
-
* 一个实例 = 一条工艺参数写通道:模板域(物理含义/单位/工艺安全量程)+ 驱动配置
|
|
6
|
-
* (连接 + 工程量→原始值线性换算,系统封装)+ 绑定关系 + 写状态(value/lastAckAt/
|
|
7
|
-
* writeState)。用户只操作工程量;PLC 原始值的换算/回读校验由运行时完成。
|
|
8
|
-
* 序列化经 toRow/fromRow 对接持久化仓库(dcws.json)。
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import { dcwKeyFromRef, type DcwDriverKind, type DcwNodeState, type DcwNodeView, type DataTransform } from '../../../../shared/dcw-protocol'
|
|
12
|
-
import { findDcwTemplate } from './dcw-templates'
|
|
13
|
-
|
|
14
|
-
interface DcwNodeOptions {
|
|
15
|
-
id: string
|
|
16
|
-
templateRef: string
|
|
17
|
-
name?: string
|
|
18
|
-
driver?: DcwDriverKind
|
|
19
|
-
enabled?: boolean
|
|
20
|
-
/** 保写周期 ms(心跳重下发;null = 仅手动下发) */
|
|
21
|
-
holdIntervalMs?: number | null
|
|
22
|
-
unit?: string
|
|
23
|
-
decimals?: number
|
|
24
|
-
min?: number
|
|
25
|
-
max?: number
|
|
26
|
-
deviceBindingId?: string | null
|
|
27
|
-
/** 多对多绑定(权威):deviceIds 去重列表 */
|
|
28
|
-
deviceIds?: string[]
|
|
29
|
-
driverConfig?: Record<string, string | number | boolean>
|
|
30
|
-
/** 数据语义标定钩子(encode:物理值 → PLC 设定值;回读经 decoder 校验) */
|
|
31
|
-
transform?: DataTransform
|
|
32
|
-
posX?: number
|
|
33
|
-
posZ?: number
|
|
34
|
-
/** 所属产线('' = 未分配) */
|
|
35
|
-
lineId?: string
|
|
36
|
-
/** 节点级工艺语义备注(覆盖模板 semantics;注入 Agent 上下文) */
|
|
37
|
-
semantics?: string
|
|
38
|
-
/** 周期读间隔 ms(null = 走网关默认;0 = 关闭周期读,仅手动读取) */
|
|
39
|
-
readIntervalMs?: number | null
|
|
40
|
-
/** 写入保持窗秒数(写成功后节点锁定该时长,防参数震荡;0 = 不锁;默认 60) */
|
|
41
|
-
writeLockSeconds?: number
|
|
42
|
-
/** 单次控制步长上限(工程量;undefined 采用量程 2% 默认值;null 显式关闭) */
|
|
43
|
-
stepLimit?: number | null
|
|
44
|
-
createdAt?: string
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function normalizeStepLimit(value: number | null): number | null {
|
|
48
|
-
if (value == null) return null
|
|
49
|
-
const n = Number(value)
|
|
50
|
-
if (!Number.isFinite(n) || n <= 0) return null
|
|
51
|
-
return n
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export class DcwNode {
|
|
55
|
-
readonly id: string
|
|
56
|
-
templateRef: string
|
|
57
|
-
name: string
|
|
58
|
-
driver: DcwDriverKind
|
|
59
|
-
enabled: boolean
|
|
60
|
-
holdIntervalMs: number | null
|
|
61
|
-
unit: string
|
|
62
|
-
decimals: number
|
|
63
|
-
min: number
|
|
64
|
-
max: number
|
|
65
|
-
deviceBindingId: string | null
|
|
66
|
-
/** 多对多设备绑定(权威字段;去重有序) */
|
|
67
|
-
deviceIds: string[]
|
|
68
|
-
driverConfig: Record<string, string | number | boolean>
|
|
69
|
-
transform?: DataTransform
|
|
70
|
-
posX?: number
|
|
71
|
-
posZ?: number
|
|
72
|
-
/** 当前设定值(工程量;null = 从未下发) */
|
|
73
|
-
value: number | null = null
|
|
74
|
-
/** PLC 当前读数(工程量物理值;null = 从未读到或驱动不支持读) */
|
|
75
|
-
readValue: number | null = null
|
|
76
|
-
lastReadAt: string | null = null
|
|
77
|
-
lastReadError: string | null = null
|
|
78
|
-
/** 周期读间隔 ms(null = 走网关默认;0 = 仅手动读取) */
|
|
79
|
-
readIntervalMs: number | null = null
|
|
80
|
-
/** 写入保持窗秒数(写成功后锁定;防参数频繁修改震荡;0 = 不锁) */
|
|
81
|
-
writeLockSeconds = 30
|
|
82
|
-
/** 单次控制步长上限(工程量);探索阶段必须有有限值 */
|
|
83
|
-
stepLimit: number | null
|
|
84
|
-
lastAckAt: string | null = null
|
|
85
|
-
/** 最近一次**成功**下发(设备已证实/受理)的时刻;失败写不更新 */
|
|
86
|
-
lastWriteAt: string | null = null
|
|
87
|
-
/**
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
this.
|
|
106
|
-
this.
|
|
107
|
-
this.
|
|
108
|
-
this.
|
|
109
|
-
|
|
110
|
-
this.
|
|
111
|
-
this.
|
|
112
|
-
this.
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
this.
|
|
117
|
-
this.
|
|
118
|
-
this.
|
|
119
|
-
this.
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
this.
|
|
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
|
-
this.
|
|
156
|
-
this.
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
this.
|
|
165
|
-
this.
|
|
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
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
1
|
+
import { maskDriverConfigSecrets } from '../../../../shared/daq-protocol'
|
|
2
|
+
/**
|
|
3
|
+
* DcwNode —— 数据写控制节点领域对象(与 DaqNode 对称)。
|
|
4
|
+
*
|
|
5
|
+
* 一个实例 = 一条工艺参数写通道:模板域(物理含义/单位/工艺安全量程)+ 驱动配置
|
|
6
|
+
* (连接 + 工程量→原始值线性换算,系统封装)+ 绑定关系 + 写状态(value/lastAckAt/
|
|
7
|
+
* writeState)。用户只操作工程量;PLC 原始值的换算/回读校验由运行时完成。
|
|
8
|
+
* 序列化经 toRow/fromRow 对接持久化仓库(dcws.json)。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { dcwKeyFromRef, type DcwDriverKind, type DcwNodeState, type DcwNodeView, type DataTransform } from '../../../../shared/dcw-protocol'
|
|
12
|
+
import { findDcwTemplate } from './dcw-templates'
|
|
13
|
+
|
|
14
|
+
interface DcwNodeOptions {
|
|
15
|
+
id: string
|
|
16
|
+
templateRef: string
|
|
17
|
+
name?: string
|
|
18
|
+
driver?: DcwDriverKind
|
|
19
|
+
enabled?: boolean
|
|
20
|
+
/** 保写周期 ms(心跳重下发;null = 仅手动下发) */
|
|
21
|
+
holdIntervalMs?: number | null
|
|
22
|
+
unit?: string
|
|
23
|
+
decimals?: number
|
|
24
|
+
min?: number
|
|
25
|
+
max?: number
|
|
26
|
+
deviceBindingId?: string | null
|
|
27
|
+
/** 多对多绑定(权威):deviceIds 去重列表 */
|
|
28
|
+
deviceIds?: string[]
|
|
29
|
+
driverConfig?: Record<string, string | number | boolean>
|
|
30
|
+
/** 数据语义标定钩子(encode:物理值 → PLC 设定值;回读经 decoder 校验) */
|
|
31
|
+
transform?: DataTransform
|
|
32
|
+
posX?: number
|
|
33
|
+
posZ?: number
|
|
34
|
+
/** 所属产线('' = 未分配) */
|
|
35
|
+
lineId?: string
|
|
36
|
+
/** 节点级工艺语义备注(覆盖模板 semantics;注入 Agent 上下文) */
|
|
37
|
+
semantics?: string
|
|
38
|
+
/** 周期读间隔 ms(null = 走网关默认;0 = 关闭周期读,仅手动读取) */
|
|
39
|
+
readIntervalMs?: number | null
|
|
40
|
+
/** 写入保持窗秒数(写成功后节点锁定该时长,防参数震荡;0 = 不锁;默认 60) */
|
|
41
|
+
writeLockSeconds?: number
|
|
42
|
+
/** 单次控制步长上限(工程量;undefined 采用量程 2% 默认值;null 显式关闭) */
|
|
43
|
+
stepLimit?: number | null
|
|
44
|
+
createdAt?: string
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function normalizeStepLimit(value: number | null): number | null {
|
|
48
|
+
if (value == null) return null
|
|
49
|
+
const n = Number(value)
|
|
50
|
+
if (!Number.isFinite(n) || n <= 0) return null
|
|
51
|
+
return n
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export class DcwNode {
|
|
55
|
+
readonly id: string
|
|
56
|
+
templateRef: string
|
|
57
|
+
name: string
|
|
58
|
+
driver: DcwDriverKind
|
|
59
|
+
enabled: boolean
|
|
60
|
+
holdIntervalMs: number | null
|
|
61
|
+
unit: string
|
|
62
|
+
decimals: number
|
|
63
|
+
min: number
|
|
64
|
+
max: number
|
|
65
|
+
deviceBindingId: string | null
|
|
66
|
+
/** 多对多设备绑定(权威字段;去重有序) */
|
|
67
|
+
deviceIds: string[]
|
|
68
|
+
driverConfig: Record<string, string | number | boolean>
|
|
69
|
+
transform?: DataTransform
|
|
70
|
+
posX?: number
|
|
71
|
+
posZ?: number
|
|
72
|
+
/** 当前设定值(工程量;null = 从未下发) */
|
|
73
|
+
value: number | null = null
|
|
74
|
+
/** PLC 当前读数(工程量物理值;null = 从未读到或驱动不支持读) */
|
|
75
|
+
readValue: number | null = null
|
|
76
|
+
lastReadAt: string | null = null
|
|
77
|
+
lastReadError: string | null = null
|
|
78
|
+
/** 周期读间隔 ms(null = 走网关默认;0 = 仅手动读取) */
|
|
79
|
+
readIntervalMs: number | null = null
|
|
80
|
+
/** 写入保持窗秒数(写成功后锁定;防参数频繁修改震荡;0 = 不锁) */
|
|
81
|
+
writeLockSeconds = 30
|
|
82
|
+
/** 单次控制步长上限(工程量);探索阶段必须有有限值 */
|
|
83
|
+
stepLimit: number | null
|
|
84
|
+
lastAckAt: string | null = null
|
|
85
|
+
/** 最近一次**成功**下发(设备已证实/受理)的时刻;失败写不更新 */
|
|
86
|
+
lastWriteAt: string | null = null
|
|
87
|
+
/**
|
|
88
|
+
* 最近一次**值发生变化**的成功下发时刻。
|
|
89
|
+
* 60s 在线写间隔以此为准:保写心跳(holdIntervalMs)按周期重发同一设定值,
|
|
90
|
+
* 若它刷新间隔基准,会让 Agent/试验计划的写入永远差 60s(实测活锁)。
|
|
91
|
+
*/
|
|
92
|
+
lastChangeAt: string | null = null
|
|
93
|
+
/** 最近一次写**尝试**的时刻(含失败);仅用于诊断/失败冷却,不参与 60s 成功间隔 */
|
|
94
|
+
lastAttemptAt: string | null = null
|
|
95
|
+
state: DcwNodeState = 'idle'
|
|
96
|
+
/** 所属产线('' = 未分配;采集门控/写联锁/场景光晕按产线隔离) */
|
|
97
|
+
lineId = ''
|
|
98
|
+
/** 节点级工艺语义备注(覆盖模板 semantics) */
|
|
99
|
+
semantics?: string
|
|
100
|
+
lastError: string | null = null
|
|
101
|
+
readonly createdAt: string
|
|
102
|
+
|
|
103
|
+
constructor(o: DcwNodeOptions) {
|
|
104
|
+
const tpl = findDcwTemplate(dcwKeyFromRef(o.templateRef))
|
|
105
|
+
this.id = o.id
|
|
106
|
+
this.templateRef = o.templateRef
|
|
107
|
+
this.name = o.name ?? (tpl ? `${tpl.name}` : '控制节点')
|
|
108
|
+
this.driver = o.driver ?? 'mock'
|
|
109
|
+
this.enabled = o.enabled ?? true
|
|
110
|
+
this.holdIntervalMs = o.holdIntervalMs ?? null
|
|
111
|
+
this.unit = o.unit ?? tpl?.unit ?? ''
|
|
112
|
+
this.decimals = o.decimals ?? tpl?.decimals ?? 2
|
|
113
|
+
this.min = o.min ?? tpl?.min ?? 0
|
|
114
|
+
this.max = o.max ?? tpl?.max ?? 100
|
|
115
|
+
const legacyD = o.deviceBindingId ?? null
|
|
116
|
+
this.deviceIds = [...new Set([...(o.deviceIds ?? []), ...(legacyD ? [legacyD] : [])])].filter(Boolean)
|
|
117
|
+
this.deviceBindingId = this.deviceIds[0] ?? legacyD ?? null
|
|
118
|
+
this.driverConfig = o.driverConfig ?? {}
|
|
119
|
+
if (o.transform) this.transform = o.transform
|
|
120
|
+
if (o.posX !== undefined) this.posX = o.posX
|
|
121
|
+
if (o.posZ !== undefined) this.posZ = o.posZ
|
|
122
|
+
this.lineId = o.lineId ?? ''
|
|
123
|
+
this.semantics = o.semantics
|
|
124
|
+
this.readIntervalMs = o.readIntervalMs ?? null
|
|
125
|
+
this.writeLockSeconds = Math.max(0, Math.min(3600, Math.round(o.writeLockSeconds ?? 60)))
|
|
126
|
+
const fallbackStep = Math.max(10 ** -this.decimals, (this.max - this.min) * 0.02)
|
|
127
|
+
this.stepLimit = o.stepLimit === undefined ? Number(fallbackStep.toFixed(this.decimals)) : normalizeStepLimit(o.stepLimit)
|
|
128
|
+
this.createdAt = o.createdAt ?? new Date().toISOString()
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
get templateKey(): string {
|
|
132
|
+
return dcwKeyFromRef(this.templateRef)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** 写入值工艺安全校验(越界即拒绝;返回错误消息或 null) */
|
|
136
|
+
validateEng(v: number): string | null {
|
|
137
|
+
if (!Number.isFinite(v)) return '设定值需为数字'
|
|
138
|
+
if (v < this.min || v > this.max) {
|
|
139
|
+
return `设定值 ${v}${this.unit} 越出工艺安全量程 [${this.min}, ${this.max}] ${this.unit}`
|
|
140
|
+
}
|
|
141
|
+
return null
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** 记录一次写 ACK(成功)/失败。
|
|
145
|
+
* P0-1 失败写不占位:value 只承载「设备确认过的值」(成功取回读,缺失回退指令值)。
|
|
146
|
+
* 此前失败写把从未被 PLC 接受的指令值占进 value —— 保写心跳按 value 周期重下发,
|
|
147
|
+
* 会把幻影指令值反复写向设备(10-07 事故放大器);同时污染步长基准与回退账本 prevValue。 */
|
|
148
|
+
applyWriteResult(eng: number, ok: boolean, message: string, at: string): void {
|
|
149
|
+
// 尝试时间恒更新;成功时间只在设备证实/受理时更新 ——
|
|
150
|
+
// 失败写不再占用 60s 成功间隔(现场瞬断恢复后可立即重试),失败冷却另由
|
|
151
|
+
// write.ts 用 lastAttemptAt 做 5s 防轰炸。
|
|
152
|
+
this.lastAttemptAt = at
|
|
153
|
+
if (ok) {
|
|
154
|
+
const prev = this.value
|
|
155
|
+
this.lastWriteAt = at
|
|
156
|
+
this.value = Number(eng.toFixed(this.decimals))
|
|
157
|
+
// 只有真正改变设定值才推进 60s 间隔基准(同值保活写不算)
|
|
158
|
+
if (prev == null || Math.abs(Number(prev) - this.value) > 1e-9) this.lastChangeAt = at
|
|
159
|
+
this.lastAckAt = at
|
|
160
|
+
this.state = 'ok'
|
|
161
|
+
this.lastError = null
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
this.state = 'error'
|
|
165
|
+
this.lastError = message
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** 记录一次读数(成功回填物理值;失败仅记原因,旧读数保留供展示) */
|
|
170
|
+
applyReadResult(eng: number | null, raw: number | null, ok: boolean, message: string, at: string): void {
|
|
171
|
+
this.lastReadAt = at
|
|
172
|
+
if (ok && eng != null) {
|
|
173
|
+
this.readValue = Number(eng.toFixed(this.decimals))
|
|
174
|
+
this.lastReadError = null
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
this.lastReadError = message
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
toRow(): Record<string, unknown> {
|
|
182
|
+
return {
|
|
183
|
+
id: this.id,
|
|
184
|
+
templateRef: this.templateRef,
|
|
185
|
+
name: this.name,
|
|
186
|
+
driver: this.driver,
|
|
187
|
+
enabled: this.enabled,
|
|
188
|
+
holdIntervalMs: this.holdIntervalMs,
|
|
189
|
+
readIntervalMs: this.readIntervalMs,
|
|
190
|
+
writeLockSeconds: this.writeLockSeconds,
|
|
191
|
+
stepLimit: this.stepLimit,
|
|
192
|
+
unit: this.unit,
|
|
193
|
+
decimals: this.decimals,
|
|
194
|
+
min: this.min,
|
|
195
|
+
max: this.max,
|
|
196
|
+
deviceBindingId: this.deviceBindingId,
|
|
197
|
+
deviceIds: this.deviceIds,
|
|
198
|
+
driverConfig: this.driverConfig,
|
|
199
|
+
transform: this.transform,
|
|
200
|
+
posX: this.posX,
|
|
201
|
+
posZ: this.posZ,
|
|
202
|
+
lineId: this.lineId,
|
|
203
|
+
semantics: this.semantics,
|
|
204
|
+
value: this.value,
|
|
205
|
+
readValue: this.readValue,
|
|
206
|
+
lastReadAt: this.lastReadAt,
|
|
207
|
+
lastReadError: this.lastReadError,
|
|
208
|
+
lastAckAt: this.lastAckAt,
|
|
209
|
+
lastWriteAt: this.lastWriteAt,
|
|
210
|
+
lastChangeAt: this.lastChangeAt,
|
|
211
|
+
lastAttemptAt: this.lastAttemptAt,
|
|
212
|
+
state: this.state,
|
|
213
|
+
lastError: this.lastError,
|
|
214
|
+
createdAt: this.createdAt,
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
static fromRow(row: Record<string, unknown>): DcwNode {
|
|
219
|
+
const node = new DcwNode({
|
|
220
|
+
id: String(row.id),
|
|
221
|
+
templateRef: String(row.templateRef ?? ''),
|
|
222
|
+
name: row.name != null ? String(row.name) : undefined,
|
|
223
|
+
driver: (row.driver as DcwDriverKind) ?? undefined,
|
|
224
|
+
enabled: row.enabled === undefined ? undefined : Boolean(row.enabled),
|
|
225
|
+
holdIntervalMs: row.holdIntervalMs == null ? null : Number(row.holdIntervalMs),
|
|
226
|
+
unit: row.unit != null ? String(row.unit) : undefined,
|
|
227
|
+
decimals: row.decimals != null ? Number(row.decimals) : undefined,
|
|
228
|
+
min: row.min != null ? Number(row.min) : undefined,
|
|
229
|
+
max: row.max != null ? Number(row.max) : undefined,
|
|
230
|
+
deviceBindingId: row.deviceBindingId === undefined ? undefined : (row.deviceBindingId == null ? null : String(row.deviceBindingId)),
|
|
231
|
+
deviceIds: Array.isArray(row.deviceIds) ? row.deviceIds.map(String).filter(Boolean) : undefined,
|
|
232
|
+
driverConfig: (row.driverConfig as Record<string, string | number | boolean>) ?? {},
|
|
233
|
+
transform: (row.transform as DataTransform | undefined) ?? undefined,
|
|
234
|
+
posX: row.posX == null ? undefined : Number(row.posX),
|
|
235
|
+
lineId: row.lineId == null ? '' : String(row.lineId),
|
|
236
|
+
semantics: row.semantics == null ? undefined : String(row.semantics),
|
|
237
|
+
readIntervalMs: row.readIntervalMs == null ? null : Number(row.readIntervalMs),
|
|
238
|
+
writeLockSeconds: row.writeLockSeconds == null ? undefined : Number(row.writeLockSeconds),
|
|
239
|
+
stepLimit: row.stepLimit === undefined ? undefined : (row.stepLimit == null ? null : Number(row.stepLimit)),
|
|
240
|
+
posZ: row.posZ == null ? undefined : Number(row.posZ),
|
|
241
|
+
createdAt: row.createdAt != null ? String(row.createdAt) : undefined,
|
|
242
|
+
})
|
|
243
|
+
if (row.value != null && !Number.isNaN(Number(row.value))) node.value = Number(row.value)
|
|
244
|
+
if (row.readValue != null && !Number.isNaN(Number(row.readValue))) node.readValue = Number(row.readValue)
|
|
245
|
+
if (row.lastReadAt != null) node.lastReadAt = String(row.lastReadAt)
|
|
246
|
+
if (row.lastReadError != null) node.lastReadError = String(row.lastReadError)
|
|
247
|
+
if (row.lastAckAt != null) node.lastAckAt = String(row.lastAckAt)
|
|
248
|
+
if (row.lastWriteAt != null) node.lastWriteAt = String(row.lastWriteAt)
|
|
249
|
+
if (row.lastChangeAt != null) node.lastChangeAt = String(row.lastChangeAt)
|
|
250
|
+
if (row.lastAttemptAt != null) node.lastAttemptAt = String(row.lastAttemptAt)
|
|
251
|
+
// 旧快照兼容:历史 lastWriteAt 是「尝试时间」语义,补一份到 lastAttemptAt
|
|
252
|
+
else if (row.lastWriteAt != null) node.lastAttemptAt = String(row.lastWriteAt)
|
|
253
|
+
if (row.state != null) {
|
|
254
|
+
const restored = String(row.state)
|
|
255
|
+
// 持久化恢复消毒:'writing' 是进程内瞬态(写事务在飞)。若被快照带出后进程
|
|
256
|
+
// 硬杀重启,原样恢复会让该节点所有写永久 409(startAll 只复位 offline)。
|
|
257
|
+
// 归一为 error:写路径放行,同时显式提示"上次写入结果未知,需核销"。
|
|
258
|
+
if (restored === 'writing') {
|
|
259
|
+
node.state = 'error'
|
|
260
|
+
node.lastError = node.lastError || '进程中断于写入,状态已复位为 error(上次写入结果未知,请回读核销)'
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
node.state = restored as DcwNodeState
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
if (row.lastError != null) node.lastError = String(row.lastError)
|
|
267
|
+
return node
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
toView(): DcwNodeView {
|
|
271
|
+
return {
|
|
272
|
+
id: this.id,
|
|
273
|
+
templateRef: this.templateRef,
|
|
274
|
+
name: this.name,
|
|
275
|
+
driver: this.driver,
|
|
276
|
+
enabled: this.enabled,
|
|
277
|
+
holdIntervalMs: this.holdIntervalMs,
|
|
278
|
+
unit: this.unit,
|
|
279
|
+
decimals: this.decimals,
|
|
280
|
+
min: this.min,
|
|
281
|
+
max: this.max,
|
|
282
|
+
deviceBindingId: this.deviceBindingId,
|
|
283
|
+
deviceIds: this.deviceIds,
|
|
284
|
+
// 凭据脱敏(同 daq 侧 toView;toRow 持久化不掩码)
|
|
285
|
+
driverConfig: maskDriverConfigSecrets(this.driverConfig),
|
|
286
|
+
transform: this.transform,
|
|
287
|
+
posX: this.posX,
|
|
288
|
+
posZ: this.posZ,
|
|
289
|
+
lineId: this.lineId,
|
|
290
|
+
semantics: this.semantics,
|
|
291
|
+
value: this.value,
|
|
292
|
+
readValue: this.readValue,
|
|
293
|
+
lastReadAt: this.lastReadAt,
|
|
294
|
+
lastReadError: this.lastReadError,
|
|
295
|
+
readIntervalMs: this.readIntervalMs,
|
|
296
|
+
writeLockSeconds: this.writeLockSeconds,
|
|
297
|
+
stepLimit: this.stepLimit,
|
|
298
|
+
lastAckAt: this.lastAckAt,
|
|
299
|
+
lastWriteAt: this.lastWriteAt,
|
|
300
|
+
lastChangeAt: this.lastChangeAt,
|
|
301
|
+
lastAttemptAt: this.lastAttemptAt,
|
|
302
|
+
state: this.state,
|
|
303
|
+
lastError: this.lastError,
|
|
304
|
+
createdAt: this.createdAt,
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|