Bladeren bron

更新接口

liuq 3 maanden geleden
bovenliggende
commit
39a6303351
5 gewijzigde bestanden met toevoegingen van 89 en 10 verwijderingen
  1. 13 0
      API_DOCUMENTATION.md
  2. 25 1
      api/kodi.py
  3. 21 0
      application/kodi_thread.py
  4. 4 0
      hardware/kodi_module.py
  5. 26 9
      templates/kodi/index.html

+ 13 - 0
API_DOCUMENTATION.md

@@ -111,6 +111,19 @@
   }
   ```
 
+### 获取全局音量
+- **接口地址**: `/api/kodi/get_volume`
+- **请求方式**: `GET`
+- **响应示例**:
+  ```json
+  {
+      "success": true,
+      "data": {
+          "volume": 60
+      }
+  }
+  ```
+
 ### 闲时自动播放控制
 - **控制启停**: `/api/kodi/free_time/control` (POST)
   - 参数: `{"action": "start"}` (开启自动播放功能) | `{"action": "stop"}` (停止自动播放功能)

+ 25 - 1
api/kodi.py

@@ -2,7 +2,7 @@ from flask import Blueprint, jsonify, request, current_app
 from application.kodi_thread import (
     start_kodi_play, is_kodi_thread_running, play_image, play_rtsp,
     revoke_individual_state, start_all_kodi_apps, get_kodi_clients,
-    get_video_list, set_volume
+    get_video_list, set_volume, get_volume
 )
 from application.kodi_free_time_thread import (
     start_kodi_free_time_play, stop_kodi_free_time_play,
@@ -402,6 +402,30 @@ def set_kodi_volume_api():
         }), 500
 
 
+@kodi_bp.route('/api/kodi/get_volume', methods=['GET'])
+@login_required
+def get_kodi_volume_api():
+    """获取Kodi全局音量接口"""
+    try:
+        volume = get_volume()
+        if volume != -1:
+            return jsonify({
+                "success": True,
+                "data": {"volume": volume}
+            })
+        else:
+            return jsonify({
+                "success": False,
+                "message": "获取音量失败"
+            }), 500
+    except Exception as e:
+        logger.error(f"获取音量异常: {str(e)}")
+        return jsonify({
+            "success": False,
+            "message": f"获取音量失败: {str(e)}"
+        }), 500
+
+
 @kodi_bp.route('/api/kodi/free_time/control', methods=['POST'])
 @login_required
 def control_free_time_play_api():

+ 21 - 0
application/kodi_thread.py

@@ -185,6 +185,15 @@ class KodiPlayThreadSingleton:
             logger.error(f"设置音量异常: {e}")
             return False
 
+    def _get_volume(self) -> int:
+        try:
+            if self.manager:
+                return self.manager.get_volume()
+            return -1
+        except Exception as e:
+            logger.error(f"获取音量异常: {e}")
+            return -1
+
 # 全局单例
 _kodi_thread = KodiPlayThreadSingleton()
 
@@ -294,6 +303,18 @@ def set_volume(volume: int) -> bool:
         volume = 100
     return _kodi_thread._set_volume(volume)
 
+def get_volume() -> int:
+    """获取当前全局音量
+    
+    Returns:
+        int: 当前音量值
+    """
+    _kodi_thread._initialize_manager()
+    if _kodi_thread.manager is None:
+        logger.error("KodiClientManager 初始化失败")
+        return -1
+    return _kodi_thread._get_volume()
+
 def get_kodi_clients() -> List[Dict[str, Any]]:
     """获取所有 Kodi 客户端列表
     

+ 4 - 0
hardware/kodi_module.py

@@ -304,6 +304,10 @@ class KodiClientManager():
                 found_first_non_individual = True
             else:
                 client.set_volume(0)
+
+    def get_volume(self):
+        """获取当前全局音量"""
+        return self.volume
     
     def _init_video_infos_from_config(self):
         config = self._load_config(self.video_config_path)

+ 26 - 9
templates/kodi/index.html

@@ -115,12 +115,12 @@
         <div style="background: white; padding: 20px; border-radius: 10px; border: 1px solid #eee; margin-bottom: 20px;">
             <h4 style="margin-bottom: 15px; color: #4ecdc4; border-left: 4px solid #4ecdc4; padding-left: 10px;">音量控制</h4>
             <div class="control-row">
-                <div class="control-group">
-                    <label for="globalVolume">全局音量 (0-100)</label>
-                    <input type="number" id="globalVolume" min="0" max="100" value="65">
-                </div>
-                <div class="control-group" style="flex: 0 0 auto;">
-                    <button class="btn btn-info" onclick="setGlobalVolume()">设置音量</button>
+                <div class="control-group" style="width: 100%;">
+                    <div style="display: flex; justify-content: space-between; margin-bottom: 5px;">
+                        <label for="globalVolume">全局音量</label>
+                        <span id="volumeValue" style="font-weight: bold; color: #4ecdc4;">65</span>
+                    </div>
+                    <input type="range" id="globalVolume" min="0" max="100" value="65" style="width: 100%; cursor: pointer;" onchange="setGlobalVolume()" oninput="updateVolumeDisplay(this.value)">
                 </div>
             </div>
         </div>
@@ -223,6 +223,7 @@
         getKodiClients();
         getVideoList();
         getFreeTimePlayStatus();
+        getGlobalVolume();
 
         window.onclick = function(event) {
             const modal = document.getElementById('tvConfigModal');
@@ -337,6 +338,24 @@
         }
     }
 
+    function updateVolumeDisplay(val) {
+        document.getElementById('volumeValue').textContent = val;
+    }
+
+    async function getGlobalVolume() {
+        try {
+            const response = await fetch('/api/kodi/get_volume');
+            const result = await response.json();
+            if (result.success) {
+                const vol = result.data.volume;
+                document.getElementById('globalVolume').value = vol;
+                updateVolumeDisplay(vol);
+            }
+        } catch (error) {
+            console.error('获取音量失败', error);
+        }
+    }
+
     async function setGlobalVolume() {
         const volume = parseInt(document.getElementById('globalVolume').value);
         if (isNaN(volume) || volume < 0 || volume > 100) {
@@ -344,7 +363,7 @@
             return;
         }
         try {
-            showLoading(true);
+            // 滑动条体验优化:不显示全屏loading
             const response = await fetch('/api/kodi/set_volume', {
                 method: 'POST',
                 headers: { 'Content-Type': 'application/json' },
@@ -358,8 +377,6 @@
             }
         } catch (error) {
             showMessage('网络错误: ' + error.message, 'error');
-        } finally {
-            showLoading(false);
         }
     }