async def get_report_info(): app_config = extract_config()["App"] return { "emails": app_config["GlobalReportingEmails"], "time": app_config["GlobalReportTime"], "daily": get_config().get_boolean("App", "DailyGlobalReport"), "weekly": get_config().get_boolean("App", "WeeklyGlobalReport") }
async def get_video_live_feed_enabled(camera_id: str): """ Returns *True* if the video live feed is enabled for the camera <camera_id> """ config_dict = extract_config() index = get_camera_index(config_dict, camera_id) config = get_config() return { "enabled": config.get_boolean(f"Source_{index}", "LiveFeedEnabled") }
def delete_area_occupancy_rules(area_id: str): area_config_path = get_config().get_area_config_path(area_id) if os.path.exists(area_config_path): with open(area_config_path, "r") as area_file: data = json.load(area_file) else: return handle_response(None, False) with open(area_config_path, "w") as area_file: if data.get("occupancy_rules") is not None: del data["occupancy_rules"] json.dump(data, area_file)
def set_occupancy_rules(area_id: str, rules): area_config_path = get_config().get_area_config_path(area_id) Path(os.path.dirname(area_config_path)).mkdir(parents=True, exist_ok=True) if os.path.exists(area_config_path): with open(area_config_path, "r") as area_file: data = json.load(area_file) else: data = {} with open(area_config_path, "w") as area_file: data["occupancy_rules"] = rules.to_store_json()["occupancy_rules"] json.dump(data, area_file)
def _get_in_out_for_camera(camera_id: str): in_out_file_path = InOutMetric.get_in_out_file_path( camera_id, get_config()) in_out_boundaries = InOutMetric.read_in_out_boundaries(in_out_file_path) if not in_out_boundaries: return [] return [{ "name": in_out["name"], "ax": in_out['in_out_boundary'][0][0], "ay": in_out['in_out_boundary'][0][1], "bx": in_out['in_out_boundary'][1][0], "by": in_out['in_out_boundary'][1][1], } for in_out in in_out_boundaries["in_out_boundaries"]]
def get_area_occupancy_rules(area_id): config = get_config() areas = config.get_areas() area = next((area for area in areas if area.id == area_id), None) if not area: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"The area: {area_id} does not exist") area_config_path = area.get_config_path() if not os.path.exists(area_config_path): return OccupancyRuleListDTO.parse_obj([]).__root__ with open(area_config_path, "r") as area_file: rules_data = json.load(area_file) return OccupancyRuleListDTO.from_store_json(rules_data)
def expected_response(config_sample_path): config_directory = config_utils.get_area_config_directory(get_config()) config_path = os.path.join(config_directory, ALL_AREAS + ".json") with open(config_path, "r") as file: json_content_from_file = json.load(file)["global_area_all"] response = {re.sub(r'(?<!^)(?=[A-Z])', '_', key).lower(): value for key, value in json_content_from_file.items()} cameras = ",".join(str(value["id"]) for key, value in get_config_file_json(config_sample_path).items() if key.startswith("source__")) response["cameras"] = cameras response["occupancy_rules"] = get_area_occupancy_rules(ALL_AREAS) response = to_boolean_if_possible(response) return response
async def create_area(new_area: AreaConfigDTO, reboot_processor: Optional[bool] = True): """ Adds a new area to the processor. """ # TODO: We have to autogenerate the ID. config = get_config() areas = config.get_areas() if new_area.id in [area.id for area in areas]: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail=bad_request_serializer("Area already exists", error_type="config duplicated area") ) elif new_area.id.upper() == ALL_AREAS: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail=bad_request_serializer("Area with ID: 'ALL' is not valid.", error_type="Invalid ID") ) cameras = config.get_video_sources() camera_ids = [camera.id for camera in cameras] if not all(x in camera_ids for x in new_area.cameras.split(",")): non_existent_cameras = set(new_area.cameras.split(",")) - set(camera_ids) raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"The cameras: {non_existent_cameras} do not exist") occupancy_rules = new_area.occupancy_rules del new_area.occupancy_rules area_dict = map_to_config_file_format(new_area) config_dict = extract_config() config_dict[f"Area_{len(areas)-1}"] = area_dict success = update_config(config_dict, reboot_processor) if occupancy_rules: set_occupancy_rules(new_area.id, occupancy_rules) if not success: return handle_response(area_dict, success, status.HTTP_201_CREATED) area_directory = os.path.join(os.getenv("AreaLogDirectory"), new_area.id, "occupancy_log") Path(area_directory).mkdir(parents=True, exist_ok=True) area_config_directory = os.path.join(os.getenv("AreaConfigDirectory"), new_area.id) Path(area_config_directory).mkdir(parents=True, exist_ok=True) # known issue: Occupancy rules not returned return next((area for area in get_areas() if area["id"] == area_dict["Id"]), None)
def area_all_data(): config = get_config() area_all = config.get_area_all() if area_all is None: raise HTTPException(status_code=status.HTTP_501_NOT_IMPLEMENTED, detail=f"The area: 'ALL' does not exist") return { "violation_threshold": area_all.violation_threshold, "notify_every_minutes": area_all.notify_every_minutes, "emails": ",".join(area_all.emails), "enable_slack_notifications": area_all.enable_slack_notifications, "daily_report": area_all.daily_report, "daily_report_time": area_all.daily_report_time, "occupancy_threshold": area_all.occupancy_threshold, "id": area_all.id, "name": area_all.name, "cameras": ",".join(area_all.cameras) }
def modify_area_all(area_information): """ Edits the configuration related to the area "ALL", an area that contains all cameras. """ config = get_config() config_path = config.get_area_config_path(ALL_AREAS) json_content = { "global_area_all": { "ViolationThreshold": area_information.violationThreshold, "NotifyEveryMinutes": area_information.notifyEveryMinutes, "Emails": area_information.emails, "EnableSlackNotifications": area_information.enableSlackNotifications, "DailyReport": area_information.dailyReport, "DailyReportTime": area_information.dailyReportTime, "OccupancyThreshold": area_information.occupancyThreshold, "Id": ALL_AREAS, "Name": ALL_AREAS, } } if not os.path.exists(config_path): # Create the file with if necessary with open(config_path, 'x') as outfile: json.dump({"global_area_all": {}}, outfile) with open(config_path, "r") as file: file_content = json.load(file) file_content["global_area_all"] = json_content["global_area_all"] with open(config_path, "w") as file: json.dump(file_content, file) area_all = config.get_area_all() json_content["global_area_all"]["Cameras"] = ",".join(area_all.cameras) return {re.sub(r'(?<!^)(?=[A-Z])', '_', key).lower(): value for key, value in json_content["global_area_all"].items()}
def get_session(db_config=None): if not db_config: db_config = get_config('DB') return sessionmaker(bind=EngineSingleton(db_config).get_engine())()
def rollback_area_config_file(area_id): """area_id must be an string""" config_directory = config_utils.get_area_config_directory(get_config()) config_path = os.path.join(config_directory, area_id + ".json") if os.path.exists(config_path): os.remove(config_path)
async def get_processor_info(): """ Returns basic info regarding this processor """ return processor_info(get_config())
def init_schema(): db_config = get_config('DB') engine = EngineSingleton(db_config).get_engine() Base.metadata.create_all(engine, checkfirst=True) return make_response(), 200