def update_device_location(t_obj, lock, device, sleep, process_id): last_teleport = 0 last_lat = 0 last_lon = 0 locked = False while True: try: is_boss = False LOG.debug('Running device controller task for {}'.format(device)) fort = None lock.acquire() locked = True if t_obj.is_teleport_locked(process_id): lock.release() locked = False time.sleep(5) continue forts_no_boss = t_obj.get_forts_no_boss() forts_no_raid = t_obj.get_forts_no_raid() forts_no_raid_priority = t_obj.get_forts_no_raid_priority() forts_no_raid_all = forts_no_raid + forts_no_raid_priority forts = t_obj.get_forts() if len(forts_no_boss) > 0: fort = forts_no_boss[0] elif len(forts_no_raid_priority) > 0: fort = forts_no_raid_priority[0] elif len(forts_no_raid) > 0: fort = forts_no_raid[0] if fort is not None: session = database.Session() close_fort_ids = database.get_fort_ids_within_range(session, forts, 600, fort.lat, fort.lon) session.close() locked_forts = t_obj.get_locked_forts() index = 0 for close_fort_id in close_fort_ids: if index == 6: break index += 1 if close_fort_id in [fort.id for fort in forts_no_boss]: lock_time = 60 close_forts = [fort for fort in forts_no_boss if fort.id == close_fort_id] close_fort = close_forts[0] forts_no_boss.remove(close_fort) elif close_fort_id in [fort.id for fort in forts_no_raid_all]: lock_time = 180 close_forts = [fort for fort in forts_no_raid_all if fort.id == close_fort_id] close_fort = close_forts[0] try: forts_no_raid.remove(close_fort) except: pass try: forts_no_raid_priority.remove(close_fort) except: pass else: continue locked_forts.append(FortTime(close_fort.id, time.time() + lock_time)) forts_fort = [fort for fort in forts if fort.id == close_fort_id] forts_fort[0].updated = time.time() t_obj.set_forts_no_raid(forts_no_raid) t_obj.set_forts_no_raid_priority(forts_no_raid_priority) t_obj.set_forts_no_boss(forts_no_boss) t_obj.set_locked_forts(locked_forts) t_obj.set_forts(forts) lock.release() locked = False FNULL = open(os.devnull, 'w') if fort.lat < 0: lat_str = '-- {}'.format(fort.lat) else: lat_str = str(fort.lat) if fort.lon < 0: lon_str = '-- {}'.format(fort.lon) else: lon_str = str(fort.lon) process = subprocess.Popen('idevicelocation -u {} {} {}'.format(device, lat_str, lon_str), shell=True, stdout=FNULL, stderr=FNULL) time_start = time.time() process.wait(5) time_end = time.time() last_tp_time = time_end - last_teleport delay = time_end - time_start last_teleport = time.time() distance = vincenty((last_lat, last_lon), (fort.lat, fort.lon)).meters last_lat = fort.lat last_lon = fort.lon LOG.info( 'Teleporting device with ID {} to {},{} over {:0.0f}m (delay: {:0.2f}s, last ago: {:0.2f}s)'.format( device, fort.lat, fort.lon, distance, delay, last_tp_time)) session = database.Session() database.add_device_location_history(session, device, time_end, fort.lat, fort.lon) session.close() else: lock.release() locked = False time_start = time.time() if is_boss: sleep_r = sleep + sleep else: sleep_r = sleep sleep_time = sleep_r - time.time() + time_start if sleep_time > 0: time.sleep(sleep_time) except KeyboardInterrupt: os.killpg(0, signal.SIGINT) sys.exit(1) except Exception as e: if locked: lock.release() LOG.error('Failed to update device location for device {}: {}'.format(device, e)) time.sleep(1)
def run_fortmatching(self, session, fort_fullpath_filename): p_url = Path(self.url_image_path) fort_filename = os.path.basename(fort_fullpath_filename) LOG.info('find fort for {}'.format(fort_filename)) max_fort_id = 0 max_value = 0.0 max_url_fullpath_filename = '' parts = str(fort_filename.replace('.jpg', '').replace('.png', '')).split('_') if len(parts) >= 3: device = parts[len(parts) - 2] time = int(parts[len(parts) - 1]) teleport_delay = 1 index = 0 for device_conf in self.config.DEVICE_LIST: if device_conf == device: teleport_delay = self.config.TELEPORT_DELAYS[index] break index += 1 time_a = math.floor(time - (teleport_delay / 2)) time_b = math.ceil(time + (teleport_delay / 2)) device_location_a = db.get_device_location_history(session, time_a, device) device_location_b = db.get_device_location_history(session, time_b, device) device_location_c = db.get_device_location_history(session, time, device) limit_forts = [] if device_location_a is not None: ids_a = db.get_fort_ids_within_range(session, None, 800, device_location_a.lat, device_location_a.lon) for fort_id in ids_a: if fort_id not in limit_forts: limit_forts.append(fort_id) if device_location_b is not None: ids_b = db.get_fort_ids_within_range(session, None, 800, device_location_b.lat, device_location_b.lon) for fort_id in ids_b: if fort_id not in limit_forts: limit_forts.append(fort_id) if device_location_c is not None: ids_c = db.get_fort_ids_within_range(session, None, 800, device_location_c.lat, device_location_c.lon) if ids_a is not ids_c and ids_b is not ids_c: for fort_id in ids_c: if fort_id not in limit_forts: limit_forts.append(fort_id) LOG.debug('Matching with gyms: {}'.format(limit_forts)) else: LOG.debug('Matching without location') limit_forts = None for url_fullpath_filename in p_url.glob('*'): url_filename = os.path.basename(url_fullpath_filename) url_filename, url_filename_ext = os.path.splitext(url_filename) if url_filename_ext != '.png' and url_filename_ext != '.jpg': continue if limit_forts is not None and len(limit_forts) != 0: if int(url_filename) not in limit_forts: continue if url_filename_ext == '.jpg' or url_filename_ext == '.png': try: result = mt.fort_image_matching(str(url_fullpath_filename), str(fort_fullpath_filename)) except KeyboardInterrupt: os.killpg(0, signal.SIGINT) sys.exit(1) except: LOG.error('Matching error with {}'.format(str(url_fullpath_filename))) else: url_filename = os.path.basename(url_fullpath_filename) fort_id, ext = os.path.splitext(url_filename) # print('fort_id:',fort_id,'result:',result,'max_value:',max_value, 'max_fort_id:', max_fort_id) if result >= max_value: max_value = result max_fort_id = fort_id max_url_fullpath_filename = url_fullpath_filename # await asyncio.sleep(0.01) LOG.info('fort_filename:{} max_fort_id: {} max_value: {}'.format(fort_filename,max_fort_id, max_value)) img = cv2.imread(str(fort_fullpath_filename), 3) gym_image_id = self.raidnearby.get_gym_image_id(img) gym_image_fort_id = db.get_gym_image_fort_id(session, gym_image_id) if float(max_value) >= 0.7: LOG.info(str(fort_fullpath_filename)) if gym_image_fort_id is not None and int(max_fort_id) == int(gym_image_fort_id): LOG.info('This gym image is already trained') fort_result_file = os.getcwd() + '/success_img/Fort_' + str(max_fort_id) + '_GymImages_' + str(gym_image_id) + '_' + '{:.3f}'.format(max_value) + '.png' url_result_file = os.getcwd() + '/success_img/Fort_'+str(max_fort_id) + '_url' + str(url_filename_ext) shutil.move(fort_fullpath_filename, fort_result_file) shutil.copy(max_url_fullpath_filename, url_result_file) else: unknown_fort_id = db.get_unknown_fort_id(session) LOG.info('gym_images id:{} fort_id:{} unknow_fort_id:{}'.format(gym_image_id,gym_image_fort_id,unknown_fort_id)) if gym_image_fort_id == unknown_fort_id: try: db.update_gym_image(session,gym_image_id,max_fort_id) except KeyboardInterrupt: os.killpg(0, signal.SIGINT) sys.exit(1) except: LOG.error('Error to update gym_images for gym_images.id:{} gym_images.fort_id:{}'.format(gym_image_id,max_fort_id)) fort_result_file = os.getcwd() + '/success_img/Fort_' + str(max_fort_id) + '_GymImages_' + str(gym_image_id) + '_' + '{:.3f}'.format(max_value) + '.png' url_result_file = os.getcwd() + '/not_find_img/Fort_'+str(max_fort_id) + '_url' + str(url_filename_ext) shutil.move(fort_fullpath_filename, fort_result_file) shutil.copy(max_url_fullpath_filename, url_result_file) LOG.error('Successfully found fort fort_id:{}, but failed to updata gym_images database. Check not_find_img with the fort_id'.format(max_fort_id)) else: fort_result_file = os.getcwd() + '/success_img/Fort_' + str(max_fort_id) + '_GymImages_' + str(gym_image_id) + '_' + '{:.3f}'.format(max_value) + '.png' url_result_file = os.getcwd() + '/success_img/Fort_'+str(max_fort_id) + '_url' + str(url_filename_ext) process_img_path = os.getcwd() + '/process_img/Fort_' + str(max_fort_id) + '.png' shutil.copy(fort_fullpath_filename, process_img_path) shutil.move(fort_fullpath_filename, fort_result_file) shutil.copy(max_url_fullpath_filename, url_result_file) LOG.info('Successfully found fort id: {}'.format(max_fort_id)) else: LOG.info('The gym image is assigned as fort id:{}'.format(gym_image_fort_id)) LOG.info('Check not_find_img directory.') LOG.info('If the Fort_{}.png and Fort_{}_url.jpg in not_find_img are correct'.format(str(max_fort_id),str(max_fort_id))) LOG.info('Run "python3.6 manualsubmit.py force"'.format(str(max_fort_id),str(max_fort_id))) fort_result_file = os.getcwd() + '/success_img/Fort_' + str(max_fort_id) + '_GymImages_' + str(gym_image_id) + '_' + '{:.3f}'.format(max_value) + '.png' url_result_file = os.getcwd() + '/not_find_img/Fort_'+str(max_fort_id) + '_url' + str(url_filename_ext) shutil.move(fort_fullpath_filename, fort_result_file) shutil.copy(max_url_fullpath_filename, url_result_file) elif float(max_value) >= 0.60: fort_result_file = os.getcwd() + '/not_find_img/LowConfidence_Fort_' + str(max_fort_id) + '_GymImages_' + str(gym_image_id) + '_' + '{:.3f}'.format(max_value) + '.png' url_result_file = os.getcwd() + '/not_find_img/LowConfidence_Fort_'+str(max_fort_id) + '_url' + str(url_filename_ext) shutil.move(fort_fullpath_filename, fort_result_file) shutil.copy(max_url_fullpath_filename, url_result_file) LOG.info('Found fort id: {} but need to verify'.format(max_fort_id)) LOG.info('If the Fort_{}.png and Fort_{}_url.jpg in not_find_img are correct'.format(str(max_fort_id),str(max_fort_id))) LOG.info('Run "python3.6 manualsubmit.py"'.format(str(max_fort_id),str(max_fort_id))) else: split = str(fort_filename).split('_') if len(split) == 4: fort_filename_real = split[0] + '_' + split[1] + '.png' else: fort_filename_real = fort_filename fort_result_file = os.getcwd() + '/not_find_img/' + str(fort_filename_real) url_result_file = os.getcwd() + '/not_find_img/'+str(max_fort_id) + str(url_filename_ext) shutil.move(fort_fullpath_filename, fort_result_file) shutil.copy(max_url_fullpath_filename, url_result_file) LOG.info('Can not find fort: {}, check the image in not_find_img'.format(max_fort_id))
def __init__(self): if len(argv) >= 2: self.config = importlib.import_module(str(argv[1])) else: self.config = importlib.import_module('config') self.all_forts_inside = [] if self.config.SCAN_AREA is not None: LOG.info('Scan-Area is set! Getting Forts...') session = database.Session() all_forts = database.get_forts(session) all_forts_to_download = [] session2 = database.Session() for fort in all_forts: if self.config.SCAN_AREA == 'All': all_forts_to_download.append(fort.id) self.all_forts_inside.append( DBFort(fort.id, fort.lat, fort.lon, 0)) if fort.lat is not None and fort.lon is not None and self.config.SCAN_AREA.contains( Point(fort.lat, fort.lon)): self.all_forts_inside.append( DBFort(fort.id, fort.lat, fort.lon, 0)) if fort.id not in all_forts_to_download: all_forts_to_download.append(fort.id) nearby_ids = database.get_fort_ids_within_range( session2, all_forts, 800, fort.lat, fort.lon) for fort_id in nearby_ids: if fort_id not in all_forts_to_download: all_forts_to_download.append(fort_id) session2.close() LOG.info('Found {} Gyms Gyms in Scan-Area'.format( len(self.all_forts_inside))) for fort in all_forts: if fort.id in all_forts_to_download: image_file = Path(os.getcwd() + '/url_img/' + str(fort.id) + '.jpg') if not os.path.isfile(image_file) and fort.url is not None: LOG.info( 'Found gym in Scan-Area without stored image! Downloading image for {}' .format(fort.id)) download_img(str(fort.url), str(image_file)) if self.config.DEVICE_LIST is None: LOG.error('SCAN_AREA set but DEVICE_LIST is empty! Skipping') session.commit() session.close() time.sleep(1) if self.config.ENABLE_NEARBY: for i in range(self.config.NEARBY_PROCESSES): self.restart_nearby(i) if self.config.ENABLE_CROP: for i in range(self.config.CROP_PROCESSES): self.restart_crop(i) if self.config.ENABLE_FINDFORT: for i in range(self.config.FINDFORT_PROCESSES): self.restart_findfort(i) if self.config.ENABLE_CONTROL and self.config.SCAN_AREA is not None and self.config.DEVICE_LIST is not None: self.restart_devicecontroller() self.restart_healthcheck()