Add history tracking to Line model and update CLI actions

Introduces a 'history' column to the Line model and database, storing line history as a JSON string. Updates line_connection service to persist history and extends Redis cleanup to 96 hours. Refactors BottomToolBar and ButtonAction components to use selectedLines for CLI actions and adjusts command sequences.
This commit is contained in:
nguyentrungthat 2025-12-02 14:23:35 +07:00
parent ffad475c2b
commit 997e9b2a1b
5 changed files with 56 additions and 11 deletions

View File

@ -51,4 +51,7 @@ export default class Line extends BaseModel {
@column.dateTime({ autoCreate: true, autoUpdate: true }) @column.dateTime({ autoCreate: true, autoUpdate: true })
declare updatedAt: DateTime declare updatedAt: DateTime
@column()
declare history: string
} }

View File

@ -15,6 +15,7 @@ import APCController from './apc_connection.js'
import path from 'node:path' import path from 'node:path'
import axios from 'axios' import axios from 'axios'
import redis from '@adonisjs/redis/services/main' import redis from '@adonisjs/redis/services/main'
import Line from '#models/line'
type Inventory = { type Inventory = {
pid: string pid: string
@ -196,7 +197,7 @@ export default class LineConnection {
if (resolvedOrRejected) return if (resolvedOrRejected) return
resolvedOrRejected = true resolvedOrRejected = true
console.error(`❌ Error line ${lineNumber}:`, err.message) console.error(`❌ Error line ${lineNumber}:`, err.message)
this.config.output += err.message this.config.output += '\r\n' + err.message + '\r\n'
this.socketIO.emit('line_error', { this.socketIO.emit('line_error', {
stationId, stationId,
lineId: id, lineId: id,
@ -707,7 +708,7 @@ export default class LineConnection {
(Tóm tắt trạng thái tổng thể của hệ thống trong 24 ý) (Tóm tắt trạng thái tổng thể của hệ thống trong 24 ý)
issue: issue:
(Tóm tắt cực ngắn gọn các lỗi/dấu hiệu bất thường, mỗi vấn đ 1 dòng, bỏ qua các vấn đ không quan trọng như về port changed state to up/down hay Invalid input, Incomplete command) (Tóm tắt cực ngắn gọn các lỗi/dấu hiệu bất thường, mỗi vấn đ 1 dòng, không bắt các vấn đ không quan trọng như về port changed state to up/down hay Invalid input, Incomplete command)
Quy tắc: Quy tắc:
Không giải thích dài dòng. Không giải thích dài dòng.
@ -759,11 +760,19 @@ export default class LineConnection {
} }
} }
const line = await Line.find(lineId)
if (line) {
const listHistory = line.history ? JSON.parse(line.history) : []
listHistory.unshift(newItem)
line.history = JSON.stringify(listHistory)
await line.save()
}
// Add vào ZSET // Add vào ZSET
await redis.zadd(key, now, newItem) await redis.zadd(key, now, newItem)
// Tự động xóa item > 72h // Tự động xóa item > 96h
const expireTime = now - 72 * 60 * 60 * 1000 const expireTime = now - 96 * 60 * 60 * 1000
await redis.zremrangebyscore(key, 0, expireTime) await redis.zremrangebyscore(key, 0, expireTime)
return true return true

View File

@ -0,0 +1,17 @@
import { BaseSchema } from '@adonisjs/lucid/schema'
export default class extends BaseSchema {
protected tableName = 'lines'
async up() {
this.schema.alterTable(this.tableName, (table) => {
table.text('history').defaultTo('').nullable()
})
}
async down() {
this.schema.alterTable(this.tableName, (table) => {
table.dropColumn('history')
})
}
}

View File

@ -623,12 +623,7 @@ const BottomToolBar = ({
className={classes.buttonControl} className={classes.buttonControl}
variant="outline" variant="outline"
onClick={() => { onClick={() => {
const lines = station.lines.filter( selectedLines.forEach((line) => {
(line) =>
!line?.userOpenCLI ||
line?.userOpenCLI === user?.userName
);
lines.forEach((line) => {
socket?.emit("close_cli", { socket?.emit("close_cli", {
lineId: line?.id, lineId: line?.id,
stationId: line.stationId || line.station_id, stationId: line.stationId || line.station_id,
@ -745,7 +740,7 @@ const BottomToolBar = ({
}); });
}); });
} else { } else {
lines.forEach((line) => { selectedLines.forEach((line) => {
socket?.emit("close_cli", { socket?.emit("close_cli", {
lineId: line?.id, lineId: line?.id,
stationId: line.stationId || line.station_id, stationId: line.stationId || line.station_id,

View File

@ -37,6 +37,27 @@ export const ButtonDPELP = ({
onClick(); onClick();
selectedLines?.forEach((el) => { selectedLines?.forEach((el) => {
const body = [ const body = [
{
expect: "",
send: "",
delay: "1000",
repeat: "1",
note: "",
},
{
expect: "",
send: " no",
delay: "1000",
repeat: "1",
note: "",
},
{
expect: "",
send: "",
delay: "1000",
repeat: "2",
note: "",
},
{ {
expect: "", expect: "",
send: " terminal length 0", send: " terminal length 0",