diff --git a/sql/sql.py b/sql/sql.py index 273d043..82cc767 100644 --- a/sql/sql.py +++ b/sql/sql.py @@ -172,7 +172,16 @@ def get_has_clientId_sql(clientid): "sql": f"select timestamp from root.farm.clientId where clientId='{clientid}'" } return send_json - +# 获取空气设备正常7天的报警数据sql +def air_device_normal_past_seven_days_sql(deviceId): + current_date = datetime.now().date() + tomorrow = current_date + timedelta(days=1) + past_6_days_date = current_date - timedelta(days=6) + sql = f"select MAX_VALUE(air_temperature),MAX_VALUE(air_humidity) from root.farm.{deviceId} group by ([{past_6_days_date}, {tomorrow}), 1d)" + send_json = { + "sql": sql + } + return send_json # 获取空气设备历史7天的报警数据sql def air_device_past_seven_days_sql(deviceId): @@ -184,7 +193,16 @@ def air_device_past_seven_days_sql(deviceId): "sql": sql } return send_json - +# 获取普通设备正常7天的报警数据sql +def common_device_normal_past_seven_days_sql(deviceId): + current_date = datetime.now().date() + tomorrow = current_date + timedelta(days=1) + past_6_days_date = current_date - timedelta(days=6) + sql = f"select MAX_VALUE(value) from root.farm.{deviceId} group by ([{past_6_days_date}, {tomorrow}), 1d)" + send_json = { + "sql": sql + } + return send_json # 获取普通设备历史7天的报警数据sql def common_device_past_seven_days_sql(deviceId): diff --git a/xumu.py b/xumu.py index c236793..0d3a105 100644 --- a/xumu.py +++ b/xumu.py @@ -194,7 +194,6 @@ async def register(request: Request): return BaseResponse(code=500, msg=str(e)) - # 数据发布接口 async def message_publish(data): try: @@ -288,7 +287,37 @@ async def process_data(request: Request): return BaseResponse(code=500, msg=str(e)) -# 报警数据最近7天的数据 +@app.get("/api/xumu/normal/past_seven_days") +async def past_seven_days(deviceId): + try: + the_decvice = await get_device(None, deviceId) + device_type = None + if the_decvice.code == 200: + device_values = the_decvice.data["values"] + if len(device_values) != 0: + device_type = device_values[1][0] + # 如果是空气设备,则有两个值 + if device_type == 1: + send_json = air_device_normal_past_seven_days_sql(deviceId) + # 如果是其他设备,则有一个值,RFID不算 + else: + send_json = common_device_normal_past_seven_days_sql(deviceId) + r = requests.post(baseHost + queryUri, headers=headers, json=send_json) + data = r.json() + timestamps = data["timestamps"] + times = [] + for timestamp in timestamps: + dt = datetime.fromtimestamp(timestamp / 1000) # 因为时间戳是以毫秒为单位的,所以需要除以1000 + # 将datetime对象格式化为只包含年月日的字符串 + year_month_day_str = dt.strftime('%Y-%m-%d') + times.append(year_month_day_str) + data["timestamps"] = times + return BaseResponse(data=data) + except Exception as e: + return BaseResponse(code=500, msg=str(e)) + + +# 正常数据最近7天的数据 @app.get("/api/xumu/warning/past_seven_days") async def past_seven_days(deviceId): try: