agentworkshop 0.9.1 → 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.
@@ -155,7 +155,7 @@ export function evaluateCrossCheck(input: CrossCheckInput): CrossCheckResult {
155
155
  export function buildFalsificationMethod(input: CrossCheckInput, modelHasEffect: boolean | null): string {
156
156
  const delta = input.proposedDelta
157
157
  const parts = [
158
- `若响应窗(≥3 个主导扰动周期)内 ${input.targetNodeId} 的变化量在 95% CI 内不含 0 且方向与模型预测(${modelHasEffect === true ? '有' : modelHasEffect === false ? '无' : '未知'}效应)不一致,则本次因果断言被否证`,
158
+ `对 ${input.controlNodeId} 的干预若在响应窗(≥3 个主导扰动周期)内使 ${input.targetNodeId} 的变化量 95% CI 不含 0 且方向与模型预测(${modelHasEffect === true ? '有' : modelHasEffect === false ? '无' : '未知'}效应)不一致,则本次因果断言被否证`,
159
159
  `比较基准 = 干预前同长窗口;置信区间用 bootstrap(种子固定可复现)`,
160
160
  `Δ控制=${delta} 的效应若小于 MDE,结论只能记为"不可辨识",不得外推`,
161
161
  ]
@@ -135,8 +135,10 @@ export abstract class DcwControllerWrite extends DcwControllerBindings {
135
135
  }
136
136
  }
137
137
  }
138
- // 60s 成功间隔:只认最近一次**成功**下发(失败尝试不占用窗口)
139
- const previousAt = node.lastWriteAt ? Date.parse(node.lastWriteAt) : NaN
138
+ // 60s 成功间隔:只认最近一次**改变设定值**的成功下发。
139
+ // 保写心跳按 holdIntervalMs 同值重发不推进基准,否则心跳与 Agent 写会互相饿死
140
+ // (2026-11-02 端到端演示实测活锁:每 60s 心跳刷新,Agent 永远"还需 60s")。
141
+ const previousAt = node.lastChangeAt ? Date.parse(node.lastChangeAt) : NaN
140
142
  if (Number.isFinite(previousAt)) {
141
143
  const elapsed = Date.now() - previousAt
142
144
  if (elapsed < 60_000) {
@@ -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
- /** 最近一次写**尝试**的时刻(含失败);仅用于诊断/失败冷却,不参与 60s 成功间隔 */
88
- lastAttemptAt: string | null = null
89
- state: DcwNodeState = 'idle'
90
- /** 所属产线('' = 未分配;采集门控/写联锁/场景光晕按产线隔离) */
91
- lineId = ''
92
- /** 节点级工艺语义备注(覆盖模板 semantics) */
93
- semantics?: string
94
- lastError: string | null = null
95
- readonly createdAt: string
96
-
97
- constructor(o: DcwNodeOptions) {
98
- const tpl = findDcwTemplate(dcwKeyFromRef(o.templateRef))
99
- this.id = o.id
100
- this.templateRef = o.templateRef
101
- this.name = o.name ?? (tpl ? `${tpl.name}` : '控制节点')
102
- this.driver = o.driver ?? 'mock'
103
- this.enabled = o.enabled ?? true
104
- this.holdIntervalMs = o.holdIntervalMs ?? null
105
- this.unit = o.unit ?? tpl?.unit ?? ''
106
- this.decimals = o.decimals ?? tpl?.decimals ?? 2
107
- this.min = o.min ?? tpl?.min ?? 0
108
- this.max = o.max ?? tpl?.max ?? 100
109
- const legacyD = o.deviceBindingId ?? null
110
- this.deviceIds = [...new Set([...(o.deviceIds ?? []), ...(legacyD ? [legacyD] : [])])].filter(Boolean)
111
- this.deviceBindingId = this.deviceIds[0] ?? legacyD ?? null
112
- this.driverConfig = o.driverConfig ?? {}
113
- if (o.transform) this.transform = o.transform
114
- if (o.posX !== undefined) this.posX = o.posX
115
- if (o.posZ !== undefined) this.posZ = o.posZ
116
- this.lineId = o.lineId ?? ''
117
- this.semantics = o.semantics
118
- this.readIntervalMs = o.readIntervalMs ?? null
119
- this.writeLockSeconds = Math.max(0, Math.min(3600, Math.round(o.writeLockSeconds ?? 60)))
120
- const fallbackStep = Math.max(10 ** -this.decimals, (this.max - this.min) * 0.02)
121
- this.stepLimit = o.stepLimit === undefined ? Number(fallbackStep.toFixed(this.decimals)) : normalizeStepLimit(o.stepLimit)
122
- this.createdAt = o.createdAt ?? new Date().toISOString()
123
- }
124
-
125
- get templateKey(): string {
126
- return dcwKeyFromRef(this.templateRef)
127
- }
128
-
129
- /** 写入值工艺安全校验(越界即拒绝;返回错误消息或 null) */
130
- validateEng(v: number): string | null {
131
- if (!Number.isFinite(v)) return '设定值需为数字'
132
- if (v < this.min || v > this.max) {
133
- return `设定值 ${v}${this.unit} 越出工艺安全量程 [${this.min}, ${this.max}] ${this.unit}`
134
- }
135
- return null
136
- }
137
-
138
- /** 记录一次写 ACK(成功)/失败。
139
- * P0-1 失败写不占位:value 只承载「设备确认过的值」(成功取回读,缺失回退指令值)。
140
- * 此前失败写把从未被 PLC 接受的指令值占进 value —— 保写心跳按 value 周期重下发,
141
- * 会把幻影指令值反复写向设备(10-07 事故放大器);同时污染步长基准与回退账本 prevValue。 */
142
- applyWriteResult(eng: number, ok: boolean, message: string, at: string): void {
143
- // 尝试时间恒更新;成功时间只在设备证实/受理时更新 ——
144
- // 失败写不再占用 60s 成功间隔(现场瞬断恢复后可立即重试),失败冷却另由
145
- // write.ts 用 lastAttemptAt 做 5s 防轰炸。
146
- this.lastAttemptAt = at
147
- if (ok) {
148
- this.lastWriteAt = at
149
- this.value = Number(eng.toFixed(this.decimals))
150
- this.lastAckAt = at
151
- this.state = 'ok'
152
- this.lastError = null
153
- }
154
- else {
155
- this.state = 'error'
156
- this.lastError = message
157
- }
158
- }
159
-
160
- /** 记录一次读数(成功回填物理值;失败仅记原因,旧读数保留供展示) */
161
- applyReadResult(eng: number | null, raw: number | null, ok: boolean, message: string, at: string): void {
162
- this.lastReadAt = at
163
- if (ok && eng != null) {
164
- this.readValue = Number(eng.toFixed(this.decimals))
165
- this.lastReadError = null
166
- }
167
- else {
168
- this.lastReadError = message
169
- }
170
- }
171
-
172
- toRow(): Record<string, unknown> {
173
- return {
174
- id: this.id,
175
- templateRef: this.templateRef,
176
- name: this.name,
177
- driver: this.driver,
178
- enabled: this.enabled,
179
- holdIntervalMs: this.holdIntervalMs,
180
- readIntervalMs: this.readIntervalMs,
181
- writeLockSeconds: this.writeLockSeconds,
182
- stepLimit: this.stepLimit,
183
- unit: this.unit,
184
- decimals: this.decimals,
185
- min: this.min,
186
- max: this.max,
187
- deviceBindingId: this.deviceBindingId,
188
- deviceIds: this.deviceIds,
189
- driverConfig: this.driverConfig,
190
- transform: this.transform,
191
- posX: this.posX,
192
- posZ: this.posZ,
193
- lineId: this.lineId,
194
- semantics: this.semantics,
195
- value: this.value,
196
- readValue: this.readValue,
197
- lastReadAt: this.lastReadAt,
198
- lastReadError: this.lastReadError,
199
- lastAckAt: this.lastAckAt,
200
- lastWriteAt: this.lastWriteAt,
201
- lastAttemptAt: this.lastAttemptAt,
202
- state: this.state,
203
- lastError: this.lastError,
204
- createdAt: this.createdAt,
205
- }
206
- }
207
-
208
- static fromRow(row: Record<string, unknown>): DcwNode {
209
- const node = new DcwNode({
210
- id: String(row.id),
211
- templateRef: String(row.templateRef ?? ''),
212
- name: row.name != null ? String(row.name) : undefined,
213
- driver: (row.driver as DcwDriverKind) ?? undefined,
214
- enabled: row.enabled === undefined ? undefined : Boolean(row.enabled),
215
- holdIntervalMs: row.holdIntervalMs == null ? null : Number(row.holdIntervalMs),
216
- unit: row.unit != null ? String(row.unit) : undefined,
217
- decimals: row.decimals != null ? Number(row.decimals) : undefined,
218
- min: row.min != null ? Number(row.min) : undefined,
219
- max: row.max != null ? Number(row.max) : undefined,
220
- deviceBindingId: row.deviceBindingId === undefined ? undefined : (row.deviceBindingId == null ? null : String(row.deviceBindingId)),
221
- deviceIds: Array.isArray(row.deviceIds) ? row.deviceIds.map(String).filter(Boolean) : undefined,
222
- driverConfig: (row.driverConfig as Record<string, string | number | boolean>) ?? {},
223
- transform: (row.transform as DataTransform | undefined) ?? undefined,
224
- posX: row.posX == null ? undefined : Number(row.posX),
225
- lineId: row.lineId == null ? '' : String(row.lineId),
226
- semantics: row.semantics == null ? undefined : String(row.semantics),
227
- readIntervalMs: row.readIntervalMs == null ? null : Number(row.readIntervalMs),
228
- writeLockSeconds: row.writeLockSeconds == null ? undefined : Number(row.writeLockSeconds),
229
- stepLimit: row.stepLimit === undefined ? undefined : (row.stepLimit == null ? null : Number(row.stepLimit)),
230
- posZ: row.posZ == null ? undefined : Number(row.posZ),
231
- createdAt: row.createdAt != null ? String(row.createdAt) : undefined,
232
- })
233
- if (row.value != null && !Number.isNaN(Number(row.value))) node.value = Number(row.value)
234
- if (row.readValue != null && !Number.isNaN(Number(row.readValue))) node.readValue = Number(row.readValue)
235
- if (row.lastReadAt != null) node.lastReadAt = String(row.lastReadAt)
236
- if (row.lastReadError != null) node.lastReadError = String(row.lastReadError)
237
- if (row.lastAckAt != null) node.lastAckAt = String(row.lastAckAt)
238
- if (row.lastWriteAt != null) node.lastWriteAt = String(row.lastWriteAt)
239
- if (row.lastAttemptAt != null) node.lastAttemptAt = String(row.lastAttemptAt)
240
- // 旧快照兼容:历史 lastWriteAt 是「尝试时间」语义,补一份到 lastAttemptAt
241
- else if (row.lastWriteAt != null) node.lastAttemptAt = String(row.lastWriteAt)
242
- if (row.state != null) {
243
- const restored = String(row.state)
244
- // 持久化恢复消毒:'writing' 是进程内瞬态(写事务在飞)。若被快照带出后进程
245
- // 硬杀重启,原样恢复会让该节点所有写永久 409(startAll 只复位 offline)。
246
- // 归一为 error:写路径放行,同时显式提示"上次写入结果未知,需核销"。
247
- if (restored === 'writing') {
248
- node.state = 'error'
249
- node.lastError = node.lastError || '进程中断于写入,状态已复位为 error(上次写入结果未知,请回读核销)'
250
- }
251
- else {
252
- node.state = restored as DcwNodeState
253
- }
254
- }
255
- if (row.lastError != null) node.lastError = String(row.lastError)
256
- return node
257
- }
258
-
259
- toView(): DcwNodeView {
260
- return {
261
- id: this.id,
262
- templateRef: this.templateRef,
263
- name: this.name,
264
- driver: this.driver,
265
- enabled: this.enabled,
266
- holdIntervalMs: this.holdIntervalMs,
267
- unit: this.unit,
268
- decimals: this.decimals,
269
- min: this.min,
270
- max: this.max,
271
- deviceBindingId: this.deviceBindingId,
272
- deviceIds: this.deviceIds,
273
- // 凭据脱敏(同 daq 侧 toView;toRow 持久化不掩码)
274
- driverConfig: maskDriverConfigSecrets(this.driverConfig),
275
- transform: this.transform,
276
- posX: this.posX,
277
- posZ: this.posZ,
278
- lineId: this.lineId,
279
- semantics: this.semantics,
280
- value: this.value,
281
- readValue: this.readValue,
282
- lastReadAt: this.lastReadAt,
283
- lastReadError: this.lastReadError,
284
- readIntervalMs: this.readIntervalMs,
285
- writeLockSeconds: this.writeLockSeconds,
286
- stepLimit: this.stepLimit,
287
- lastAckAt: this.lastAckAt,
288
- lastWriteAt: this.lastWriteAt,
289
- lastAttemptAt: this.lastAttemptAt,
290
- state: this.state,
291
- lastError: this.lastError,
292
- createdAt: this.createdAt,
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
+ }