init: PLC data collection system
This commit is contained in:
commit
ae90bc9368
38 files changed
+5234
No files matched your search
@@ -0,0 +1,104 @@
|
||||
import { Hono } from 'hono'
|
||||
import { getDb } from '../database/connection.js'
|
||||
import { getAllStations, getMPRecord } from '../database/schema.js'
|
||||
|
||||
const query = new Hono()
|
||||
|
||||
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}`
|
||||
}
|
||||
|
||||
query.get('/:productId', (c) => {
|
||||
const productId = c.req.param('productId')
|
||||
const dateStr = c.req.query('date') || todayDateStr()
|
||||
const db = getDb()
|
||||
|
||||
const points = db.prepare('SELECT Id, PointName, FieldUnit FROM MeasurementPoints ORDER BY Id').all() as any[]
|
||||
const result: any[] = []
|
||||
|
||||
for (const p of points) {
|
||||
const table = `MPData_P${p.Id}_${dateStr}`
|
||||
const tableExists = db.prepare(
|
||||
"SELECT name FROM sqlite_master WHERE type='table' AND name=?"
|
||||
).get(table)
|
||||
|
||||
if (!tableExists) continue
|
||||
|
||||
const row = db.prepare(`
|
||||
SELECT RecordTime as recordTime, IsQualified as isQualified,
|
||||
InspectionCount as inspectionCount,
|
||||
UpperLimit as upperLimit, LowerLimit as lowerLimit,
|
||||
MeasuredValue as measuredValue
|
||||
FROM ${table} WHERE ProductID = ?
|
||||
`).get(productId) as any
|
||||
|
||||
if (row) {
|
||||
result.push({
|
||||
pointId: p.Id,
|
||||
pointName: p.PointName,
|
||||
fieldUnit: p.FieldUnit,
|
||||
...row,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if (result.length === 0) {
|
||||
return c.json({ productId, data: [], message: '该产品暂无数据记录' })
|
||||
}
|
||||
|
||||
return c.json({ productId, data: result })
|
||||
})
|
||||
|
||||
query.get('/:productId/interlock/:stationName', (c) => {
|
||||
const productId = c.req.param('productId')
|
||||
const stationName = c.req.param('stationName')
|
||||
const dateStr = c.req.query('date') || todayDateStr()
|
||||
|
||||
const stations = getAllStations()
|
||||
const names = stations.map(s => s.stationName)
|
||||
const idx = names.indexOf(stationName)
|
||||
|
||||
if (idx === -1) {
|
||||
return c.json({ error: `未找到工序: ${stationName}`, available: names }, 404)
|
||||
}
|
||||
|
||||
const current = stations[idx]
|
||||
if (idx === 0) {
|
||||
return c.json({
|
||||
currentStation: current.stationName,
|
||||
currentPoint: current.pointName,
|
||||
prevStation: null,
|
||||
prevResult: 1,
|
||||
message: '当前为产线首个工序,视为合格',
|
||||
})
|
||||
}
|
||||
|
||||
const prev = stations[idx - 1]
|
||||
const record = getMPRecord(dateStr, prev.pointId, productId)
|
||||
|
||||
if (!record) {
|
||||
return c.json({
|
||||
currentStation: current.stationName,
|
||||
currentPoint: current.pointName,
|
||||
prevStation: prev.stationName,
|
||||
prevPoint: prev.pointName,
|
||||
prevResult: 0,
|
||||
message: '前工序尚未检测此产品,视为不合格',
|
||||
})
|
||||
}
|
||||
|
||||
return c.json({
|
||||
currentStation: current.stationName,
|
||||
currentPoint: current.pointName,
|
||||
prevStation: prev.stationName,
|
||||
prevPoint: prev.pointName,
|
||||
prevResult: record.isQualified,
|
||||
prevData: record,
|
||||
})
|
||||
})
|
||||
|
||||
export default query
|
||||
Reference in new issue
Block a user