feat: household TV companion with scoped pairing and direct LAN camera strip
This commit is contained in:
@@ -42,6 +42,7 @@ import backups
|
||||
import baidu
|
||||
import journal
|
||||
import ptz
|
||||
import displays
|
||||
|
||||
ROOT = Path(__file__).resolve().parent
|
||||
os.umask(0o077)
|
||||
@@ -93,6 +94,7 @@ BACKUPS = backups.Backups(sys.modules[__name__])
|
||||
BAIDU = baidu.Baidu(sys.modules[__name__])
|
||||
JOURNAL = journal.Journal(sys.modules[__name__])
|
||||
CLOUD_IMAGES = cloud_images.CloudImages(sys.modules[__name__])
|
||||
DISPLAYS = displays.Displays(sys.modules[__name__])
|
||||
|
||||
|
||||
def setting(key, default=None):
|
||||
@@ -665,6 +667,12 @@ class Handler(BaseHTTPRequestHandler):
|
||||
def do_POST(self):
|
||||
try:
|
||||
path = url.urlsplit(self.path).path
|
||||
if path in ('/api/display/heartbeat','/api/display/preview'):
|
||||
authorization = self.headers.get('Authorization','')
|
||||
grant = DISPLAYS.authorize(authorization[7:] if authorization.startswith('Bearer ') else '')
|
||||
data = self.body(250000 if path.endswith('preview') else 16384)
|
||||
self.answer(DISPLAYS.accept_preview(grant,data) if path.endswith('preview') else DISPLAYS.heartbeat(grant,data))
|
||||
return
|
||||
if path == '/api/journal/chunk':
|
||||
self.validate_origin(); self.require_auth()
|
||||
query = dict(url.parse_qsl(url.urlsplit(self.path).query))
|
||||
@@ -680,6 +688,9 @@ class Handler(BaseHTTPRequestHandler):
|
||||
# 12,000 Chinese characters plus JSON escapes and linked people exceed 16 KiB.
|
||||
data = self.body(3*1024*1024 if path in ('/api/appearance','/api/households') else 131072 if path in ('/api/family/event','/api/heritage','/api/journal') else 16384)
|
||||
self.validate_origin()
|
||||
if path == '/api/display/pair':
|
||||
self.answer(DISPLAYS.pair(data, self.client_address[0]))
|
||||
return
|
||||
if path in ('/api/login', '/api/setup'):
|
||||
self.login(data, path == '/api/setup')
|
||||
return
|
||||
@@ -775,6 +786,10 @@ class Handler(BaseHTTPRequestHandler):
|
||||
self.answer(HERITAGE.save(data, self.user))
|
||||
elif path == '/api/devices':
|
||||
self.answer(INVENTORY.save(data, self.user))
|
||||
elif path == '/api/devices/transfer':
|
||||
self.answer(INVENTORY.transfer(data, self.user))
|
||||
elif path == '/api/displays':
|
||||
self.answer(DISPLAYS.manage(self.user, data))
|
||||
elif path in ('/api/family/person', '/api/family/link', '/api/family/event'):
|
||||
self.answer(FAMILY.save(path.rsplit('/', 1)[-1], data, self.user))
|
||||
elif path == '/api/sites':
|
||||
@@ -947,12 +962,33 @@ class Handler(BaseHTTPRequestHandler):
|
||||
if path == '/healthz':
|
||||
self.answer({'status': 'running', 'version': (ROOT / 'VERSION').read_text().strip()})
|
||||
return
|
||||
if path in ('/api/display/snapshot', '/api/display/cameras'):
|
||||
authorization = self.headers.get('Authorization', '')
|
||||
token = authorization[7:] if authorization.startswith('Bearer ') else ''
|
||||
if path.endswith('/cameras'):
|
||||
self.answer(DISPLAYS.cameras(DISPLAYS.authorize_native(token)))
|
||||
else:
|
||||
self.answer(DISPLAYS.snapshot(DISPLAYS.authorize(token)))
|
||||
return
|
||||
if path.startswith(('/api/', '/media/')):
|
||||
self.require_auth()
|
||||
if path == '/api/state':
|
||||
self.answer(ACCOUNTS.state(self.user, status()))
|
||||
elif path == '/api/devices/network':
|
||||
self.require_manager(); self.answer(NETWORK.read(self.user, query))
|
||||
elif path == '/api/displays':
|
||||
self.require_manager(); self.answer(DISPLAYS.listing(self.user, query.get('deviceId')))
|
||||
elif path == '/api/displays/hardware':
|
||||
self.require_manager(); self.answer(DISPLAYS.hardware(self.user, query.get('deviceId')))
|
||||
elif path == '/api/displays/preview':
|
||||
self.require_manager()
|
||||
at, raw = DISPLAYS.preview(self.user, query.get('deviceId'))
|
||||
self.send_response(200)
|
||||
self.send_header('Content-Type','image/jpeg')
|
||||
self.send_header('Content-Length',str(len(raw)))
|
||||
self.send_header('Cache-Control','no-store')
|
||||
self.send_header('X-Content-Type-Options','nosniff')
|
||||
self.end_headers(); self.wfile.write(raw)
|
||||
elif path == '/api/baidu/config':
|
||||
self.require_admin(); self.answer(BAIDU.public_config())
|
||||
elif path == '/api/baidu/status':
|
||||
@@ -1100,8 +1136,10 @@ class Handler(BaseHTTPRequestHandler):
|
||||
'/monitor.js': 'monitor.js', '/calendar.js': 'calendar.js', '/date-fields.js': 'date-fields.js',
|
||||
'/zhao-icon.png': 'zhao-icon.png',
|
||||
'/vendor/hls.min.js': 'vendor/hls.min.js'}
|
||||
if re.fullmatch(r'/ui/assets/[A-Za-z0-9_-]+\.(?:js|css)', path):
|
||||
if re.fullmatch(r'/ui/assets/[A-Za-z0-9_.-]+\.(?:js|css)', path):
|
||||
files[path] = path[1:]
|
||||
if path in ('/tv', '/tv/'):
|
||||
files[path] = 'ui/tv.html'
|
||||
if path not in files or not (ROOT / 'web' / files[path]).is_file():
|
||||
raise Problem('页面不存在', 404)
|
||||
file = ROOT / 'web' / files[path]
|
||||
@@ -1131,6 +1169,7 @@ def initialize():
|
||||
FAMILY.initialize()
|
||||
ACCOUNTS.initialize()
|
||||
HOUSEHOLDS.initialize()
|
||||
DISPLAYS.initialize()
|
||||
HERITAGE.initialize()
|
||||
JOURNAL.initialize()
|
||||
if not setting('storage'):
|
||||
@@ -1149,6 +1188,7 @@ if __name__ == '__main__':
|
||||
threading.Thread(target=storage_watch, daemon=True).start()
|
||||
threading.Thread(target=media_config_watch, daemon=True).start()
|
||||
threading.Thread(target=BACKUPS.watch, daemon=True).start()
|
||||
threading.Thread(target=DISPLAYS.watch, daemon=True).start()
|
||||
bindings = [h.strip() for h in os.environ.get('VISION_BIND', '127.0.0.1').split(',') if h.strip()]
|
||||
port = int(os.environ.get('VISION_PORT', '8790'))
|
||||
servers = []
|
||||
|
||||
Reference in New Issue
Block a user