diff --git a/mapadroid/db/DbWebhookReader.py b/mapadroid/db/DbWebhookReader.py index 2689cfeb0..d23ddf324 100644 --- a/mapadroid/db/DbWebhookReader.py +++ b/mapadroid/db/DbWebhookReader.py @@ -7,10 +7,9 @@ from mapadroid.db.helper.PokemonHelper import PokemonHelper from mapadroid.db.helper.PokestopHelper import PokestopHelper from mapadroid.db.helper.RaidHelper import RaidHelper -from mapadroid.db.helper.StationHelper import StationHelper from mapadroid.db.helper.WeatherHelper import WeatherHelper from mapadroid.db.model import Raid, Gym, GymDetail, Weather, TrsQuest, Pokestop, Pokemon, TrsSpawn, PokemonDisplay, \ - PokestopIncident, Station + PokestopIncident from mapadroid.utils.WebhookJsonEncoder import WebhookJsonEncoder from mapadroid.utils.logging import get_logger, LoggerEnums from mapadroid.utils.madGlobals import MonSeenTypes @@ -198,35 +197,3 @@ async def get_mon_changed_since(session: AsyncSession, _timestamp: int, "size": mon.size }) return ret - - @staticmethod - async def get_stations_changed_since(session: AsyncSession, _timestamp: int): - logger.debug2("DbWebhookReader::get_stations_changed_since called with timestamp {}", _timestamp) - stations_changed: List[Station] = await StationHelper.get_changed_since(session, _timestamp=_timestamp) - - ret = [] - for station in stations_changed: - ret.append({ - "station_id": station.station_id, - "battle_level": station.battle_level, - "battle_spawn": int(station.battle_spawn.timestamp()) if station.battle_spawn else None, - "battle_start": int(station.battle_window_start.timestamp()) if station.battle_window_start else None, - "battle_end": int(station.battle_window_end.timestamp()) if station.battle_window_end else None, - "battle_pokemon_move_1": station.battle_pokemon_move_1, - "battle_pokemon_move_2": station.battle_pokemon_move_2, - "battle_pokemon_bread_mode": station.battle_pokemon_bread_mode, - "battle_pokemon_id": station.battle_pokemon_id, - "battle_pokemon_costume": station.battle_pokemon_costume, - "battle_pokemon_form": station.battle_pokemon_form, - "battle_pokemon_gender": station.battle_pokemon_gender, - "battle_pokemon_alignment": station.battle_pokemon_alignment, - "start_time": station.start_time, - "end_time": station.end_time, - "name": station.name, - "longitude": station.latitude, - "latitude": station.longitude, - "inactive": station.inactive, - "bread_battle_available": station.bread_battle_available, - "last_scanned": station.last_updated - }) - return ret diff --git a/mapadroid/webhook/webhookworker.py b/mapadroid/webhook/webhookworker.py index d81a63be5..5a32d3409 100644 --- a/mapadroid/webhook/webhookworker.py +++ b/mapadroid/webhook/webhookworker.py @@ -4,9 +4,12 @@ from asyncio import Task from typing import Any, Dict, List, Set, Tuple +from sqlalchemy.ext.asyncio import AsyncSession + from mapadroid.db.DbWebhookReader import DbWebhookReader from mapadroid.db.DbWrapper import DbWrapper -from mapadroid.db.model import Pokestop, TrsQuest +from mapadroid.db.helper.StationHelper import StationHelper +from mapadroid.db.model import Pokestop, TrsQuest, Station from mapadroid.geofence.geofenceHelper import GeofenceHelper from mapadroid.mapping_manager import MappingManager from mapadroid.utils.gamemechanicutil import calculate_mon_level @@ -119,7 +122,8 @@ async def __send_webhook(self, payloads): logger.success("Successfully sent payload to webhook{}{}. Stats: {}", whchunk_text, whcount_text, await mad_json_dumps(self.__payload_type_count(payload_chunk))) except Exception as e: - logger.warning("Exception occured while sending webhook: {}", e) + logger.warning("Exception occurred while sending webhook: {}", e) + logger.exception(e) current_pl_num += 1 current_wh_num += 1 @@ -687,11 +691,8 @@ async def __create_payload(self): try: # stations if 'station' in self.__webhook_types: - stations = self.__prepare_station_data( - await DbWebhookReader.get_stations_changed_since(session, self.__last_check) - ) + stations = self.__prepare_station_data(session, self.__last_check) full_payload += stations - # raids if 'raid' in self.__webhook_types: raids = self.__prepare_raid_data( @@ -769,57 +770,52 @@ async def __run_worker(self): logger.info("Stopping webhook worker thread") - def __prepare_station_data(self, station_data): + def __prepare_station_data(self, session: AsyncSession, _timestamp: int): + logger.debug2("WebhookWorker/DbWebhookReader::__prepare_stations_data called with timestamp {}", _timestamp) + ret = [] + stations_changed: List[Station] = await StationHelper.get_changed_since(session, _timestamp=_timestamp) - for station in station_data: - if self.__is_in_excluded_area([station["latitude"], station["longitude"]]): + for station in stations_changed: + if self.__is_in_excluded_area([station.latitude, station.longitude]): continue station_payload = { - "station_id": station["station_id"], - "latitude": station["latitude"], - "longitude": station["longitude"], - "start": station["start_time"], - "end": station["end_time"], - "name": station["name"], - "inactive": station["inactive"], - "bread_battle_available": station["bread_battle_available"], - "last_updated": station["last_scanned"], + "station_id": station.station_id, + "latitude": station.latitude, + "longitude": station.longitude, + "start": station.start_time, + "end": station.end_time, + "name": station.name, + "inactive": station.inactive, + "bread_battle_available": station.bread_battle_available, + "last_updated": station.last_updated, } - if station["battle_spawn"] is not None: - station_payload["battle_spawn"] = station["battle_spawn"] - station_payload["battle_start"] = station["battle_start"] - station_payload["battle_end"] = station["battle_end"] - station_payload["battle_level"] = station["battle_level"] - - if station["battle_pokemon_id"] is not None: - station_payload["battle_pokemon_id"] = station["battle_pokemon_id"] - - if station["battle_pokemon_form"] is not None: - station_payload["battle_pokemon_form"] = station["battle_pokemon_form"] - - if station["battle_pokemon_gender"] is not None: - station_payload["battle_pokemon_gender"] = station["battle_pokemon_gender"] - - if station["battle_pokemon_costume"] is not None: - station_payload["battle_pokemon_costume"] = station["battle_pokemon_costume"] + if station.battle_spawn is not None: + station_payload["battle_spawn"] = station.battle_spawn + station_payload["battle_start"] = station.battle_window_start.timestamp() + station_payload["battle_end"] = station.battle_window_end.timestamp() + station_payload["battle_level"] = station.battle_level - if station["battle_pokemon_alignment"] is not None: - station_payload["battle_pokemon_alignment"] = station["battle_pokemon_alignment"] + if station.battle_pokemon_id is not None: + station_payload["battle_pokemon_id"] = station.battle_pokemon_id + station_payload["battle_pokemon_move_1"] = station.battle_pokemon_move_1 + station_payload["battle_pokemon_move_2"] = station.battle_pokemon_move_2 + station_payload["battle_pokemon_bread_mode"] = station.battle_pokemon_bread_mode - if station["battle_pokemon_move_1"] is not None: - station_payload["battle_pokemon_move_1"] = station["battle_pokemon_move_1"] + # Those could be nulls so to avoid sending 0/false we don't include it at all. + if station.battle_pokemon_form is not None: + station_payload["battle_pokemon_form"] = station.battle_pokemon_form - if station["battle_pokemon_move_2"] is not None: - station_payload["battle_pokemon_move_2"] = station["battle_pokemon_move_2"] + if station.battle_pokemon_gender is not None: + station_payload["battle_pokemon_gender"] = station.battle_pokemon_gender - if station["battle_pokemon_bread_mode"] is not None: - station_payload["battle_pokemon_bread_mode"] = station["battle_pokemon_bread_mode"] + if station.battle_pokemon_costume is not None: + station_payload["battle_pokemon_costume"] = station.battle_pokemon_costume - if station["battle_pokemon_bread_mode"] is not None: - station_payload["battle_pokemon_bread_mode"] = station["battle_pokemon_bread_mode"] + if station.battle_pokemon_alignment is not None: + station_payload["battle_pokemon_alignment"] = station.battle_pokemon_alignment # create final message entire_payload = {"type": "station", "message": station_payload}