class Config: json_encoders = {ObjectId: str} schema_extra = { "example": { "_id": "60901b909232236ad8c4f0d6", "device": "12341b909232236a2314f0d6", "user": "******", "date": get_current_datetime_str(), "reward_date": get_current_datetime_str(), } }
async def person_reward(device_id: str, uid: str, data_id: str): # 포인트 데이터 조회 data = db.points.find_one({"_id": ObjectId(data_id)}) # data_id 검사 if not data: print("Access denied: data_id is invaild!") raise HTTPException(status_code=400, detail="Access denied") data = PointData.parse_obj(data) # device_id 검사 if data.device != ObjectId(device_id): print("Access denied: device_id is not matched!") raise HTTPException(status_code=400, detail="Access denied") # 중복 요청 검사 if data.user: print("Access denied: Already rewarded!") raise HTTPException(status_code=400, detail="Access denied") data.user = uid data.reward_date = get_current_datetime_str() # 기기ID로 기관 조회 device = db.devices.find_one({"_id": ObjectId(device_id)}) organization = db.organizations.find_one({"_id": device["organization"]}) # 당월 티켓발행수 조회 thismonth = int(datetime.today().strftime("%Y%m")) num_tickets = len( list(db.tickets.find({ "user": uid, "yearmonth": thismonth }))) result, m_user = get_user(uid) if result != "success": raise HTTPException(status_code=400, detail=result) if num_tickets < 3: # 추첨권 발행 new_ticket = TicketData( organization=ObjectId(organization["_id"]), user=uid, date=get_current_datetime_str(), yearmonth=thismonth, ) db.tickets.insert_one(new_ticket.dict(by_alias=False)) else: # 개인 포인트 적립 m_user.point += 1 db.users.update({"uid": uid}, m_user.dict(by_alias=False)) # 적립 기록 레코드 갱신 db.points.update({"_id": ObjectId(data_id)}, data.dict(by_alias=False)) return m_user
def get_point_data_dict() -> str: """Point data 문서 생성""" logger.info(mqtt.device_id) logger.info(mqtt.organization_id) point_data = PointData.validate({ "device": mqtt.device_id, "organization": mqtt.organization_id, "date": get_current_datetime_str(), }) point_data = point_data.json(exclude={"id", "point"}) return point_data
class Config: json_encoders = {ObjectId: str} schema_extra = { "example": { "device": "1", "sensor": "plastic", "percentage": 10.3, "state": "ON", "date": get_current_datetime_str(), } }
class Config: json_encoders = {ObjectId: str} schema_extra = { "example": { "organization": "1", "user": "******", "team": "소융대18", "point": 40, "date": get_current_datetime_str(), } }
class Config: json_encoders = {ObjectId: str} schema_extra = { "example": { "_id": "55551b909232236ad8c4f011", "organization": "60901b909232236a2314f0d6", "user": "******", "date": get_current_datetime_str(), "yearmonth": 202101, } }
class Config: json_encoders = {ObjectId: str} schema_extra = { "example": { "_id": "60901b909232236ad8c4f0d6", "device": "60901b909232236a2341f0d6", "sensor": "plastic", "percentage": 10.3, "state": "ON", "date": get_current_datetime_str(), } }
def get_capacity_data_dict(sensor_type: str, sensor_value: float) -> str: """Capacity data 문서 생성""" state = "ON" if sensor_value >= 100 else "OFF" capacity_data = CapacityData.validate({ "device": mqtt.device_id, "organization": mqtt.organization_id, "sensor": sensor_type, "percentage": sensor_value, "state": state, "date": get_current_datetime_str(), }) capacity_data = capacity_data.json(exclude={"id"}) return capacity_data
class Config: json_encoders = {ObjectId: str} schema_extra = { "example": { "name": "국민쓰샘1호", "model": "model_1", "organization": "국민대학교", "install_date": get_current_datetime_str(), "latitude": 37.61090337619938, "longitude": 126.99727816928652, "location_description": "국민대학교 미래관 4층 자율주행스튜디오 앞", "point": 377, } }