修正接口故障时程序报错的问题
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
import requests
|
||||
import hashlib
|
||||
from config.settings import BASE_URL, LOGIN_DATA
|
||||
import time
|
||||
|
||||
|
||||
class APIClient:
|
||||
@ -22,50 +23,81 @@ class APIClient:
|
||||
login_data = LOGIN_DATA.copy()
|
||||
login_data["password"] = self.hash_password(login_data["password"])
|
||||
|
||||
response = requests.post(self.login_url, json=login_data)
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
if data.get("Code") == 200:
|
||||
self.app_id = data["Data"]["AppID"]
|
||||
return self.app_id
|
||||
raise Exception("登录失败,无法获取AppID")
|
||||
try:
|
||||
response = requests.post(self.login_url, json=login_data, timeout=10)
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
if data.get("Code") == 200:
|
||||
self.app_id = data["Data"]["AppID"]
|
||||
return self.app_id
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"登录请求异常: {e}")
|
||||
except Exception as e:
|
||||
print(f"登录过程中发生未知错误: {e}")
|
||||
|
||||
print("登录失败,无法获取AppID")
|
||||
return None
|
||||
|
||||
def get_mould_info(self):
|
||||
"""获取模具的管片信息并提取TaskID"""
|
||||
if not self.app_id:
|
||||
raise Exception("请先登录获取AppID")
|
||||
print("请先登录获取AppID")
|
||||
return None
|
||||
|
||||
headers = {"AppID": self.app_id}
|
||||
response = requests.get(self.mould_info_url, headers=headers)
|
||||
if response.status_code == 205:
|
||||
data = response.json()
|
||||
if data.get("Code") == 200:
|
||||
return data["Data"]["BetonTaskID"]
|
||||
raise Exception("获取模具信息失败")
|
||||
try:
|
||||
headers = {"AppID": self.app_id}
|
||||
response = requests.get(self.mould_info_url, headers=headers, timeout=10)
|
||||
if response.status_code == 205:
|
||||
data = response.json()
|
||||
if data.get("Code") == 200:
|
||||
return data["Data"]["BetonTaskID"]
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"获取模具信息请求异常: {e}")
|
||||
except Exception as e:
|
||||
print(f"获取模具信息过程中发生未知错误: {e}")
|
||||
|
||||
print("获取模具信息失败")
|
||||
return None
|
||||
|
||||
def get_task_info(self, task_id):
|
||||
"""获取任务单信息"""
|
||||
if not self.app_id:
|
||||
raise Exception("请先登录获取AppID")
|
||||
print("请先登录获取AppID")
|
||||
return None
|
||||
|
||||
headers = {"AppID": self.app_id}
|
||||
url = f"{self.task_info_url}?TaskId={task_id}"
|
||||
response = requests.get(url, headers=headers)
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
if data.get("Code") == 200:
|
||||
return data["Data"]
|
||||
raise Exception("获取任务单信息失败")
|
||||
try:
|
||||
headers = {"AppID": self.app_id}
|
||||
url = f"{self.task_info_url}?TaskId={task_id}"
|
||||
response = requests.get(url, headers=headers, timeout=10)
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
if data.get("Code") == 200:
|
||||
return data["Data"]
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"获取任务单信息请求异常: {e}")
|
||||
except Exception as e:
|
||||
print(f"获取任务单信息过程中发生未知错误: {e}")
|
||||
|
||||
print("获取任务单信息失败")
|
||||
return None
|
||||
|
||||
def get_not_pour_info(self):
|
||||
"""获取所有未浇筑信息"""
|
||||
if not self.app_id:
|
||||
raise Exception("请先登录获取AppID")
|
||||
print("请先登录获取AppID")
|
||||
return None
|
||||
|
||||
headers = {"AppID": self.app_id}
|
||||
response = requests.get(self.not_pour_info_url, headers=headers)
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
if data.get("Code") == 200:
|
||||
return data["Data"]
|
||||
raise Exception("获取未浇筑信息失败")
|
||||
try:
|
||||
headers = {"AppID": self.app_id}
|
||||
response = requests.get(self.not_pour_info_url, headers=headers, timeout=10)
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
if data.get("Code") == 200:
|
||||
return data["Data"]
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"获取未浇筑信息请求异常: {e}")
|
||||
except Exception as e:
|
||||
print(f"获取未浇筑信息过程中发生未知错误: {e}")
|
||||
|
||||
print("获取未浇筑信息失败")
|
||||
return None
|
||||
@ -27,7 +27,7 @@ TCP_HOST = '127.0.0.1'
|
||||
TCP_PORT = 8888
|
||||
|
||||
# 新增TCP客户端配置
|
||||
TCP_CLIENT_HOST = '192.168.1.100'
|
||||
TCP_CLIENT_HOST = '10.6.242.150'
|
||||
TCP_CLIENT_PORT = 8889
|
||||
|
||||
# 其他配置
|
||||
|
||||
39
main.py
39
main.py
@ -16,10 +16,6 @@ from config.settings import (
|
||||
)
|
||||
from utils.helpers import cleanup_old_timestamps
|
||||
|
||||
# 假设同事提供的函数
|
||||
def save_to_custom_table(misid, flag, task_id, produce_mix_id, project_name, beton_grade, adjusted_volume, artifact_id):
|
||||
"""保存到自定义数据表的函数"""
|
||||
print(f"保存到自定义数据表: MISID={misid}, Flag={flag}, TaskID={task_id}, 调整后方量={adjusted_volume}")
|
||||
|
||||
def start_api_service():
|
||||
"""启动配比重量API服务"""
|
||||
@ -52,7 +48,10 @@ def main():
|
||||
|
||||
# 步骤1:获取AppID
|
||||
app_id = api_client.login()
|
||||
task_service.api_client.app_id = app_id
|
||||
if app_id is None:
|
||||
print("无法获取AppID,将在稍后重试...")
|
||||
else:
|
||||
task_service.api_client.app_id = app_id
|
||||
|
||||
# 存储上次获取的所有ArtifactID
|
||||
last_artifact_ids = set()
|
||||
@ -65,8 +64,27 @@ def main():
|
||||
|
||||
while True:
|
||||
try:
|
||||
# 步骤2:获取所有未浇筑信息
|
||||
# 如果没有有效的app_id,尝试重新登录
|
||||
if task_service.api_client.app_id is None:
|
||||
print("尝试重新登录获取AppID...")
|
||||
app_id = api_client.login()
|
||||
if app_id is not None:
|
||||
task_service.api_client.app_id = app_id
|
||||
print("重新登录成功")
|
||||
else:
|
||||
print("重新登录失败,稍后重试...")
|
||||
time.sleep(10) # 等待10秒再重试
|
||||
continue
|
||||
|
||||
# 获取所有未浇筑信息
|
||||
tasks, artifact_list, send_list, half_volume = task_service.process_not_pour_info()
|
||||
|
||||
# 如果API调用失败,等待一段时间再重试
|
||||
if tasks is None or artifact_list is None:
|
||||
print("获取未浇筑信息失败,稍后重试...")
|
||||
time.sleep(10)
|
||||
continue
|
||||
|
||||
current_artifact_ids = {task["artifact_id"] for task in tasks}
|
||||
|
||||
# 检查artifact_list是否发生变化
|
||||
@ -81,8 +99,13 @@ def main():
|
||||
for task in tasks:
|
||||
if task["artifact_id"] in new_artifact_ids:
|
||||
task_info = api_client.get_task_info(task["beton_task_id"])
|
||||
|
||||
# 如果获取任务信息失败,跳过该任务
|
||||
if task_info is None:
|
||||
print(f"无法获取任务信息,跳过任务: {task['artifact_id']}")
|
||||
continue
|
||||
|
||||
# 步骤4:连接Access数据库并获取最大Mark值
|
||||
# 连接Access数据库并获取最大Mark值
|
||||
access_db = AccessDB(ACCESS_DB_PATH, ACCESS_DB_PASSWORD)
|
||||
try:
|
||||
max_mark = access_db.get_max_mark()
|
||||
@ -91,7 +114,7 @@ def main():
|
||||
|
||||
erp_id = int(max_mark) + 1
|
||||
|
||||
# 步骤5:连接SQL Server数据库并插入数据
|
||||
# 连接SQL Server数据库并插入数据
|
||||
sql_db = SQLServerDB()
|
||||
try:
|
||||
# 检查 block_number 是否为 "补方"
|
||||
|
||||
@ -24,6 +24,10 @@ class TaskService:
|
||||
def process_not_pour_info(self):
|
||||
"""处理未浇筑信息"""
|
||||
artifact_list = self.api_client.get_not_pour_info()
|
||||
|
||||
# 如果API调用失败,返回空列表而不是抛出异常
|
||||
if artifact_list is None:
|
||||
return [], [], [], self.half_volume
|
||||
|
||||
if not artifact_list:
|
||||
return [], [], [], self.half_volume
|
||||
@ -37,8 +41,12 @@ class TaskService:
|
||||
|
||||
# 处理当前任务
|
||||
current_task = self._process_current_task(artifact_list[0])
|
||||
|
||||
# 如果获取任务信息失败,则返回空结果
|
||||
if current_task is None:
|
||||
return [], [], [], self.half_volume
|
||||
|
||||
# 根据F块情况处理任务
|
||||
# 根据F块情况处理任务
|
||||
task_result = self._handle_tasks_by_f_blocks(
|
||||
f_block_count, f_positions, current_task,
|
||||
f_blocks, total_f_volume, artifact_list
|
||||
@ -69,6 +77,11 @@ class TaskService:
|
||||
def _process_current_task(self, latest_artifact):
|
||||
"""处理当前任务信息"""
|
||||
task_data = self.api_client.get_task_info(latest_artifact["BetonTaskID"])
|
||||
|
||||
# 如果API调用失败,返回None
|
||||
if task_data is None:
|
||||
return None
|
||||
|
||||
return {
|
||||
"beton_task_id": latest_artifact["BetonTaskID"],
|
||||
"beton_volume": latest_artifact["BetonVolume"],
|
||||
@ -252,27 +265,31 @@ class TaskService:
|
||||
|
||||
if second_task:
|
||||
task_data_second = self.api_client.get_task_info(second_task["BetonTaskID"])
|
||||
send_list.append({
|
||||
"beton_task_id": second_task["BetonTaskID"],
|
||||
"beton_volume": volumes[0],
|
||||
"artifact_id": second_task["ArtifactActionID"],
|
||||
"block_number": second_task["BlockNumber"],
|
||||
"beton_grade": task_data_second["BetonGrade"],
|
||||
"mix_id": task_data_second["MixID"],
|
||||
"time": self.artifact_timestamps.get(second_task["ArtifactActionID"], datetime.now())
|
||||
})
|
||||
# 如果获取任务信息失败,跳过该任务
|
||||
if task_data_second is not None:
|
||||
send_list.append({
|
||||
"beton_task_id": second_task["BetonTaskID"],
|
||||
"beton_volume": volumes[0],
|
||||
"artifact_id": second_task["ArtifactActionID"],
|
||||
"block_number": second_task["BlockNumber"],
|
||||
"beton_grade": task_data_second["BetonGrade"],
|
||||
"mix_id": task_data_second["MixID"],
|
||||
"time": self.artifact_timestamps.get(second_task["ArtifactActionID"], datetime.now())
|
||||
})
|
||||
|
||||
if third_task:
|
||||
task_data_third = self.api_client.get_task_info(third_task["BetonTaskID"])
|
||||
send_list.append({
|
||||
"beton_task_id": third_task["BetonTaskID"],
|
||||
"beton_volume": volumes[1],
|
||||
"artifact_id": third_task["ArtifactActionID"],
|
||||
"block_number": third_task["BlockNumber"],
|
||||
"beton_grade": task_data_third["BetonGrade"],
|
||||
"mix_id": task_data_third["MixID"],
|
||||
"time": self.artifact_timestamps.get(third_task["ArtifactActionID"], datetime.now())
|
||||
})
|
||||
# 如果获取任务信息失败,跳过该任务
|
||||
if task_data_third is not None:
|
||||
send_list.append({
|
||||
"beton_task_id": third_task["BetonTaskID"],
|
||||
"beton_volume": volumes[1],
|
||||
"artifact_id": third_task["ArtifactActionID"],
|
||||
"block_number": third_task["BlockNumber"],
|
||||
"beton_grade": task_data_third["BetonGrade"],
|
||||
"mix_id": task_data_third["MixID"],
|
||||
"time": self.artifact_timestamps.get(third_task["ArtifactActionID"], datetime.now())
|
||||
})
|
||||
|
||||
def _append_additional_tasks_for_single_f(self, send_list, artifact_list, f_positions):
|
||||
"""为单个F块情况添加额外任务"""
|
||||
@ -281,30 +298,34 @@ class TaskService:
|
||||
|
||||
if second_task:
|
||||
task_data_second = self.api_client.get_task_info(second_task["BetonTaskID"])
|
||||
volume = (third_task["BetonVolume"] if third_task else 0) if f_positions != [2] else (
|
||||
second_task["BetonVolume"] + self.half_volume[1])
|
||||
send_list.append({
|
||||
"beton_task_id": second_task["BetonTaskID"],
|
||||
"beton_volume": volume,
|
||||
"artifact_id": second_task["ArtifactActionID"],
|
||||
"block_number": second_task["BlockNumber"],
|
||||
"beton_grade": task_data_second["BetonGrade"],
|
||||
"mix_id": task_data_second["MixID"],
|
||||
"time": self.artifact_timestamps.get(second_task["ArtifactActionID"], datetime.now())
|
||||
})
|
||||
# 如果获取任务信息失败,跳过该任务
|
||||
if task_data_second is not None:
|
||||
volume = (third_task["BetonVolume"] if third_task else 0) if f_positions != [2] else (
|
||||
second_task["BetonVolume"] + self.half_volume[1])
|
||||
send_list.append({
|
||||
"beton_task_id": second_task["BetonTaskID"],
|
||||
"beton_volume": volume,
|
||||
"artifact_id": second_task["ArtifactActionID"],
|
||||
"block_number": second_task["BlockNumber"],
|
||||
"beton_grade": task_data_second["BetonGrade"],
|
||||
"mix_id": task_data_second["MixID"],
|
||||
"time": self.artifact_timestamps.get(second_task["ArtifactActionID"], datetime.now())
|
||||
})
|
||||
|
||||
if third_task:
|
||||
task_data_third = self.api_client.get_task_info(third_task["BetonTaskID"])
|
||||
volume = third_task["BetonVolume"] if f_positions in [[1], [0]] else 0
|
||||
send_list.append({
|
||||
"beton_task_id": third_task["BetonTaskID"],
|
||||
"beton_volume": volume,
|
||||
"artifact_id": third_task["ArtifactActionID"],
|
||||
"block_number": third_task["BlockNumber"],
|
||||
"beton_grade": task_data_third["BetonGrade"],
|
||||
"mix_id": task_data_third["MixID"],
|
||||
"time": self.artifact_timestamps.get(third_task["ArtifactActionID"], datetime.now())
|
||||
})
|
||||
# 如果获取任务信息失败,跳过该任务
|
||||
if task_data_third is not None:
|
||||
volume = third_task["BetonVolume"] if f_positions in [[1], [0]] else 0
|
||||
send_list.append({
|
||||
"beton_task_id": third_task["BetonTaskID"],
|
||||
"beton_volume": volume,
|
||||
"artifact_id": third_task["ArtifactActionID"],
|
||||
"block_number": third_task["BlockNumber"],
|
||||
"beton_grade": task_data_third["BetonGrade"],
|
||||
"mix_id": task_data_third["MixID"],
|
||||
"time": self.artifact_timestamps.get(third_task["ArtifactActionID"], datetime.now())
|
||||
})
|
||||
|
||||
def _update_artifact_timestamps(self, send_list):
|
||||
"""更新artifact时间戳"""
|
||||
@ -431,4 +452,3 @@ class TaskService:
|
||||
except Exception as e:
|
||||
print(f"发送状态更新到另一台电脑时出错: {e}")
|
||||
print(f"原计划更新自定义数据表状态: ERP ID={erp_id}, 状态={status}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user