def __get_latest_routemanagers(self) -> Optional[Dict[str, dict]]: global mode_mapping areas: Optional[Dict[str, dict]] = {} if self.__configmode: return areas area_arr = self.__raw_json["areas"] thread_pool = ThreadPool(processes=4) areas_procs = {} for area in area_arr: if area["geofence_included"] is None: raise RuntimeError("Cannot work without geofence_included") geofence_included = Path(area["geofence_included"]) if not geofence_included.is_file(): raise RuntimeError( "geofence_included for area '{}' is specified but file does not exist ('{}').".format( area["name"], geofence_included.resolve() ) ) geofence_excluded_raw_path = area.get("geofence_excluded", None) if geofence_excluded_raw_path is not None: geofence_excluded = Path(geofence_excluded_raw_path) if not geofence_excluded.is_file(): raise RuntimeError( "geofence_excluded for area '{}' is specified but file does not exist ('{}').".format( area["name"], geofence_excluded.resolve() ) ) area_dict = {"mode": area["mode"], "geofence_included": area["geofence_included"], "geofence_excluded": area.get("geofence_excluded", None), "routecalc": area["routecalc"]} # also build a routemanager for each area... # grab coords # first check if init is false, if so, grab the coords from DB # coords = np.loadtxt(area["coords"], delimiter=',') geofence_helper = GeofenceHelper( area["geofence_included"], area.get("geofence_excluded", None)) mode = area["mode"] # build routemanagers #map iv list to ids if area.get('settings', None) is not None and 'mon_ids_iv' in area['settings']: # replace list name area['settings']['mon_ids_iv_raw'] = \ self.get_monlist(area['settings'].get('mon_ids_iv', None), area.get("name", "unknown")) route_manager = RouteManagerFactory.get_routemanager(self.__db_wrapper, None, mode_mapping.get(mode, {}).get("range", 0), mode_mapping.get(mode, {}).get("max_count", 99999999), area["geofence_included"], area.get("geofence_excluded", None), mode=mode, settings=area.get("settings", None), init=area.get("init", False), name=area.get("name", "unknown"), level=area.get("level", False), coords_spawns_known=area.get( "coords_spawns_known", False), routefile=area["routecalc"], calctype=area.get("route_calc_algorithm", "optimized"), joinqueue=self.join_routes_queue ) if mode not in ("iv_mitm", "idle"): coords = self.__fetch_coords(mode, geofence_helper, coords_spawns_known=area.get("coords_spawns_known", False), init=area.get("init", False), range_init=mode_mapping.get(area["mode"], {}).get("range_init", 630), including_stops=area.get("including_stops", False)) route_manager.add_coords_list(coords) max_radius = mode_mapping[area["mode"]]["range"] max_count_in_radius = mode_mapping[area["mode"]]["max_count"] if not area.get("init", False): logger.info("Initializing area {}", area["name"]) proc = thread_pool.apply_async(route_manager.recalc_route, args=(max_radius, max_count_in_radius, 0, False)) areas_procs[area["name"]] = proc else: logger.info( "Init mode enabled. Going row-based for {}", str(area.get("name", "unknown"))) # we are in init, let's write the init route to file to make it visible in madmin if area["routecalc"] is not None: routefile = os.path.join( self.__args.file_path, area["routecalc"]) if os.path.isfile(routefile + '.calc'): os.remove(routefile + '.calc') with open(routefile + '.calc', 'a') as f: for loc in coords: f.write(str(loc.lat) + ', ' + str(loc.lng) + '\n') # gotta feed the route to routemanager... TODO: without recalc... proc = thread_pool.apply_async(route_manager.recalc_route, args=(1, 99999999, 0, False)) areas_procs[area["name"]] = proc area_dict["routemanager"] = route_manager areas[area["name"]] = area_dict for area in areas_procs.keys(): to_be_checked = areas_procs[area] to_be_checked.get() thread_pool.close() thread_pool.join() return areas
def __get_latest_routemanagers(self) -> Optional[Dict[str, dict]]: global mode_mapping areas: Optional[Dict[str, dict]] = {} if self.__configmode: return areas raw_areas = self.__data_manager.get_root_resource('area') thread_pool = ThreadPool(processes=4) areas_procs = {} for area_id, area_true in raw_areas.items(): area = area_true.get_resource() if area["geofence_included"] is None: raise RuntimeError("Cannot work without geofence_included") try: geofence_included = self.__data_manager.get_resource( 'geofence', identifier=area["geofence_included"]) except: raise RuntimeError( "geofence_included for area '{}' is specified but does not exist ('{}')." .format(area["name"], geofence_included)) geofence_excluded_raw_path = area.get("geofence_excluded", None) try: if geofence_excluded_raw_path is not None: geofence_excluded = self.__data_manager.get_resource( 'geofence', identifier=geofence_excluded_raw_path) else: geofence_excluded = None except: raise RuntimeError( "geofence_excluded for area '{}' is specified but file does not exist ('{}')." .format(area["name"], geofence_excluded_raw_path)) area_dict = { "mode": area_true.area_type, "geofence_included": geofence_included, "geofence_excluded": geofence_excluded, "routecalc": area["routecalc"], "name": area['name'] } # also build a routemanager for each area... # grab coords # first check if init is false, if so, grab the coords from DB # coords = np.loadtxt(area["coords"], delimiter=',') geofence_helper = GeofenceHelper(geofence_included, geofence_excluded) mode = area_true.area_type # build routemanagers # map iv list to ids if area.get('settings', None) is not None and 'mon_ids_iv' in area['settings']: # replace list name area['settings']['mon_ids_iv_raw'] = \ self.get_monlist(area['settings'].get('mon_ids_iv', None), area.get("name", "unknown")) route_resource = self.__data_manager.get_resource( 'routecalc', identifier=area["routecalc"]) route_manager = RouteManagerFactory.get_routemanager( self.__db_wrapper, self.__data_manager, area_id, None, mode_mapping.get(mode, {}).get("range", 0), mode_mapping.get(mode, {}).get("max_count", 99999999), geofence_included, path_to_exclude_geofence=geofence_excluded, mode=mode, settings=area.get("settings", None), init=area.get("init", False), name=area.get("name", "unknown"), level=area.get("level", False), coords_spawns_known=area.get("coords_spawns_known", False), routefile=route_resource, calctype=area.get("route_calc_algorithm", "optimized"), joinqueue=self.join_routes_queue, S2level=mode_mapping.get(mode, {}).get("s2_cell_level", 30)) if mode not in ("iv_mitm", "idle"): coords = self.__fetch_coords( mode, geofence_helper, coords_spawns_known=area.get("coords_spawns_known", False), init=area.get("init", False), range_init=mode_mapping.get(area_true.area_type, {}).get("range_init", 630), including_stops=area.get("including_stops", False)) route_manager.add_coords_list(coords) max_radius = mode_mapping[area_true.area_type]["range"] max_count_in_radius = mode_mapping[ area_true.area_type]["max_count"] if not area.get("init", False): logger.info("Initializing area {}", area["name"]) proc = thread_pool.apply_async( route_manager.initial_calculation, args=(max_radius, max_count_in_radius, 0, False)) areas_procs[area_id] = proc else: logger.info("Init mode enabled. Going row-based for {}", str(area.get("name", "unknown"))) # we are in init, let's write the init route to file to make it visible in madmin calc_coords = [] if area["routecalc"] is not None: for loc in coords: calc_coord = '%s,%s' % (str(loc.lat), str(loc.lng)) calc_coords.append(calc_coord) route_resource['routefile'] = calc_coords route_resource.save() # gotta feed the route to routemanager... TODO: without recalc... proc = thread_pool.apply_async(route_manager.recalc_route, args=(1, 99999999, 0, False)) areas_procs[area_id] = proc area_dict["routemanager"] = route_manager areas[area_id] = area_dict for area in areas_procs.keys(): to_be_checked = areas_procs[area] to_be_checked.get() thread_pool.close() thread_pool.join() return areas