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) // DSHWatch bridge: @system.interconnect wrapper (real shell glue).
// 真正的 interconnect 逻辑后续接入。这里先留一个数据/回调占位,方便页面引用。 // 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) { let conn = null
// 空壳:后续改为广播给页面 let seq = 0
listeners.forEach((fn) => fn && fn(text)) 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() { function emit(type, payload) {
// 空壳 for (const l of listeners) {
setStatus('未连接') 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) }
+23 -11
View File
@@ -50,17 +50,14 @@
<script> <script>
import router from '@system.router' import router from '@system.router'
import bridge from '../../common/bridge.js'
export default { export default {
private: { private: {
currentIndex: 1, currentIndex: 1,
sessions: [ sessions: [],
{ sessionId: 'demo-1', title: '原型演示会话', running: false },
{ sessionId: 'demo-2', title: 'UI 对齐中', running: true },
{ sessionId: 'demo-3', title: '待接入 Harness', running: false }
],
statusText: '未连接', statusText: '未连接',
activeTitle: '会话 demo-1', activeTitle: '未选择会话',
settings: { settings: {
serverUrl: 'ws://127.0.0.1:8787', serverUrl: 'ws://127.0.0.1:8787',
userId: '', userId: '',
@@ -70,12 +67,19 @@ export default {
}, },
onInit() { onInit() {
// 空壳:暂无逻辑 // 空壳:仅预留
}, },
onReady() { onReady() {
// 空壳:后续在此初始化 interconnect bridge.init({
this.refreshConn() status: (s) => {
this.statusText = s
this.connText = s
},
message: (msg) => {
this.handleBridgeMessage(msg)
}
})
}, },
onSwipeChange(e) { onSwipeChange(e) {
@@ -116,8 +120,16 @@ export default {
onTokenChange(e) { this.settings.token = e && e.value || '' }, onTokenChange(e) { this.settings.token = e && e.value || '' },
refreshConn() { refreshConn() {
// 空壳:后续通过 interconnect getReadyState 获取真实连接状态 bridge.refreshConn()
this.connText = '手机/手表未连接' },
handleBridgeMessage(msg) {
// 空壳:按 PROTOCOL.md 处理手机下发的 status/event 等
if (msg && msg.type === 'status' && msg.payload) {
this.statusText = msg.payload.statusText || this.statusText
this.activeTitle = msg.payload.activeTitle || this.activeTitle
if (msg.payload.sessions) this.sessions = msg.payload.sessions
}
} }
} }
</script> </script>
+3 -4
View File
@@ -16,6 +16,7 @@
<script> <script>
import router from '@system.router' import router from '@system.router'
import bridge from '../../common/bridge.js'
export default { export default {
private: { private: {
@@ -27,13 +28,11 @@ export default {
}, },
sendQuick(index) { sendQuick(index) {
const item = this.quick[index] const item = this.quick[index]
// 空壳:后续通过 interconnect 发给手机/服务器 bridge.sendCommand({ kind: 'quick', quick: item, content: item })
console.log('quick send', item)
this.text = item this.text = item
}, },
sendText() { sendText() {
// 空壳:后续通过 interconnect 发送 bridge.sendText(this.text)
console.log('send text', this.text)
}, },
goBack() { goBack() {
router.back() router.back()
+2 -2
View File
@@ -19,6 +19,7 @@
<script> <script>
import router from '@system.router' import router from '@system.router'
import bridge from '../../common/bridge.js'
export default { export default {
private: { private: {
@@ -31,8 +32,7 @@ export default {
onUserChange(e) { this.userId = e && e.value || '' }, onUserChange(e) { this.userId = e && e.value || '' },
onTokenChange(e) { this.token = e && e.value || '' }, onTokenChange(e) { this.token = e && e.value || '' },
save() { save() {
// 空壳:后续持久化并与 index 页同步 bridge.sendSettings({ serverUrl: this.serverUrl, userId: this.userId, token: this.token })
console.log('save settings', this.serverUrl, this.userId)
}, },
goBack() { goBack() {
router.back() router.back()