Skip to content

Commit

Permalink
Merge branch 'develop_back' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin0409 committed Jun 20, 2024
2 parents 672c3d8 + e65c1d6 commit d73d7dd
Show file tree
Hide file tree
Showing 9 changed files with 1,608 additions and 41 deletions.
15 changes: 4 additions & 11 deletions backend/fastapi/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,9 @@

# 라우트 설정
def create_app():
from . import routes
app.include_router(routes.router)
from . import routes2
app.include_router(routes2.router)

routes.sched.start()
routes2.sched.start()

return app

@app.exception_handler(HTTPException)
async def http_exception_handler(exc):
return JSONResponse(
status_code=exc.status_code,
content={"message": exc.detail}
)
return app
149 changes: 138 additions & 11 deletions backend/fastapi/app/bodymodel.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from pydantic import BaseModel, Field
from typing import List, Dict
from typing import List, Dict, Optional

# Define request and response models
class accessToken(BaseModel):
'''class accessToken(BaseModel):
accessToken: str = Field(examples=["ksjdnfjkdasnfljsknafljansdfjlsakn"])
class CommonResponse(BaseModel):
status: str = Field("success")
message: str = Field("메~시~지~")
result : accessToken
result : accessToken'''

class CommonResponse(BaseModel):
status: str = Field("success")
message: str = Field("메~시~지~")

class dementiaInfoRecord(BaseModel):
dementiaKey : str = Field(examples=["123456"])
Expand All @@ -29,13 +33,14 @@ class UserRecord(BaseModel):
class nokResult(BaseModel):
dementiaInfoRecord: dementiaInfoRecord
nokKey: str = Field(examples=["123456"])
accessToken: str = Field(examples=["ksjdnfjkdasnfljsknafljansdfjlsakn"])
refreshToken: str = Field(examples=["ksjdnfjkdasnfljsknafljansdfjlsakn"])
#accessToken: str = Field(examples=["ksjdnfjkdasnfljsknafljansdfjlsakn"])
#refreshToken: str = Field(examples=["ksjdnfjkdasnfljsknafljansdfjlsakn"])

class ReceiveNokInfoRequest(BaseModel):
keyFromDementia : int = Field(examples=["123456"])
nokName : str = Field(examples=["홍길동"])
nokPhoneNumber : str = Field(examples=["010-1234-5678"])
fcmToken : str = Field(examples=["ksjdnfjkdasnfljsknafljansdfjlsakn"])

class ReceiveNokInfoResponse(BaseModel):
status: str = Field("success")
Expand All @@ -57,8 +62,8 @@ class ReceiveDementiaInfoResponse(BaseModel):

class connectionResult(BaseModel):
nokInfoRecord: nokInfoRecord
accessToken: str = Field(examples=["ksjdnfjkdasnfljsknafljansdfjlsakn"])
refreshToken: str = Field(examples=["ksjdnfjkdasnfljsknafljansdfjlsakn"])
#accessToken: str = Field(examples=["ksjdnfjkdasnfljsknafljansdfjlsakn"])
#refreshToken: str = Field(examples=["ksjdnfjkdasnfljsknafljansdfjlsakn"])

class ConnectionRequest(BaseModel):
dementiaKey : int = Field(examples=["123456"])
Expand All @@ -73,6 +78,7 @@ class loginRequest(BaseModel):
isDementia : int = Field(examples=["1"], description="1 : 보호대상자, 0 : 보호자")

class ReceiveLocationRequest(BaseModel):
dementiaKey : str = Field(examples=["123456"])
date : str = Field(examples=["2024-03-19"], description="yyyy-mm-dd")
time : str = Field(examples=["12:00:00"])
latitude : float = Field(examples=["37.123456"])
Expand Down Expand Up @@ -105,11 +111,15 @@ class GetLocationResponse(BaseModel):
result: LastLoc

class ModifyUserInfoRequest(BaseModel):
key: str = Field(examples=["123456"])
isDementia : int = Field(examples=[1], description="1 : 보호대상자, 0 : 보호자")
name : str = Field(examples=["김이름"])
phoneNumber : str = Field(examples=["010-1234-5678"])

class ModifyUserUpdateRateRequest(BaseModel):
updateRate : int = Field(examples=["15"], description="초 단위")
key: str = Field(examples=["123456"])
isDementia : int = Field(examples=[1], description="1 : 보호대상자, 0 : 보호자")
updateRate : int = Field(examples=[1], description="분 단위")

class AverageAndLastLoc(BaseModel):
averageSpeed : float = Field(examples=["2.0"])
Expand Down Expand Up @@ -172,8 +182,7 @@ class LocHistoryResponse(BaseModel):
result : LocHisRecord

class ErrorResponse(BaseModel):
status: str = Field("error")
message: str = Field("에러 내용")
detail: str = Field("에러 내용")

class TempResponse(BaseModel):
status: str = Field("success")
Expand Down Expand Up @@ -202,4 +211,122 @@ class locpredict(BaseModel):
class PredictLocationResponse(BaseModel):
status: str = Field("success")
message: str = Field("메~시~지~")
result : locpredict
result : locpredict

class AverageWalkingSpeedRequest(BaseModel):
dementiaKey : str = Field(examples=["123456"])

class AverageAndLastLoc(BaseModel):
averageSpeed : float = Field(examples=["2.0"])
lastLatitude : float = Field(examples=["37.123456"])
lastLongitude : float = Field(examples=["127.123456"])
addressName : str = Field(examples=["서울특별시 강남구 니가 사는 그 집"])

class AverageWalkingSpeedResponse(BaseModel):
status: str = Field("success")
message: str = Field("메~시~지~")
result: AverageAndLastLoc

class RegisterSafeAreaRequest(BaseModel):
dementiaKey : str = Field(examples=["123456"])
groupKey : str = Field(examples=["123456"])
areaName : str = Field(examples=["집"])
latitude : float = Field(examples=["37.123456"])
longitude : float = Field(examples=["127.123456"])
radius : float = Field(examples=["0.5"], description="킬로미터 단위")

class RegisterSafeAreaGroupRequest(BaseModel):
dementiaKey : str = Field(examples=["123456"])
groupName : str = Field(examples=["안심구역 그룹 1"])

class safeAreaList(BaseModel):
areaName : str = Field(examples=["집"])
areaKey : str = Field(examples=["123456"])
latitude : float = Field(examples=["37.123456"])
longitude : float = Field(examples=["127.123456"])
radius : float = Field(examples=["0.5"], description="킬로미터 단위")

class safeAreaGroupInfo(BaseModel):
groupName : str = Field(examples=["안심구역 그룹 1"])
groupKey : str = Field(examples=["123456"])

class groupList(BaseModel):
groupList : List[safeAreaGroupInfo]

class GetSafeAreaResponse(BaseModel):
status: str = Field("success")
message: str = Field("메~시~지~")
result: groupList

class ModifySafeAreaName(BaseModel):
dementiaKey : str = Field(examples=["123456"])
areaKey : str = Field(examples=["123456"])
afterAreaName : Optional[str] = Field(examples=["집2"])

class ModifySafeAreaGroup(BaseModel):
dementiaKey : str = Field(examples=["123456"])
areaKey : str = Field(examples=["123456"])
groupKey : str = Field(examples=["123456"])

class safeAreaGruop(BaseModel):
safeAreas : List[safeAreaList]

class GetSafeAreaGroupResponse(BaseModel):
status: str = Field("success")
message: str = Field("메~시~지~")
result: safeAreaGruop

class ModifySafeAreaGroupName(BaseModel):
dementiaKey : str = Field(examples=["123456"])
groupKey : str = Field(examples=["123456"])
afterGroupName : Optional[str] = Field(examples=["안심구역 그룹 2"])

class DeleteSafeAreaRequest(BaseModel):
dementiaKey : str = Field(examples=["123456"])
areaKey : str = Field(examples=["123456"])

class DeleteSafeAreaGroupRequest(BaseModel):
dementiaKey : str = Field(examples=["123456"])
groupKey : str = Field(examples=["123456"])

class FCMRequest(BaseModel):
token : str = Field(examples=["ksjdnfjkdasnfljsknafljansdfjlsakn"])
title : str = Field(examples=["어디U"])
body : str = Field(examples=["안심 구역 진입"])
data : Dict[str, str] = Field(examples=[{"safeAreaName" : "집", "time" : "12:00:00"}])


class AddressConversionRequest(BaseModel):
address : str = Field(examples=["서울특별시 강남구 니가 사는 그 집"])

class latilongi(BaseModel):
latitude : float = Field(examples=["37.123456"])
longitude : float = Field(examples=["127.123456"])

class AddressConversionResponse(BaseModel):
status: str = Field("success")
message: str = Field("메~시~지~")
result: Dict[str, float] = latilongi

class GPTRequest(BaseModel):
dementiaKey : str = Field(examples=["123456"])
date : str = Field(examples=["2024-03-19"], description="yyyy-mm-dd")

class groupKey(BaseModel):
groupKey : str = Field(examples=["123456"])

class RegisterSafeAreaGroupResponse(BaseModel):
status: str = Field("success")
message: str = Field("메~시~지~")
result: groupKey

class safeArea(BaseModel):
areaName : str = Field(examples=["집"])
latitude : float = Field(examples=["37.123456"])
longitude : float = Field(examples=["127.123456"])
radius : float = Field(examples=["0.5"], description="킬로미터 단위")

class GetSafeAreaAllResponse(BaseModel):
status: str = Field("success")
message: str = Field("메~시~지~")
result: List[safeArea]
33 changes: 27 additions & 6 deletions backend/fastapi/app/fcm_notification.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
from pyfcm import FCMNotification
import firebase_admin
from firebase_admin import credentials
from firebase_admin import messaging

from .config import Config

push_service = FCMNotification(Config.fcm_server_key)
cred_path = Config.cred_path
cred = credentials.Certificate(cred_path)
firebase_admin.initialize_app(cred)



async def send_push_notification(token, title, body, data):

'''result = push_service.notify_single_device(
registration_id=token,
message_title=title,
message_body=body,
data_message=data
)'''

message = messaging.Message(
notification=messaging.Notification(
title=title,
body=body
),
data=data,
token=token
)

def send_push_notification(token, body, title):

result = push_service.notify_single_device(registration_id=token, message_title=title, message_body=body)
response = messaging.send(message)

return result
print('Successfully sent message:', response)

27 changes: 24 additions & 3 deletions backend/fastapi/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class nok_info(Base):
nok_phonenumber = Column(String)
update_rate = Column(String)
dementia_info_key = Column(String)
fcm_token = Column(String)

class dementia_info(Base):
__tablename__ = 'dementia_info'
Expand Down Expand Up @@ -48,6 +49,8 @@ class location_info(Base):
isRingstoneOn = Column(Integer)
isGpsOn = Column(Boolean)
current_speed = Column(String)
isInSafeArea = Column(Boolean)
nearSafeArea = Column(String)

class meaningful_location_info(Base):
__tablename__ = 'meaningful_location_info'
Expand All @@ -69,7 +72,6 @@ class police_info(Base):
key = Column(String)
policeName = Column(String)
policeAddress = Column(String)
roadAddress = Column(String)
policePhoneNumber = Column(String)
distance = Column(Integer)
latitude = Column(Double)
Expand All @@ -78,5 +80,24 @@ class police_info(Base):
class refresh_token_info(Base):
__tablename__ = 'refresh_token_info'

key = Column(String)
refresh_token = Column(String)
key = Column(String, primary_key=True)
refresh_token = Column(String)
fcm_token = Column(String)

class safe_area_info(Base):
__tablename__ = 'safe_area_info'

area_key = Column(String, primary_key=True)
group_key = Column(String)
area_name = Column(String)
dementia_key = Column(String)
latitude = Column(Double)
longitude = Column(Double)
radius = Column(Double)

class safe_area_group_info(Base):
__tablename__ = 'safe_area_group_info'

group_key = Column(String, primary_key=True)
group_name = Column(String)
dementia_key = Column(String)
14 changes: 5 additions & 9 deletions backend/fastapi/app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from .config import Config
from .schedularFunc import SchedulerFunc
from .fcm_notification import send_push_notification
from .LocationPredict import ForecastLSTMClassification, Preprocessing
#from .LocationPredict import ForecastLSTMClassification, Preprocessing

import asyncio
import datetime
Expand Down Expand Up @@ -868,15 +868,11 @@ async def predict_location(user_info : int = Depends(APIKeyHeader(name = "Author
session.close()


@router.post("/test/fcm", responses = {200 : {"model" : CommonResponse, "description" : "FCM 전송 성공" }}, description="FCM 테스트")
async def send_fcm(title: str, body: str, token: str):
@router.post("/test/fcm", description="FCM 테스트")
async def send_fcm(title: str, body: str, token: str, data : str):

send_push_notification(token, body, title)

return {
'status': 'success',
'message': 'FCM sent'
}
return send_push_notification(token, body, title, data)



'''#스케줄러 비활성화
Expand Down
Loading

0 comments on commit d73d7dd

Please sign in to comment.