132 lines
4.1 KiB
TypeScript
132 lines
4.1 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
import {
|
|
getLoopbackPrinterAccessHint,
|
|
printerComponents,
|
|
startPrinterComponentConnections,
|
|
stopPrinterComponentConnections
|
|
} from '@/core/printerRuntime'
|
|
import type {
|
|
PrinterComponentPort,
|
|
PrinterComponentStatus,
|
|
PrinterComponentStatusItem
|
|
} from '@/core/printerComponentCore'
|
|
|
|
const AUTO_RECONNECT_STORAGE_KEY = 'printer_component_auto_reconnect'
|
|
|
|
function readAutoReconnectFromStorage(): boolean {
|
|
try {
|
|
const raw = localStorage.getItem(AUTO_RECONNECT_STORAGE_KEY)
|
|
if (raw === '0' || raw === 'false') return false
|
|
if (raw === '1' || raw === 'true') return true
|
|
} catch {
|
|
// ignore
|
|
}
|
|
return false
|
|
}
|
|
|
|
function writeAutoReconnectToStorage(enabled: boolean): void {
|
|
try {
|
|
localStorage.setItem(AUTO_RECONNECT_STORAGE_KEY, enabled ? '1' : '0')
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}
|
|
|
|
export type PrinterComponentRow = PrinterComponentPort & {
|
|
status: PrinterComponentStatus
|
|
}
|
|
|
|
interface PrinterComponentState {
|
|
statusList: PrinterComponentStatusItem[]
|
|
components: PrinterComponentPort[]
|
|
autoReconnectEnabled: boolean
|
|
/** 公网 HTTP 等场景下无法连接本机组件时的提示 */
|
|
accessBlockedHint: string | null
|
|
}
|
|
|
|
export const usePrinterComponentStore = defineStore('printerComponent', {
|
|
state: (): PrinterComponentState => ({
|
|
statusList: printerComponents.getAllStatus(),
|
|
components: printerComponents.getAllPorts(),
|
|
autoReconnectEnabled: readAutoReconnectFromStorage(),
|
|
accessBlockedHint: getLoopbackPrinterAccessHint()
|
|
}),
|
|
getters: {
|
|
getStatus:
|
|
(state) =>
|
|
(name: string): PrinterComponentStatus | '未知' => {
|
|
const item = state.statusList.find((s) => s.name === name)
|
|
return item?.status ?? '未知'
|
|
},
|
|
isConnected:
|
|
(state) =>
|
|
(name: string): boolean => {
|
|
const item = state.statusList.find((s) => s.name === name)
|
|
return item?.status === 'connected'
|
|
},
|
|
componentRows(state): PrinterComponentRow[] {
|
|
return state.components.map((item) => ({
|
|
...item,
|
|
status: state.statusList.find((s) => s.name === item.name)?.status ?? 'disconnected'
|
|
}))
|
|
},
|
|
connectedCount(state): number {
|
|
return state.statusList.filter((s) => s.status === 'connected').length
|
|
},
|
|
isAccessBlocked(state): boolean {
|
|
return Boolean(state.accessBlockedHint)
|
|
}
|
|
},
|
|
actions: {
|
|
bindRuntimeEvents() {
|
|
const saved = readAutoReconnectFromStorage()
|
|
printerComponents.setAutoReconnectEnabled(saved)
|
|
this.autoReconnectEnabled = saved
|
|
|
|
printerComponents.on('printerComponentStatusChange', (payload) => {
|
|
const data = payload as { name: string; status: PrinterComponentStatus }
|
|
const idx = this.statusList.findIndex((s) => s.name === data.name)
|
|
if (idx === -1) {
|
|
this.statusList.push({ name: data.name, status: data.status })
|
|
} else {
|
|
this.statusList[idx] = { name: data.name, status: data.status }
|
|
}
|
|
})
|
|
|
|
printerComponents.on('autoReconnectChange', (payload) => {
|
|
const data = payload as { enabled: boolean }
|
|
this.autoReconnectEnabled = data.enabled
|
|
})
|
|
|
|
printerComponents.on('loopbackAccessBlocked', () => {
|
|
this.accessBlockedHint = getLoopbackPrinterAccessHint()
|
|
})
|
|
},
|
|
refreshFromRuntime() {
|
|
this.statusList = printerComponents.getAllStatus()
|
|
this.components = printerComponents.getAllPorts()
|
|
this.autoReconnectEnabled = printerComponents.getAutoReconnectEnabled()
|
|
this.accessBlockedHint = getLoopbackPrinterAccessHint()
|
|
},
|
|
setAutoReconnect(enabled: boolean) {
|
|
writeAutoReconnectToStorage(enabled)
|
|
printerComponents.setAutoReconnectEnabled(enabled)
|
|
this.autoReconnectEnabled = enabled
|
|
},
|
|
reconnectComponent(name: string) {
|
|
printerComponents.reconnectComponent(name)
|
|
},
|
|
reconnectAll() {
|
|
printerComponents.reconnectAll()
|
|
},
|
|
startConnections() {
|
|
startPrinterComponentConnections()
|
|
this.refreshFromRuntime()
|
|
},
|
|
stopConnections() {
|
|
stopPrinterComponentConnections()
|
|
this.refreshFromRuntime()
|
|
}
|
|
}
|
|
})
|