init: PLC data collection system
This commit is contained in:
commit
ae90bc9368
38 files changed
+5234
No files matched your search
@@ -0,0 +1,60 @@
|
||||
import { getDb } from '../database/connection.js'
|
||||
import { logger } from '../logger.js'
|
||||
import type { PLCConfig, StationData } from '../plc/client.js'
|
||||
|
||||
const TAG = '采集'
|
||||
|
||||
function todayDateStr(): string {
|
||||
const d = new Date()
|
||||
const y = d.getFullYear()
|
||||
const m = String(d.getMonth() + 1).padStart(2, '0')
|
||||
const day = String(d.getDate()).padStart(2, '0')
|
||||
return `${y}_${m}_${day}`
|
||||
}
|
||||
|
||||
export function handleDataCollection(
|
||||
plc: PLCConfig,
|
||||
stationIndex: number,
|
||||
data: StationData,
|
||||
): void {
|
||||
const db = getDb()
|
||||
const dateStr = todayDateStr()
|
||||
|
||||
const station = db.prepare(`
|
||||
SELECT s.Id, s.PointId, mp.PointName
|
||||
FROM Stations s
|
||||
JOIN MeasurementPoints mp ON mp.Id = s.PointId
|
||||
WHERE s.PLCId = ? AND s.StationIndex = ?
|
||||
`).get(plc.id, stationIndex) as { Id: number; PointId: number; PointName: string } | undefined
|
||||
|
||||
if (!station) {
|
||||
logger.warn(TAG, `PLC#${plc.id} 工序索引 ${stationIndex} 未找到对应配置`)
|
||||
return
|
||||
}
|
||||
|
||||
const productIdStr = String(data.productID)
|
||||
const table = `MPData_P${station.PointId}_${dateStr}`
|
||||
|
||||
const existing = db.prepare(`SELECT InspectionCount FROM ${table} WHERE ProductID = ?`).get(productIdStr) as { InspectionCount: number } | undefined
|
||||
|
||||
if (!existing) {
|
||||
db.prepare(`
|
||||
INSERT INTO ${table} (ProductID, RecordTime, IsQualified, InspectionCount,
|
||||
UpperLimit, LowerLimit, MeasuredValue)
|
||||
VALUES (?, datetime('now'), ?, 1, ?, ?, ?)
|
||||
`).run(productIdStr, data.isQualified ? 1 : 0, data.upperLimit, data.lowerLimit, data.measuredValue)
|
||||
logger.info(TAG, `${station.PointName} 产品 ${productIdStr}: 首次检测 ${data.measuredValue}`)
|
||||
} else {
|
||||
db.prepare(`
|
||||
UPDATE ${table}
|
||||
SET RecordTime = datetime('now'),
|
||||
IsQualified = ?,
|
||||
InspectionCount = InspectionCount + 1,
|
||||
UpperLimit = ?,
|
||||
LowerLimit = ?,
|
||||
MeasuredValue = ?
|
||||
WHERE ProductID = ?
|
||||
`).run(data.isQualified ? 1 : 0, data.upperLimit, data.lowerLimit, data.measuredValue, productIdStr)
|
||||
logger.info(TAG, `${station.PointName} 产品 ${productIdStr}: 第${existing.InspectionCount + 1}次检测(返修) ${data.measuredValue}`)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import { getDb } from '../database/connection.js'
|
||||
import { logger } from '../logger.js'
|
||||
|
||||
const TAG = '互锁'
|
||||
|
||||
function todayDateStr(): string {
|
||||
const d = new Date()
|
||||
const y = d.getFullYear()
|
||||
const m = String(d.getMonth() + 1).padStart(2, '0')
|
||||
const day = String(d.getDate()).padStart(2, '0')
|
||||
return `${y}_${m}_${day}`
|
||||
}
|
||||
|
||||
export function handleInterlock(
|
||||
stationName: string,
|
||||
productId: number,
|
||||
): { prevResult: number; prevStationName: string | null } {
|
||||
const db = getDb()
|
||||
const dateStr = todayDateStr()
|
||||
const productIdStr = String(productId)
|
||||
|
||||
const allRows = db.prepare(`
|
||||
SELECT s.StationName, s.PointId, mp.PointName
|
||||
FROM Stations s
|
||||
JOIN MeasurementPoints mp ON mp.Id = s.PointId
|
||||
ORDER BY s.Id
|
||||
`).all() as { StationName: string; PointId: number; PointName: string }[]
|
||||
|
||||
const currentIdx = allRows.findIndex(r => r.StationName === stationName)
|
||||
|
||||
if (currentIdx === -1) {
|
||||
logger.warn(TAG, `工序 ${stationName} 未找到`)
|
||||
return { prevResult: 0, prevStationName: null }
|
||||
}
|
||||
|
||||
if (currentIdx === 0) {
|
||||
logger.info(TAG, `${stationName} 是首工序,PrevResult=1`)
|
||||
return { prevResult: 1, prevStationName: null }
|
||||
}
|
||||
|
||||
const prev = allRows[currentIdx - 1]
|
||||
const table = `MPData_P${prev.PointId}_${dateStr}`
|
||||
|
||||
try {
|
||||
const row = db.prepare(`
|
||||
SELECT IsQualified FROM ${table} WHERE ProductID = ?
|
||||
`).get(productIdStr) as { IsQualified: number } | undefined
|
||||
|
||||
const result = row ? row.IsQualified : 0
|
||||
logger.info(TAG, `${stationName} 查询 ${prev.StationName} → ${result ? '合格' : '不合格'}`)
|
||||
return { prevResult: result, prevStationName: prev.StationName }
|
||||
} catch {
|
||||
logger.warn(TAG, `表 ${table} 不存在或查询失败`)
|
||||
return { prevResult: 0, prevStationName: prev.StationName }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user