feat(location): durable offline GPS outbox and hourly daily uploads v0.1.50
This commit is contained in:
+22
-9
@@ -4,11 +4,12 @@ import json
|
||||
import math
|
||||
import secrets
|
||||
import time
|
||||
from location_sync import Sync, interval_valid
|
||||
|
||||
|
||||
class Locations:
|
||||
def __init__(self, app):
|
||||
self.a=app; self.codes={}; self.attempts={}; self.latest={}
|
||||
self.a=app; self.codes={}; self.attempts={}; self.latest={}; self.sync=Sync(app)
|
||||
|
||||
def initialize(self):
|
||||
with self.a.LOCK,self.a.DB:
|
||||
@@ -19,6 +20,7 @@ class Locations:
|
||||
if self.a.DB.execute("SELECT name FROM sqlite_master WHERE name='location_shares'").fetchone():
|
||||
self.a.DB.execute('UPDATE location_shares SET enabled=0')
|
||||
self.codes.clear(); self.latest.clear(); self.attempts.clear()
|
||||
self.sync.initialize()
|
||||
|
||||
def eligible(self, family, key):
|
||||
try: return self.a.MEMBERS.member(family,key,active=True)
|
||||
@@ -116,6 +118,7 @@ class Locations:
|
||||
return dict(token=token,**{k:grant[k] for k in ('home','memberName','viewers','displays','phones','viewOnly','intervalSeconds','expires','receipt','consentProtocol')})
|
||||
|
||||
def native(self, token, body):
|
||||
if body.get('action')=='history-batch':return self.sync.batch(token,body)
|
||||
if body.get('action') in ('history-list','history-delete'):
|
||||
return self.a.HISTORY.native(token,body)
|
||||
result=self._native(token,body)
|
||||
@@ -125,7 +128,7 @@ class Locations:
|
||||
if row:
|
||||
fix=self.latest.get(row['id'],{}).get('fix');grant=json.loads(row['grant_json'])
|
||||
if fix and fix['at']==body['position']['at']:
|
||||
self.a.HISTORY.record(row,grant,fix)
|
||||
if not body.get('bufferedHistory'):self.a.HISTORY.record(row,grant,fix)
|
||||
else:fix=None
|
||||
else:fix=None
|
||||
if fix:
|
||||
@@ -141,6 +144,7 @@ class Locations:
|
||||
if not row:raise a.Problem('手机绑定已解除,请重新配对',401)
|
||||
sid=row['id'];action=body.get('action');grant=json.loads(row['grant_json'])
|
||||
if action in ('stop','revoke'):
|
||||
self.sync.close(sid)
|
||||
if action=='revoke':a.DB.execute('DELETE FROM member_location_shares WHERE id=?',(sid,))
|
||||
else:a.DB.execute('UPDATE member_location_shares SET enabled=0 WHERE id=?',(sid,))
|
||||
self.latest.pop(sid,None);return {'ok':True}
|
||||
@@ -154,27 +158,32 @@ class Locations:
|
||||
viewerIds=[v['id'] for v in grant['viewers']],displayIds=[v['id'] for v in grant['displays']],
|
||||
phoneIds=[v['id'] for v in grant.get('phones',[])],receipt=grant['receipt'],
|
||||
intervalSeconds=grant.get('intervalSeconds',300),viewOnly=grant.get('viewOnly',False),
|
||||
sampleIntervalSeconds=grant.get('sampleIntervalSeconds',60),
|
||||
historyEnabled=grant.get('historyEnabled',False),historyDefault=grant.get('historyDefault',False) and 'historyEnabled' not in grant,
|
||||
history=self.a.HISTORY.own_status(row['member_id']))
|
||||
if action=='configure':
|
||||
if body.get('consent') is not True or body.get('receipt')!=grant['receipt']:raise a.Problem('请本人确认新的共享设置',403)
|
||||
chosen=self.select_recipients(row['family_id'],body,own_id=sid)
|
||||
interval=body.get('intervalSeconds',300)
|
||||
if type(interval) is not int or interval not in (10,60,300):raise a.Problem('请选择实时、1 分钟或 5 分钟')
|
||||
if not interval_valid(interval):raise a.Problem('上传周期可选 10 秒、1/5 分钟,或 1 至 720 小时(最多 30 天)')
|
||||
sample=body.get('sampleIntervalSeconds',60)
|
||||
if type(sample) is not int or sample not in (10,60,300):raise a.Problem('采集周期可选 10 秒、1 分钟或 5 分钟')
|
||||
if not any(chosen.values()):raise a.Problem('至少选择一位查看人;只查看家人无需开启自己的共享')
|
||||
history=body.get('historyEnabled',False)
|
||||
if type(history) is not bool or history and body.get('historyConsent') is not True:raise a.Problem('长期保存轨迹需要本人单独同意',403)
|
||||
grant.update(chosen,viewOnly=False,intervalSeconds=interval,consentProtocol=3,historyEnabled=history)
|
||||
self.sync.close(sid)
|
||||
grant.update(chosen,viewOnly=False,intervalSeconds=interval,sampleIntervalSeconds=sample,consentProtocol=3,historyEnabled=history)
|
||||
self.refresh_receipt(grant)
|
||||
a.DB.execute('UPDATE member_location_shares SET enabled=0,grant_json=? WHERE id=?',(json.dumps(grant),sid))
|
||||
self.latest.pop(sid,None)
|
||||
return dict(ok=True,receipt=grant['receipt'],intervalSeconds=interval,**chosen)
|
||||
return dict(ok=True,receipt=grant['receipt'],intervalSeconds=interval,sampleIntervalSeconds=sample,historyEnabled=history,**chosen)
|
||||
if action=='start':
|
||||
if grant.get('viewOnly'):raise a.Problem('此手机仅用于查看家人,请先由本人设置共享对象',403)
|
||||
if body.get('consent') is not True or body.get('receipt')!=grant['receipt']:raise a.Problem('请本人核对姓名及查看人,并同意开启共享',403)
|
||||
a.DB.execute('UPDATE member_location_shares SET enabled=1 WHERE id=?',(sid,))
|
||||
self.latest[sid]=dict(seen=0,started=now,fix=None,signal='waiting')
|
||||
return dict(ok=True,expires=row['expires'],intervalSeconds=grant.get('intervalSeconds',300))
|
||||
collection=self.sync.start(row,grant) if body.get('bufferedHistory') is True else {}
|
||||
return dict(ok=True,expires=row['expires'],intervalSeconds=grant.get('intervalSeconds',300),sampleIntervalSeconds=grant.get('sampleIntervalSeconds',60),**collection)
|
||||
if action!='update' or not row['enabled']:raise a.Problem('位置共享尚未开启',403)
|
||||
old=self.latest.get(sid,dict(started=now,fix=None))
|
||||
if now-old.get('seen',0)<10:raise a.Problem('位置更新过于频繁',429)
|
||||
@@ -189,6 +198,7 @@ class Locations:
|
||||
if not fix or pos['at']>fix['at']:fix={k:pos[k] for k in ('latitude','longitude','accuracy','at')}
|
||||
if signal in ('permission_denied','location_disabled'):fix=None
|
||||
self.latest[sid]=dict(started=old['started'],seen=now,fix=fix,signal=signal)
|
||||
self.sync.seen(sid,old['started'],now)
|
||||
return dict(ok=True,expires=row['expires'])
|
||||
|
||||
def listing(self, family, user=None, display_id=None, phone_id=None):
|
||||
@@ -213,12 +223,15 @@ class Locations:
|
||||
if user and user['id'] not in {v['id'] for v in grant['viewers']}:continue
|
||||
if display_id and display_id not in {v['id'] for v in grant['displays']}:continue
|
||||
if phone_id and phone_id!=row['id'] and phone_id not in {v['id'] for v in grant.get('phones',[])}:continue
|
||||
recent=self.latest.get(row['id'],{});fix=recent.get('fix');seen=recent.get('seen',0)
|
||||
recent=self.latest.get(row['id']) or self.sync.timing(row['id']);fix=recent.get('fix');seen=recent.get('seen',0)
|
||||
age=now-(fix['at'] if fix else recent.get('started',row['created']))
|
||||
state='stale' if age>900 or seen and now-seen>900 else 'live' if fix else 'waiting'
|
||||
scheduled=grant.get('intervalSeconds',300)>900 and now-(seen or recent.get('started',row['created']))<=grant['intervalSeconds']+900
|
||||
stale=age>900 or seen and now-seen>900
|
||||
state='scheduled' if stale and scheduled else 'stale' if stale else 'live' if fix else 'waiting'
|
||||
position=fix if fix and state=='live' else None
|
||||
items.append(dict(id=row['member_id'],personId=valid[1]['id'],name=valid[1]['name'],status=state,
|
||||
signal=recent.get('signal','waiting'),lastSeen=seen or None,position=position,expires=row['expires']))
|
||||
signal=recent.get('signal','waiting'),lastSeen=seen or None,position=position,expires=row['expires'],
|
||||
intervalSeconds=grant.get('intervalSeconds',300),nextUploadAt=(seen or recent.get('started',row['created']))+grant.get('intervalSeconds',300)))
|
||||
return items
|
||||
|
||||
def snapshot(self,user):
|
||||
|
||||
Reference in New Issue
Block a user