feat: household TV companion with scoped pairing and direct LAN camera strip

This commit is contained in:
Codex
2026-10-16 00:35:17 +08:00
parent fb87ae4de9
commit 6aa20391b2
42 changed files with 1153 additions and 33 deletions
@@ -7,6 +7,8 @@ import {
} from "../composables/bridge.js";
import AppIcon from "./AppIcon.vue";
import NetworkStatus from "./NetworkStatus.vue";
import DisplayManager from './DisplayManager.vue';
import DeviceTransfer from './DeviceTransfer.vue';
const channels = (d) =>
(snapshot.value.state.cameras || []).filter((c) =>
d.source === "camera"
@@ -118,6 +120,8 @@ const monitored = (d) =>
snapshot.page === 'objects' && deviceView.mode === 'cards'
"
/>
<DisplayManager v-if="d.source==='device' && /电视|显示|大屏/.test(d.deviceType)" :device="d"/>
<DeviceTransfer :device="d"/>
</div>
<div class="card-footer device-card-actions">
<button
@@ -1,5 +1,7 @@
<script setup>
import {snapshot} from '../composables/bridge.js'
import NetworkStatus from './NetworkStatus.vue'
import DisplayManager from './DisplayManager.vue'
import DeviceTransfer from './DeviceTransfer.vue'
</script>
<template><section v-if="snapshot.admin&&snapshot.device&&snapshot.page==='device'" class="panel"><h3>网络状态与管理入口</h3><NetworkStatus :device="snapshot.device"/><p class="description mb-0">只检查已登记的设备地址。常用端口未响应不一定表示断电;Tailscale 节点在线也不代表下级网段可达。</p></section></template>
<template><section v-if="snapshot.admin&&snapshot.device&&snapshot.page==='device'" class="panel"><h3>网络状态与管理入口</h3><DisplayManager v-if="snapshot.device.source==='device' && /电视|显示|大屏/.test(snapshot.device.deviceType)" :device="snapshot.device"/><NetworkStatus v-else :device="snapshot.device"/><DeviceTransfer :device="snapshot.device"/></section></template>
@@ -0,0 +1,11 @@
<script setup>
import {ref,computed,watch} from 'vue'
import {snapshot,bridge} from '../composables/bridge.js'
const props=defineProps({device:Object})
const open=ref(false),target=ref(''),confirm=ref(false),busy=ref(false),message=ref('')
const targets=computed(()=>(snapshot.value.state.households||[]).filter(h=>h.id!==snapshot.value.state.familyId))
watch(()=>snapshot.value.state.familyId,()=>{open.value=false;target.value='';confirm.value=false;message.value=''})
async function transfer(){if(!confirm.value||!target.value)return;busy.value=true;message.value=''
try{await bridge().api('/api/devices/transfer',{source:props.device.source,id:props.device.id,targetFamilyId:target.value});bridge().notify('设备已转移,电视需在新家庭重新授权。');open.value=false;await bridge().refresh()}catch(e){message.value=e.message}finally{busy.value=false}}
</script>
<template><div v-if="snapshot.user?.role==='admin'" class="mt-3"><button class="btn btn-sm btn-outline-secondary" @click="open=!open">转移到其他家庭</button><div v-if="open" class="border rounded p-3 mt-2"><label class="w-100">目标家庭 / 公司 / 工厂<select v-model="target" class="form-select mt-2"><option value="">请选择</option><option v-for="h in targets" :key="h.id" :value="h.id">{{h.name}}</option></select></label><p class="small text-secondary mt-2">保留设备编号与基本档案。所属区域、负责人和上级设备需重新设置;电视旧展示授权会停用。关联录像机的摄像头需先解除关联。</p><label class="d-flex gap-2 small mb-2"><input v-model="confirm" type="checkbox">确认将这台设备转移给目标家庭管理</label><button class="btn btn-primary btn-sm" :disabled="busy||!confirm||!target" @click="transfer">确认转移</button></div><p v-if="message" class="small mt-2" role="status">{{message}}</p></div></template>
@@ -0,0 +1,45 @@
<script setup>
import {ref,watch,onUnmounted,computed} from 'vue'
import {snapshot,bridge} from '../composables/bridge.js'
const props=defineProps({device:Object})
const open=ref(false),result=ref(null),grants=ref([]),error=ref(''),busy=ref(false),link=ref(''),preview=ref(''),previewUntil=ref(0)
let timer,epoch=0,blob=''
const home=computed(()=>snapshot.value.state.households?.find(h=>h.id===snapshot.value.state.familyId))
const city=ref(''),latitude=ref(''),longitude=ref('')
const pairingCode=ref('')
const endpoint=path=>path+'?deviceId='+encodeURIComponent(props.device.id)
const hardware=computed(()=>result.value?.hardware||{})
const number=(value,digits=1)=>typeof value==='number'?value.toFixed(digits):'未提供'
const formatTime=value=>value?new Date(value*1000).toLocaleString('zh-CN'):'尚未上报'
async function reload(){const e=epoch;if(!open.value)return
try{const value=await bridge().api(endpoint('/api/displays/hardware'));if(e!==epoch)return;result.value=value
if(Date.now()<previewUntil.value&&value.previewAt){const response=await fetch(endpoint('/api/displays/preview'),{headers:{'X-Household-Id':snapshot.value.state.familyId},cache:'no-store'});if(response.ok){const image=await response.blob();if(e!==epoch)return;if(blob)URL.revokeObjectURL(blob);blob=URL.createObjectURL(image);preview.value=blob}}
}catch(e2){if(e===epoch)error.value=e2.message}finally{if(e===epoch&&open.value)timer=setTimeout(reload,5000)}
}
async function change(action,id){busy.value=true;error.value='';const e=epoch
try{const value=await bridge().api('/api/displays',{action,deviceId:props.device.id,id,city:city.value,latitude:latitude.value,longitude:longitude.value});if(e!==epoch)return
if(value.path)link.value=location.origin+value.path
if(value.pairingCode)pairingCode.value=value.pairingCode
if(action==='preview')previewUntil.value=Date.now()+60000
if(action==='revoke'){link.value='';pairingCode.value='';preview.value='';previewUntil.value=0}
grants.value=(await bridge().api(endpoint('/api/displays'))).items;clearTimeout(timer);reload()
}catch(e2){if(e===epoch)error.value=e2.message}finally{if(e===epoch)busy.value=false}
}
watch(open,async value=>{epoch++;clearTimeout(timer);link.value='';error.value='';preview.value='';previewUntil.value=0;if(blob){URL.revokeObjectURL(blob);blob=''}if(value){city.value=home.value?.city||'';latitude.value=city.value==='济南市'?'36.67':'';longitude.value=city.value==='济南市'?'116.99':'';reload();try{grants.value=(await bridge().api(endpoint('/api/displays'))).items}catch(e){error.value=e.message}}})
watch(()=>[snapshot.value.state.familyId,snapshot.value.session,snapshot.value.page,props.device.id].join('|'),()=>open.value=false)
onUnmounted(()=>{epoch++;clearTimeout(timer);if(blob)URL.revokeObjectURL(blob)})
</script>
<template><div v-if="snapshot.admin" class="display-manager mt-3"><button class="btn btn-outline-primary btn-sm" @click="open=!open">{{open?'收起电视管理':'电视管理 · 状态与模拟屏'}}</button>
<section v-if="open" class="border rounded-3 p-3 mt-3">
<div class="d-flex justify-content-between align-items-center gap-2 mb-3"><strong>{{device.name}}</strong><span class="badge" :class="result?.online?'text-bg-success':'text-bg-secondary'">{{result?.online?'在线 · 心跳正常':result?.paired?'未收到心跳':'尚未绑定'}}</span></div>
<p class="small text-secondary">所属:{{home?.name}} · 最后心跳 {{formatTime(result?.lastSeen)}}</p>
<div class="row g-2 hardware-grid"><div class="col-6"><span>处理器温度</span><strong>{{number(hardware.temperatureC)}}{{hardware.temperatureC!=null?' °C':''}}</strong><small>{{hardware.temperatureSource||'等待传感器上报'}}</small></div><div class="col-6"><span>整机功率</span><strong>{{number(hardware.powerW)}}{{hardware.powerW!=null?' W':''}}</strong><small>{{hardware.powerSource||'需设备传感器或计量插座'}}</small></div><div class="col-6"><span>可用内存</span><strong>{{number(hardware.memoryAvailableMB,0)}} / {{number(hardware.memoryTotalMB,0)}} MB</strong></div><div class="col-6"><span>可用存储</span><strong>{{number(hardware.storageAvailableGB)}} / {{number(hardware.storageTotalGB)}} GB</strong></div></div>
<p class="small text-secondary mt-3">{{hardware.model||device.model}} · Android {{hardware.osVersion||'—'}} · IP {{hardware.ip||device.managementHost||'—'}}<br>{{hardware.screenWidth||'—'}} × {{hardware.screenHeight||'—'}} · {{hardware.foreground?'看板在前台':'看板未在前台'}} · {{hardware.screenOn?'屏幕亮':'屏幕状态待确认'}}<span v-if="!result?.online"> · 当前数值为最后一次上报</span></p>
<h4 class="fs-6">异常与报警</h4><p v-if="!result?.alerts?.some(a=>!a.cleared)" class="small text-secondary">{{result?.online?'当前无活动报警':'连接恢复后继续检查硬件状态'}}</p><div v-for="a in result?.alerts?.filter(a=>!a.cleared)" :key="a.code" class="alert py-2 small" :class="a.severity==='critical'?'alert-danger':'alert-warning'">{{a.message}} · {{formatTime(a.opened)}}</div>
<details class="small mb-3"><summary>报警记录与规则</summary><p>心跳中断超过 100 秒;处理器 ≥75°C 提醒、≥85°C 严重;可用内存低于 5%;可用存储低于 0.5 GB;看板退出前台。数值来自电视上报。</p><div v-for="(a,i) in result?.alerts" :key="i">{{a.cleared?'已恢复':'处理中'}} · {{a.message}} · {{formatTime(a.opened)}}</div></details>
<button class="btn btn-primary btn-sm" :disabled="busy||!result?.online" @click="change('preview')">查看模拟屏 · 60 秒</button><p class="small text-secondary mt-2">显示电视当前家庭看板的应用画面,约 20 秒更新一次;退出看板后不显示其他应用。预览仅短时保存在内存。</p><figure v-if="preview" class="tv-remote-preview"><img :src="preview" alt="电视家庭看板的真实应用画面"><figcaption class="small text-secondary">画面时间:{{formatTime(result?.previewAt)}}</figcaption></figure>
<details class="mt-3"><summary>展示授权与天气设置</summary><div class="row g-2 my-2"><label class="col-12">天气城市<input v-model="city" class="form-control" maxlength="60"></label><label class="col-6">纬度<input v-model="latitude" class="form-control" type="number" step="0.0001" min="-90" max="90"></label><label class="col-6">经度<input v-model="longitude" class="form-control" type="number" step="0.0001" min="-180" max="180"></label></div><p class="small text-secondary">链接有效期一年,仅可展示本家庭概况、设备状态与已发布日程。重新生成会停用旧链接。</p><button class="btn btn-outline-primary btn-sm" :disabled="busy" @click="change('create')">生成新的电视展示链接</button><div v-if="link" class="my-3"><label>本次展示链接(请仅交给这台电视)<input class="form-control" :value="link" readonly @focus="$event.target.select()"></label><a class="btn btn-sm btn-outline-secondary mt-2" :href="link" target="_blank" rel="noreferrer">打开看板</a></div><div v-for="g in grants.filter(g=>!g.revoked)" :key="g.id" class="d-flex gap-2 align-items-center small mt-2"><span>授权至 {{formatTime(g.expires_at)}}</span><button class="btn btn-sm btn-outline-danger" :disabled="busy" @click="change('revoke',g.id)">停用展示权限</button></div></details>
<div v-if="pairingCode&&link" class="alert alert-success mt-3">App 配对码:<strong class="fs-3 font-monospace">{{pairingCode}}</strong><div class="small">在“赵府家庭屏”App 输入,10 分钟内有效,使用一次即失效。App 摄像头画面在本地网络直接播放。</div></div>
<p v-if="error" class="text-danger mt-2" role="alert">{{error}}</p>
</section></div></template>
<style scoped>.hardware-grid>div{display:flex;flex-direction:column;gap:4px;background:#f5f7f7;padding:12px;border:3px solid white;border-radius:12px}.hardware-grid span,.hardware-grid small{font-size:12px;color:#637471}.hardware-grid strong{font-size:18px}.tv-remote-preview{margin:16px 0;text-align:center}.tv-remote-preview img{max-height:640px;max-width:100%;border:6px solid #303b38;border-radius:12px}.display-manager summary{cursor:pointer}details p{margin-top:12px}</style>