def _main_work_thread(self): current_thread().name = self.id log.debug("Sub called") # first check if pogo is running etc etc self._workMutex.acquire() try: self._initRoutine() except WebsocketWorkerRemovedException: log.error("Timeout during init of worker %s" % str(self.id)) self._stop_worker_event.set() self._workMutex.release() return self._workMutex.release() # loop = asyncio.get_event_loop() # TODO:loop.create_task(self._speed_weather_check_thread()) speedWeatherCheckThread = Thread(name='speedWeatherCheckThread%s' % self.id, target=self._speed_weather_check_thread) speedWeatherCheckThread.daemon = False speedWeatherCheckThread.start() currentLocation = self._last_known_state.get("last_location", None) if currentLocation is None: currentLocation = Location(0.0, 0.0) lastLocation = None while not self._stop_worker_event.isSet(): while MadGlobals.sleep and self._route_manager_nighttime is None: time.sleep(1) __time = time.time() log.debug("Worker: acquiring lock for restart check") self._workMutex.acquire() log.debug("Worker: acquired lock") # Restart pogo every now and then... if self._devicesettings.get("restart_pogo", 80) > 0: # log.debug("main: Current time - lastPogoRestart: %s" % str(curTime - lastPogoRestart)) # if curTime - lastPogoRestart >= (args.restart_pogo * 60): self._locationCount += 1 if self._locationCount > self._devicesettings.get("restart_pogo", 80): log.error("scanned " + str(self._devicesettings.get("restart_pogo", 80)) + " locations, restarting pogo") self._restartPogo() self._locationCount = 0 self._workMutex.release() log.debug("Worker: lock released") # TODO: consider adding runWarningThreadEvent.set() lastLocation = currentLocation self._last_known_state["last_location"] = lastLocation if MadGlobals.sleep: currentLocation = self._route_manager_nighttime.getNextLocation() settings = self._route_manager_nighttime.settings else: currentLocation = self._route_manager_daytime.getNextLocation() settings = self._route_manager_daytime.settings # TODO: set position... needs to be adjust for multidevice posfile = open(self.id+'.position', "w") posfile.write(str(currentLocation.lat)+", "+str(currentLocation.lng)) posfile.close() log.debug("main: next stop: %s" % (str(currentLocation))) log.debug('main: LastLat: %s, LastLng: %s, CurLat: %s, CurLng: %s' % (lastLocation.lat, lastLocation.lng, currentLocation.lat, currentLocation.lng)) # get the distance from our current position (last) to the next gym (cur) distance = getDistanceOfTwoPointsInMeters(float(lastLocation.lat), float(lastLocation.lng), float(currentLocation.lat), float(currentLocation.lng)) log.info('main: Moving %s meters to the next position' % distance) delayUsed = 0 if MadGlobals.sleep: speed = self._route_manager_nighttime.settings.get("speed", 0) else: speed = self._route_manager_daytime.settings.get("speed", 0) if (speed == 0 or (settings["max_distance"] and 0 < settings["max_distance"] < distance) or (lastLocation.lat == 0.0 and lastLocation.lng == 0.0)): log.info("main: Teleporting...") self._communicator.setLocation(currentLocation.lat, currentLocation.lng, 0) delayUsed = self._devicesettings.get("post_teleport_delay",7) # Test for cooldown / teleported distance TODO: check this block... if self._devicesettings.get("cool_down_sleep",False): if distance > 2500: delayUsed = 30 elif distance > 5000: delayUsed = 45 elif distance > 10000: delayUsed = 60 log.info("Need more sleep after Teleport: %s seconds!" % str(delayUsed)) if 0 < self._devicesettings.get("walk_after_teleport_distance",0) < distance: toWalk = getDistanceOfTwoPointsInMeters(float(currentLocation.lat), float(currentLocation.lng), float(currentLocation.lat) + 0.0001, float(currentLocation.lng) + 0.0001) log.info("Walking a bit: %s" % str(toWalk)) time.sleep(0.3) self._communicator.walkFromTo(currentLocation.lat, currentLocation.lng, currentLocation.lat + 0.0001, currentLocation.lng + 0.0001, 11) log.debug("Walking back") time.sleep(0.3) self._communicator.walkFromTo(currentLocation.lat + 0.0001, currentLocation.lng + 0.0001, currentLocation.lat, currentLocation.lng, 11) log.debug("Done walking") else: log.info("main: Walking...") self._communicator.walkFromTo(lastLocation.lat, lastLocation.lng, currentLocation.lat, currentLocation.lng, speed) delayUsed = self._devicesettings.get("post_walk_delay",7) log.info("Sleeping %s" % str(delayUsed)) time.sleep(delayUsed) log.debug("main: Acquiring lock") while MadGlobals.sleep: # or not runWarningThreadEvent.isSet(): time.sleep(0.1) log.debug("Worker: acquiring lock") self._workMutex.acquire() log.debug("main: Lock acquired") if not self._takeScreenshot(): self._workMutex.release() log.debug("Worker: Lock released") continue log.debug("Worker: Got screenshot") curTime = time.time() if self._applicationArgs.last_scanned: log.info('main: Set new scannedlocation in Database') self._db_wrapper.set_scanned_location(str(currentLocation.lat), str(currentLocation.lng), str(curTime)) log.info("main: Checking raidcount and copying raidscreen if raids present") countOfRaids = self._pogoWindowManager.readRaidCircles(os.path.join( self._applicationArgs.temp_path, 'screenshot%s.png' % str(self.id)), self.id) if countOfRaids == -1: log.debug("Worker: Count present but no raid shown") log.warning("main: Count present but no raid shown, reopening raidTab") self._reopenRaidTab() # tabOutAndInPogo() log.debug("Done reopening raidtab") if not self._takeScreenshot(): self._workMutex.release() log.debug("Worker: Lock released") continue countOfRaids = self._pogoWindowManager.readRaidCircles(os.path.join( self._applicationArgs.temp_path, 'screenshot%s.png' % str(self.id)), self.id) # elif countOfRaids == 0: # emptycount += 1 # if emptycount > 30: # emptycount = 0 # log.error("Had 30 empty scans, restarting pogo") # restartPogo() # not an elif since we may have gotten a new screenshot.. # detectin weather if self._applicationArgs.weather: log.debug("Worker: Checking weather...") weather = checkWeather(os.path.join(self._applicationArgs.temp_path, 'screenshot%s.png' % str(self.id))) if weather[0]: log.debug('Submit Weather') cell_id = S2Helper.lat_lng_to_cell_id(currentLocation.lat, currentLocation.lng) self._db_wrapper.update_insert_weather(cell_id, weather[1], curTime) else: log.error('Weather could not detected') if countOfRaids > 0: log.debug("Worker: Count of raids >0") log.debug("main: New und old Screenshoot are different - starting OCR") log.debug("main: countOfRaids: %s" % str(countOfRaids)) curTime = time.time() copyFileName = self._applicationArgs.raidscreen_path + '/raidscreen_' + str(curTime) \ + "_" + str(currentLocation.lat) + "_" + str(currentLocation.lng) + "_" \ + str(countOfRaids) + '.png' log.debug('Copying file: ' + copyFileName) log.debug("Worker: Copying file to %s" % str(copyFileName)) copyfile(os.path.join(self._applicationArgs.temp_path, 'screenshot%s.png' % str(self.id)), copyFileName) os.remove(os.path.join(self._applicationArgs.temp_path, 'screenshot%s.png' % str(self.id))) log.debug("main: Releasing lock") self._workMutex.release() log.debug("Worker: Lock released")
def _post_move_location_routine(self, timestamp): # check if the speed_weather_check_thread signalled an abort by setting the stop_worker_event if self._stop_worker_event.is_set(): raise InternalStopWorkerException logger.debug("Main: acquiring lock") self._work_mutex.acquire() logger.debug("main: Lock acquired") # TODO: takeScreenshot can throw, should we care about releasing locks or just cleanup everything else? if not self._takeScreenshot(): self._work_mutex.release() logger.debug( "Worker: couldn't take screenshot before radscreen check, lock released" ) return logger.debug("Worker: Got screenshot") # curTime = time.time() logger.info( "main: Checking raidcount and copying raidscreen if raids present") count_of_raids = self._pogoWindowManager.readRaidCircles( self.get_screenshot_path(), self._id, self._communicator) if count_of_raids == -1: logger.debug("Worker: Count present but no raid shown") logger.warning( "main: Count present but no raid shown, reopening raidTab") self._reopenRaidTab() logger.debug("Done reopening raidtab") if not self._takeScreenshot(): self._work_mutex.release() logger.debug( "Worker: couldn't take screenshot after opening raidtab, lock released" ) return count_of_raids = self._pogoWindowManager.readRaidCircles( self.get_screenshot_path(), self._id, self._communicator) # elif countOfRaids == 0: # emptycount += 1 # if emptycount > 30: # emptycount = 0 # logger.error("Had 30 empty scans, restarting pogo") # restartPogo() # not an elif since we may have gotten a new screenshot.. # detectin weather if self._applicationArgs.weather: logger.debug("Worker: Checking weather...") weather = checkWeather(self.get_screenshot_path()) if weather[0]: logger.debug('Submit Weather') cell_id = S2Helper.lat_lng_to_cell_id( self.current_location.lat, self.current_location.lng) self._db_wrapper.update_insert_weather(cell_id, weather[1], timestamp) else: logger.error('Weather could not detected') if count_of_raids > 0: logger.debug("Worker: Count of raids >0") logger.debug( "main: New und old Screenshoot are different - starting OCR") logger.debug("main: countOfRaids: {}", str(count_of_raids)) timestamp = time.time() copyFileName = self._applicationArgs.raidscreen_path + '/raidscreen_' + str(timestamp) \ + "_" + str(self.current_location.lat) + "_" + str(self.current_location.lng) + "_" \ + str(count_of_raids) + '.png' logger.debug('Copying file: ' + copyFileName) logger.debug("Worker: Copying file to {}", str(copyFileName)) copyfile(self.get_screenshot_path(), copyFileName) os.remove(self.get_screenshot_path()) logger.debug("main: Releasing lock") self._work_mutex.release() logger.debug("Worker: Lock released")