feat: wire watch pages to @system.interconnect bridge
This commit is contained in:
+87
-10
@@ -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) }
|
||||
|
||||
+23
-11
@@ -50,17 +50,14 @@
|
||||
|
||||
<script>
|
||||
import router from '@system.router'
|
||||
import bridge from '../../common/bridge.js'
|
||||
|
||||
export default {
|
||||
private: {
|
||||
currentIndex: 1,
|
||||
sessions: [
|
||||
{ sessionId: 'demo-1', title: '原型演示会话', running: false },
|
||||
{ sessionId: 'demo-2', title: 'UI 对齐中', running: true },
|
||||
{ sessionId: 'demo-3', title: '待接入 Harness', running: false }
|
||||
],
|
||||
sessions: [],
|
||||
statusText: '未连接',
|
||||
activeTitle: '会话 demo-1',
|
||||
activeTitle: '未选择会话',
|
||||
settings: {
|
||||
serverUrl: 'ws://127.0.0.1:8787',
|
||||
userId: '',
|
||||
@@ -70,12 +67,19 @@ export default {
|
||||
},
|
||||
|
||||
onInit() {
|
||||
// 空壳:暂无逻辑
|
||||
// 空壳:仅预留
|
||||
},
|
||||
|
||||
onReady() {
|
||||
// 空壳:后续在此初始化 interconnect
|
||||
this.refreshConn()
|
||||
bridge.init({
|
||||
status: (s) => {
|
||||
this.statusText = s
|
||||
this.connText = s
|
||||
},
|
||||
message: (msg) => {
|
||||
this.handleBridgeMessage(msg)
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
onSwipeChange(e) {
|
||||
@@ -116,8 +120,16 @@ export default {
|
||||
onTokenChange(e) { this.settings.token = e && e.value || '' },
|
||||
|
||||
refreshConn() {
|
||||
// 空壳:后续通过 interconnect getReadyState 获取真实连接状态
|
||||
this.connText = '手机/手表未连接'
|
||||
bridge.refreshConn()
|
||||
},
|
||||
|
||||
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>
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
<script>
|
||||
import router from '@system.router'
|
||||
import bridge from '../../common/bridge.js'
|
||||
|
||||
export default {
|
||||
private: {
|
||||
@@ -27,13 +28,11 @@ export default {
|
||||
},
|
||||
sendQuick(index) {
|
||||
const item = this.quick[index]
|
||||
// 空壳:后续通过 interconnect 发给手机/服务器
|
||||
console.log('quick send', item)
|
||||
bridge.sendCommand({ kind: 'quick', quick: item, content: item })
|
||||
this.text = item
|
||||
},
|
||||
sendText() {
|
||||
// 空壳:后续通过 interconnect 发送
|
||||
console.log('send text', this.text)
|
||||
bridge.sendText(this.text)
|
||||
},
|
||||
goBack() {
|
||||
router.back()
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
<script>
|
||||
import router from '@system.router'
|
||||
import bridge from '../../common/bridge.js'
|
||||
|
||||
export default {
|
||||
private: {
|
||||
@@ -31,8 +32,7 @@ export default {
|
||||
onUserChange(e) { this.userId = e && e.value || '' },
|
||||
onTokenChange(e) { this.token = e && e.value || '' },
|
||||
save() {
|
||||
// 空壳:后续持久化并与 index 页同步
|
||||
console.log('save settings', this.serverUrl, this.userId)
|
||||
bridge.sendSettings({ serverUrl: this.serverUrl, userId: this.userId, token: this.token })
|
||||
},
|
||||
goBack() {
|
||||
router.back()
|
||||
|
||||
Reference in New Issue
Block a user