/** * 设备管理相关API */ import { http, Method } from '@/utils/request.js'; import storage from "@/utils/storage.js"; import api from "@/config/api.js"; import config from "@/config/config.js"; // const request = http.request; const userInfo = storage.getUserInfo() /** * 登录wvp获取token */ export function loginWvp() { return uni.request({ url: `/wvp/api/user/login?username=${config.wvpUsername}&password=${config.wvpPassword}`, method: Method.GET, needToken: false, }); } /** * 获取通道列表 */ export async function getChannels(deviceId) { try { // 登录获取token const loginRes = await loginWvp(); console.log("WVP登录结果:", loginRes); if (loginRes[0]) { // 处理错误 console.error('登录失败', loginRes[0]); throw new Error('登录失败'); } const response = loginRes[1].data; if (response.code === 0) { console.log("WVP登录成功"); storage.setWvpAccessToken(response.data.accessToken); // 查询通道列表 const channelsRes = await uni.request({ url: `/wvp/api/device/query/devices/${deviceId}/channels`, method: Method.GET, data: { page: 1, count: 10, online: true, }, header: { 'Access-Token': `${storage.getWvpAccessToken()}`, }, }); console.log("获取通道结果:", channelsRes); return channelsRes; } else { console.error("WVP登录失败", loginRes); throw new Error("WVP登录失败"); } } catch (error) { console.error("获取通道失败", error); throw error; } } /** * 开始点播 */ export async function playStart(deviceId, channelId) { return await uni.request({ url: `/wvp/api/play/start/${deviceId}/${channelId}`, method: Method.GET, header: { 'Access-Token': `${storage.getWvpAccessToken()}`, }, }) } /** * 暂停点播 */ export async function pause(deviceId, channelId) { return await uni.request({ url: `/wvp/api/play/stop/${deviceId}/${channelId}`, method: Method.GET, header: { 'Access-Token': `${storage.getWvpAccessToken()}`, }, }) } /** * 获取设备概览数据 * @param {string} fieldId - 地块ID,可选 * @returns {Promise} 设备概览数据 */ export function fetchDeviceOverview(fieldId) { const params = {}; console.log("userInfo请求:",userInfo); if (userInfo.userid) params.userId = userInfo.userid; if (fieldId) params.fieldId = fieldId; return http.request({ url: '/base/device/overview', method: Method.POST, needToken: true, data: params }); } /** * 根据设备类型获取设备列表 * @param {string} type - 设备类型(monitor-监控设备,sensor-采集设备,control-控制设备,irrigation-灌溉设备,tractor-农机设备) * @param {Object} params - 查询参数 * @returns {Promise} 设备列表 */ export function fetchDevicesByType(params) { if (userInfo.userid) params.userId = userInfo.userid; return http.request({ url: `/base/device/typeList`, method: Method.POST, needToken: true, data: params }); } /** * 获取所有设备列表 * @param {Object} params - 查询参数 * @param {number} params.pageNum - 页码 * @param {number} params.pageSize - 每页数量 * @param {string} params.deviceName - 设备名称,可选 * @param {string} params.deviceTypeId - 设备类型ID,可选 * @param {number} params.status - 设备状态,可选 * @returns {Promise} 设备列表 */ export function fetchDeviceList(params = {}) { return http.request({ url: 'uniapp/device/list', method: Method.GET, needToken: true, params: params }); } /** * 获取设备详情 * @param {number} id - 设备ID * @returns {Promise} 设备详情 */ export function getDeviceDetail(id) { return http.request({ url: `uniapp/device/${id}`, needToken: true, method: Method.GET }); } /** * 获取设备采集器详情(包含气象/土壤数据和告警信息) * @param {number} id - 设备编码 * @param {string} code - 设备编码,可选,用于判断设备类型 * @returns {Promise} 设备详情数据 */ export function getDeviceCollectorDetail(deviceId, code) { return http.request({ url: `/base/device/collector/detail/${deviceId}`, method: Method.GET, needToken: true, params: code ? { code } : {} }); } /** * 添加设备 * @param {Object} data - 设备信息 * @returns {Promise} 添加结果 */ export function addDevice(data) { return http.request({ url: 'uniapp/device', method: Method.POST, needToken: true, data: data }); } /** * 更新设备信息 * @param {Object} data - 设备信息 * @returns {Promise} 更新结果 */ export function updateDevice(data) { return http.request({ url: 'uniapp/device', method: Method.PUT, needToken: true, data: data }); } /** * 删除设备 * @param {number|number[]} ids - 设备ID或ID数组 * @returns {Promise} 删除结果 */ export function deleteDevice(ids) { const idStr = Array.isArray(ids) ? ids.join(',') : ids; return http.request({ url: `uniapp/device/${idStr}`, needToken: true, method: Method.DELETE }); } /** * 根据用户ID获取关联的设备列表 * @param {Object} params - 查询参数 * @returns {Promise} 设备列表 */ export function fetchUserDeviceList(params = {}) { const user = storage.getUserInfo(); return http.request({ url: 'uniapp/device/user/list', method: Method.GET, params: { ...params, userId: user.userId }, needToken: true, }); } /** * 根据地块ID获取设备列表 * @param {string} fieldId - 地块ID * @param {Object} params - 其他查询参数 * @returns {Promise} 设备列表 */ export function fetchFieldDeviceList(fieldId, params = {}) { return http.request({ url: 'uniapp/device/list', method: Method.GET, params: { ...params, fieldId: fieldId }, needToken: true }); } /** * 获取设备类型统计数据 * @param {string} fieldId - 地块ID,可选 * @returns {Promise} 设备类型统计数据 */ export function fetchDeviceTypeStats(fieldId) { const params = {}; if (fieldId) params.fieldId = fieldId; return http.request({ url: 'uniapp/device/stats/type', method: Method.GET, params: params, needToken: true, }); }