@libs-scripts-mep/inv-img-tool
Version:
Ferramenta de visão computacional com implementações dedicadas à validação de displays em testes automatizados de linha de produção.
177 lines (136 loc) • 6.48 kB
JavaScript
import FWLink from "../daq-fwlink/FWLink.js"
export class IITClient {
/**@type {WebSocket} */
static ws = null
static debugMode = true
static reconnectAttempts = null
static workflowDone = null
/**@type {HTMLVideoElement | null} */
static streamVideoElement = null
/**@type {HTMLVideoElement | null} */
static resultVideoElement = null
/**@type {Map<[image: string], { result: boolean, name: string, image: string, size: [number, number] }>} */
static workflowResult = new Map()
/**
* Starts a workflow in the Image Tool.
*
* @param {string} wfPath - The path to the workflow file.
* @param {string} wfName - The name of the workflow file.
* @param {HTMLVideoElement} [streamVideoElement] - The video element where the stream will be set.
* @param {HTMLVideoElement} [resultVideoElement] - The video element where the result will be set.
* @param {boolean} [debugMode] - Whether the workflow should run in debug mode.
*/
static async startWorkflow(wfPath, wfName, streamVideoElement = null, resultVideoElement = null, debugMode = false) {
this.workflowDone = false
if (IITClient.ws == null) {
IITClient.ws = new WebSocket('ws://localhost:8765')
await IITClient.delay(1000)
IITClient.initCallbacks()
}
IITClient.streamVideoElement = streamVideoElement
IITClient.resultVideoElement = resultVideoElement
IITClient.workflowResult = new Map()
IITClient.ws.send(JSON.stringify({ wfpath: wfPath, wffilename: wfName, debug: debugMode }))
}
static async stopWorkflow() {
if (this.workflowDone || this.workflowDone == null) return
console.warn("Parando workflow...")
IITClient.ws.send(JSON.stringify({ stop: true }))
while (!this.workflowDone) { await this.delay(100) }
}
static async connect() {
if (IITClient.ws == null || IITClient.ws.readyState == WebSocket.CLOSED) {
IITClient.ws = new WebSocket('ws://localhost:8765')
await IITClient.delay(1000)
IITClient.initCallbacks()
} else {
console.warn("Client já conectado")
}
}
static initCallbacks() {
IITClient.ws.onopen = () => { console.warn("Websocket conectado") }
IITClient.ws.onclose = () => { console.warn("Websocket fechado"); IITClient.connect() }
IITClient.ws.onerror = (error) => { console.error(error) }
IITClient.ws.onmessage = (event) => {
try {
const obj = JSON.parse(event.data)
if (obj.stream && IITClient.streamVideoElement != null) {
IITClient.streamVideoElement.src = 'data:image/jpeg;base64,' + obj.stream
} else if (obj.img_result) {
IITClient.workflowResult.set(obj.img_result.name, obj.img_result)
if (obj.img_result.image != null && IITClient.resultVideoElement != null) {
IITClient.resultVideoElement.src = 'data:image/jpeg;base64,' + obj.img_result.image
if (obj.img_result.result != null) {
console.log(obj.img_result)
}
}
} else {
console.log(obj)
if (obj.done) this.workflowDone = true
}
} catch (error) {
console.log(error)
}
}
}
static showImageResult(name) {
if (!IITClient.workflowResult.has(name)) { return }
if (IITClient.resultVideoElement == null) { return }
IITClient.resultVideoElement.src = 'data:image/jpeg;base64,' + IITClient.workflowResult.get(name).image
return
}
static async killWebsocket() {
if (IITClient.ws == null) {
IITClient.ws = new WebSocket('ws://localhost:8765')
await IITClient.delay(1000)
}
IITClient.ws.send(JSON.stringify({ kill: true }))
}
static async delay(ms) { return new Promise(resolve => setTimeout(resolve, ms)) }
static { window.IITClient = IITClient }
}
export class IITServer {
static getScriptPath() {
let pathC = location.pathname.slice(location.pathname.indexOf("C:/"), location.pathname.lastIndexOf("/"))
let pathI = location.pathname.slice(location.pathname.indexOf("I:/"), location.pathname.lastIndexOf("/"))
if (pathC.length > 0) {
return pathC
} else if (pathI.length > 0) {
return pathI
}
}
static async startWebsocket(pyFilePath = IITServer.getScriptPath() + '/node_modules/@libs-scripts-mep/inv-img-tool/websocket.py') {
return new Promise((resolve) => {
FWLink.runInstructionS("EXEC", ["Python", pyFilePath, "true", "true"])
const id = FWLink.PVIEventObserver.add((message, params) => {
const msg = params[0]
if (msg != undefined) {
console.log(`${message} ${msg}`)
if (msg.includes("Servidor WebSocket IIT iniciado")) {
FWLink.PVIEventObserver.remove(id)
resolve({ success: true, msg: msg, })
}
}
}, "PVI.Sniffer.sniffer.PID_")
})
}
static { window.IITServer = IITServer }
}
export class DepChecker {
static async checkDependencies(pyFilePath = IITServer.getScriptPath() + '/node_modules/@libs-scripts-mep/inv-img-tool/dep_checker.py') {
return new Promise((resolve) => {
FWLink.runInstructionS("EXEC", ["Python", pyFilePath, "true", "true"])
const id = FWLink.PVIEventObserver.add((message, params) => {
const msg = params[0]
if (msg != undefined) {
console.log(`${message} ${msg}`)
if (msg.includes("Package version check and update completed.")) {
FWLink.PVIEventObserver.remove(id)
resolve({ success: true, msg: msg, })
}
}
}, "PVI.Sniffer.sniffer.PID_")
})
}
static { window.DepChecker = DepChecker }
}