ota管理界面
This commit is contained in:
184
src/api/ota.ts
Normal file
184
src/api/ota.ts
Normal file
@@ -0,0 +1,184 @@
|
||||
// OTA 管理 Mock API 层
|
||||
// 后端接口就绪后,把每个函数内部的 setTimeout + 内存数据替换为 request 调用即可,页面无需改动。
|
||||
// 真实接口建议:
|
||||
// GET /iot/ota/machines → getMachineVersionList
|
||||
// GET /iot/ota/firmwares → getFirmwarePackageList
|
||||
// POST /iot/ota/firmwares/upload (multipart) → uploadFirmwarePackage
|
||||
// POST /iot/ota/upgrade → upgradeMachines
|
||||
// POST /iot/ota/rollback → rollbackMachines
|
||||
// DELETE /iot/ota/firmwares/{id} → deleteFirmwarePackage
|
||||
|
||||
export interface MachineVersion {
|
||||
id: number;
|
||||
code: string;
|
||||
name: string;
|
||||
type: string;
|
||||
online: 'ONLINE' | 'OFFLINE';
|
||||
currentVersion: string;
|
||||
previousVersion: string | null;
|
||||
lastUpgradeTime: string | null;
|
||||
status: 'IDLE' | 'UPGRADING' | 'FAILED' | 'SUCCESS';
|
||||
}
|
||||
|
||||
export interface FirmwarePackage {
|
||||
id: number;
|
||||
version: string;
|
||||
fileName: string;
|
||||
fileSize: number;
|
||||
description?: string;
|
||||
uploadTime: string;
|
||||
}
|
||||
|
||||
interface ApiResponse<T> {
|
||||
code: number;
|
||||
msg: string;
|
||||
data: T;
|
||||
}
|
||||
|
||||
function mockResponse<T>(data: T, delay = 300): Promise<ApiResponse<T>> {
|
||||
return new Promise(resolve => {
|
||||
setTimeout(() => resolve({ code: 200, msg: 'ok', data }), delay);
|
||||
});
|
||||
}
|
||||
|
||||
// ====== 模块级内存数据(刷新即重置) ======
|
||||
|
||||
let firmwareIdSeed = 100;
|
||||
const firmwareStore: FirmwarePackage[] = [
|
||||
{
|
||||
id: firmwareIdSeed++,
|
||||
version: 'v1.0.0',
|
||||
fileName: 'firmware-v1.0.0.bin',
|
||||
fileSize: 2_359_296,
|
||||
description: '初始正式版本',
|
||||
uploadTime: '2026-08-12 10:23:45',
|
||||
},
|
||||
{
|
||||
id: firmwareIdSeed++,
|
||||
version: 'v1.1.0',
|
||||
fileName: 'firmware-v1.1.0.zip',
|
||||
fileSize: 3_145_728,
|
||||
description: '新增路径规划算法优化',
|
||||
uploadTime: '2026-09-05 14:50:12',
|
||||
},
|
||||
];
|
||||
|
||||
const machineStore: MachineVersion[] = [
|
||||
{ id: 1, code: 'ROBOT-01', name: '运维机器人 ROBOT-01', type: 'ROBOT', online: 'ONLINE', currentVersion: 'v1.1.0', previousVersion: 'v1.0.0', lastUpgradeTime: '2026-09-06 09:12:33', status: 'SUCCESS' },
|
||||
{ id: 2, code: 'ROBOT-02', name: '运维机器人 ROBOT-02', type: 'ROBOT', online: 'ONLINE', currentVersion: 'v1.1.0', previousVersion: 'v1.0.0', lastUpgradeTime: '2026-09-06 09:15:10', status: 'SUCCESS' },
|
||||
{ id: 3, code: 'ROBOT-03', name: '运维机器人 ROBOT-03', type: 'ROBOT', online: 'OFFLINE', currentVersion: 'v1.0.0', previousVersion: null, lastUpgradeTime: null, status: 'IDLE' },
|
||||
{ id: 4, code: 'DRONE-01', name: '无人机 DRONE-01', type: 'DRONE', online: 'ONLINE', currentVersion: 'v1.1.0', previousVersion: 'v1.0.0', lastUpgradeTime: '2026-09-07 11:08:21', status: 'SUCCESS' },
|
||||
{ id: 5, code: 'DRONE-02', name: '无人机 DRONE-02', type: 'DRONE', online: 'ONLINE', currentVersion: 'v1.0.0', previousVersion: null, lastUpgradeTime: null, status: 'IDLE' },
|
||||
{ id: 6, code: 'CAM-01', name: '摄像头 CAM-01', type: 'CAMERA', online: 'ONLINE', currentVersion: 'v1.1.0', previousVersion: 'v1.0.0', lastUpgradeTime: '2026-09-08 16:42:50', status: 'SUCCESS' },
|
||||
{ id: 7, code: 'CAM-02', name: '摄像头 CAM-02', type: 'CAMERA', online: 'OFFLINE', currentVersion: 'v1.0.0', previousVersion: null, lastUpgradeTime: null, status: 'IDLE' },
|
||||
{ id: 8, code: 'CLEAN-01', name: '清扫机器人 CLEAN-01', type: 'CLEANING_DEVICE', online: 'ONLINE', currentVersion: 'v1.1.0', previousVersion: 'v1.0.0', lastUpgradeTime: '2026-09-09 08:30:00', status: 'FAILED' },
|
||||
{ id: 9, code: 'WEATHER-01', name: '气象站 WEATHER-01', type: 'WEATHER_STATION', online: 'ONLINE', currentVersion: 'v1.0.0', previousVersion: null, lastUpgradeTime: null, status: 'IDLE' },
|
||||
{ id: 10, code: 'INV-A01', name: '逆变器 INV-A01', type: 'INVERTER', online: 'ONLINE', currentVersion: 'v1.1.0', previousVersion: 'v1.0.0', lastUpgradeTime: '2026-09-10 10:15:22', status: 'SUCCESS' },
|
||||
];
|
||||
|
||||
function nowStr(): string {
|
||||
const d = new Date();
|
||||
const pad = (n: number) => String(n).padStart(2, '0');
|
||||
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
|
||||
}
|
||||
|
||||
// ====== 导出 Mock API 函数 ======
|
||||
|
||||
export function getMachineVersionList(): Promise<ApiResponse<MachineVersion[]>> {
|
||||
return mockResponse([...machineStore]);
|
||||
}
|
||||
|
||||
export function getFirmwarePackageList(): Promise<ApiResponse<FirmwarePackage[]>> {
|
||||
return mockResponse([...firmwareStore]);
|
||||
}
|
||||
|
||||
export function uploadFirmwarePackage(params: {
|
||||
file: File;
|
||||
version: string;
|
||||
description?: string;
|
||||
}): Promise<ApiResponse<FirmwarePackage>> {
|
||||
const pkg: FirmwarePackage = {
|
||||
id: firmwareIdSeed++,
|
||||
version: params.version,
|
||||
fileName: params.file.name,
|
||||
fileSize: params.file.size,
|
||||
description: params.description || '',
|
||||
uploadTime: nowStr(),
|
||||
};
|
||||
firmwareStore.unshift(pkg);
|
||||
return mockResponse(pkg, 500);
|
||||
}
|
||||
|
||||
export function deleteFirmwarePackage(id: number): Promise<ApiResponse<null>> {
|
||||
const idx = firmwareStore.findIndex(p => p.id === id);
|
||||
if (idx >= 0) firmwareStore.splice(idx, 1);
|
||||
return mockResponse(null, 200);
|
||||
}
|
||||
|
||||
export function upgradeMachines(params: {
|
||||
machineIds: number[];
|
||||
targetVersion: string;
|
||||
}): Promise<ApiResponse<{ upgraded: number[]; skipped: number[] }>> {
|
||||
const upgraded: number[] = [];
|
||||
const skipped: number[] = [];
|
||||
const time = nowStr();
|
||||
params.machineIds.forEach(id => {
|
||||
const m = machineStore.find(x => x.id === id);
|
||||
if (!m) {
|
||||
skipped.push(id);
|
||||
return;
|
||||
}
|
||||
if (m.currentVersion === params.targetVersion) {
|
||||
// 目标版本与当前版本一致,跳过
|
||||
skipped.push(id);
|
||||
return;
|
||||
}
|
||||
m.previousVersion = m.currentVersion;
|
||||
m.currentVersion = params.targetVersion;
|
||||
m.lastUpgradeTime = time;
|
||||
m.status = 'UPGRADING';
|
||||
upgraded.push(id);
|
||||
});
|
||||
// 模拟升级完成
|
||||
setTimeout(() => {
|
||||
upgraded.forEach(id => {
|
||||
const m = machineStore.find(x => x.id === id);
|
||||
if (m) m.status = 'SUCCESS';
|
||||
});
|
||||
}, 1500);
|
||||
return mockResponse({ upgraded, skipped }, 600);
|
||||
}
|
||||
|
||||
export function rollbackMachines(params: {
|
||||
machineIds: number[];
|
||||
}): Promise<ApiResponse<{ rolledBack: number[]; skipped: number[]; skippedNames: string[] }>> {
|
||||
const rolledBack: number[] = [];
|
||||
const skipped: number[] = [];
|
||||
const skippedNames: string[] = [];
|
||||
const time = nowStr();
|
||||
params.machineIds.forEach(id => {
|
||||
const m = machineStore.find(x => x.id === id);
|
||||
if (!m) {
|
||||
skipped.push(id);
|
||||
return;
|
||||
}
|
||||
if (!m.previousVersion) {
|
||||
skipped.push(id);
|
||||
skippedNames.push(m.name);
|
||||
return;
|
||||
}
|
||||
const prev = m.previousVersion;
|
||||
m.previousVersion = m.currentVersion;
|
||||
m.currentVersion = prev;
|
||||
m.lastUpgradeTime = time;
|
||||
m.status = 'UPGRADING';
|
||||
rolledBack.push(id);
|
||||
});
|
||||
setTimeout(() => {
|
||||
rolledBack.forEach(id => {
|
||||
const m = machineStore.find(x => x.id === id);
|
||||
if (m) m.status = 'SUCCESS';
|
||||
});
|
||||
}, 1500);
|
||||
return mockResponse({ rolledBack, skipped, skippedNames }, 600);
|
||||
}
|
||||
Reference in New Issue
Block a user