Skip to content

complexed_chart.py api 수정해야될 부분 #7

@CutTheWire

Description

@CutTheWire

./Py/api/complexed_chart.py

def execute_read_query(control):
    conn = get_db_connection()
    cursor = conn.cursor()
    query = '''
        SELECT idx, temperature, humidity, ground1, ground2, created_at 
        FROM smartFarm 
    '''

    if control == 0:
        print("전체 데이터 출력")
        query += 'WHERE date(created_at) <= date()'
    elif control == 1:
        print("최신 데이터 출력")
        query += 'ORDER BY created_at DESC LIMIT 1'
    elif control == 2:
        print("7일 데이터 출력")
        query += "WHERE date(created_at) BETWEEN date('now', '-7 days') AND date('now')"

    cursor.execute(query)
    logging.info("데이터베이스 쿼리 실행")
    rows = cursor.fetchall()
    conn.close()
    return rows

. . .
 
@app.get("/api")
async def get_sensor_data():
    logging.info("API /api 호출됨")
    rows = execute_read_query(control=0)
    data = [dict(temperature=row[1], humidity=row[2], ground1=row[3], ground2=row[4], ceated_at=row[5]) for row in rows]
    if data:
        return data
    else:
        return JSONResponse(content={"message": "데이터가 없습니다."})

@app.get("/api/latest")
async def get_latest_sensor_data():
    logging.info("API /api/latest 호출됨")
    rows = execute_read_query(control=1)
    data = [dict(temperature=row[1], humidity=row[2], ground1=row[3], ground2=row[4]) for row in rows]
    if data:
        return data[0]
    else:
        return JSONResponse(content={"message": "데이터가 없습니다."})
    
@app.get("/api/week")
async def get_week_sensor_data():
    logging.info("API /api/week 호출됨")
    rows = execute_read_query(control=2)
    data = [dict(temperature=row[1], humidity=row[2], ground1=row[3], ground2=row[4]) for row in rows]
    if data:
        return data[0]
    else:
        return JSONResponse(content={"message": "데이터가 없습니다."})

최신(latest), 일(day), 주(week), 월(month)단위로 JMSPlant.db의 smartFarm 테이블 값을 보내주는 형태로 변경

일 단위

  • 시간별로 0~24시까지 24개의 데이터를 전송

    • 각 시간별 가장 처음 생성된 데이터 전송
    • ex_1) 11시 기준 [10:59:50, 11:00:01, 11:00:05] 데이터들이 있다면 11:00:01 데이터 전송
    • ex_2) 15시 기준 [14:59:59, 15:01:03, 15:01:04] 데이터들이 있다면 15:01:03 데이터 전송
  • 현재 월 기준으로 일 선택하여 데이터 받아오도록 기능 수정

    • 24.05. 기준 5월 1일 ~ 31일에서 원하는 날짜를 선택하면 해당 값을 보내주도록

주 단위

  • 요일별로 월~일요일까지 7개의 데이터를 전송

    • 각 요일별 가장 처음 생성된 데이터 전송
    • ex_1) 24.05.13 월요일 기준 [00:00:50, 00:00:55, 00:01:03] 데이터들이 있다면 00:00:50 데이터 전송
  • 현재 월 기준으로 주차를 선택하여 데이터 받아오도록 기능 수정

    • 24.05. 기준 5월 1일 ~ 5일 까지 1추자, 5월 6일 ~ 12일 까지 2추자, ... 주차를 선택하면 해당 값을 보내주도록

월 단위

  • 월별로 1일 ~ 해당 월의 마지막 n일까지 n개의 데이터를 전송

    • 일별 가장 처음 생성된 데이터 전송
    • ex_1) 24.05.13 기준 [00:00:50, 00:00:55, 00:01:03] 데이터들이 있다면 00:00:50 데이터 전송
  • 현재 월 기준으로 현재 진행된 날짜까지 선택하여 데이터 받아오도록 기능 수정

공통사항

  • 각 api는 현재 기준 진행된 부분까지만 전송
  • 누락된 기간이 있다면 Null값으로 전송

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions