def teleport(self, device, lat, lon, session): FNULL = open(os.devnull, 'w') if lat < 0: lat_str = '-- {}'.format(lat) else: lat_str = str(lat) if lon < 0: lon_str = '-- {}'.format(lon) else: lon_str = str(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() distance = vincenty((self.last_lat, self.last_lon), (lat, lon)).meters LOG.info( 'Teleporting device with ID {} to {},{} over {:0.0f}m (delay: {:0.2f}s)' .format(device, lat, lon, distance, time_end - time_start)) self.last_lon = lon self.last_lat = lat database.add_device_location_history(session, device, time.time(), lat, lon)
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)