fix: 修复多个外设无法认证的问题

refactor: 同时使用更加优雅的方式处理injection
This commit is contained in:
2025-07-15 11:30:09 +08:00
parent 705e322e41
commit 49cbdc51d9
6 changed files with 184 additions and 84 deletions

View File

@@ -1,42 +1,55 @@
import { ref, reactive, watchPostEffect } from 'vue'
import { defineStore } from 'pinia'
import { useLocalStorage } from '@vueuse/core'
import { isString, toNumber } from 'lodash';
import { Common } from '@/utils/Common';
import z from "zod"
import { isNumber } from 'mathjs';
import { ref, reactive, watchPostEffect } from "vue";
import { defineStore } from "pinia";
import { useLocalStorage } from "@vueuse/core";
import { isString, toNumber } from "lodash";
import z from "zod";
import { isNumber } from "mathjs";
import { JtagClient, MatrixKeyClient, PowerClient } from "@/APIClient";
import { Mutex, withTimeout } from 'async-mutex';
import { Mutex, withTimeout } from "async-mutex";
import { useConstraintsStore } from "@/stores/constraints";
import { useDialogStore } from './dialog';
import { useDialogStore } from "./dialog";
import { toFileParameterOrUndefined } from "@/utils/Common";
import { AuthManager } from "@/utils/AuthManager";
export const useEquipments = defineStore('equipments', () => {
export const useEquipments = defineStore("equipments", () => {
// Global Stores
const constrainsts = useConstraintsStore();
const dialog = useDialogStore();
const boardAddr = useLocalStorage('fpga-board-addr', "127.0.0.1");
const boardPort = useLocalStorage('fpga-board-port', 1234);
const boardAddr = useLocalStorage("fpga-board-addr", "127.0.0.1");
const boardPort = useLocalStorage("fpga-board-port", 1234);
// Jtag
const jtagBitstream = ref<File>();
const jtagBoundaryScanFreq = ref(100);
const jtagClientMutex = withTimeout(new Mutex(), 1000, new Error("JtagClient Mutex Timeout!"))
const jtagClient = new JtagClient();
const jtagClientMutex = withTimeout(
new Mutex(),
1000,
new Error("JtagClient Mutex Timeout!"),
);
const jtagClient = AuthManager.createAuthenticatedJtagClient();
// Matrix Key
const matrixKeyStates = reactive(new Array<boolean>(16).fill(false))
const matrixKeypadClientMutex = withTimeout(new Mutex(), 1000, new Error("Matrixkeyclient Mutex Timeout!"));
const matrixKeypadClient = new MatrixKeyClient();
const matrixKeyStates = reactive(new Array<boolean>(16).fill(false));
const matrixKeypadClientMutex = withTimeout(
new Mutex(),
1000,
new Error("Matrixkeyclient Mutex Timeout!"),
);
const matrixKeypadClient = AuthManager.createAuthenticatedMatrixKeyClient();
// Power
const powerClientMutex = withTimeout(new Mutex(), 1000, new Error("Matrixkeyclient Mutex Timeout!"));
const powerClient = new PowerClient();
const powerClientMutex = withTimeout(
new Mutex(),
1000,
new Error("Matrixkeyclient Mutex Timeout!"),
);
const powerClient = AuthManager.createAuthenticatedPowerClient();
// Enable Setting
const enableJtagBoundaryScan = ref(false);
const enableMatrixKey = ref(false);
const enablePower = ref(false)
const enablePower = ref(false);
// Watch
watchPostEffect(async () => {
@@ -60,8 +73,7 @@ export const useEquipments = defineStore('equipments', () => {
boardPort.value = portNumber;
return true;
}
}
else if (isNumber(port)) {
} else if (isNumber(port)) {
if (z.number().nonnegative().max(65535).safeParse(port).success) {
boardPort.value = port;
return true;
@@ -70,7 +82,10 @@ export const useEquipments = defineStore('equipments', () => {
return false;
}
function setMatrixKey(keyNum: number | string | undefined, keyValue: boolean): boolean {
function setMatrixKey(
keyNum: number | string | undefined,
keyValue: boolean,
): boolean {
let _keyNum: number;
if (isString(keyNum)) {
_keyNum = toNumber(keyNum);
@@ -112,7 +127,7 @@ export const useEquipments = defineStore('equipments', () => {
try {
const resp = await jtagClient.uploadBitstream(
boardAddr.value,
Common.toFileParameterOrNull(bitstream),
toFileParameterOrUndefined(bitstream),
);
return resp;
} catch (e) {
@@ -127,7 +142,7 @@ export const useEquipments = defineStore('equipments', () => {
try {
const resp = await jtagClient.downloadBitstream(
boardAddr.value,
boardPort.value
boardPort.value,
);
return resp;
} catch (e) {
@@ -144,7 +159,7 @@ export const useEquipments = defineStore('equipments', () => {
try {
const resp = await jtagClient.getDeviceIDCode(
boardAddr.value,
boardPort.value
boardPort.value,
);
return resp;
} catch (e) {
@@ -161,7 +176,7 @@ export const useEquipments = defineStore('equipments', () => {
const resp = await jtagClient.setSpeed(
boardAddr.value,
boardPort.value,
speed
speed,
);
return resp;
} catch (e) {
@@ -179,7 +194,7 @@ export const useEquipments = defineStore('equipments', () => {
const resp = await matrixKeypadClient.setMatrixKeyStatus(
boardAddr.value,
boardPort.value,
keyStates
keyStates,
);
return resp;
} catch (e) {
@@ -223,7 +238,7 @@ export const useEquipments = defineStore('equipments', () => {
const resp = await powerClient.setPowerOnOff(
boardAddr.value,
boardPort.value,
enable
enable,
);
return resp;
} catch (e) {
@@ -266,6 +281,5 @@ export const useEquipments = defineStore('equipments', () => {
powerClient,
powerClientMutex,
powerSetOnOff,
}
})
};
});