Push geo-filter: Giftköder-Alert nur im 30km-Radius, Standort via Alerts-Check gespeichert
This commit is contained in:
parent
9213b58d3c
commit
b5e4eab84d
4 changed files with 44 additions and 5 deletions
|
|
@ -140,3 +140,26 @@ def send_push_to_all(payload: dict):
|
|||
sent += 1
|
||||
logger.info(f"Push an {sent}/{len(rows)} Subscriptions gesendet.")
|
||||
return sent
|
||||
|
||||
|
||||
def send_push_nearby(lat: float, lon: float, radius_m: float, payload: dict):
|
||||
"""Schickt Push nur an User deren letzter bekannter Standort innerhalb radius_m liegt.
|
||||
User ohne gespeicherten Standort werden übersprungen."""
|
||||
import math
|
||||
def _dist(la1, lo1, la2, lo2):
|
||||
R = 6_371_000
|
||||
p1, p2 = math.radians(la1), math.radians(la2)
|
||||
a = math.sin(math.radians(la2-la1)/2)**2 + math.cos(p1)*math.cos(p2)*math.sin(math.radians(lo2-lo1)/2)**2
|
||||
return 2*R*math.asin(math.sqrt(a))
|
||||
|
||||
with db() as conn:
|
||||
rows = conn.execute(
|
||||
"SELECT * FROM push_subscriptions WHERE last_lat IS NOT NULL"
|
||||
).fetchall()
|
||||
sent = 0
|
||||
for row in rows:
|
||||
if _dist(lat, lon, row["last_lat"], row["last_lon"]) <= radius_m:
|
||||
if send_push(row, payload):
|
||||
sent += 1
|
||||
logger.info(f"Push nearby ({radius_m/1000:.0f}km): {sent}/{len(rows)} gesendet.")
|
||||
return sent
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue