修正接口故障时程序报错的问题
This commit is contained in:
@ -2,6 +2,7 @@
|
|||||||
import requests
|
import requests
|
||||||
import hashlib
|
import hashlib
|
||||||
from config.settings import BASE_URL, LOGIN_DATA
|
from config.settings import BASE_URL, LOGIN_DATA
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
class APIClient:
|
class APIClient:
|
||||||
@ -22,50 +23,81 @@ class APIClient:
|
|||||||
login_data = LOGIN_DATA.copy()
|
login_data = LOGIN_DATA.copy()
|
||||||
login_data["password"] = self.hash_password(login_data["password"])
|
login_data["password"] = self.hash_password(login_data["password"])
|
||||||
|
|
||||||
response = requests.post(self.login_url, json=login_data)
|
try:
|
||||||
|
response = requests.post(self.login_url, json=login_data, timeout=10)
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
data = response.json()
|
data = response.json()
|
||||||
if data.get("Code") == 200:
|
if data.get("Code") == 200:
|
||||||
self.app_id = data["Data"]["AppID"]
|
self.app_id = data["Data"]["AppID"]
|
||||||
return self.app_id
|
return self.app_id
|
||||||
raise Exception("登录失败,无法获取AppID")
|
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):
|
def get_mould_info(self):
|
||||||
"""获取模具的管片信息并提取TaskID"""
|
"""获取模具的管片信息并提取TaskID"""
|
||||||
if not self.app_id:
|
if not self.app_id:
|
||||||
raise Exception("请先登录获取AppID")
|
print("请先登录获取AppID")
|
||||||
|
return None
|
||||||
|
|
||||||
|
try:
|
||||||
headers = {"AppID": self.app_id}
|
headers = {"AppID": self.app_id}
|
||||||
response = requests.get(self.mould_info_url, headers=headers)
|
response = requests.get(self.mould_info_url, headers=headers, timeout=10)
|
||||||
if response.status_code == 205:
|
if response.status_code == 205:
|
||||||
data = response.json()
|
data = response.json()
|
||||||
if data.get("Code") == 200:
|
if data.get("Code") == 200:
|
||||||
return data["Data"]["BetonTaskID"]
|
return data["Data"]["BetonTaskID"]
|
||||||
raise Exception("获取模具信息失败")
|
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):
|
def get_task_info(self, task_id):
|
||||||
"""获取任务单信息"""
|
"""获取任务单信息"""
|
||||||
if not self.app_id:
|
if not self.app_id:
|
||||||
raise Exception("请先登录获取AppID")
|
print("请先登录获取AppID")
|
||||||
|
return None
|
||||||
|
|
||||||
|
try:
|
||||||
headers = {"AppID": self.app_id}
|
headers = {"AppID": self.app_id}
|
||||||
url = f"{self.task_info_url}?TaskId={task_id}"
|
url = f"{self.task_info_url}?TaskId={task_id}"
|
||||||
response = requests.get(url, headers=headers)
|
response = requests.get(url, headers=headers, timeout=10)
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
data = response.json()
|
data = response.json()
|
||||||
if data.get("Code") == 200:
|
if data.get("Code") == 200:
|
||||||
return data["Data"]
|
return data["Data"]
|
||||||
raise Exception("获取任务单信息失败")
|
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):
|
def get_not_pour_info(self):
|
||||||
"""获取所有未浇筑信息"""
|
"""获取所有未浇筑信息"""
|
||||||
if not self.app_id:
|
if not self.app_id:
|
||||||
raise Exception("请先登录获取AppID")
|
print("请先登录获取AppID")
|
||||||
|
return None
|
||||||
|
|
||||||
|
try:
|
||||||
headers = {"AppID": self.app_id}
|
headers = {"AppID": self.app_id}
|
||||||
response = requests.get(self.not_pour_info_url, headers=headers)
|
response = requests.get(self.not_pour_info_url, headers=headers, timeout=10)
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
data = response.json()
|
data = response.json()
|
||||||
if data.get("Code") == 200:
|
if data.get("Code") == 200:
|
||||||
return data["Data"]
|
return data["Data"]
|
||||||
raise Exception("获取未浇筑信息失败")
|
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_PORT = 8888
|
||||||
|
|
||||||
# 新增TCP客户端配置
|
# 新增TCP客户端配置
|
||||||
TCP_CLIENT_HOST = '192.168.1.100'
|
TCP_CLIENT_HOST = '10.6.242.150'
|
||||||
TCP_CLIENT_PORT = 8889
|
TCP_CLIENT_PORT = 8889
|
||||||
|
|
||||||
# 其他配置
|
# 其他配置
|
||||||
|
|||||||
37
main.py
37
main.py
@ -16,10 +16,6 @@ from config.settings import (
|
|||||||
)
|
)
|
||||||
from utils.helpers import cleanup_old_timestamps
|
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():
|
def start_api_service():
|
||||||
"""启动配比重量API服务"""
|
"""启动配比重量API服务"""
|
||||||
@ -52,6 +48,9 @@ def main():
|
|||||||
|
|
||||||
# 步骤1:获取AppID
|
# 步骤1:获取AppID
|
||||||
app_id = api_client.login()
|
app_id = api_client.login()
|
||||||
|
if app_id is None:
|
||||||
|
print("无法获取AppID,将在稍后重试...")
|
||||||
|
else:
|
||||||
task_service.api_client.app_id = app_id
|
task_service.api_client.app_id = app_id
|
||||||
|
|
||||||
# 存储上次获取的所有ArtifactID
|
# 存储上次获取的所有ArtifactID
|
||||||
@ -65,8 +64,27 @@ def main():
|
|||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
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()
|
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}
|
current_artifact_ids = {task["artifact_id"] for task in tasks}
|
||||||
|
|
||||||
# 检查artifact_list是否发生变化
|
# 检查artifact_list是否发生变化
|
||||||
@ -82,7 +100,12 @@ def main():
|
|||||||
if task["artifact_id"] in new_artifact_ids:
|
if task["artifact_id"] in new_artifact_ids:
|
||||||
task_info = api_client.get_task_info(task["beton_task_id"])
|
task_info = api_client.get_task_info(task["beton_task_id"])
|
||||||
|
|
||||||
# 步骤4:连接Access数据库并获取最大Mark值
|
# 如果获取任务信息失败,跳过该任务
|
||||||
|
if task_info is None:
|
||||||
|
print(f"无法获取任务信息,跳过任务: {task['artifact_id']}")
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 连接Access数据库并获取最大Mark值
|
||||||
access_db = AccessDB(ACCESS_DB_PATH, ACCESS_DB_PASSWORD)
|
access_db = AccessDB(ACCESS_DB_PATH, ACCESS_DB_PASSWORD)
|
||||||
try:
|
try:
|
||||||
max_mark = access_db.get_max_mark()
|
max_mark = access_db.get_max_mark()
|
||||||
@ -91,7 +114,7 @@ def main():
|
|||||||
|
|
||||||
erp_id = int(max_mark) + 1
|
erp_id = int(max_mark) + 1
|
||||||
|
|
||||||
# 步骤5:连接SQL Server数据库并插入数据
|
# 连接SQL Server数据库并插入数据
|
||||||
sql_db = SQLServerDB()
|
sql_db = SQLServerDB()
|
||||||
try:
|
try:
|
||||||
# 检查 block_number 是否为 "补方"
|
# 检查 block_number 是否为 "补方"
|
||||||
|
|||||||
@ -25,6 +25,10 @@ class TaskService:
|
|||||||
"""处理未浇筑信息"""
|
"""处理未浇筑信息"""
|
||||||
artifact_list = self.api_client.get_not_pour_info()
|
artifact_list = self.api_client.get_not_pour_info()
|
||||||
|
|
||||||
|
# 如果API调用失败,返回空列表而不是抛出异常
|
||||||
|
if artifact_list is None:
|
||||||
|
return [], [], [], self.half_volume
|
||||||
|
|
||||||
if not artifact_list:
|
if not artifact_list:
|
||||||
return [], [], [], self.half_volume
|
return [], [], [], self.half_volume
|
||||||
|
|
||||||
@ -38,6 +42,10 @@ class TaskService:
|
|||||||
# 处理当前任务
|
# 处理当前任务
|
||||||
current_task = self._process_current_task(artifact_list[0])
|
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(
|
task_result = self._handle_tasks_by_f_blocks(
|
||||||
f_block_count, f_positions, current_task,
|
f_block_count, f_positions, current_task,
|
||||||
@ -69,6 +77,11 @@ class TaskService:
|
|||||||
def _process_current_task(self, latest_artifact):
|
def _process_current_task(self, latest_artifact):
|
||||||
"""处理当前任务信息"""
|
"""处理当前任务信息"""
|
||||||
task_data = self.api_client.get_task_info(latest_artifact["BetonTaskID"])
|
task_data = self.api_client.get_task_info(latest_artifact["BetonTaskID"])
|
||||||
|
|
||||||
|
# 如果API调用失败,返回None
|
||||||
|
if task_data is None:
|
||||||
|
return None
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"beton_task_id": latest_artifact["BetonTaskID"],
|
"beton_task_id": latest_artifact["BetonTaskID"],
|
||||||
"beton_volume": latest_artifact["BetonVolume"],
|
"beton_volume": latest_artifact["BetonVolume"],
|
||||||
@ -252,6 +265,8 @@ class TaskService:
|
|||||||
|
|
||||||
if second_task:
|
if second_task:
|
||||||
task_data_second = self.api_client.get_task_info(second_task["BetonTaskID"])
|
task_data_second = self.api_client.get_task_info(second_task["BetonTaskID"])
|
||||||
|
# 如果获取任务信息失败,跳过该任务
|
||||||
|
if task_data_second is not None:
|
||||||
send_list.append({
|
send_list.append({
|
||||||
"beton_task_id": second_task["BetonTaskID"],
|
"beton_task_id": second_task["BetonTaskID"],
|
||||||
"beton_volume": volumes[0],
|
"beton_volume": volumes[0],
|
||||||
@ -264,6 +279,8 @@ class TaskService:
|
|||||||
|
|
||||||
if third_task:
|
if third_task:
|
||||||
task_data_third = self.api_client.get_task_info(third_task["BetonTaskID"])
|
task_data_third = self.api_client.get_task_info(third_task["BetonTaskID"])
|
||||||
|
# 如果获取任务信息失败,跳过该任务
|
||||||
|
if task_data_third is not None:
|
||||||
send_list.append({
|
send_list.append({
|
||||||
"beton_task_id": third_task["BetonTaskID"],
|
"beton_task_id": third_task["BetonTaskID"],
|
||||||
"beton_volume": volumes[1],
|
"beton_volume": volumes[1],
|
||||||
@ -281,6 +298,8 @@ class TaskService:
|
|||||||
|
|
||||||
if second_task:
|
if second_task:
|
||||||
task_data_second = self.api_client.get_task_info(second_task["BetonTaskID"])
|
task_data_second = self.api_client.get_task_info(second_task["BetonTaskID"])
|
||||||
|
# 如果获取任务信息失败,跳过该任务
|
||||||
|
if task_data_second is not None:
|
||||||
volume = (third_task["BetonVolume"] if third_task else 0) if f_positions != [2] else (
|
volume = (third_task["BetonVolume"] if third_task else 0) if f_positions != [2] else (
|
||||||
second_task["BetonVolume"] + self.half_volume[1])
|
second_task["BetonVolume"] + self.half_volume[1])
|
||||||
send_list.append({
|
send_list.append({
|
||||||
@ -295,6 +314,8 @@ class TaskService:
|
|||||||
|
|
||||||
if third_task:
|
if third_task:
|
||||||
task_data_third = self.api_client.get_task_info(third_task["BetonTaskID"])
|
task_data_third = self.api_client.get_task_info(third_task["BetonTaskID"])
|
||||||
|
# 如果获取任务信息失败,跳过该任务
|
||||||
|
if task_data_third is not None:
|
||||||
volume = third_task["BetonVolume"] if f_positions in [[1], [0]] else 0
|
volume = third_task["BetonVolume"] if f_positions in [[1], [0]] else 0
|
||||||
send_list.append({
|
send_list.append({
|
||||||
"beton_task_id": third_task["BetonTaskID"],
|
"beton_task_id": third_task["BetonTaskID"],
|
||||||
@ -431,4 +452,3 @@ class TaskService:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"发送状态更新到另一台电脑时出错: {e}")
|
print(f"发送状态更新到另一台电脑时出错: {e}")
|
||||||
print(f"原计划更新自定义数据表状态: ERP ID={erp_id}, 状态={status}")
|
print(f"原计划更新自定义数据表状态: ERP ID={erp_id}, 状态={status}")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user