diff --git a/main/manager-web/public/static/wakenet_model/mn5q8_cn/_MODEL_INFO_ b/main/manager-web/public/static/wakenet_model/mn5q8_cn/_MODEL_INFO_ new file mode 100644 index 000000000..fe11de6cc --- /dev/null +++ b/main/manager-web/public/static/wakenet_model/mn5q8_cn/_MODEL_INFO_ @@ -0,0 +1,2 @@ +# (neural network type)_(model data version)_(lable1_detection windown length_threshold for 90%_threshold for 95%)_(lable2 ...)_... +MN5Q8_v2_chinese_8_0.9_0.90 diff --git a/main/manager-web/public/static/wakenet_model/mn5q8_cn/mn5q8_data b/main/manager-web/public/static/wakenet_model/mn5q8_cn/mn5q8_data new file mode 100644 index 000000000..b5d272c85 Binary files /dev/null and b/main/manager-web/public/static/wakenet_model/mn5q8_cn/mn5q8_data differ diff --git a/main/manager-web/public/static/wakenet_model/mn5q8_cn/mn5q8_index b/main/manager-web/public/static/wakenet_model/mn5q8_cn/mn5q8_index new file mode 100644 index 000000000..d55526a54 Binary files /dev/null and b/main/manager-web/public/static/wakenet_model/mn5q8_cn/mn5q8_index differ diff --git a/main/manager-web/src/components/custom-theme/CustomThemeDesign.vue b/main/manager-web/src/components/custom-theme/CustomThemeDesign.vue index 823fad53c..c32797a19 100644 --- a/main/manager-web/src/components/custom-theme/CustomThemeDesign.vue +++ b/main/manager-web/src/components/custom-theme/CustomThemeDesign.vue @@ -26,8 +26,10 @@
{ switch (tabId) { case 'wakeword': + if (this.localValue.wakeword === 'custom') { + return !!(this.localValue.customWakeword && this.localValue.customWakeword.chinese && this.localValue.customWakeword.pinyin); + } return !!this.localValue.wakeword; case 'font': return this.localValue.font.preset || this.localValue.font.custom.file; @@ -143,6 +148,29 @@ export default { }, handlePrev() { this.$emit('prev'); + }, + handleWakewordInput(value) { + // 更新唤醒词值 + const newValue = { + ...this.localValue, + wakeword: value + }; + this.$emit('input', newValue); + }, + handleCustomWakewordChange(data) { + // 创建新的配置对象,确保响应式更新 + const newValue = { + ...this.localValue, + wakeword: data.wakeword + }; + // 保存自定义唤醒词数据 + if (data.customWakeword) { + newValue.customWakeword = data.customWakeword; + } else { + delete newValue.customWakeword; + } + // 触发更新 + this.$emit('input', newValue); } } }; diff --git a/main/manager-web/src/components/custom-theme/CustomThemeGenerateModal.vue b/main/manager-web/src/components/custom-theme/CustomThemeGenerateModal.vue index 1c8c1e6a5..176b4c0d2 100644 --- a/main/manager-web/src/components/custom-theme/CustomThemeGenerateModal.vue +++ b/main/manager-web/src/components/custom-theme/CustomThemeGenerateModal.vue @@ -720,7 +720,8 @@ export default { for (const token of tokens) { const headerVariants = [ { Authorization: `Bearer ${token}` }, - { Authorization: token } + { Authorization: token }, + { token } ]; for (const headers of headerVariants) { const res = await fetch(`${base}/api/messages/publish`, { diff --git a/main/manager-web/src/components/custom-theme/tabs/CustomThemeBackgroundConfig.vue b/main/manager-web/src/components/custom-theme/tabs/CustomThemeBackgroundConfig.vue index f66f418c6..6824c629d 100644 --- a/main/manager-web/src/components/custom-theme/tabs/CustomThemeBackgroundConfig.vue +++ b/main/manager-web/src/components/custom-theme/tabs/CustomThemeBackgroundConfig.vue @@ -59,9 +59,10 @@
- +
{{ $t('device.customThemeBackgroundImageUpload') }}
- {{ fileName(modelValue.light.backgroundImage) }} - - {{ $t('device.customThemeRemoveImage') }} - +
+
+ 背景预览 +
+ + {{ $t('device.customThemeBackgroundImageUpload') }} + + + {{ $t('device.customThemeRemoveImage') }} + +
+ +
@@ -134,9 +166,10 @@
- +
{{ $t('device.customThemeBackgroundImageUpload') }}
- {{ fileName(modelValue.dark.backgroundImage) }} - - {{ $t('device.customThemeRemoveImage') }} - +
+
+ 背景预览 +
+ + {{ $t('device.customThemeBackgroundImageUpload') }} + + + {{ $t('device.customThemeRemoveImage') }} + +
+ +
@@ -337,20 +401,105 @@ export default { return false; } const url = URL.createObjectURL(f); + // 同时保存 File 对象和 URL,以便后续处理 if (mode === 'light') { - this.modelValue = { ...this.modelValue, light: { ...this.modelValue.light, backgroundImage: url, backgroundType: 'image' } }; + this.modelValue = { + ...this.modelValue, + light: { + ...this.modelValue.light, + backgroundImage: url, + backgroundImageFile: f, // 保存原始 File 对象 + backgroundType: 'image' + } + }; } else { - this.modelValue = { ...this.modelValue, dark: { ...this.modelValue.dark, backgroundImage: url, backgroundType: 'image' } }; + this.modelValue = { + ...this.modelValue, + dark: { + ...this.modelValue.dark, + backgroundImage: url, + backgroundImageFile: f, // 保存原始 File 对象 + backgroundType: 'image' + } + }; } return false; }, clearBg(mode) { if (mode === 'light') { - this.modelValue = { ...this.modelValue, light: { ...this.modelValue.light, backgroundImage: '', backgroundType: 'color' } }; + // 释放 blob URL + if (this.modelValue.light.backgroundImage && this.modelValue.light.backgroundImage.startsWith('blob:')) { + URL.revokeObjectURL(this.modelValue.light.backgroundImage) + } + this.modelValue = { + ...this.modelValue, + light: { + ...this.modelValue.light, + backgroundImage: '', + backgroundImageFile: null, // 清除 File 对象 + backgroundType: 'color' + } + }; } else { - this.modelValue = { ...this.modelValue, dark: { ...this.modelValue.dark, backgroundImage: '', backgroundType: 'color' } }; + // 释放 blob URL + if (this.modelValue.dark.backgroundImage && this.modelValue.dark.backgroundImage.startsWith('blob:')) { + URL.revokeObjectURL(this.modelValue.dark.backgroundImage) + } + this.modelValue = { + ...this.modelValue, + dark: { + ...this.modelValue.dark, + backgroundImage: '', + backgroundImageFile: null, // 清除 File 对象 + backgroundType: 'color' + } + }; } }, + triggerReupload(mode) { + // 触发文件选择 + const inputRef = mode === 'light' ? this.$refs.lightFileInput : this.$refs.darkFileInput + if (inputRef) { + inputRef.click() + } + }, + handleFileInputChange(event, mode) { + const file = event.target.files?.[0] + if (file && /\.(png|jpg|jpeg)$/i.test(file.name)) { + // 释放旧的 blob URL + if (mode === 'light' && this.modelValue.light.backgroundImage && this.modelValue.light.backgroundImage.startsWith('blob:')) { + URL.revokeObjectURL(this.modelValue.light.backgroundImage) + } else if (mode === 'dark' && this.modelValue.dark.backgroundImage && this.modelValue.dark.backgroundImage.startsWith('blob:')) { + URL.revokeObjectURL(this.modelValue.dark.backgroundImage) + } + + // 创建新的 blob URL 并保存 File 对象 + const url = URL.createObjectURL(file) + if (mode === 'light') { + this.modelValue = { + ...this.modelValue, + light: { + ...this.modelValue.light, + backgroundImage: url, + backgroundImageFile: file, + backgroundType: 'image' + } + } + } else { + this.modelValue = { + ...this.modelValue, + dark: { + ...this.modelValue.dark, + backgroundImage: url, + backgroundImageFile: file, + backgroundType: 'image' + } + } + } + } + // 清空 input,以便可以再次选择同一文件 + event.target.value = '' + }, fileName(url) { if (!url) return ''; const parts = url.split('/'); @@ -439,14 +588,15 @@ export default { } .upload-center { display: flex; - flex: 1; + flex: 0 0 auto; justify-content: center; align-items: center; - min-width: 120px; - max-width: 100%; + width: 360px; + min-width: 360px; + max-width: 360px; overflow: visible; box-sizing: border-box; - padding: 0 10px; + padding: 0; } .color-item { display: flex; @@ -487,14 +637,15 @@ export default { border: 2px dashed #909399; border-radius: 4px; padding: 2px 0; - width: 100px; - min-width: 100px; - max-width: 100px; + width: 360px; + min-width: 360px; + max-width: 360px; + height: 180px; + min-height: 180px; display: inline-flex; flex-direction: column; align-items: center; justify-content: center; - height: 100%; box-sizing: border-box; flex-shrink: 0; } @@ -509,15 +660,62 @@ export default { flex-direction: column; justify-content: center; gap: 4px; - width: 120px; - min-width: 120px; - max-width: 120px; + width: 360px; + min-width: 360px; + max-width: 360px; flex-shrink: 0; flex-grow: 0; - align-items: flex-start; + align-items: center; box-sizing: border-box; overflow: visible; } +.upload-wrapper { + width: 100%; +} +.preview-wrapper { + position: relative; + width: 360px !important; + min-width: 360px !important; + max-width: 360px !important; + height: 180px !important; + min-height: 180px !important; + border: 2px dashed #909399; + border-radius: 4px; + overflow: hidden; + box-sizing: border-box; + flex-shrink: 0; +} +.preview-image { + width: 360px !important; + height: 180px !important; + min-width: 360px !important; + min-height: 180px !important; + object-fit: cover; + display: block; +} +.preview-overlay { + position: absolute; + top: 0; + left: 0; + right: 0; + bottom: 0; + background: rgba(0, 0, 0, 0.5); + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + gap: 8px; + opacity: 0; + transition: opacity 0.3s; +} +.preview-wrapper:hover .preview-overlay { + opacity: 1; +} +.preview-btn { + color: #fff !important; + padding: 4px 8px; + font-size: 12px; +} .dashed-upload .el-upload__text { font-size: 12px; line-height: 16px; diff --git a/main/manager-web/src/components/custom-theme/tabs/CustomThemeWakewordConfig.vue b/main/manager-web/src/components/custom-theme/tabs/CustomThemeWakewordConfig.vue index 4b8473362..920f73c3f 100644 --- a/main/manager-web/src/components/custom-theme/tabs/CustomThemeWakewordConfig.vue +++ b/main/manager-web/src/components/custom-theme/tabs/CustomThemeWakewordConfig.vue @@ -19,6 +19,10 @@ style="width: 100%" @change="selectWakeword" > + -
+ +
+
+
+ + +
+
+ + +
+
+
+ +
@@ -39,6 +65,17 @@
+
+
+
+
+
自定义唤醒词: {{ customWakeword.chinese }}
+
拼音: {{ customWakeword.pinyin }}
+
模型: Multinet5Q8 (mn5q8_cn)
+
+
+
+
{{ $t('device.customThemeTips') }}
    @@ -72,7 +109,11 @@ export default { { id: 'wn9s_hilexin', name: 'Hi,乐鑫', model: 'WakeNet9s' }, { id: 'wn9s_nihaoxiaozhi', name: '你好小智', model: 'WakeNet9s' }, { id: 'wn7_xiaoaitongxue', name: '小爱同学', model: 'WakeNet7' } - ] + ], + customWakeword: { + chinese: '', + pinyin: '' + } }; }, computed: { @@ -94,7 +135,29 @@ export default { methods: { selectWakeword(id) { console.log('[CustomThemeWakeword] select', id); - this.localValue = id || ''; + const newValue = id || ''; + // 如果切换到非自定义选项,清空自定义唤醒词 + if (id !== 'custom') { + this.customWakeword = { chinese: '', pinyin: '' }; + } + // 更新 localValue,这会触发 input 事件 + this.$emit('input', newValue); + // 通知父组件更新自定义唤醒词配置 + this.$nextTick(() => { + this.$emit('custom-wakeword-change', { + wakeword: newValue, + customWakeword: newValue === 'custom' ? this.customWakeword : null + }); + }); + }, + updateCustomWakeword() { + // 通知父组件更新自定义唤醒词配置 + if (this.localValue === 'custom') { + this.$emit('custom-wakeword-change', { + wakeword: this.localValue, + customWakeword: this.customWakeword + }); + } }, getSelectedWakewordName() { const selected = this.wakewordData.find(w => w.id === this.localValue); @@ -182,5 +245,23 @@ export default { color: #1e3a8a; line-height: 1.6; } +.custom-wakeword-section { + margin: 14px 0; +} +.custom-input-row { + display: flex; + gap: 16px; + align-items: flex-start; +} +.custom-input-item { + flex: 1; +} +.custom-input-label { + display: block; + font-size: 13px; + font-weight: 600; + margin-bottom: 6px; + color: #606266; +} diff --git a/main/manager-web/src/utils/AssetsBuilder.js b/main/manager-web/src/utils/AssetsBuilder.js index 9b3367fb7..70ceabb2b 100644 --- a/main/manager-web/src/utils/AssetsBuilder.js +++ b/main/manager-web/src/utils/AssetsBuilder.js @@ -249,6 +249,21 @@ class AssetsBuilder { if (!wakeword) return null + // 处理自定义唤醒词(使用 multinet 模型) + if (wakeword === 'custom') { + const customWakeword = this.config.theme.customWakeword + if (!customWakeword || !customWakeword.chinese || !customWakeword.pinyin) { + throw new Error('自定义唤醒词配置不完整,请填写中文和拼音') + } + return { + name: 'mn5q8_cn', + type: 'Multinet5Q8', + filename: 'srmodels.bin', + isCustom: true, + customWakeword: customWakeword + } + } + // 根据唤醒词模型名称确定类型(而不是根据芯片型号) // 因为实际文件目录中只有 WakeNet9s 和 WakeNet7 模型,没有 WakeNet9 模型 let modelType = 'WakeNet9s' // 默认值 @@ -267,7 +282,8 @@ class AssetsBuilder { return { name: wakeword, type: modelType, - filename: 'srmodels.bin' + filename: 'srmodels.bin', + isCustom: false } } @@ -451,6 +467,26 @@ class AssetsBuilder { })) } + // 添加自定义唤醒词配置(multinet_model_info) + // wakewordInfo 已在上面声明,直接使用 + if (wakewordInfo && wakewordInfo.isCustom && wakewordInfo.customWakeword) { + indexData.multinet_model_info = { + language: 'cn', + duration: 3000, + threshold: 20, // 默认阈值 + commands: [ + { + command: wakewordInfo.customWakeword.pinyin, + text: wakewordInfo.customWakeword.chinese, + action: 'wake' + } + ] + } + // 添加 CONFIG 字段 + indexData.CONFIG_CUSTOM_WAKE_WORD = wakewordInfo.customWakeword.pinyin + indexData.CONFIG_CUSTOM_WAKE_WORD_DISPLAY = wakewordInfo.customWakeword.chinese + } + return indexData } @@ -472,7 +508,9 @@ class AssetsBuilder { type: 'wakeword', name: wakewordInfo.name, filename: wakewordInfo.filename, - modelType: wakewordInfo.type + modelType: wakewordInfo.type, + isCustom: wakewordInfo.isCustom || false, + customWakeword: wakewordInfo.customWakeword || null }) } @@ -501,21 +539,29 @@ class AssetsBuilder { // 添加背景图片 const skin = this.config?.theme?.skin - if (skin?.light?.backgroundType === 'image' && skin.light.backgroundImage) { - resources.files.push({ - type: 'background', - filename: 'background_light.raw', - source: skin.light.backgroundImage, - mode: 'light' - }) + if (skin?.light?.backgroundType === 'image') { + // 优先使用保存的 File 对象,否则使用 URL + const source = skin.light.backgroundImageFile || skin.light.backgroundImage + if (source) { + resources.files.push({ + type: 'background', + filename: 'background_light.raw', + source: source, + mode: 'light' + }) + } } - if (skin?.dark?.backgroundType === 'image' && skin.dark.backgroundImage) { - resources.files.push({ - type: 'background', - filename: 'background_dark.raw', - source: skin.dark.backgroundImage, - mode: 'dark' - }) + if (skin?.dark?.backgroundType === 'image') { + // 优先使用保存的 File 对象,否则使用 URL + const source = skin.dark.backgroundImageFile || skin.dark.backgroundImage + if (source) { + resources.files.push({ + type: 'background', + filename: 'background_dark.raw', + source: source, + mode: 'dark' + }) + } } return resources @@ -918,7 +964,33 @@ class AssetsBuilder { * @param {Object} resource - 资源配置 */ async processBackgroundFile(resource) { - const imageData = await this.fileToArrayBuffer(resource.source) + let imageData + + // 如果 source 是 URL 字符串,需要先通过 fetch 获取 + if (typeof resource.source === 'string') { + try { + const response = await fetch(resource.source) + if (!response.ok) { + throw new Error(`HTTP ${response.status}: ${response.statusText}`) + } + const blob = await response.blob() + + // 验证 blob 类型 + if (!(blob instanceof Blob)) { + throw new Error(`fetch 返回的不是 Blob 对象,类型: ${typeof blob}`) + } + + imageData = await this.fileToArrayBuffer(blob) + } catch (error) { + console.error('加载背景图片失败:', error) + throw new Error(`加载背景图片失败: ${resource.filename} - ${error.message}`) + } + } else if (resource.source instanceof File || resource.source instanceof Blob) { + // 如果是 File 或 Blob 对象,直接转换 + imageData = await this.fileToArrayBuffer(resource.source) + } else { + throw new Error(`不支持的背景图片格式: ${resource.filename},source 类型: ${typeof resource.source}`) + } // 将图片转换为RGB565格式的原始数据 const rawData = await this.convertImageToRgb565(imageData) @@ -970,14 +1042,36 @@ class AssetsBuilder { /** * 将文件转换为ArrayBuffer - * @param {File|Blob} file - 文件对象 + * @param {File|Blob|ArrayBuffer} file - 文件对象或 ArrayBuffer * @returns {Promise} 文件数据 */ fileToArrayBuffer(file) { + // 如果已经是 ArrayBuffer,直接返回 + if (file instanceof ArrayBuffer) { + return Promise.resolve(file) + } + + // 验证类型 + if (!(file instanceof File) && !(file instanceof Blob)) { + const errorMsg = `fileToArrayBuffer 需要 File 或 Blob 对象,但收到: ${typeof file}, 构造函数: ${file?.constructor?.name || 'unknown'}` + console.error('fileToArrayBuffer 类型错误:', { + file, + type: typeof file, + constructor: file?.constructor?.name, + isFile: file instanceof File, + isBlob: file instanceof Blob, + isArrayBuffer: file instanceof ArrayBuffer + }) + return Promise.reject(new Error(errorMsg)) + } + return new Promise((resolve, reject) => { const reader = new FileReader() reader.onload = () => resolve(reader.result) - reader.onerror = () => reject(new Error('读取文件失败')) + reader.onerror = (e) => { + console.error('FileReader 错误:', e) + reject(new Error('读取文件失败')) + } reader.readAsArrayBuffer(file) }) } diff --git a/main/manager-web/src/utils/WakenetModelPacker.js b/main/manager-web/src/utils/WakenetModelPacker.js index c10237eae..5a81b1e2f 100644 --- a/main/manager-web/src/utils/WakenetModelPacker.js +++ b/main/manager-web/src/utils/WakenetModelPacker.js @@ -52,17 +52,30 @@ class WakenetModelPacker { /** * 从share/wakenet_model目录加载模型 - * @param {string} modelName - 模型名称 (例如: wn9s_nihaoxiaozhi) + * @param {string} modelName - 模型名称 (例如: wn9s_nihaoxiaozhi 或 mn5q8_cn) * @returns {Promise} 加载是否成功 */ async loadModelFromShare(modelName) { try { - // 所有wakenet模型都使用相同的文件名 - const modelFiles = [ - '_MODEL_INFO_', - 'wn9_data', - 'wn9_index' - ] + // 根据模型类型确定文件列表 + let modelFiles = [] + if (modelName.startsWith('mn')) { + // Multinet 模型 (例如: mn5q8_cn) + // 根据模型名称提取前缀 (mn5q8) + const prefix = modelName.split('_')[0] + modelFiles = [ + '_MODEL_INFO_', + `${prefix}_data`, + `${prefix}_index` + ] + } else { + // Wakenet 模型 (例如: wn9s_nihaoxiaozhi) + modelFiles = [ + '_MODEL_INFO_', + 'wn9_data', + 'wn9_index' + ] + } let loadedFiles = 0 const failedFiles = [] diff --git a/main/manager-web/src/views/DeviceManagement.vue b/main/manager-web/src/views/DeviceManagement.vue index 4a48e52b4..f98adab74 100644 --- a/main/manager-web/src/views/DeviceManagement.vue +++ b/main/manager-web/src/views/DeviceManagement.vue @@ -43,7 +43,12 @@ @@ -228,6 +233,10 @@ export default { }, methods: { handleCustomThemeConfig(row) { + if (row.deviceStatus !== 'online') { + this.$message.warning(this.$t('device.customThemeConfig') + ':设备不在线,无法配置'); + return; + } this.selectedDeviceForTheme = row; this.customThemeDialogVisible = true; },