feat: wire watch pages to @system.interconnect bridge

This commit is contained in:
2026-09-01 18:50:32 +08:00
parent 9694f69579
commit a4bc6cffc3
4 changed files with 115 additions and 27 deletions
+87 -10
View File
@@ -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) }