diff --git a/src/pages/mixer/mixer.vue b/src/pages/mixer/mixer.vue index 62f1f35..5b96318 100644 --- a/src/pages/mixer/mixer.vue +++ b/src/pages/mixer/mixer.vue @@ -138,6 +138,30 @@ + + + + 🐛 调试日志 + + 清空 + 关闭 + + + + + {{ log.time }} + {{ log.message }} + + + 暂无日志 + + + + + + + {{ showDebugPanel ? '隐藏' : '调试' }} + @@ -186,6 +210,11 @@ const hasRegisteredListener = ref(false) // 数据接收缓冲区(用于处理分包数据) const receiveBuffer = ref('') +// 🔧 调试日志面板 +const showDebugPanel = ref(false) +const debugLogs = ref([]) +const MAX_DEBUG_LOGS = 100 // 最多保留100条日志 + // 通道数据 const channels = ref([ { @@ -425,6 +454,41 @@ const saveChannelData = (channelId) => { } } +// 🔧 调试日志工具函数 +const addDebugLog = (message, type = 'info') => { + const now = new Date() + const time = `${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}:${String(now.getSeconds()).padStart(2, '0')}` + + debugLogs.value.unshift({ + time, + message, + type // 'info', 'success', 'error', 'warn' + }) + + // 限制日志数量 + if (debugLogs.value.length > MAX_DEBUG_LOGS) { + debugLogs.value.pop() + } + + // 同时输出到控制台 + console.log(`[${time}] ${message}`) +} + +// 切换调试面板显示 +const toggleDebugPanel = () => { + showDebugPanel.value = !showDebugPanel.value +} + +// 清空调试日志 +const clearDebugLogs = () => { + debugLogs.value = [] + uni.showToast({ + title: '日志已清空', + icon: 'success', + duration: 1000 + }) +} + // 选择/取消选择通道 const toggleChannelSelection = (channelId) => { const index = selectedChannels.value.indexOf(channelId) @@ -558,13 +622,19 @@ const handleChannelStop = (channelId) => { const startChannel = (channelId) => { const channel = channels.value.find(c => c.id === channelId) if (channel) { - // ✅ 不立即修改状态,只发送命令 - // 等待蓝牙设备反馈后,通过 onBLECharacteristicValueChange 监听器更新状态 + // 🔧 乐观更新:立即修改本地状态,提升用户体验 + // 如果设备反馈失败,会通过监听器自动恢复正确状态 + const oldStatus = channel.status + channel.status = 'running' + console.log(`🚀 乐观更新: ${channel.name} ${oldStatus} -> running`) + addDebugLog(`🚀 ${channel.name} 启动: ${oldStatus} -> running`, 'info') + + // 发送蓝牙命令 sendBluetoothCommand('start', channelId) // 显示发送中提示 uni.showToast({ - title: `${channel.name} Start Command Sent`, + title: `${channel.name} Starting...`, icon: 'none', duration: 1500 }) @@ -575,13 +645,22 @@ const startChannel = (channelId) => { const stopChannel = (channelId) => { const channel = channels.value.find(c => c.id === channelId) if (channel) { - // ✅ 不立即修改状态,只发送命令 - // 等待蓝牙设备反馈后,通过 onBLECharacteristicValueChange 监听器更新状态 + // 🔧 乐观更新:立即修改本地状态,提升用户体验 + // 如果设备反馈失败,会通过监听器自动恢复正确状态 + const oldStatus = channel.status + channel.status = 'stopped' + console.log(`🛑 乐观更新: ${channel.name} ${oldStatus} -> stopped`) + addDebugLog(`🛑 ${channel.name} 停止: ${oldStatus} -> stopped`, 'info') + + // 停止定时器 + stopTimer(channelId) + + // 发送蓝牙命令 sendBluetoothCommand('stop', channelId) // 显示发送中提示 uni.showToast({ - title: `${channel.name} Stop Command Sent`, + title: `${channel.name} Stopping...`, icon: 'none', duration: 1500 }) @@ -978,12 +1057,16 @@ const readMotorAllProperties = (showConfirm = false) => { const readCharId = characteristicIds.value.allProps || '0xFF06' const executeRead = () => { + console.log(`📥 [定时查询] 开始读取所有属性,特征值: ${readCharId}`) + addDebugLog(`📥 READ请求: ${readCharId}`, 'info') + uni.readBLECharacteristicValue({ deviceId: deviceId.value, serviceId: serviceId.value, characteristicId: readCharId, success: (res) => { - console.log(`✅ Successfully read all properties from ${readCharId}`) + console.log(`✅ [定时查询] 读取成功,特征值: ${readCharId}`) + addDebugLog(`✅ READ成功: ${readCharId}`, 'success') if (showConfirm) { uni.showToast({ title: 'Read Success', @@ -993,7 +1076,17 @@ const readMotorAllProperties = (showConfirm = false) => { } }, fail: (err) => { - console.error(`❌ Failed to read all properties from ${readCharId}:`, err) + console.error(`❌ [定时查询] 读取失败,特征值: ${readCharId}`, err) + console.error(` 错误码: ${err.errCode}, 错误信息: ${err.errMsg}`) + console.error(` ⚠️ 可能原因: 设备特征值不支持 READ 操作,只支持 WRITE/NOTIFY`) + addDebugLog(`❌ READ失败: ${readCharId} - ${err.errMsg}`, 'error') + addDebugLog(`⚠️ 设备不支持READ,只支持WRITE/NOTIFY`, 'warn') + + // 🔧 即使静默模式,也在控制台输出详细错误,方便调试 + if (!showConfirm) { + console.warn(`💡 提示: 定时查询失败,请检查设备特征值是否支持READ属性`) + } + if (showConfirm) { // 构建错误信息 const errorInfo = `Device ID: ${deviceId.value} @@ -1242,14 +1335,17 @@ const initBluetoothCommunication = () => { uni.onBLECharacteristicValueChange((res) => { console.log('📡 收到蓝牙数据:', res) console.log(' 特征值UUID:', res.characteristicId) + addDebugLog(`📡 收到BLE数据: ${res.characteristicId.substring(0, 8)}...`, 'info') // 根据特征值ID处理数据 const dataView = new Uint8Array(res.value) const dataLength = dataView.length const charId = res.characteristicId + const hexData = Array.from(dataView).map(b => '0x' + b.toString(16).toUpperCase().padStart(2, '0')).join(' ') console.log(' 数据长度:', dataLength, 'bytes') - console.log(' HEX:', Array.from(dataView).map(b => '0x' + b.toString(16).toUpperCase().padStart(2, '0')).join(' ')) + console.log(' HEX:', hexData) + addDebugLog(` 数据[${dataLength}bytes]: ${hexData}`, 'info') // 🔧 灵活匹配特征值ID(支持16位和128位UUID) const matchCharacteristic = (target) => { @@ -1264,6 +1360,7 @@ const initBluetoothCommunication = () => { if (matchCharacteristic(characteristicIds.value.status)) { // 电机状态 - 根据设备反馈更新本地状态 console.log('📊 解析为电机状态数据') + addDebugLog('📊 解析: 电机状态数据', 'success') channels.value.forEach((channel, index) => { const status = dataView[index] const statusMap = { 0: 'stopped', 1: 'stopped', 2: 'timing', 3: 'running' } @@ -1272,6 +1369,7 @@ const initBluetoothCommunication = () => { // 状态变化时才处理定时器 if (channel.status !== newStatus) { console.log(`${channel.name} 状态变化: ${channel.status} -> ${newStatus}`) + addDebugLog(` ${channel.name}: ${channel.status} -> ${newStatus}`, 'warn') // 如果从非定时状态变为定时状态,启动定时器 if (newStatus === 'timing' && channel.timerMinutes > 0) { @@ -1290,12 +1388,14 @@ const initBluetoothCommunication = () => { } else if (matchCharacteristic(characteristicIds.value.speed)) { // 电机速度 - 根据设备反馈更新本地速度 console.log('📊 解析为电机速度数据') + addDebugLog('📊 解析: 电机速度数据', 'success') channels.value.forEach((channel, index) => { const speed = dataView[index] // 只在速度真正变化时才更新和保存 if (channel.runValue !== speed) { console.log(`${channel.name} 速度变化: ${channel.runValue} -> ${speed}`) + addDebugLog(` ${channel.name}: 速度 ${channel.runValue} -> ${speed}`, 'warn') channel.runValue = speed // 保存到本地存储 saveChannelData(channel.id) @@ -1304,6 +1404,7 @@ const initBluetoothCommunication = () => { } else if (matchCharacteristic(characteristicIds.value.direction)) { // 运行方向 console.log('📊 解析为运行方向数据') + addDebugLog('📊 解析: 运行方向数据', 'success') channels.value.forEach((channel, index) => { const direction = dataView[index] console.log(`${channel.name} 方向:`, direction === 0 ? '正转' : '反转') @@ -1311,26 +1412,33 @@ const initBluetoothCommunication = () => { } else { // 未知特征值,使用智能判断 console.log('⚠️ 未知特征值,使用智能判断') + addDebugLog('⚠️ 未知特征值,智能判断中...', 'warn') parseMotorStatusOrSpeed(dataView) } } else if (dataLength === 12) { // 12字节数据,根据特征值ID区分 if (matchCharacteristic(characteristicIds.value.timer)) { console.log('📊 解析为定时设置数据') + addDebugLog('📊 解析: 定时设置数据', 'success') parseMotorTimer(dataView, 'setup') // 定时设置 } else if (matchCharacteristic(characteristicIds.value.remaining)) { console.log('📊 解析为剩余时间数据') + addDebugLog('📊 解析: 剩余时间数据', 'success') parseMotorTimer(dataView, 'remaining') // 剩余时间 } else { console.log('⚠️ 未知特征值,使用智能判断') + addDebugLog('⚠️ 未知特征值(12字节),智能判断中...', 'warn') parseMotorTimer(dataView, 'unknown') // 未知,智能判断 } } else if (dataLength === 42) { // 42字节数据 - 所有属性 console.log('📊 解析为所有属性数据') + addDebugLog('📊 解析: 完整属性数据(42字节)', 'success') parseMotorAllProperties(dataView) } else { // 其他长度数据,使用通用处理 + console.log('⚠️ 未知数据长度:', dataLength) + addDebugLog(`⚠️ 未知数据长度: ${dataLength}bytes`, 'error') handleBluetoothData(res.value) } }) @@ -1581,9 +1689,14 @@ const stopDataQuery = () => { // 静默模式:不显示确认框 const queryAllChannelsData = () => { if (!connected.value || isQuerying.value) { + const reason = !connected.value ? '蓝牙未连接' : '正在查询中' + console.log('⏭️ [定时查询] 跳过本次查询,原因:', reason) + addDebugLog(`⏭️ 跳过查询: ${reason}`, 'warn') return } + console.log('🔄 [定时查询] 开始查询所有通道数据...') + addDebugLog('🔄 开始定时查询设备状态', 'info') isQuerying.value = true // 优先查询完整属性(0xFF06),包含所有信息(不显示确认框) @@ -1599,6 +1712,8 @@ const queryAllChannelsData = () => { // 查询完成后重置标志 setTimeout(() => { isQuerying.value = false + console.log('✅ [定时查询] 查询周期结束') + addDebugLog('✅ 查询周期结束', 'success') }, 1000) } @@ -2325,4 +2440,138 @@ page { } } +// 🔧 调试日志面板样式 +.debug-panel { + position: fixed; + bottom: 0; + left: 0; + right: 0; + height: 300rpx; + background: rgba(0, 0, 0, 0.95); + backdrop-filter: blur(10rpx); + z-index: 9999; + display: flex; + flex-direction: column; + border-top: 2rpx solid rgba(255, 255, 255, 0.1); + box-shadow: 0 -8rpx 32rpx rgba(0, 0, 0, 0.3); +} + +.debug-header { + display: flex; + justify-content: space-between; + align-items: center; + padding: 16rpx 24rpx; + background: rgba(255, 255, 255, 0.05); + border-bottom: 2rpx solid rgba(255, 255, 255, 0.1); + flex-shrink: 0; +} + +.debug-title { + font-size: 28rpx; + font-weight: 700; + color: #fff; +} + +.debug-actions { + display: flex; + gap: 24rpx; +} + +.debug-action { + font-size: 24rpx; + color: #667eea; + padding: 8rpx 16rpx; + border-radius: 8rpx; + background: rgba(102, 126, 234, 0.2); + font-weight: 600; + cursor: pointer; + transition: all 0.2s; + + &:active { + background: rgba(102, 126, 234, 0.4); + transform: scale(0.95); + } +} + +.debug-content { + flex: 1; + overflow-y: auto; + padding: 12rpx 24rpx; +} + +.debug-log-item { + display: flex; + flex-direction: column; + padding: 12rpx 0; + border-bottom: 1rpx solid rgba(255, 255, 255, 0.05); + + &.info .debug-message { + color: #60a5fa; + } + + &.success .debug-message { + color: #4ade80; + } + + &.warn .debug-message { + color: #fbbf24; + } + + &.error .debug-message { + color: #f87171; + } +} + +.debug-time { + font-size: 20rpx; + color: #9ca3af; + margin-bottom: 4rpx; + font-family: monospace; +} + +.debug-message { + font-size: 24rpx; + line-height: 1.5; + word-break: break-all; + font-family: monospace; +} + +.debug-empty { + display: flex; + align-items: center; + justify-content: center; + height: 200rpx; + color: #6b7280; + font-size: 24rpx; +} + +.debug-toggle-btn { + position: fixed; + bottom: 180rpx; + right: 30rpx; + width: 100rpx; + height: 100rpx; + border-radius: 50%; + background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); + box-shadow: 0 8rpx 24rpx rgba(102, 126, 234, 0.4); + display: flex; + align-items: center; + justify-content: center; + z-index: 9998; + cursor: pointer; + transition: all 0.3s; + + &:active { + transform: scale(0.9); + box-shadow: 0 4rpx 12rpx rgba(102, 126, 234, 0.5); + } +} + +.debug-btn-text { + font-size: 24rpx; + font-weight: 700; + color: #fff; + letter-spacing: 2rpx; +} +