smart-home/smart-home-app/utils/acControlHelper.js
2026-02-26 09:16:34 +08:00

75 lines
2.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 空调控制助手 - 发送SDK转换的脉冲数组
import { getPowerIRByRemoteId } from '@/utils/tiqiaa.js'
/**
* 发送空调命令使用SDK转换的脉冲数组
* @param {string} esp32IP - ESP32设备IP
* @param {string} remoteId - 遥控器ID
* @param {string} command - 命令如power_on
* @param {number} timeout - 超时时间
*/
export async function sendACCommandWithPulses(esp32IP, remoteId, command = 'power_on', timeout = 8000) {
try {
console.log('🚀 开始发送空调命令:', { esp32IP, remoteId, command })
// 1. 使用SDK获取脉冲数组
const ir = await getPowerIRByRemoteId(remoteId)
console.log('✅ SDK转换完成:', { freq: ir.freq, pulsesCount: ir.timings.length })
// 2. 发送脉冲数组到ESP32的/api/ir/raw接口
const response = await uni.request({
url: `http://${esp32IP}:80/api/ir/raw`,
method: 'POST',
data: {
freq: ir.freq,
timings: ir.timings,
channel: 0
},
timeout: timeout
})
console.log('✅ 空调命令发送成功:', response.data)
return response.data
} catch (error) {
console.error('❌ 发送空调命令失败:', error)
throw error
}
}
/**
* 测试空调遥控器发送到test-remote接口但包含脉冲数组
* @param {string} esp32IP - ESP32设备IP
* @param {string} remoteId - 遥控器ID
* @param {string} command - 命令
*/
export async function testACRemoteWithPulses(esp32IP, remoteId, command = 'power_on') {
try {
console.log('🧪 开始测试空调遥控器:', { esp32IP, remoteId, command })
// 1. 使用SDK获取脉冲数组
const ir = await getPowerIRByRemoteId(remoteId)
console.log('✅ SDK转换完成:', { freq: ir.freq, pulsesCount: ir.timings.length })
// 2. 发送到test-remote接口但包含脉冲数组
const response = await uni.request({
url: `http://${esp32IP}:80/api/ac/test-remote`,
method: 'POST',
data: {
remoteId: remoteId,
command: command,
freq: ir.freq,
pulses: ir.timings // 关键发送SDK转换的脉冲数组
},
timeout: 8000
})
console.log('✅ 遥控器测试成功:', response.data)
return response.data
} catch (error) {
console.error('❌ 遥控器测试失败:', error)
throw error
}
}