forked from chiyu1468/AirTrapMine
Upload files to 'src/unitdev01/unitdev01'
parent
14b70f6e2e
commit
00eb6b512d
@ -0,0 +1,398 @@
|
||||
#!/usr/bin/env python3
|
||||
from PyQt6.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout, QLabel,
|
||||
QPushButton, QLineEdit)
|
||||
from PyQt6.QtCore import pyqtSignal
|
||||
|
||||
class CommPanel(QWidget):
|
||||
"""通讯设置面板类"""
|
||||
|
||||
# 定义信号
|
||||
udp_connection_added = pyqtSignal(str, int) # ip, port
|
||||
udp_connection_toggled = pyqtSignal(dict, QPushButton, QLabel) # conn, btn, status_label
|
||||
udp_connection_removed = pyqtSignal(dict, QWidget) # conn, panel
|
||||
ws_connection_added = pyqtSignal(str) # url
|
||||
ws_connection_toggled = pyqtSignal(dict, QPushButton, QLabel) # conn, btn, status_label
|
||||
ws_connection_removed = pyqtSignal(dict, QWidget) # conn, panel
|
||||
status_message = pyqtSignal(str, int) # message, timeout
|
||||
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
self.udp_connections = []
|
||||
self.ws_connections = []
|
||||
self._init_ui()
|
||||
|
||||
def _init_ui(self):
|
||||
"""初始化UI"""
|
||||
layout = QVBoxLayout(self)
|
||||
layout.setContentsMargins(10, 10, 10, 10)
|
||||
layout.setSpacing(10)
|
||||
|
||||
# ========== UDP MAVLink 區域 ==========
|
||||
udp_title = QLabel("UDP")
|
||||
udp_title.setStyleSheet("""
|
||||
color: #DDD;
|
||||
font-size: 14px;
|
||||
font-weight: bold;
|
||||
padding: 5px;
|
||||
background-color: #333;
|
||||
border-radius: 4px;
|
||||
""")
|
||||
layout.addWidget(udp_title)
|
||||
|
||||
# UDP 連接列表容器
|
||||
self.udp_list_widget = QWidget()
|
||||
self.udp_list_layout = QVBoxLayout(self.udp_list_widget)
|
||||
self.udp_list_layout.setContentsMargins(0, 0, 0, 0)
|
||||
self.udp_list_layout.setSpacing(5)
|
||||
layout.addWidget(self.udp_list_widget)
|
||||
|
||||
# UDP 添加新連接區域
|
||||
add_udp_widget = QWidget()
|
||||
add_udp_layout = QHBoxLayout(add_udp_widget)
|
||||
add_udp_layout.setContentsMargins(0, 0, 0, 0)
|
||||
|
||||
self.udp_ip_input = QLineEdit()
|
||||
self.udp_ip_input.setText("127.0.0.1")
|
||||
self.udp_ip_input.setPlaceholderText("IP")
|
||||
self.udp_ip_input.setStyleSheet("""
|
||||
QLineEdit {
|
||||
background-color: #333;
|
||||
color: #DDD;
|
||||
border: 1px solid #555;
|
||||
border-radius: 4px;
|
||||
padding: 5px;
|
||||
}
|
||||
""")
|
||||
|
||||
self.udp_port_input = QLineEdit()
|
||||
self.udp_port_input.setText("14540")
|
||||
self.udp_port_input.setPlaceholderText("Port")
|
||||
self.udp_port_input.setFixedWidth(80)
|
||||
self.udp_port_input.setStyleSheet("""
|
||||
QLineEdit {
|
||||
background-color: #333;
|
||||
color: #DDD;
|
||||
border: 1px solid #555;
|
||||
border-radius: 4px;
|
||||
padding: 5px;
|
||||
}
|
||||
""")
|
||||
|
||||
add_udp_btn = QPushButton("添加")
|
||||
add_udp_btn.clicked.connect(self._handle_add_udp)
|
||||
add_udp_btn.setStyleSheet("""
|
||||
QPushButton {
|
||||
background-color: #4CAF50;
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 6px 12px;
|
||||
border-radius: 4px;
|
||||
min-width: 30px;
|
||||
}
|
||||
QPushButton:hover { background-color: #45a049; }
|
||||
""")
|
||||
|
||||
add_udp_layout.addWidget(QLabel("IP:", styleSheet="color: #DDD;"))
|
||||
add_udp_layout.addWidget(self.udp_ip_input)
|
||||
add_udp_layout.addWidget(QLabel("Port:", styleSheet="color: #DDD;"))
|
||||
add_udp_layout.addWidget(self.udp_port_input)
|
||||
add_udp_layout.addWidget(add_udp_btn)
|
||||
|
||||
layout.addWidget(add_udp_widget)
|
||||
|
||||
# 分隔線
|
||||
separator = QWidget()
|
||||
separator.setFixedHeight(20)
|
||||
layout.addWidget(separator)
|
||||
|
||||
# ========== WebSocket 區域 ==========
|
||||
ws_title = QLabel("WebSocket")
|
||||
ws_title.setStyleSheet("""
|
||||
color: #DDD;
|
||||
font-size: 14px;
|
||||
font-weight: bold;
|
||||
padding: 5px;
|
||||
background-color: #333;
|
||||
border-radius: 4px;
|
||||
""")
|
||||
layout.addWidget(ws_title)
|
||||
|
||||
# WebSocket 連接列表容器
|
||||
self.ws_list_widget = QWidget()
|
||||
self.ws_list_layout = QVBoxLayout(self.ws_list_widget)
|
||||
self.ws_list_layout.setContentsMargins(0, 0, 0, 0)
|
||||
self.ws_list_layout.setSpacing(5)
|
||||
layout.addWidget(self.ws_list_widget)
|
||||
|
||||
# WebSocket 添加新連接區域
|
||||
add_ws_widget = QWidget()
|
||||
add_ws_layout = QHBoxLayout(add_ws_widget)
|
||||
add_ws_layout.setContentsMargins(0, 0, 0, 0)
|
||||
|
||||
self.ws_url_input = QLineEdit()
|
||||
self.ws_url_input.setPlaceholderText("host")
|
||||
self.ws_url_input.setStyleSheet("""
|
||||
QLineEdit {
|
||||
background-color: #333;
|
||||
color: #DDD;
|
||||
border: 1px solid #555;
|
||||
border-radius: 4px;
|
||||
padding: 5px;
|
||||
}
|
||||
""")
|
||||
|
||||
add_ws_btn = QPushButton("添加")
|
||||
add_ws_btn.clicked.connect(self._handle_add_ws)
|
||||
add_ws_btn.setStyleSheet("""
|
||||
QPushButton {
|
||||
background-color: #4CAF50;
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 6px 12px;
|
||||
border-radius: 4px;
|
||||
min-width: 30px;
|
||||
}
|
||||
QPushButton:hover { background-color: #45a049; }
|
||||
""")
|
||||
|
||||
add_ws_layout.addWidget(QLabel("URL:", styleSheet="color: #DDD;"))
|
||||
add_ws_layout.addWidget(self.ws_url_input)
|
||||
add_ws_layout.addWidget(add_ws_btn)
|
||||
|
||||
layout.addWidget(add_ws_widget)
|
||||
layout.addStretch()
|
||||
|
||||
def _handle_add_udp(self):
|
||||
"""處理添加 UDP 連接"""
|
||||
ip = self.udp_ip_input.text().strip()
|
||||
port_text = self.udp_port_input.text().strip()
|
||||
|
||||
if not ip or not port_text:
|
||||
self.status_message.emit("請輸入 IP 和 Port", 3000)
|
||||
return
|
||||
|
||||
try:
|
||||
port = int(port_text)
|
||||
if port < 1 or port > 65535:
|
||||
raise ValueError("Port 超出範圍")
|
||||
except ValueError:
|
||||
self.status_message.emit("Port 必須是 1-65535 的數字", 3000)
|
||||
return
|
||||
|
||||
# 檢查是否已存在相同連接
|
||||
for conn in self.udp_connections:
|
||||
if conn['ip'] == ip and conn['port'] == port:
|
||||
self.status_message.emit("連接已存在", 3000)
|
||||
return
|
||||
|
||||
# 發送信號通知主窗口
|
||||
self.udp_connection_added.emit(ip, port)
|
||||
|
||||
# 清空輸入框
|
||||
self.udp_ip_input.clear()
|
||||
self.udp_port_input.clear()
|
||||
|
||||
def _handle_add_ws(self):
|
||||
"""處理添加 WebSocket 連接"""
|
||||
input_url = self.ws_url_input.text().strip()
|
||||
|
||||
if not input_url:
|
||||
self.status_message.emit("請輸入 WebSocket URL", 3000)
|
||||
return
|
||||
|
||||
# 自動添加 ws:// 前綴
|
||||
if not input_url.startswith('ws://') and not input_url.startswith('wss://'):
|
||||
url = f'ws://{input_url}'
|
||||
else:
|
||||
url = input_url
|
||||
|
||||
# 基本 URL 格式驗證
|
||||
try:
|
||||
if '://' in url:
|
||||
parts = url.split('://', 1)
|
||||
if len(parts) == 2 and ':' not in parts[1]:
|
||||
self.status_message.emit("URL 格式錯誤,需要包含端口號 (例如: 127.0.0.1:8756)", 3000)
|
||||
return
|
||||
except:
|
||||
self.status_message.emit("URL 格式錯誤", 3000)
|
||||
return
|
||||
|
||||
# 檢查是否已存在相同連接
|
||||
for conn in self.ws_connections:
|
||||
if conn['url'] == url:
|
||||
self.status_message.emit("連接已存在", 3000)
|
||||
return
|
||||
|
||||
# 發送信號通知主窗口
|
||||
self.ws_connection_added.emit(url)
|
||||
|
||||
# 清空輸入框
|
||||
self.ws_url_input.clear()
|
||||
|
||||
def create_udp_connection_panel(self, conn):
|
||||
"""創建 UDP 連接面板"""
|
||||
panel = QWidget()
|
||||
panel.setStyleSheet("""
|
||||
QWidget {
|
||||
background-color: #2A2A2A;
|
||||
border-radius: 6px;
|
||||
padding: 8px;
|
||||
border: 1px solid #444;
|
||||
}
|
||||
""")
|
||||
|
||||
layout = QHBoxLayout(panel)
|
||||
layout.setContentsMargins(8, 8, 8, 8)
|
||||
|
||||
# 連接資訊
|
||||
info_label = QLabel(f"{conn['name']} - {conn['ip']}:{conn['port']}")
|
||||
info_label.setStyleSheet("color: #DDD; font-size: 12px;")
|
||||
|
||||
# 狀態指示器
|
||||
status_label = QLabel("●")
|
||||
if conn.get('enabled', False):
|
||||
status_label.setStyleSheet("color: #4CAF50; font-size: 16px;")
|
||||
status_label.setToolTip("運行中")
|
||||
else:
|
||||
status_label.setStyleSheet("color: #888; font-size: 16px;")
|
||||
status_label.setToolTip("已停止")
|
||||
|
||||
# 控制按鈕
|
||||
toggle_btn = QPushButton("停止" if conn.get('enabled', False) else "啟動")
|
||||
toggle_btn.setFixedWidth(60)
|
||||
toggle_btn.clicked.connect(lambda: self.udp_connection_toggled.emit(conn, toggle_btn, status_label))
|
||||
toggle_btn.setStyleSheet("""
|
||||
QPushButton {
|
||||
background-color: #444;
|
||||
color: #DDD;
|
||||
border: none;
|
||||
padding: 4px 8px;
|
||||
border-radius: 3px;
|
||||
font-size: 11px;
|
||||
}
|
||||
QPushButton:hover { background-color: #555; }
|
||||
""")
|
||||
|
||||
remove_btn = QPushButton("移除")
|
||||
remove_btn.setFixedWidth(60)
|
||||
remove_btn.clicked.connect(lambda: self.udp_connection_removed.emit(conn, panel))
|
||||
remove_btn.setStyleSheet("""
|
||||
QPushButton {
|
||||
background-color: #d32f2f;
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 4px 8px;
|
||||
border-radius: 3px;
|
||||
font-size: 11px;
|
||||
}
|
||||
QPushButton:hover { background-color: #b71c1c; }
|
||||
""")
|
||||
|
||||
layout.addWidget(status_label)
|
||||
layout.addWidget(info_label)
|
||||
layout.addStretch()
|
||||
layout.addWidget(toggle_btn)
|
||||
layout.addWidget(remove_btn)
|
||||
|
||||
# 儲存引用
|
||||
panel.connection = conn
|
||||
panel.toggle_btn = toggle_btn
|
||||
panel.status_label = status_label
|
||||
|
||||
return panel
|
||||
|
||||
def create_ws_connection_panel(self, conn):
|
||||
"""創建 WebSocket 連接面板"""
|
||||
panel = QWidget()
|
||||
panel.setStyleSheet("""
|
||||
QWidget {
|
||||
background-color: #2A2A2A;
|
||||
border-radius: 6px;
|
||||
padding: 8px;
|
||||
border: 1px solid #444;
|
||||
}
|
||||
""")
|
||||
|
||||
layout = QHBoxLayout(panel)
|
||||
layout.setContentsMargins(8, 8, 8, 8)
|
||||
|
||||
# 連接資訊
|
||||
info_label = QLabel(f"{conn['name']} - {conn['url']}")
|
||||
info_label.setStyleSheet("color: #DDD; font-size: 12px;")
|
||||
|
||||
# 狀態指示器
|
||||
status_label = QLabel("●")
|
||||
if conn.get('enabled', False):
|
||||
status_label.setStyleSheet("color: #4CAF50; font-size: 16px;")
|
||||
status_label.setToolTip("運行中")
|
||||
else:
|
||||
status_label.setStyleSheet("color: #888; font-size: 16px;")
|
||||
status_label.setToolTip("已停止")
|
||||
|
||||
# 控制按鈕
|
||||
toggle_btn = QPushButton("停止" if conn.get('enabled', False) else "啟動")
|
||||
toggle_btn.setFixedWidth(60)
|
||||
toggle_btn.clicked.connect(lambda: self.ws_connection_toggled.emit(conn, toggle_btn, status_label))
|
||||
toggle_btn.setStyleSheet("""
|
||||
QPushButton {
|
||||
background-color: #444;
|
||||
color: #DDD;
|
||||
border: none;
|
||||
padding: 4px 8px;
|
||||
border-radius: 3px;
|
||||
font-size: 11px;
|
||||
}
|
||||
QPushButton:hover { background-color: #555; }
|
||||
""")
|
||||
|
||||
remove_btn = QPushButton("移除")
|
||||
remove_btn.setFixedWidth(60)
|
||||
remove_btn.clicked.connect(lambda: self.ws_connection_removed.emit(conn, panel))
|
||||
remove_btn.setStyleSheet("""
|
||||
QPushButton {
|
||||
background-color: #d32f2f;
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 4px 8px;
|
||||
border-radius: 3px;
|
||||
font-size: 11px;
|
||||
}
|
||||
QPushButton:hover { background-color: #b71c1c; }
|
||||
""")
|
||||
|
||||
layout.addWidget(status_label)
|
||||
layout.addWidget(info_label)
|
||||
layout.addStretch()
|
||||
layout.addWidget(toggle_btn)
|
||||
layout.addWidget(remove_btn)
|
||||
|
||||
# 儲存引用
|
||||
panel.connection = conn
|
||||
panel.toggle_btn = toggle_btn
|
||||
panel.status_label = status_label
|
||||
|
||||
return panel
|
||||
|
||||
def add_udp_panel(self, conn):
|
||||
"""添加 UDP 連接面板到列表"""
|
||||
panel = self.create_udp_connection_panel(conn)
|
||||
self.udp_list_layout.addWidget(panel)
|
||||
self.udp_connections.append(conn)
|
||||
return panel
|
||||
|
||||
def add_ws_panel(self, conn):
|
||||
"""添加 WebSocket 連接面板到列表"""
|
||||
panel = self.create_ws_connection_panel(conn)
|
||||
self.ws_list_layout.addWidget(panel)
|
||||
self.ws_connections.append(conn)
|
||||
return panel
|
||||
|
||||
def remove_udp_connection_from_list(self, conn):
|
||||
"""從列表中移除 UDP 連接"""
|
||||
if conn in self.udp_connections:
|
||||
self.udp_connections.remove(conn)
|
||||
|
||||
def remove_ws_connection_from_list(self, conn):
|
||||
"""從列表中移除 WebSocket 連接"""
|
||||
if conn in self.ws_connections:
|
||||
self.ws_connections.remove(conn)
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,292 @@
|
||||
#!/usr/bin/env python3
|
||||
from PyQt6.QtWebEngineWidgets import QWebEngineView
|
||||
from PyQt6.QtCore import QTimer, pyqtSignal, QObject, pyqtSlot
|
||||
from PyQt6.QtWebChannel import QWebChannel
|
||||
|
||||
class DroneMap:
|
||||
"""無人機地圖類別 - 負責管理 Leaflet 地圖顯示"""
|
||||
|
||||
def __init__(self):
|
||||
"""初始化地圖"""
|
||||
self.map_view = QWebEngineView()
|
||||
self.map_loaded = False
|
||||
self.pending_map_updates = {}
|
||||
|
||||
# 創建橋接對象
|
||||
self.bridge = MapBridge()
|
||||
|
||||
# 設置 QWebChannel
|
||||
self.channel = QWebChannel()
|
||||
self.channel.registerObject('bridge', self.bridge)
|
||||
self.map_view.page().setWebChannel(self.channel)
|
||||
|
||||
# 設置地圖 HTML
|
||||
inline_html = '''
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css" />
|
||||
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
|
||||
<script src="https://unpkg.com/leaflet-rotatedmarker/leaflet.rotatedMarker.js"></script>
|
||||
<script src="qrc:///qtwebchannel/qwebchannel.js"></script>
|
||||
<style>
|
||||
html, body, #map { height: 100%; margin: 0; }
|
||||
.map-controls {
|
||||
position: absolute;
|
||||
top: 10px;
|
||||
right: 10px;
|
||||
z-index: 1000;
|
||||
}
|
||||
.control-button {
|
||||
padding: 8px 12px;
|
||||
background-color: #f44336;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
font-size: 12px;
|
||||
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
|
||||
}
|
||||
.control-button:hover {
|
||||
background-color: #d32f2f;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="map"></div>
|
||||
<div class="map-controls">
|
||||
<button class="control-button" onclick="clearAllTrajectories()">清除軌跡</button>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
var bridge;
|
||||
new QWebChannel(qt.webChannelTransport, function(channel) {
|
||||
bridge = channel.objects.bridge;
|
||||
});
|
||||
|
||||
var map = L.map('map').setView([0, 0], 19);
|
||||
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
||||
maxZoom: 19,
|
||||
attribution: '© OpenStreetMap contributors'
|
||||
}).addTo(map);
|
||||
|
||||
// 地圖點擊事件
|
||||
map.on('click', function(e) {
|
||||
if (bridge) {
|
||||
bridge.emitGpsSignal(e.latlng.lat, e.latlng.lng);
|
||||
console.log('點擊位置:', e.latlng.lat, e.latlng.lng);
|
||||
}
|
||||
});
|
||||
|
||||
var arrowIcon = L.icon({
|
||||
iconUrl: 'https://cdn-icons-png.flaticon.com/512/399/399308.png',
|
||||
iconSize: [40, 40],
|
||||
iconAnchor: [20, 20]
|
||||
});
|
||||
|
||||
function getColorBySocketId(id) {
|
||||
if (id.startsWith("s0_")) return "#00BFFF"; // 天藍色
|
||||
if (id.startsWith("s1_")) return "#FFD700"; // 金色
|
||||
if (id.startsWith("s2_")) return "#FF6969"; // 淺紅色
|
||||
if (id.startsWith("s3_")) return "#FF69B4"; // 熱粉紅
|
||||
if (id.startsWith("s4_")) return "#00FA9A"; // 中春綠
|
||||
if (id.startsWith("s5_")) return "#9370DB"; // 中紫色 (串口)
|
||||
if (id.startsWith("s6_")) return "#FFA500"; // 橙色
|
||||
if (id.startsWith("s7_")) return "#20B2AA"; // 淺海綠
|
||||
if (id.startsWith("s8_")) return "#7CFC00"; // 草綠色
|
||||
if (id.startsWith("s9_")) return "#FF8C00"; // 深橙色
|
||||
return "#AAAAAA"; // 灰色 (預設)
|
||||
}
|
||||
|
||||
function createIdIcon(id) {
|
||||
const color = getColorBySocketId(id);
|
||||
const sysid = id.split('_')[1];
|
||||
return L.divIcon({
|
||||
className: 'drone-id',
|
||||
html: `<div style="
|
||||
position: relative;
|
||||
left: 2px;
|
||||
background-color: ${color};
|
||||
color: black;
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
border-radius: 50%;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-weight: bold;
|
||||
font-size: 10px;
|
||||
">${sysid}</div>`,
|
||||
iconSize: [20, 20],
|
||||
iconAnchor: [10, 10]
|
||||
});
|
||||
}
|
||||
|
||||
var markers = {};
|
||||
var idLabels = {};
|
||||
var trajectories = {};
|
||||
var trajectoryLines = {};
|
||||
var focusedId = null;
|
||||
var initialized = false;
|
||||
var trajectoryGroup = L.layerGroup().addTo(map);
|
||||
|
||||
function initTrajectory(id) {
|
||||
if (!trajectories[id]) {
|
||||
trajectories[id] = [];
|
||||
const color = getColorBySocketId(id);
|
||||
trajectoryLines[id] = L.polyline([], {
|
||||
color: color,
|
||||
weight: 3,
|
||||
opacity: 0.7,
|
||||
smoothFactor: 1
|
||||
}).addTo(trajectoryGroup);
|
||||
}
|
||||
}
|
||||
|
||||
function addTrajectoryPoint(id, lat, lon) {
|
||||
initTrajectory(id);
|
||||
const point = [lat, lon];
|
||||
trajectories[id].push(point);
|
||||
|
||||
if (trajectories[id].length > 1000) {
|
||||
trajectories[id].shift();
|
||||
}
|
||||
|
||||
trajectoryLines[id].setLatLngs([...trajectories[id]]);
|
||||
}
|
||||
|
||||
function focusOn(id) {
|
||||
if (!markers[id]) return;
|
||||
focusedId = id;
|
||||
var latlng = markers[id].getLatLng();
|
||||
map.flyTo(latlng, map.getZoom());
|
||||
}
|
||||
|
||||
setInterval(() => {
|
||||
if (focusedId && markers[focusedId]) {
|
||||
var latlng = markers[focusedId].getLatLng();
|
||||
map.panTo(latlng);
|
||||
}
|
||||
}, 1000);
|
||||
|
||||
function updateDrone(lat, lon, id, heading) {
|
||||
if (markers[id]) {
|
||||
const lastPos = markers[id].getLatLng();
|
||||
const distance = lastPos.distanceTo([lat, lon]);
|
||||
if (distance > 1) {
|
||||
addTrajectoryPoint(id, lat, lon);
|
||||
}
|
||||
|
||||
markers[id]
|
||||
.setLatLng([lat, lon])
|
||||
.setRotationAngle(heading);
|
||||
idLabels[id].setLatLng([lat, lon]);
|
||||
} else {
|
||||
initTrajectory(id);
|
||||
addTrajectoryPoint(id, lat, lon);
|
||||
|
||||
markers[id] = L.marker([lat, lon], {
|
||||
icon: arrowIcon,
|
||||
rotationAngle: heading,
|
||||
rotationOrigin: 'center'
|
||||
})
|
||||
.on('click', function () {
|
||||
focusOn(id);
|
||||
})
|
||||
.addTo(map);
|
||||
|
||||
idLabels[id] = L.marker([lat, lon], {
|
||||
icon: createIdIcon(id),
|
||||
zIndexOffset: 1000
|
||||
})
|
||||
.on('click', function() {
|
||||
focusOn(id);
|
||||
})
|
||||
.addTo(map);
|
||||
|
||||
if (!initialized || id < focusedId) {
|
||||
focusOn(id);
|
||||
initialized = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function clearAllTrajectories() {
|
||||
trajectories = {};
|
||||
Object.values(trajectoryLines).forEach(line => {
|
||||
trajectoryGroup.removeLayer(line);
|
||||
});
|
||||
trajectoryLines = {};
|
||||
console.log('所有軌跡已清除');
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
'''
|
||||
|
||||
self.map_view.setHtml(inline_html)
|
||||
self.map_view.loadFinished.connect(self._on_map_loaded)
|
||||
|
||||
# 設置地圖更新計時器
|
||||
self.map_update_timer = QTimer()
|
||||
self.map_update_timer.timeout.connect(self.update_map_positions)
|
||||
self.map_update_timer.start(200) # 每 200ms 更新一次
|
||||
|
||||
def _on_map_loaded(self, ok: bool):
|
||||
"""地圖加載完成回調"""
|
||||
if ok:
|
||||
self.map_loaded = True
|
||||
else:
|
||||
print("⚠️ 地圖加載失敗")
|
||||
|
||||
def update_drone_position(self, drone_id, lat, lon, heading):
|
||||
"""更新無人機位置(加入待處理隊列)"""
|
||||
self.pending_map_updates[drone_id] = (lat, lon, heading)
|
||||
|
||||
def update_map_positions(self):
|
||||
"""批量更新地圖上的無人機位置"""
|
||||
if not self.map_loaded or not self.pending_map_updates:
|
||||
return
|
||||
|
||||
# 批量執行所有待更新的位置
|
||||
js_commands = []
|
||||
for drone_id, (lat, lon, heading) in self.pending_map_updates.items():
|
||||
js_commands.append(f"updateDrone({lat:.6f}, {lon:.6f}, '{drone_id}', {heading:.1f});")
|
||||
|
||||
if js_commands:
|
||||
combined_js = "\n".join(js_commands)
|
||||
self.map_view.page().runJavaScript(combined_js)
|
||||
|
||||
# 清空待更新緩存
|
||||
self.pending_map_updates.clear()
|
||||
|
||||
def clear_trajectories(self):
|
||||
"""清除所有軌跡"""
|
||||
if self.map_loaded:
|
||||
self.map_view.page().runJavaScript("clearAllTrajectories();")
|
||||
|
||||
def focus_on_drone(self, drone_id):
|
||||
"""聚焦到指定無人機"""
|
||||
if self.map_loaded:
|
||||
self.map_view.page().runJavaScript(f"focusOn('{drone_id}');")
|
||||
|
||||
def get_widget(self):
|
||||
"""獲取地圖 widget"""
|
||||
return self.map_view
|
||||
|
||||
def get_gps_signal(self):
|
||||
"""獲取 GPS 信號"""
|
||||
return self.bridge.gps_signal
|
||||
|
||||
class MapBridge(QObject):
|
||||
"""JavaScript 和 Python 之間的橋接類"""
|
||||
gps_signal = pyqtSignal(float, float) # lat, lon
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
@pyqtSlot(float, float)
|
||||
def emitGpsSignal(self, lat, lon):
|
||||
"""供 JavaScript 調用的方法"""
|
||||
self.gps_signal.emit(lat, lon)
|
||||
Loading…
Reference in New Issue