OTA管理界面
This commit is contained in:
286
src/api/ota.ts
286
src/api/ota.ts
@@ -1,184 +1,148 @@
|
||||
// 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
|
||||
// OTA 管理 API 层
|
||||
// 后端接口:
|
||||
// POST /iot/deviceOta/firmware/save → uploadFirmwarePackage
|
||||
// GET /iot/deviceOta/firmware/list → getFirmwarePackageList
|
||||
// GET /iot/deviceOta/firmware/detail → getFirmwareDetail (deviceType)
|
||||
// POST /iot/deviceOta/firmware/upgrade → operateFirmware (upgrade/rollback/reboot)
|
||||
// POST /iot/deviceOta/firmware/delete → deleteFirmwarePackage (id)
|
||||
// GET /iot/deviceOta/record/detail → getRecordDetail (id)
|
||||
// GET /iot/deviceOta/record/selectByDeviceId → getRecordByDeviceId (deviceId)
|
||||
// POST /iot/deviceOta/record/select → getRecords (ids)
|
||||
|
||||
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';
|
||||
}
|
||||
import request from './request';
|
||||
|
||||
// ====== 类型定义 ======
|
||||
|
||||
export type OtaOperateType = 'upload' | 'upgrade' | 'rollback' | 'reboot';
|
||||
|
||||
export interface FirmwarePackage {
|
||||
id: number;
|
||||
version: string;
|
||||
fileName: string;
|
||||
fileSize: number;
|
||||
description?: string;
|
||||
uploadTime: string;
|
||||
id: number | string;
|
||||
version: string; // 版本号
|
||||
fileName: string; // 文件名
|
||||
fileType?: string; // 服务端文件类型
|
||||
md5?: string; // md5 校验
|
||||
fileSize?: number; // 文件大小(bytes)
|
||||
description?: string; // 描述
|
||||
uploadTime?: string; // 上传时间
|
||||
deviceType?: string; // 设备类型
|
||||
}
|
||||
|
||||
interface ApiResponse<T> {
|
||||
code: number;
|
||||
msg: string;
|
||||
data: T;
|
||||
export interface MachineVersion {
|
||||
id: number | string;
|
||||
code: string; // 设备编号/SN
|
||||
name: string; // 设备名称
|
||||
type: string; // 设备类型
|
||||
online: 'ONLINE' | 'OFFLINE';
|
||||
currentVersion: string; // 当前固件版本
|
||||
previousVersion: string | null;
|
||||
lastUpgradeTime: string | null;
|
||||
status: 'IDLE' | 'UPGRADING' | 'FAILED' | 'SUCCESS';
|
||||
}
|
||||
|
||||
function mockResponse<T>(data: T, delay = 300): Promise<ApiResponse<T>> {
|
||||
return new Promise(resolve => {
|
||||
setTimeout(() => resolve({ code: 200, msg: 'ok', data }), delay);
|
||||
});
|
||||
export interface OtaRecord {
|
||||
id: number | string;
|
||||
deviceId?: string;
|
||||
deviceName?: string;
|
||||
firmwareId?: number | string;
|
||||
operateType?: OtaOperateType | string;
|
||||
fromVersion?: string;
|
||||
toVersion?: string;
|
||||
status?: string;
|
||||
createTime?: string;
|
||||
finishTime?: string;
|
||||
message?: string;
|
||||
}
|
||||
|
||||
// ====== 模块级内存数据(刷新即重置) ======
|
||||
|
||||
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())}`;
|
||||
interface ApiResponse<T = any> {
|
||||
code: number;
|
||||
msg: string;
|
||||
data: T;
|
||||
rows?: T[];
|
||||
total?: number;
|
||||
}
|
||||
|
||||
// ====== 导出 Mock API 函数 ======
|
||||
// ====== 固件包相关 ======
|
||||
|
||||
export function getMachineVersionList(): Promise<ApiResponse<MachineVersion[]>> {
|
||||
return mockResponse([...machineStore]);
|
||||
/** 上传固件包 (multipart/form-data),直接接收已构建好的 FormData */
|
||||
export function uploadFirmwarePackage(formData: FormData) {
|
||||
return request({
|
||||
url: '/iot/deviceOta/firmware/save',
|
||||
method: 'post',
|
||||
data: formData,
|
||||
headers: { 'Content-Type': 'multipart/form-data' },
|
||||
}) as Promise<ApiResponse<FirmwarePackage>>;
|
||||
}
|
||||
|
||||
export function getFirmwarePackageList(): Promise<ApiResponse<FirmwarePackage[]>> {
|
||||
return mockResponse([...firmwareStore]);
|
||||
/** 获取固件包列表 */
|
||||
export function getFirmwarePackageList(params?: { deviceType?: string }) {
|
||||
return request({
|
||||
url: '/iot/deviceOta/firmware/list',
|
||||
method: 'post',
|
||||
params,
|
||||
}) as Promise<ApiResponse<FirmwarePackage[]>>;
|
||||
}
|
||||
|
||||
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 getFirmwareDetail(deviceType: string) {
|
||||
return request({
|
||||
url: '/iot/deviceOta/firmware/detail',
|
||||
method: 'get',
|
||||
params: { deviceType },
|
||||
}) as Promise<ApiResponse<FirmwarePackage>>;
|
||||
}
|
||||
|
||||
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 deleteFirmwarePackage(ids: number[] | string[]) {
|
||||
return request({
|
||||
url: '/iot/deviceOta/firmware/delete',
|
||||
method: 'post',
|
||||
data: ids
|
||||
}) as Promise<ApiResponse<null>>;
|
||||
}
|
||||
|
||||
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);
|
||||
// ====== 设备升级操作 ======
|
||||
|
||||
/**
|
||||
* 批量操作设备固件(升级 / 回退 / 重启)
|
||||
* operateType: upgrade-升级, rollback-回退, reboot-重启
|
||||
*/
|
||||
export function operateFirmware(params: {
|
||||
operateType: OtaOperateType;
|
||||
deviceIds: (string | number)[];
|
||||
firmwareId?: string | number;
|
||||
}) {
|
||||
return request({
|
||||
url: '/iot/deviceOta/firmware/upgrade',
|
||||
method: 'post',
|
||||
data: params,
|
||||
}) as Promise<ApiResponse<{ upgraded?: (string | number)[]; skipped?: (string | number)[] }>>;
|
||||
}
|
||||
|
||||
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);
|
||||
// ====== 操作记录相关 ======
|
||||
|
||||
/** 根据记录 id 查询操作记录详情 */
|
||||
export function getRecordDetail(id: number | string) {
|
||||
return request({
|
||||
url: '/iot/deviceOta/record/detail',
|
||||
method: 'get',
|
||||
params: { id },
|
||||
}) as Promise<ApiResponse<OtaRecord>>;
|
||||
}
|
||||
|
||||
/** 根据设备 id 查询升级记录 */
|
||||
export function getRecordByDeviceId(deviceId: string | number) {
|
||||
return request({
|
||||
url: '/iot/deviceOta/record/selectListByDeviceId',
|
||||
method: 'get',
|
||||
params: { deviceId },
|
||||
}) as Promise<ApiResponse<OtaRecord[]>>;
|
||||
}
|
||||
|
||||
/** 根据记录 id 列表批量查询操作记录 */
|
||||
export function getRecords(ids: (string | number)[]) {
|
||||
return request({
|
||||
url: '/iot/deviceOta/record/select',
|
||||
method: 'post',
|
||||
data: ids,
|
||||
}) as Promise<ApiResponse<OtaRecord[]>>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user