修改尾数控制和状态监控逻辑,添加TCP数据传输和数据库存储

This commit is contained in:
xiongyi
2025-10-30 20:42:57 +08:00
parent bbbe478d3e
commit d3d358762a
6 changed files with 1148 additions and 133 deletions

View File

@ -1,3 +1,5 @@
#在线版本实时监控Flag信息但是还没有实现每次只取最上面那条
import requests
import hashlib
import pyodbc
@ -5,6 +7,7 @@ from datetime import datetime
import time
import json
import threading
from TCPServer import TCPServer
# 配置信息
BASE_URL = "https://www.shnthy.com:9154" # 外网地址
@ -26,6 +29,9 @@ monitored_tasks = set() # 存储需要监控的erp_id
inserted_tasks = {} # {erp_id: artifact_id}
tasks_lock = threading.Lock()
# TCP服务端实例
tcp_server = TCPServer
# 计算SHA256密码
def hash_password(password):
@ -202,22 +208,40 @@ def insert_into_produce_table(connection, task_info, beton_volume, erp_id, artif
try:
# 假设同事提供的函数名为 save_to_custom_table
# 参数包括: MISID(即erp_id), Flag, TaskID, ProduceMixID, ProjectName, BetonGrade, 调整后的方量
save_to_custom_table(
misid=erp_id,
flag="1", # 初始Flag值
task_id=task_info["TaskID"],
produce_mix_id=task_info["ProduceMixID"],
project_name=task_info["ProjectName"],
beton_grade=task_info["BetonGrade"],
adjusted_volume=beton_volume # 已经调整后的方量
)
# save_to_custom_table(
# misid=erp_id,
# flag="1", # 初始Flag值
# task_id=task_info["TaskID"],
# produce_mix_id=task_info["ProduceMixID"],
# project_name=task_info["ProjectName"],
# beton_grade=task_info["BetonGrade"],
# adjusted_volume=beton_volume # 已经调整后的方量
# )
print(f"任务 {erp_id} 的数据已保存到自定义数据表")
except Exception as e:
print(f"调用保存函数时出错: {e}")
# 发送数据给TCP客户端
try:
time.sleep(5)
task_data = {
"erp_id": erp_id,#车号,相当于序号
"task_id": task_info["TaskID"],#任务单号
"produce_mix_id": task_info["ProduceMixID"],#配比号
"project_name": task_info["ProjectName"],#任务名
"beton_grade": task_info["BetonGrade"],#砼强度
"adjusted_volume": beton_volume,#方量
"flag": "1x",#状态
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")#时间
}
tcp_server.send_data(task_data)
print(f"任务 {erp_id} 的数据已发送给TCP客户端")
except Exception as e:
print(f"发送数据给TCP客户端时出错: {e}")
return erp_id
# 监控Access数据库中特定任务的Flag字段变化
# 监控Access数据库中特定任务的Flag字段变化
def monitor_access_flag_changes(access_db_path, access_password):
"""监控Access数据库中派发任务的Flag状态"""
@ -314,7 +338,7 @@ def monitor_access_flag_changes(access_db_path, access_password):
print(f"检查任务 {erp_id} - 当前Flag: '{current_flag}', 之前Flag: '{previous_flag}'")
# 如果状态发生变化
if current_flag != previous_flag:
if current_flag != previous_flag: # 添加这行
with tasks_lock:
artifact_id = inserted_tasks.get(erp_id, "Unknown")
print(
@ -322,50 +346,113 @@ def monitor_access_flag_changes(access_db_path, access_password):
task_flags[erp_id_str] = current_flag
# 根据Flag值末尾的字母执行相应操作并更新自定义数据表状态
if current_flag.endswith('d'):
if current_flag.endswith('d'): # 将 elif 改为 if
print(f"派发任务 ErpID {erp_id}: 未进行生产")
# 调用同事提供的状态更新函数
try:
update_custom_table_status(erp_id, "未进行生产")
except Exception as e:
print(f"更新状态时出错: {e}")
elif current_flag.endswith('w'):
print(f"派发任务 ErpID {erp_id}: 正在生产中")
# 调用同事提供的状态更新函数
try:
update_custom_table_status(erp_id, "正在生产中")
except Exception as e:
print(f"更新状态时出错: {e}")
elif current_flag.endswith('n'):
print(f"派发任务 ErpID {erp_id}: 生产完毕")
# 任务完成,可以从监控列表中移除
with tasks_lock:
monitored_tasks.discard(erp_id)
print(f"派发任务 ErpID {erp_id} 已完成,停止监控")
# 调用同事提供的状态更新函数
try:
update_custom_table_status(erp_id, "生产完毕")
except Exception as e:
print(f"更新状态时出错: {e}")
elif current_flag.endswith('p'):
print(f"派发任务 ErpID {erp_id}: 生产中断")
# 任务中断,可以从监控列表中移除
with tasks_lock:
monitored_tasks.discard(erp_id)
print(f"派发任务 ErpID {erp_id} 已中断,停止监控")
# 调用同事提供的状态更新函数
try:
update_custom_table_status(erp_id, "生产中断")
except Exception as e:
print(f"更新状态时出错: {e}")
elif current_flag.endswith('x'):
print(f"派发任务 ErpID {erp_id}: 数据已接收")
# 调用同事提供的状态更新函数
try:
update_custom_table_status(erp_id, "数据已接收")
print(1)
# update_custom_table_status(erp_id, "未进行生产")
except Exception as e:
print(f"更新状态时出错: {e}")
# 发送数据给TCP客户端只发送erp_id和状态
try:
status_data = {
"erp_id": erp_id,
"status": "未进行生产"
}
tcp_server.send_data(status_data)
except Exception as e:
print(f"发送状态数据给TCP客户端时出错: {e}")
# 在"正在生产中"分支中:
elif current_flag.endswith('w'):
print(f"派发任务 ErpID {erp_id}: 正在生产中")
# 调用同事提供的状态更新函数
try:
print(2)
# update_custom_table_status(erp_id, "正在生产中")
except Exception as e:
print(f"更新状态时出错: {e}")
# 发送数据给TCP客户端只发送erp_id和状态
try:
status_data = {
"erp_id": erp_id,
"status": "正在生产中"
}
tcp_server.send_data(status_data)
except Exception as e:
print(f"发送状态数据给TCP客户端时出错: {e}")
# 在"生产完毕"分支中:
elif current_flag.endswith('n'):
print(f"派发任务 ErpID {erp_id}: 生产完毕")
# 任务完成,可以从监控列表中移除
with tasks_lock:
monitored_tasks.discard(erp_id)
print(f"派发任务 ErpID {erp_id} 已完成,停止监控")
# 调用同事提供的状态更新函数
try:
print(3)
# update_custom_table_status(erp_id, "生产完毕")
except Exception as e:
print(f"更新状态时出错: {e}")
# 发送数据给TCP客户端只发送erp_id和状态
try:
status_data = {
"erp_id": erp_id,
"status": "生产完毕"
}
tcp_server.send_data(status_data)
except Exception as e:
print(f"发送状态数据给TCP客户端时出错: {e}")
# 在"生产中断"分支中:
elif current_flag.endswith('p'):
print(f"派发任务 ErpID {erp_id}: 生产中断")
# 任务中断,可以从监控列表中移除
with tasks_lock:
monitored_tasks.discard(erp_id)
print(f"派发任务 ErpID {erp_id} 已中断,停止监控")
# 调用同事提供的状态更新函数
try:
print(4)
# update_custom_table_status(erp_id, "生产中断")
except Exception as e:
print(f"更新状态时出错: {e}")
# 发送数据给TCP客户端只发送erp_id和状态
try:
status_data = {
"erp_id": erp_id,
"status": "生产中断"
}
tcp_server.send_data(status_data)
except Exception as e:
print(f"发送状态数据给TCP客户端时出错: {e}")
# 在"数据已接收"分支中:
elif current_flag.endswith('x'):
print(f"派发任务 ErpID {erp_id}: 数据已接收")
# 调用同事提供的状态更新函数
try:
print(5)
# update_custom_table_status(erp_id, "数据已接收")
except Exception as e:
print(f"更新状态时出错: {e}")
# 发送数据给TCP客户端只发送erp_id和状态
try:
status_data = {
"erp_id": erp_id,
"status": "数据已接收"
}
tcp_server.send_data(status_data)
except Exception as e:
print(f"发送状态数据给TCP客户端时出错: {e}")
# 检查是否有任务记录已被删除(不在查询结果中但仍在监控列表中)
# 这表示任务可能已完成或从系统中移除
missing_tasks = set(deleted_from_sql_tasks) - set(current_tasks.keys())
@ -388,10 +475,20 @@ def monitor_access_flag_changes(access_db_path, access_password):
continue
# 在 main 函数中修改任务处理逻辑
def main():
global tcp_server
try:
# 初始化TCP服务端
tcp_server = TCPServer(host='127.0.0.1', port=8888)
tcp_server_thread = threading.Thread(target=tcp_server.start)
tcp_server_thread.daemon = True
tcp_server_thread.start()
# 等待服务端启动
time.sleep(1)
# 步骤1获取AppID
app_id = get_app_id()
@ -455,6 +552,9 @@ def main():
except KeyboardInterrupt:
print("程序已停止")
# 停止TCP服务端
if tcp_server:
tcp_server.stop()
if __name__ == "__main__":