优化代码风格
This commit is contained in:
@ -37,7 +37,7 @@ def increment_alarm_count_by_ip(client_ip: str) -> bool:
|
||||
if not cursor.fetchone():
|
||||
raise ValueError(f"客户端IP为 {client_ip} 的设备不存在")
|
||||
|
||||
# 报警次数加1,并更新时间戳
|
||||
# 报警次数加1、并更新时间戳
|
||||
update_query = """
|
||||
UPDATE devices
|
||||
SET alarm_count = alarm_count + 1,
|
||||
@ -51,7 +51,7 @@ def increment_alarm_count_by_ip(client_ip: str) -> bool:
|
||||
except MySQLError as e:
|
||||
if conn:
|
||||
conn.rollback()
|
||||
raise Exception(f"更新报警次数失败:{str(e)}") from e
|
||||
raise Exception(f"更新报警次数失败: {str(e)}") from e
|
||||
finally:
|
||||
db.close_connection(conn, cursor)
|
||||
|
||||
@ -99,7 +99,7 @@ def update_online_status_by_ip(client_ip: str, online_status: int) -> bool:
|
||||
except MySQLError as e:
|
||||
if conn:
|
||||
conn.rollback()
|
||||
raise Exception(f"更新设备在线状态失败:{str(e)}") from e
|
||||
raise Exception(f"更新设备在线状态失败: {str(e)}") from e
|
||||
finally:
|
||||
db.close_connection(conn, cursor)
|
||||
|
||||
@ -124,7 +124,7 @@ async def create_device(device_data: DeviceCreateRequest, request: Request): #
|
||||
# 返回信息
|
||||
return APIResponse(
|
||||
code=200,
|
||||
message=f"设备IP {device_data.ip} 已存在,返回已有设备信息",
|
||||
message=f"设备IP {device_data.ip} 已存在、返回已有设备信息",
|
||||
data=DeviceResponse(** existing_device)
|
||||
)
|
||||
|
||||
@ -173,9 +173,9 @@ async def create_device(device_data: DeviceCreateRequest, request: Request): #
|
||||
except MySQLError as e:
|
||||
if conn:
|
||||
conn.rollback()
|
||||
raise Exception(f"创建设备失败:{str(e)}") from e
|
||||
raise Exception(f"创建设备失败: {str(e)}") from e
|
||||
except json.JSONDecodeError as e:
|
||||
raise Exception(f"设备详细信息JSON序列化失败:{str(e)}") from e
|
||||
raise Exception(f"设备详细信息JSON序列化失败: {str(e)}") from e
|
||||
except Exception as e:
|
||||
if conn:
|
||||
conn.rollback()
|
||||
@ -185,8 +185,8 @@ async def create_device(device_data: DeviceCreateRequest, request: Request): #
|
||||
|
||||
@router.get("/", response_model=APIResponse, summary="获取设备列表(支持筛选分页)")
|
||||
async def get_device_list(
|
||||
page: int = Query(1, ge=1, description="页码,默认第1页"),
|
||||
page_size: int = Query(10, ge=1, le=100, description="每页条数,1-100之间"),
|
||||
page: int = Query(1, ge=1, description="页码、默认第1页"),
|
||||
page_size: int = Query(10, ge=1, le=100, description="每页条数、1-100之间"),
|
||||
device_type: str = Query(None, description="按设备类型筛选"),
|
||||
online_status: int = Query(None, ge=0, le=1, description="按在线状态筛选")
|
||||
):
|
||||
@ -233,6 +233,6 @@ async def get_device_list(
|
||||
)
|
||||
|
||||
except MySQLError as e:
|
||||
raise Exception(f"获取设备列表失败:{str(e)}") from e
|
||||
raise Exception(f"获取设备列表失败: {str(e)}") from e
|
||||
finally:
|
||||
db.close_connection(conn, cursor)
|
||||
|
Reference in New Issue
Block a user