From a4bc6cffc3a439f174fb2c306600037de701a807 Mon Sep 17 00:00:00 2001 From: jiangchen <1703935164@qq.com> Date: Tue, 1 Sep 2026 18:50:32 +0800 Subject: [PATCH] feat: wire watch pages to @system.interconnect bridge --- src/common/bridge.js | 97 ++++++++++++++++++++++++++++++---- src/pages/index/index.ux | 34 ++++++++---- src/pages/input/input.ux | 7 ++- src/pages/settings/settings.ux | 4 +- 4 files changed, 115 insertions(+), 27 deletions(-) diff --git a/src/common/bridge.js b/src/common/bridge.js index bea4005..c97684e 100644 --- a/src/common/bridge.js +++ b/src/common/bridge.js @@ -1,16 +1,93 @@ -// DSHWatch bridge stub (empty-shell UI phase) -// 真正的 interconnect 逻辑后续接入。这里先留一个数据/回调占位,方便页面引用。 +// DSHWatch bridge: @system.interconnect wrapper (real shell glue). +// Sends/receives JSON envelopes to the DSHWatch phone APP over the Xiaomi +// Vela interconnect BLE channel. Protocol per DSHWatch_Server/docs/PROTOCOL.md. -const listeners = [] +import interconnect from '@system.interconnect' -function setStatus(text) { - // 空壳:后续改为广播给页面 - listeners.forEach((fn) => fn && fn(text)) +let conn = null +let seq = 0 +let listeners = [] +let currentStatus = '未连接' + +function getConn() { + if (!conn) { + try { + conn = interconnect.instance() + } catch (e) { + return null + } + conn.onopen = () => { + currentStatus = '已连接' + emit('status', currentStatus) + } + conn.onclose = () => { + currentStatus = '已断开' + emit('status', currentStatus) + } + conn.onerror = () => { + currentStatus = '连接异常' + emit('status', currentStatus) + } + conn.onmessage = (data) => { + let msg = data + if (typeof data === 'string') { + try { msg = JSON.parse(data) } catch (e) { return } + } + emit('message', msg) + } + } + return conn } -function connect() { - // 空壳 - setStatus('未连接') +function emit(type, payload) { + for (const l of listeners) { + if (l && typeof l[type] === 'function') { + try { l[type](payload) } catch (e) {} + } + } } -export { connect, setStatus } +export function init(callbacks) { + listeners = [callbacks] + refreshConn() +} + +export function refreshConn() { + const c = getConn() + if (!c) { + currentStatus = '未连接' + emit('status', currentStatus) + return + } + c.getReadyState({ + success: (d) => { + currentStatus = d && d.status === 1 ? '已连接' : '已断开' + emit('status', currentStatus) + }, + fail: (data, code) => { + currentStatus = '连接异常(' + code + ')' + emit('status', currentStatus) + } + }) +} + +function wrap(type, payload) { + return { to: 'phone', type, seq: ++seq, ts: Date.now(), payload: payload || {} } +} + +function sendWrap(type, payload) { + const c = getConn() + if (!c) return false + const msg = wrap(type, payload) + c.send({ + data: msg, + success: () => {}, + fail: () => {} + }) + return true +} + +export function sendCommand(payload) { return sendWrap('command', payload) } +export function sendText(text) { return sendWrap('text', { text: text }) } +export function sendCancel() { return sendWrap('cancel', {}) } +export function sendSettings(settings) { return sendWrap('settings', settings) } diff --git a/src/pages/index/index.ux b/src/pages/index/index.ux index f03236e..2586367 100644 --- a/src/pages/index/index.ux +++ b/src/pages/index/index.ux @@ -50,17 +50,14 @@ diff --git a/src/pages/input/input.ux b/src/pages/input/input.ux index 6316d79..cdd304f 100644 --- a/src/pages/input/input.ux +++ b/src/pages/input/input.ux @@ -16,6 +16,7 @@