init: PLC data collection system

This commit is contained in:
zlm committed 2026-06-09 14:32:13 +08:00
commit ae90bc9368
38 files changed
+5234

No files matched your search

+108
View File
@@ -0,0 +1,108 @@
import { Hono } from 'hono'
import { getDb } from '../database/connection.js'
import {
insertMeasurementPoint, updateMeasurementPoint, deleteMeasurementPoint,
insertPLC, updatePLC, deletePLC,
insertStation, updateStation, deleteStation,
createMPDataTable, dropMPDataTable,
} from '../database/schema.js'
const config = new Hono()
/* ===== GET ===== */
config.get('/plcs', (c) => {
const db = getDb()
return c.json(db.prepare('SELECT * FROM PLCs ORDER BY Id').all())
})
config.get('/plcs/:id', (c) => {
const db = getDb()
const plc = db.prepare('SELECT * FROM PLCs WHERE Id = ?').get(c.req.param('id'))
return plc ? c.json(plc) : c.json({ error: 'Not found' }, 404)
})
config.get('/stations', (c) => {
const db = getDb()
const plcId = c.req.query('plcId')
const sql = plcId
? `SELECT s.*, mp.PointName as pointName FROM Stations s JOIN MeasurementPoints mp ON mp.Id = s.PointId WHERE s.PLCId = ? ORDER BY s.Id`
: `SELECT s.*, mp.PointName as pointName FROM Stations s JOIN MeasurementPoints mp ON mp.Id = s.PointId ORDER BY s.Id`
const rows = plcId ? db.prepare(sql).all(plcId) : db.prepare(sql).all()
return c.json(rows)
})
config.get('/measurement-points', (c) => {
const db = getDb()
return c.json(db.prepare('SELECT * FROM MeasurementPoints ORDER BY Id').all())
})
/* ===== MeasurementPoints CRUD ===== */
config.post('/measurement-points', (c) => c.req.json().then(body => {
const id = insertMeasurementPoint(body.PointName, body.FieldUnit || '')
createMPDataTable(id, todayStr())
return c.json({ Id: id })
}))
config.put('/measurement-points/:id', (c) => c.req.json().then(body => {
updateMeasurementPoint(Number(c.req.param('id')), body.PointName, body.FieldUnit || '')
return c.json({ success: true })
}))
config.delete('/measurement-points/:id', (c) => {
const id = Number(c.req.param('id'))
dropMPDataTable(id, todayStr())
deleteMeasurementPoint(id)
return c.json({ success: true })
})
/* ===== PLCs CRUD ===== */
config.post('/plcs', (c) => c.req.json().then(body => {
const id = insertPLC(body.PLCName, body.IPAddress, body.DBNumber, body.StationCount || 0)
return c.json({ Id: id })
}))
config.put('/plcs/:id', (c) => c.req.json().then(body => {
updatePLC(Number(c.req.param('id')), body.PLCName, body.IPAddress, body.DBNumber, body.StationCount || 0)
return c.json({ success: true })
}))
config.delete('/plcs/:id', (c) => {
deletePLC(Number(c.req.param('id')))
return c.json({ success: true })
})
/* ===== Stations CRUD ===== */
config.post('/stations', (c) => c.req.json().then(body => {
const db = getDb()
let name = body.StationName
if (!name) {
const mp = db.prepare('SELECT PointName FROM MeasurementPoints WHERE Id=?').get(body.PointId) as any
name = mp?.PointName || ''
}
const id = insertStation(body.PLCId, body.StationIndex || 0, name, body.PointId)
return c.json({ Id: id })
}))
config.put('/stations/:id', (c) => c.req.json().then(body => {
updateStation(Number(c.req.param('id')), body.PLCId, body.StationIndex || 0, body.StationName, body.PointId)
return c.json({ success: true })
}))
config.delete('/stations/:id', (c) => {
deleteStation(Number(c.req.param('id')))
return c.json({ success: true })
})
function todayStr(): string {
const d = new Date()
const y = d.getFullYear()
const m = String(d.getMonth() + 1).padStart(2, '0')
const dd = String(d.getDate()).padStart(2, '0')
return `${y}_${m}_${dd}`
}
export default config
+34
View File
@@ -0,0 +1,34 @@
import { Hono } from 'hono'
import { getDb } from '../database/connection.js'
const monitor = new Hono()
monitor.get('/status', (c) => {
const db = getDb()
const plcCount = (db.prepare('SELECT COUNT(*) as c FROM PLCs').get() as any).c
const stationCount = (db.prepare('SELECT COUNT(*) as c FROM Stations').get() as any).c
const pointCount = (db.prepare('SELECT COUNT(*) as c FROM MeasurementPoints').get() as any).c
const tables = db.prepare(
"SELECT name FROM sqlite_master WHERE type='table' AND name LIKE 'MPData_%' ORDER BY name"
).all() as any[]
let totalRows = 0
for (const t of tables) {
try {
const r = db.prepare(`SELECT COUNT(*) as c FROM ${t.name}`).get() as any
totalRows += r.c
} catch { /* skip */ }
}
return c.json({
plcCount,
stationCount,
pointCount,
dataTableCount: tables.length,
totalRecords: totalRows,
})
})
export default monitor
+104
View File
@@ -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