def get_geofenced_coordinates(self, coordinates): # Import: We are working with n-tuples in some functions be carefull # and do not break compatibility logger.debug('Using matplotlib: {}.', self.use_matplotlib) logger.debug('Found {} coordinates to geofence.', len(coordinates)) geofenced_coordinates = [] for c in coordinates: # Coordinate is not valid if in one excluded area. if self._is_excluded(c): continue # Coordinate is geofenced if in one geofenced area. if self.geofenced_areas: for va in self.geofenced_areas: if self._in_area(c, va): geofenced_coordinates.append(c) break else: geofenced_coordinates.append(c) logger.debug2("Geofenced to {} coordinates", len(geofenced_coordinates)) return geofenced_coordinates
def clean_up_user(self, worker_id, worker_instance): logger.debug2("Cleanup of {} called with ref {}".format( worker_id, str(worker_instance))) with self.__loop_mutex: future = asyncio.run_coroutine_threadsafe( self.__internal_clean_up_user(worker_id, worker_instance), self.__loop) future.result()
def get_devicesettings_value(self, key: str, default_value: object = None): logger.debug2("Fetching devicemappings of {}".format(self._id)) try: devicemappings: Optional[dict] = self._mapping_manager.get_devicemappings_of(self._id) except (EOFError, FileNotFoundError) as e: logger.warning("Failed fetching devicemappings in worker {} with description: {}. Stopping worker" .format(str(self._id), str(e))) return None if devicemappings is None: return default_value return devicemappings.get("settings", {}).get(key, default_value)
def __init__(self, include_geofence, exclude_geofence): self.geofenced_areas = [] self.excluded_areas = [] self.use_matplotlib = 'matplotlib' in sys.modules if include_geofence or exclude_geofence: self.geofenced_areas = self.parse_geofences_file(include_geofence, excluded=False) self.excluded_areas = self.parse_geofences_file(exclude_geofence, excluded=True) logger.debug2("Loaded {} geofenced and {} excluded areas.", len(self.geofenced_areas), len(self.excluded_areas))
def __create_payload(self): logger.debug("Fetching data changed since {}", self.__last_check) # the payload that is about to be sent full_payload = [] try: # raids raids = self.__prepare_raid_data( self.__db_wrapper.get_raids_changed_since(self.__last_check)) full_payload += raids # quests if self.__args.quest_webhook: quest = self.__prepare_quest_data( self.__db_wrapper.quests_from_db( timestamp=self.__last_check)) full_payload += quest # weather if self.__args.weather_webhook: weather = self.__prepare_weather_data( self.__db_wrapper.get_weather_changed_since( self.__last_check)) full_payload += weather # gyms if self.__args.gym_webhook: gyms = self.__prepare_gyms_data( self.__db_wrapper.get_gyms_changed_since( self.__last_check)) full_payload += gyms # stops if self.__args.pokestop_webhook: pokestops = self.__prepare_stops_data( self.__db_wrapper.get_stops_changed_since( self.__last_check)) full_payload += pokestops # mon if self.__args.pokemon_webhook: mon = self.__prepare_mon_data( self.__db_wrapper.get_mon_changed_since(self.__last_check)) full_payload += mon except Exception: logger.exception("Error while creating webhook payload") logger.debug2("Done fetching data + building payload") return full_payload
def stats_collector(self): logger.debug2("Creating stats_collector task for {}".format(self._id)) with self.__mapping_mutex: if not self._stats_collector_start: if time.time() - self._last_processed_timestamp > 600 or self.compare_hour(self._last_processed_timestamp): stats_collected_tmp = deepcopy(self.__stats_collected) del self.__stats_collected self.__stats_collected = {} self._last_processed_timestamp = time.time() self.__mitm_mapper_parent.add_stats_to_process(self._id, stats_collected_tmp, self._last_processed_timestamp) else: self._stats_collector_start = False self._last_processed_timestamp = time.time()
def proto_endpoint(self, origin, data): logger.debug2("Receiving proto from {}".format(origin)) logger.debug4("Proto data received from {}: {}".format(origin, str(data))) type = data.get("type", None) if type is None or type == 0: logger.warning( "Could not read method ID. Stopping processing of proto") return None if not self.__mitm_mapper.get_injection_status(origin): logger.info("Worker {} is injected now", str(origin)) self.__mitm_mapper.set_injection_status(origin) # extract timestamp from data timestamp: float = data.get("timestamp", int(time.time())) self.__mitm_mapper.update_latest( origin, timestamp_received_raw=timestamp, timestamp_received_receiver=time.time(), key=type, values_dict=data) logger.debug3("Placing data received by {} to data_queue".format(origin)) self._data_queue.put( (timestamp, data, origin) ) return None
def process_data(self, received_timestamp, data, origin): data_type = data.get("type", None) raw = data.get("raw", False) logger.debug2("Processing data of {}".format(origin)) if raw: logger.debug5("Received raw payload: {}", data["payload"]) if data_type and not raw: logger.debug2("Running stats collector of {}".format(origin)) if self.__application_args.game_stats: self.__mitm_mapper.run_stats_collector(origin) logger.debug4("Received payload: {}", data["payload"]) if data_type == 106: # process GetMapObject logger.success( "Processing GMO received from {}. Received at {}", str(origin), str(datetime.fromtimestamp(received_timestamp))) if self.__application_args.weather: self.__db_submit.weather(origin, data["payload"], received_timestamp) self.__db_submit.stops(origin, data["payload"]) self.__db_submit.gyms(origin, data["payload"]) self.__db_submit.raids(origin, data["payload"], self.__mitm_mapper) self.__db_submit.spawnpoints(origin, data["payload"]) mon_ids_iv = self.__mitm_mapper.get_mon_ids_iv(origin) self.__db_submit.mons(origin, data["payload"], mon_ids_iv, self.__mitm_mapper) self.__db_submit.cells(origin, data["payload"]) self.__mitm_mapper.submit_gmo_for_location( origin, data["payload"]) logger.debug2("Done processing GMO of {}".format(origin)) elif data_type == 102: playerlevel = self.__mitm_mapper.get_playerlevel(origin) if playerlevel >= 30: logger.info("Processing Encounter received from {} at {}", str(origin), str(received_timestamp)) self.__db_submit.mon_iv(origin, received_timestamp, data["payload"], self.__mitm_mapper) logger.debug2( "Done processing encounter of {}".format(origin)) else: logger.debug( 'Playerlevel lower than 30 - not processing encounter Data' ) elif data_type == 101: logger.debug2("Processing proto 101 of {}".format(origin)) self.__db_submit.quest(origin, data["payload"], self.__mitm_mapper) logger.debug2("Done processing proto 101 of {}".format(origin)) elif data_type == 104: logger.debug2("Processing proto 104 of {}".format(origin)) self.__db_submit.stop_details(data["payload"]) logger.debug2("Done processing proto 104 of {}".format(origin)) elif data_type == 4: logger.debug2("Processing proto 4 of {}".format(origin)) self.__mitm_mapper.generate_player_stats( origin, data["payload"]) logger.debug2("Done processing proto 4 of {}".format(origin)) elif data_type == 156: logger.debug2("Processing proto 156 of {}".format(origin)) self.__db_submit.gym(origin, data["payload"]) logger.debug2("Done processing proto 156 of {}".format(origin))
def _move_to_location(self): if not self._mapping_manager.routemanager_present(self._routemanager_name) \ or self._stop_worker_event.is_set(): raise InternalStopWorkerException routemanager_settings = self._mapping_manager.routemanager_get_settings( self._routemanager_name) # get the distance from our current position (last) to the next gym (cur) distance = get_distance_of_two_points_in_meters( float(self.last_location.lat), float(self.last_location.lng), float(self.current_location.lat), float(self.current_location.lng)) logger.debug('Moving {} meters to the next position', round(distance, 2)) if not self._mapping_manager.routemanager_get_init( self._routemanager_name): speed = routemanager_settings.get("speed", 0) max_distance = routemanager_settings.get("max_distance", None) else: speed = int(25) max_distance = int(200) if (speed == 0 or (max_distance and 0 < max_distance < distance) or (self.last_location.lat == 0.0 and self.last_location.lng == 0.0)): logger.debug("main: Teleporting...") self._transporttype = 0 self._communicator.setLocation(self.current_location.lat, self.current_location.lng, 0) # the time we will take as a starting point to wait for data... cur_time = math.floor(time.time()) delay_used = self.get_devicesettings_value('post_teleport_delay', 7) # Test for cooldown / teleported distance TODO: check this block... if self.get_devicesettings_value('cool_down_sleep', False): if distance > 10000: delay_used = 15 elif distance > 5000: delay_used = 10 elif distance > 2500: delay_used = 8 logger.debug("Need more sleep after Teleport: {} seconds!", str(delay_used)) # curTime = math.floor(time.time()) # the time we will take as a starting point to wait for data... walk_distance_post_teleport = self.get_devicesettings_value( 'walk_after_teleport_distance', 0) if 0 < walk_distance_post_teleport < distance: # TODO: actually use to_walk for distance lat_offset, lng_offset = get_lat_lng_offsets_by_distance( walk_distance_post_teleport) to_walk = get_distance_of_two_points_in_meters( float(self.current_location.lat), float(self.current_location.lng), float(self.current_location.lat) + lat_offset, float(self.current_location.lng) + lng_offset) logger.info("Walking roughly: {}", str(to_walk)) time.sleep(0.3) self._communicator.walkFromTo( self.current_location.lat, self.current_location.lng, self.current_location.lat + lat_offset, self.current_location.lng + lng_offset, 11) logger.debug("Walking back") time.sleep(0.3) self._communicator.walkFromTo( self.current_location.lat + lat_offset, self.current_location.lng + lng_offset, self.current_location.lat, self.current_location.lng, 11) logger.debug("Done walking") time.sleep(1) else: logger.info("main: Walking...") self._transporttype = 1 self._communicator.walkFromTo(self.last_location.lat, self.last_location.lng, self.current_location.lat, self.current_location.lng, speed) # the time we will take as a starting point to wait for data... cur_time = math.floor(time.time()) logger.debug2("Done walking, fetching time to sleep") delay_used = self.get_devicesettings_value('post_walk_delay', 7) logger.debug2("Sleeping for {}s".format(str(delay_used))) time.sleep(float(delay_used)) self.set_devicesettings_value("last_location", self.current_location) self.last_location = self.current_location self._waittime_without_delays = time.time() return cur_time, True