feat: household TV companion with scoped pairing and direct LAN camera strip
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"""Common device responsibility fields and non-video device records."""
|
||||
import ipaddress
|
||||
import re
|
||||
import json
|
||||
class Inventory:
|
||||
def __init__(self, app):
|
||||
self.a = app
|
||||
@@ -92,3 +93,40 @@ class Inventory:
|
||||
a.save_object('devices', item)
|
||||
a.audit('保存设备档案', item['name'])
|
||||
return item
|
||||
|
||||
def transfer(self, data, user):
|
||||
a = self.a
|
||||
if user.get('role') != 'admin':
|
||||
raise a.Problem('跨家庭转移需要超级管理员同时管理双方家庭',403)
|
||||
table = {'device':'devices','camera':'assets','recorder':'recorders'}.get(data.get('source'))
|
||||
if not table: raise a.Problem('请选择设备类型')
|
||||
with a.LOCK:
|
||||
item = a.HOUSEHOLDS.owns(user, a.get_object(table,data.get('id')))
|
||||
target = a.get_object('households',data.get('targetFamilyId'))
|
||||
if not target: raise a.Problem('目标家庭不存在',404)
|
||||
if target['id'] == a.HOUSEHOLDS.family_of(item): raise a.Problem('设备已经属于这个家庭')
|
||||
site = a.get_object('sites',data.get('targetSiteId')) if data.get('targetSiteId') else None
|
||||
if data.get('targetSiteId') and (not site or a.HOUSEHOLDS.family_of(site)!=target['id']):
|
||||
raise a.Problem('目标区域不属于所选家庭')
|
||||
channels = [c for c in a.objects('cameras') if table=='assets' and c.get('assetId')==item['id']
|
||||
or table=='recorders' and (c.get('recorderId')==item['id'] or c.get('archiveRecorderId')==item['id'])]
|
||||
if table=='recorders' and channels:
|
||||
raise a.Problem('录像机仍关联摄像头,请先在镜头设置解除取流及录像来源关联,再转移')
|
||||
if table=='assets' and any(c.get('recorderId') or c.get('archiveRecorderId') for c in channels):
|
||||
raise a.Problem('摄像头仍关联原家庭录像机,请先解除录像机关联,再转移')
|
||||
old_family = a.HOUSEHOLDS.family_of(item)
|
||||
with a.DB:
|
||||
for row_table, row in [(table,item)]+[('cameras',c) for c in channels]:
|
||||
updated = dict(row,familyId=target['id'],siteId=site['id'] if site else '',
|
||||
uplinkId='',ownerRef='',ownerName='',managerRef='',managerName='')
|
||||
a.DB.execute('UPDATE '+row_table+' SET body=? WHERE id=?',(json.dumps(updated),row['id']))
|
||||
# Do not leave topology links crossing family boundaries.
|
||||
for other_table in ('devices','assets','recorders'):
|
||||
for child in a.objects(other_table):
|
||||
if child.get('uplinkId') == item['id']:
|
||||
child['uplinkId']=''
|
||||
a.DB.execute('UPDATE '+other_table+' SET body=? WHERE id=?',(json.dumps(child),child['id']))
|
||||
a.DB.execute('UPDATE household_displays SET revoked=1 WHERE device_id=?',(item['id'],))
|
||||
a.audit('转移设备家庭',item['name']+':'+old_family+' → '+target['name'])
|
||||
if channels: a.write_media_config()
|
||||
return {'ok':True,'id':item['id'],'familyId':target['id']}
|
||||
|
||||
Reference in New Issue
Block a user