async def get_my_profile(user_details: dict = Depends(is_authenticated)): try: return JSONResponse(status_code=status.HTTP_200_OK, content=jsonable_encoder( BaseResponseModel(data=[user_details]))) except Exception as e: raise_exception(e)
async def get_profile(id: int): try: return JSONResponse(status_code=status.HTTP_200_OK, content=jsonable_encoder( get_profile_details( await ProfilesTableManager.read_record( profile, id)))) except Exception as e: raise_exception(e)
async def put_profile(id: int, item: BaseProfileIn): try: return JSONResponse(status_code=status.HTTP_200_OK, content=jsonable_encoder( get_profile_details( await ProfilesTableManager.update_record( profile, id, item)))) except Exception as e: raise_exception(e)
async def post_profile(item: ProfileIn): try: return JSONResponse(status_code=status.HTTP_201_CREATED, content=jsonable_encoder( get_profile_details( await ProfilesTableManager.create_record( profile, item)))) except Exception as e: raise_exception(e)
async def put_group(id: int, item: BaseGroupIn): try: return JSONResponse(status_code=status.HTTP_200_OK, content=jsonable_encoder( get_group_details( await GroupsTableManager.update_record( group, id, item)))) except Exception as e: raise_exception(e)
async def get_profiles(query: str = Query(None)): try: return JSONResponse( status_code=status.HTTP_200_OK, content=jsonable_encoder( get_profiles_list(await ProfilesTableManager.read_all_records( profile, query)))) except Exception as e: raise_exception(e)
async def logout(authorization=Security(APIKeyHeader(name="Authorization", auto_error=False))): try: response = requests.post(f"{DB_SERVER_URL}/logout", headers={"Authorization": authorization}) return JSONResponse( status_code=response.status_code, content=response.json() ) except Exception as e: raise_exception(e)
async def post_group(item: GroupIn): try: return JSONResponse(status_code=status.HTTP_201_CREATED, content=jsonable_encoder( get_group_details( await GroupsTableManager.create_record( group, item)))) except Exception as e: raise_exception(e)
async def login(item: UserCredentialsIn): try: response = requests.post(f"{DB_SERVER_URL}/login", json=item.dict()) return JSONResponse( status_code=response.status_code, content=response.json() ) except Exception as e: raise_exception(e)
async def post_device(item: DeviceIn, user_details: dict = Depends(is_authenticated)): try: return JSONResponse(status_code=status.HTTP_201_CREATED, content=jsonable_encoder( get_device_details( await DevicesTableManager.create_record( device, item)))) except Exception as e: raise_exception(e)
async def get_group_devices(id: int): try: return JSONResponse( status_code=status.HTTP_200_OK, content=jsonable_encoder( get_device_group_list( await DevicegroupTableManager.read_devices_groups(group_id=id)))) except Exception as e: raise_exception(e)
async def post_user(item: UserIn): try: return JSONResponse( status_code=status.HTTP_201_CREATED, content=jsonable_encoder( get_user_details(await UsersTableManager.create_record(user, item)) ) ) except Exception as e: raise_exception(e)
async def put_type(id: int, item: BaseDeviceTypeIn): try: return JSONResponse( status_code=status.HTTP_200_OK, content=jsonable_encoder( get_device_type_details(await DevicesTypesTableManager.update_record( device_type, id, item)))) except Exception as e: raise_exception(e)
async def post_type(item: DeviceTypeIn): try: return JSONResponse( status_code=status.HTTP_201_CREATED, content=jsonable_encoder( get_device_type_details(await DevicesTypesTableManager.create_record( device_type, item)))) except Exception as e: raise_exception(e)
async def get_group_profiles(id: int): try: return JSONResponse( status_code=status.HTTP_200_OK, content=jsonable_encoder( get_profile_group_list( await ResponsibilitiesTableManager.read_responsibilities( group_id=id)))) except Exception as e: raise_exception(e)
async def delete_user(id: int, authorization=Security( APIKeyHeader(name="Authorization", auto_error=False))): try: response = requests.delete(f"{DB_SERVER_URL}/users/{id}", headers={"Authorization": authorization}) return JSONResponse(status_code=response.status_code, content=response.json()) except Exception as e: raise_exception(e)
async def post_alert(item: AlertModel): try: item.alert_date = datetime.utcnow() await AlertsTableManager.create_alert(item) return JSONResponse( status_code=status.HTTP_201_CREATED, content=jsonable_encoder( BaseResponseModel( info=[InfoModel(type='success', message='Alert added')]))) except Exception as e: raise_exception(e)
async def get_users(query: str = Query(None), authorization=Security( APIKeyHeader(name="Authorization", auto_error=False))): try: response = requests.get(f"{DB_SERVER_URL}/users", headers={"Authorization": authorization}, params={'query': query}) return JSONResponse(status_code=response.status_code, content=response.json()) except Exception as e: raise_exception(e)
async def post_user(item: UserIn, authorization=Security( APIKeyHeader(name="Authorization", auto_error=False))): try: response = requests.post(f"{DB_SERVER_URL}/users", headers={"Authorization": authorization}, json=item.dict()) return JSONResponse(status_code=response.status_code, content=response.json()) except Exception as e: raise_exception(e)
async def update_sensors_parameters(item): try: return JSONResponse( status_code=status.HTTP_200_OK, content=jsonable_encoder( BaseResponseModel(info=[ InfoModel(type='success', message='Sensors parameters updated') ]))) except Exception as e: raise_exception(e)
async def logout(user_details: dict = Depends(is_authenticated)): try: await UsersTableManager.update_token(user_details['username']) return JSONResponse( status_code=status.HTTP_200_OK, content=jsonable_encoder( BaseResponseModel(info=[ InfoModel(type='success', message='User logged out successfully') ]))) except Exception as e: raise_exception(e)
async def put_group(id: int, item: BaseGroupIn, authorization=Security( APIKeyHeader(name="Authorization", auto_error=False))): try: response = requests.put(f"{DB_SERVER_URL}/groups/{id}", headers={"Authorization": authorization}, json=item.dict()) return JSONResponse(status_code=response.status_code, content=response.json()) except Exception as e: raise_exception(e)
async def post_device_group(item: DevicegroupIn): try: await DevicegroupTableManager.create_device_group(item) return JSONResponse( status_code=status.HTTP_201_CREATED, content=jsonable_encoder( BaseResponseModel(info=[ InfoModel(type='success', message='Devicegroup added successfully') ]))) except Exception as e: raise_exception(e)
async def delete_profile(id: int): try: await ProfilesTableManager.delete_record(profile, id) return JSONResponse( status_code=status.HTTP_200_OK, content=jsonable_encoder( BaseResponseModel(info=[ InfoModel(type='success', message='Profile deleted successfully') ]))) except Exception as e: raise_exception(e)
async def post_responsibility(item: ResponsibilityIn): try: await ResponsibilitiesTableManager.create_responsibility(item) return JSONResponse( status_code=status.HTTP_201_CREATED, content=jsonable_encoder( BaseResponseModel(info=[ InfoModel(type='success', message='Responsibility added successfully') ]))) except Exception as e: raise_exception(e)
async def delete_device_group(device_id: int, group_id: int): try: await DevicegroupTableManager.delete_device_group(device_id=device_id, group_id=group_id) return JSONResponse( status_code=status.HTTP_200_OK, content=jsonable_encoder( BaseResponseModel(info=[ InfoModel(type='success', message='Devicegroup deleted successfully') ]))) except Exception as e: raise_exception(e)
async def delete_responsibility(profile_id: int, group_id: int): try: await ResponsibilitiesTableManager.delete_responsibility( profile_id=profile_id, group_id=group_id) return JSONResponse( status_code=status.HTTP_200_OK, content=jsonable_encoder( BaseResponseModel(info=[ InfoModel(type='success', message='Responsibility deleted successfully') ]))) except Exception as e: raise_exception(e)
async def post_report(items: List[ReportModel]): try: for item in items: item.report_date = datetime.utcnow() await DailyReportsTableManager.create_reports(items) return JSONResponse( status_code=status.HTTP_201_CREATED, content=jsonable_encoder( BaseResponseModel( info=[InfoModel(type='success', message='Reports added') ]))) except Exception as e: raise_exception(e)
async def login(data: UserCredentialsIn): try: await UsersTableManager.validate_user_credentials( data.username, data.password) access_token = secrets.token_urlsafe(20) await UsersTableManager.update_token(data.username, access_token) return JSONResponse(status_code=status.HTTP_200_OK, content=jsonable_encoder( BaseResponseModel(data=[{ "token": access_token }]))) except Exception as e: raise_exception(e)
async def get_real_time_values(authorization=Security( APIKeyHeader(name="Authorization", auto_error=False))): try: response_db = requests.get(f"{DB_SERVER_URL}/devices", headers={"Authorization": authorization}) response_rpi = requests.get(f"{RPI_SERVER_URL}/sensors", headers={"Authorization": authorization}) response_rpi_json = response_rpi.json() response_db_json = response_db.json() return JSONResponse(status_code=status.HTTP_200_OK, content=jsonable_encoder( get_sensors_values(response_rpi_json['data'], response_db_json['data']))) except Exception as e: raise_exception(e)