Update
This commit is contained in:
parent
ef7ea5c9af
commit
9c4d8d4041
|
|
@ -51,6 +51,7 @@ export default class LineConnection {
|
||||||
private isRunningScript: boolean
|
private isRunningScript: boolean
|
||||||
private connecting: boolean
|
private connecting: boolean
|
||||||
private waitingScenario: boolean
|
private waitingScenario: boolean
|
||||||
|
private outputScenario: string
|
||||||
|
|
||||||
constructor(config: LineConfig, socketIO: any) {
|
constructor(config: LineConfig, socketIO: any) {
|
||||||
this.config = config
|
this.config = config
|
||||||
|
|
@ -60,6 +61,7 @@ export default class LineConnection {
|
||||||
this.isRunningScript = false
|
this.isRunningScript = false
|
||||||
this.connecting = false
|
this.connecting = false
|
||||||
this.waitingScenario = false
|
this.waitingScenario = false
|
||||||
|
this.outputScenario = ''
|
||||||
}
|
}
|
||||||
|
|
||||||
connect(timeoutMs = 5000) {
|
connect(timeoutMs = 5000) {
|
||||||
|
|
@ -94,6 +96,8 @@ export default class LineConnection {
|
||||||
if (this.isRunningScript) {
|
if (this.isRunningScript) {
|
||||||
this.waitingScenario = true
|
this.waitingScenario = true
|
||||||
this.outputBuffer += message
|
this.outputBuffer += message
|
||||||
|
if (!this.config.inventory)
|
||||||
|
this.outputScenario = this.outputScenario.slice(-3000) + message
|
||||||
}
|
}
|
||||||
if (message.includes('--More--')) this.writeCommand(' ')
|
if (message.includes('--More--')) this.writeCommand(' ')
|
||||||
|
|
||||||
|
|
@ -114,6 +118,7 @@ export default class LineConnection {
|
||||||
lineId: id,
|
lineId: id,
|
||||||
data: message,
|
data: message,
|
||||||
})
|
})
|
||||||
|
if (!this.config.inventory) this.getInventory(this.outputScenario)
|
||||||
appendLog(message, this.config.stationId, this.config.lineNumber, this.config.port)
|
appendLog(message, this.config.stationId, this.config.lineNumber, this.config.port)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -219,6 +224,7 @@ export default class LineConnection {
|
||||||
const timeoutTimer = setTimeout(() => {
|
const timeoutTimer = setTimeout(() => {
|
||||||
this.isRunningScript = false
|
this.isRunningScript = false
|
||||||
this.outputBuffer = ''
|
this.outputBuffer = ''
|
||||||
|
this.outputScenario = ''
|
||||||
this.config.output += 'Timeout run scenario'
|
this.config.output += 'Timeout run scenario'
|
||||||
this.socketIO.emit('line_output', {
|
this.socketIO.emit('line_output', {
|
||||||
stationId: this.config.stationId,
|
stationId: this.config.stationId,
|
||||||
|
|
@ -245,6 +251,7 @@ export default class LineConnection {
|
||||||
} else clearTimeout(timeoutTimer)
|
} else clearTimeout(timeoutTimer)
|
||||||
this.isRunningScript = false
|
this.isRunningScript = false
|
||||||
this.outputBuffer = ''
|
this.outputBuffer = ''
|
||||||
|
this.outputScenario = ''
|
||||||
appendLog(
|
appendLog(
|
||||||
`\n---end-scenarios---${now}---\n`,
|
`\n---end-scenarios---${now}---\n`,
|
||||||
this.config.stationId,
|
this.config.stationId,
|
||||||
|
|
@ -457,4 +464,31 @@ export default class LineConnection {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getInventory = (log: string) => {
|
||||||
|
const data = textfsmResults(log, 'show inventory')
|
||||||
|
try {
|
||||||
|
data.forEach((item) => {
|
||||||
|
if (item?.textfsm && isValidJson(item?.textfsm)) {
|
||||||
|
if (['show inventory', 'sh inventory', 'show inv', 'sh inv'].includes(item.command)) {
|
||||||
|
this.config.inventory = JSON.parse(item.textfsm)[0]
|
||||||
|
}
|
||||||
|
item.textfsm = JSON.parse(item.textfsm)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if (this.config.inventory) {
|
||||||
|
this.config.data = data
|
||||||
|
this.socketIO.emit('data_textfsm', {
|
||||||
|
stationId: this.config.stationId,
|
||||||
|
lineId: this.config.id,
|
||||||
|
data,
|
||||||
|
inventory: this.config.inventory || null,
|
||||||
|
latestScenario: this.config.latestScenario || null,
|
||||||
|
})
|
||||||
|
this.outputScenario = ''
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import net from 'node:net'
|
||||||
import fs from 'node:fs'
|
import fs from 'node:fs'
|
||||||
import { Server as SocketIOServer } from 'socket.io'
|
import { Server as SocketIOServer } from 'socket.io'
|
||||||
import http from 'node:http'
|
import http from 'node:http'
|
||||||
|
|
@ -8,6 +9,7 @@ import { CustomServer, CustomSocket } from '../app/ultils/types.js'
|
||||||
import Line from '#models/line'
|
import Line from '#models/line'
|
||||||
import Station from '#models/station'
|
import Station from '#models/station'
|
||||||
import APCController from '#services/apc_connection'
|
import APCController from '#services/apc_connection'
|
||||||
|
import { sleep } from '../app/ultils/helper.js'
|
||||||
|
|
||||||
interface HandleOptions {
|
interface HandleOptions {
|
||||||
command?: string
|
command?: string
|
||||||
|
|
@ -287,6 +289,10 @@ export class WebSocketIo {
|
||||||
},
|
},
|
||||||
socket
|
socket
|
||||||
)
|
)
|
||||||
|
// 👉 Bước 1: clear line trước khi connect
|
||||||
|
if (line.lineClear && line.lineClear > 0)
|
||||||
|
await this.clearLineBeforeConnect(station.id, line.lineClear)
|
||||||
|
|
||||||
this.lineMap.set(line.id, lineConn)
|
this.lineMap.set(line.id, lineConn)
|
||||||
await lineConn.connect()
|
await lineConn.connect()
|
||||||
lineConn.writeCommand('\r\n\r\n')
|
lineConn.writeCommand('\r\n\r\n')
|
||||||
|
|
@ -397,4 +403,41 @@ export class WebSocketIo {
|
||||||
console.log(error)
|
console.log(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async clearLineBeforeConnect(stationId: number, clearLine: number) {
|
||||||
|
const station = await Station.find(stationId)
|
||||||
|
if (!station) throw new Error(`Station ${stationId} not found`)
|
||||||
|
|
||||||
|
// Kết nối tới station qua Telnet / Socket
|
||||||
|
const client = new net.Socket()
|
||||||
|
return new Promise<void>((resolve, reject) => {
|
||||||
|
client.connect(station.port, station.ip, async () => {
|
||||||
|
console.log(
|
||||||
|
`Connected to station ${station.name} (${station.ip}) to clear line ${clearLine}`
|
||||||
|
)
|
||||||
|
// Gửi lệnh clear line
|
||||||
|
client.write(`clear line ${clearLine}\r\n`)
|
||||||
|
await sleep(500)
|
||||||
|
client.write(`\r\n\r\n`)
|
||||||
|
})
|
||||||
|
|
||||||
|
client.on('data', (data) => {
|
||||||
|
const output = data.toString()
|
||||||
|
if (output.includes('Clear completed') || output.includes('OK')) {
|
||||||
|
console.log(`Line ${clearLine} cleared successfully.`)
|
||||||
|
client.destroy()
|
||||||
|
resolve()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
client.on('error', (err) => {
|
||||||
|
console.error(`Error clearing line ${clearLine}:`, err)
|
||||||
|
reject(err)
|
||||||
|
})
|
||||||
|
|
||||||
|
client.on('close', () => {
|
||||||
|
console.log(`Station connection closed (line ${clearLine})`)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -268,7 +268,7 @@ function DrawerScenario({
|
||||||
marginBottom: "8px",
|
marginBottom: "8px",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Checkbox
|
{/* <Checkbox
|
||||||
label="Reboot"
|
label="Reboot"
|
||||||
style={{ color: "red" }}
|
style={{ color: "red" }}
|
||||||
checked={form.values.isReboot}
|
checked={form.values.isReboot}
|
||||||
|
|
@ -278,7 +278,7 @@ function DrawerScenario({
|
||||||
event.currentTarget.checked
|
event.currentTarget.checked
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
/>
|
/> */}
|
||||||
</Grid.Col>
|
</Grid.Col>
|
||||||
<Grid.Col span={3}>
|
<Grid.Col span={3}>
|
||||||
<div
|
<div
|
||||||
|
|
|
||||||
|
|
@ -114,7 +114,8 @@ const ModalTerminal = ({
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Text size="md" mr={10}>
|
<Text size="md" mr={10}>
|
||||||
Line number: <strong>{line?.line_number || ""}</strong>
|
Line number:{" "}
|
||||||
|
<strong>{line?.lineNumber || line?.line_number || ""}</strong>
|
||||||
</Text>
|
</Text>
|
||||||
<Text size="md" mr={10}>
|
<Text size="md" mr={10}>
|
||||||
- <strong>{line?.port || ""}</strong>
|
- <strong>{line?.port || ""}</strong>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue