feat: 使用静态arp处理通信
This commit is contained in:
118
src/APIClient.ts
118
src/APIClient.ts
@@ -1278,6 +1278,124 @@ export class DataClient {
|
||||
}
|
||||
return Promise.resolve<Board[]>(null as any);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新板卡名称(管理员权限)
|
||||
* @param boardId (optional)
|
||||
* @param newName (optional)
|
||||
*/
|
||||
updateBoardName(boardId: string | undefined, newName: string | undefined): Promise<number> {
|
||||
let url_ = this.baseUrl + "/api/Data/UpdateBoardName?";
|
||||
if (boardId === null)
|
||||
throw new Error("The parameter 'boardId' cannot be null.");
|
||||
else if (boardId !== undefined)
|
||||
url_ += "boardId=" + encodeURIComponent("" + boardId) + "&";
|
||||
if (newName === null)
|
||||
throw new Error("The parameter 'newName' cannot be null.");
|
||||
else if (newName !== undefined)
|
||||
url_ += "newName=" + encodeURIComponent("" + newName) + "&";
|
||||
url_ = url_.replace(/[?&]$/, "");
|
||||
|
||||
let options_: RequestInit = {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Accept": "application/json"
|
||||
}
|
||||
};
|
||||
|
||||
return this.http.fetch(url_, options_).then((_response: Response) => {
|
||||
return this.processUpdateBoardName(_response);
|
||||
});
|
||||
}
|
||||
|
||||
protected processUpdateBoardName(response: Response): Promise<number> {
|
||||
const status = response.status;
|
||||
let _headers: any = {}; if (response.headers && response.headers.forEach) { response.headers.forEach((v: any, k: any) => _headers[k] = v); };
|
||||
if (status === 200) {
|
||||
return response.text().then((_responseText) => {
|
||||
let result200: any = null;
|
||||
let resultData200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver);
|
||||
result200 = resultData200 !== undefined ? resultData200 : <any>null;
|
||||
|
||||
return result200;
|
||||
});
|
||||
} else if (status === 400) {
|
||||
return response.text().then((_responseText) => {
|
||||
let result400: any = null;
|
||||
let resultData400 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver);
|
||||
result400 = ProblemDetails.fromJS(resultData400);
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers, result400);
|
||||
});
|
||||
} else if (status === 500) {
|
||||
return response.text().then((_responseText) => {
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers);
|
||||
});
|
||||
} else if (status !== 200 && status !== 204) {
|
||||
return response.text().then((_responseText) => {
|
||||
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
|
||||
});
|
||||
}
|
||||
return Promise.resolve<number>(null as any);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新板卡状态(管理员权限)
|
||||
* @param boardId (optional)
|
||||
* @param newStatus (optional)
|
||||
*/
|
||||
updateBoardStatus(boardId: string | undefined, newStatus: BoardStatus | undefined): Promise<number> {
|
||||
let url_ = this.baseUrl + "/api/Data/UpdateBoardStatus?";
|
||||
if (boardId === null)
|
||||
throw new Error("The parameter 'boardId' cannot be null.");
|
||||
else if (boardId !== undefined)
|
||||
url_ += "boardId=" + encodeURIComponent("" + boardId) + "&";
|
||||
if (newStatus === null)
|
||||
throw new Error("The parameter 'newStatus' cannot be null.");
|
||||
else if (newStatus !== undefined)
|
||||
url_ += "newStatus=" + encodeURIComponent("" + newStatus) + "&";
|
||||
url_ = url_.replace(/[?&]$/, "");
|
||||
|
||||
let options_: RequestInit = {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Accept": "application/json"
|
||||
}
|
||||
};
|
||||
|
||||
return this.http.fetch(url_, options_).then((_response: Response) => {
|
||||
return this.processUpdateBoardStatus(_response);
|
||||
});
|
||||
}
|
||||
|
||||
protected processUpdateBoardStatus(response: Response): Promise<number> {
|
||||
const status = response.status;
|
||||
let _headers: any = {}; if (response.headers && response.headers.forEach) { response.headers.forEach((v: any, k: any) => _headers[k] = v); };
|
||||
if (status === 200) {
|
||||
return response.text().then((_responseText) => {
|
||||
let result200: any = null;
|
||||
let resultData200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver);
|
||||
result200 = resultData200 !== undefined ? resultData200 : <any>null;
|
||||
|
||||
return result200;
|
||||
});
|
||||
} else if (status === 400) {
|
||||
return response.text().then((_responseText) => {
|
||||
let result400: any = null;
|
||||
let resultData400 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver);
|
||||
result400 = ProblemDetails.fromJS(resultData400);
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers, result400);
|
||||
});
|
||||
} else if (status === 500) {
|
||||
return response.text().then((_responseText) => {
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers);
|
||||
});
|
||||
} else if (status !== 200 && status !== 204) {
|
||||
return response.text().then((_responseText) => {
|
||||
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
|
||||
});
|
||||
}
|
||||
return Promise.resolve<number>(null as any);
|
||||
}
|
||||
}
|
||||
|
||||
export class DDSClient {
|
||||
|
||||
@@ -134,21 +134,29 @@
|
||||
<h4 class="font-semibold">网络配置信息:</h4>
|
||||
<div class="bg-base-200 p-3 rounded">
|
||||
<div class="text-sm space-y-1">
|
||||
<div><span class="font-medium">主机IP:</span> {{ networkConfig.hostIP }}</div>
|
||||
<div><span class="font-medium">板卡IP:</span> {{ networkConfig.boardIP }}</div>
|
||||
<div><span class="font-medium">主机MAC:</span> {{ networkConfig.hostMAC }}</div>
|
||||
<div><span class="font-medium">板卡MAC:</span> {{ networkConfig.boardMAC }}</div>
|
||||
<div>
|
||||
<span class="font-medium">主机IP:</span>
|
||||
{{ networkConfig.hostIP }}
|
||||
</div>
|
||||
<div>
|
||||
<span class="font-medium">板卡IP:</span>
|
||||
{{ networkConfig.boardIP }}
|
||||
</div>
|
||||
<div>
|
||||
<span class="font-medium">主机MAC:</span>
|
||||
{{ networkConfig.hostMAC }}
|
||||
</div>
|
||||
<div>
|
||||
<span class="font-medium">板卡MAC:</span>
|
||||
{{ networkConfig.boardMAC }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 操作按钮 -->
|
||||
<div class="modal-action">
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-primary"
|
||||
@click="handleSuccess"
|
||||
>
|
||||
<button type="button" class="btn btn-primary" @click="handleSuccess">
|
||||
确认
|
||||
</button>
|
||||
</div>
|
||||
@@ -166,7 +174,9 @@
|
||||
import { ref, reactive, watch } from "vue";
|
||||
import { AuthManager } from "../../utils/AuthManager";
|
||||
import { useAlertStore } from "../../components/Alert";
|
||||
import type { NetworkConfigDto } from "../../APIClient";
|
||||
import { BoardStatus, type NetworkConfigDto } from "../../APIClient";
|
||||
import { useRequiredInjection } from "@/utils/Common";
|
||||
import { useBoardManager } from "@/utils/BoardManager";
|
||||
|
||||
// Props 和 Emits
|
||||
interface Props {
|
||||
@@ -184,8 +194,12 @@ const emit = defineEmits<Emits>();
|
||||
// 使用 Alert
|
||||
const alertStore = useAlertStore();
|
||||
|
||||
const boardManager = useRequiredInjection(useBoardManager);
|
||||
|
||||
// 当前步骤
|
||||
const currentStep = ref<'input' | 'pairing' | 'configuring' | 'result'>('input');
|
||||
const currentStep = ref<"input" | "pairing" | "configuring" | "result">(
|
||||
"input",
|
||||
);
|
||||
|
||||
// 表单数据
|
||||
const form = reactive({
|
||||
@@ -231,7 +245,7 @@ function validateForm(): boolean {
|
||||
function resetForm() {
|
||||
form.name = "Board1";
|
||||
errors.name = "";
|
||||
currentStep.value = 'input';
|
||||
currentStep.value = "input";
|
||||
addedBoardId.value = "";
|
||||
networkConfig.value = null;
|
||||
}
|
||||
@@ -255,13 +269,13 @@ async function handleSubmit() {
|
||||
try {
|
||||
// 通过 AuthManager 获取认证的 DataClient
|
||||
const dataClient = AuthManager.createAuthenticatedDataClient();
|
||||
|
||||
|
||||
// 添加板卡到数据库
|
||||
const boardId = await dataClient.addBoard(form.name.trim());
|
||||
|
||||
|
||||
if (boardId) {
|
||||
addedBoardId.value = boardId;
|
||||
currentStep.value = 'pairing';
|
||||
currentStep.value = "pairing";
|
||||
alertStore?.success("板卡添加成功,请开启配对模式");
|
||||
} else {
|
||||
alertStore?.error("板卡添加失败");
|
||||
@@ -281,10 +295,10 @@ async function handleCancelPairing() {
|
||||
try {
|
||||
// 通过 AuthManager 获取认证的 DataClient
|
||||
const dataClient = AuthManager.createAuthenticatedDataClient();
|
||||
|
||||
|
||||
// 删除添加的板卡
|
||||
await dataClient.deleteBoard(addedBoardId.value);
|
||||
|
||||
|
||||
alertStore?.info("已取消添加实验板");
|
||||
emit("update:visible", false);
|
||||
resetForm();
|
||||
@@ -299,7 +313,7 @@ async function handlePairingConfirm() {
|
||||
if (!addedBoardId.value) return;
|
||||
|
||||
isConfiguring.value = true;
|
||||
currentStep.value = 'configuring';
|
||||
currentStep.value = "configuring";
|
||||
|
||||
try {
|
||||
// 通过 AuthManager 获取认证的客户端
|
||||
@@ -308,7 +322,7 @@ async function handlePairingConfirm() {
|
||||
|
||||
// 获取数据库中对应分配的板卡信息
|
||||
const boardInfo = await dataClient.getBoardByID(addedBoardId.value);
|
||||
|
||||
|
||||
if (!boardInfo) {
|
||||
throw new Error("无法获取板卡信息");
|
||||
}
|
||||
@@ -321,21 +335,34 @@ async function handlePairingConfirm() {
|
||||
await netConfigClient.setBoardIP(boardInfo.ipAddr);
|
||||
await netConfigClient.setBoardMAC(boardInfo.macAddr);
|
||||
|
||||
// 更新板卡状态为可用
|
||||
if (
|
||||
(await dataClient.updateBoardStatus(
|
||||
boardInfo.id,
|
||||
BoardStatus.Available,
|
||||
)) != 1
|
||||
) {
|
||||
throw new Error("无法更新板卡状态");
|
||||
}
|
||||
|
||||
if (!(await boardManager.getAllBoards()).success) {
|
||||
alertStore?.error("无法获取板卡列表");
|
||||
}
|
||||
|
||||
// 获取实验板网络信息
|
||||
const networkInfo = await netConfigClient.getNetworkConfig();
|
||||
|
||||
|
||||
if (networkInfo) {
|
||||
networkConfig.value = networkInfo;
|
||||
currentStep.value = 'result';
|
||||
currentStep.value = "result";
|
||||
alertStore?.success("实验板配置成功");
|
||||
} else {
|
||||
throw new Error("无法获取网络配置信息");
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error("配置实验板失败:", error);
|
||||
alertStore?.error("配置实验板失败");
|
||||
|
||||
|
||||
// 配置失败,删除数据库中的板卡信息
|
||||
try {
|
||||
const dataClient = AuthManager.createAuthenticatedDataClient();
|
||||
@@ -343,9 +370,9 @@ async function handlePairingConfirm() {
|
||||
} catch (deleteError) {
|
||||
console.error("删除板卡失败:", deleteError);
|
||||
}
|
||||
|
||||
|
||||
// 返回输入步骤
|
||||
currentStep.value = 'input';
|
||||
currentStep.value = "input";
|
||||
} finally {
|
||||
isConfiguring.value = false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user