Files
MoviconHub/MoviconHub.App/frmMain.cs
2025-09-15 17:59:48 +08:00

959 lines
43 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using HslCommunication;
using HslCommunication.Profinet.Melsec;
using MoviconHub.App.Com;
using MoviconHub.App.Models;
using MoviconHub.App.Services;
using NLog;
using ReaLTaiizor.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MoviconHub.App
{
public partial class frmMain : MaterialForm
{
public frmMain()
{
InitializeComponent();
}
/// <summary>
/// 当前的日志记录
/// </summary>
private Logger Logger { get; set; } = LogManager.GetCurrentClassLogger();
/// <summary>
/// 当前的信号模型
/// </summary>
public SglModel CurSglModel { get; set; }
/// <summary>
/// PLCScanTask扫描Task
/// </summary>
private static Task PLCScanTask { get; set; }
/// <summary>
/// 扫描线程使能
/// </summary>
public bool ThreadEnable { get; set; } = true;
/// <summary>
/// 三菱连接驱动程序
/// </summary>
public MelsecMcNet MelsecMcNetDrive { get; set; }
/// <summary>
/// DB连接服务
/// </summary>
public DBServices CurDBServices { get; set; }
private void frmMain_Load(object sender, EventArgs e)
{
CurSglModel = new SglModel();
CurSglModel.SglModelChanged += CurSglModel_SglModelChanged;
CurDBServices = new DBServices(CurSglModel);
CurDBServices.StartPolling();
// 初始化API服务
ApiHelper.Initialize();
WebSocketClientHelper.Initialize();
WebSocketClientHelper.PubRtDataStart();
ClearActionInstance = new ClearAction();
ClearActionInstance.ClearActionEvent += ClearActionInstance_ClearActionEvent;
ListAlarmModels = new List<AlarmModel>()
{
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="热水罐液位 H报警",
Index=0,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="热水罐液位 L报警",
Index=1,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="热水罐液位 LL报警",
Index=2,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="清洗剂罐液位 H报警",
Index=3,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="清洗剂罐液位 L报警",
Index=4,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="清洗剂罐液位 LL报警",
Index=5,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="桁架平移前进极限报警",
Index=6,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="桁架平移后退极限报警",
Index=7,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="桁架吊装上升极限报警",
Index=8,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="桁架吊装下降极限报警",
Index=9,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="漫射喷淋AXIS1 X1伺服报警",
Index=10,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="漫射喷淋AXIS2 伺服报警",
Index=11,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="漫射喷淋AXIS3 X2伺服报警",
Index=12,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="上位机 急停被按下报警",
Index=13,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="大部件 急停安全继电器未吸合报警",
Index=14,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="R08与FX3U 通讯丢失,请查看网线是否松动",
Index=15,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="N1C1 XME02(零部件)变频器 CC LINK网络报警 站号:1",
Index=16,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="N1C1 XME03(大部件)变频器 CC LINK网络报警 站号:2",
Index=17,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="N1C1 XME04(ROB1高压泵)变频器 CC LINK网络报警 站号:3",
Index=18,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="N1C1 XME05(ROB2高压泵)变频器 CC LINK网络报警 站号:4",
Index=19,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="机器人1 CC LINK网络报警 站号:5",
Index=20,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="机器人2 CC LINK网络报警 站号:9",
Index=21,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="JCB03 A3119_1 远程IO从站 CC LINK网络报警 站号:13",
Index=22,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="JCB03 A4119_1 远程IO从站 CC LINK网络报警 站号:14",
Index=23,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="JCB01 A119_1 远程IO从站 CC LINK网络报警 站号:15",
Index=24,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="JCB01 A124_1 远程IO从站 CC LINK网络报警 站号:16",
Index=25,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="JCB01 A129_1 远程IO从站 CC LINK网络报警 站号:17",
Index=26,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="JCB01 A200_1 远程IO从站 CC LINK网络报警 站号:18",
Index=27,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="JCB01 A205_1 远程IO从站 CC LINK网络报警 站号:19",
Index=28,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="JCB02 A2119_1 远程IO从站 CC LINK网络报警 站号:20",
Index=29,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="ROB1 机器人故障报警",
Index=30,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="ROB2 机器人故障报警",
Index=31,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="N1C1 XME02 变频器存在错误",
Index=32,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="N1C1 XME03 变频器存在错误",
Index=33,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="N1C1 XME04 变频器存在错误",
Index=34,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="N1C1 XME05 变频器存在错误",
Index=35,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="ROB1第七轴伺服故障",
Index=36,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="ROB2第七轴伺服故障",
Index=37,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="零部件吊装伺服故障",
Index=38,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="零部件平移伺服故障",
Index=39,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="N1C1 XME04 高压泵温度指示报警",
Index=40,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="N1C1 XME04 高压泵压力指示报警",
Index=41,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="N1C1 XME05 高压泵温度指示报警",
Index=42,
DeviceState=2,
},
new AlarmModel(FRemoteSqlContext.FDb){
DeviceName="机车构架及大部件自动化智能清洗设备",
DeviceCode="942010002",
AlarmMessage="N1C1 XME05 高压泵压力指示报警",
Index=43,
DeviceState=2,
},
};
ListDeviceStateStaticModels = new List<DeviceStateStaticModel>()
{
new DeviceStateStaticModel()
{
Name="开机时长",
Index=2
},
new DeviceStateStaticModel()
{
Name="运行时长",
Index=6
},
new DeviceStateStaticModel()
{
Name="待机时长",
Index=8
},
new DeviceStateStaticModel()
{
Name="关机时长",
Index=10
},
new DeviceStateStaticModel()
{
Name="故障时长",
Index=14
},
new DeviceStateStaticModel()
{
Name="使用率分子",
Index=18
},
new DeviceStateStaticModel()
{
Name="作业计次",
Index=100
},
new DeviceStateStaticModel()
{
Name="故障计次",
Index=102
},
};
InitCom();
//扫描线程成功
RtScanDeviceStart();
//dBServices.StartPolling();
//var data = ApiHelper.GetPartInfoAsync("df3976d89a8f482685d0fb2ba5ed11ba", "");
}
/// <summary>
/// 清洗动作实例信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <exception cref="NotImplementedException"></exception>
private void ClearActionInstance_ClearActionEvent(object sender, string Par)
{
switch (Par)
{
case "设备关机":
{
var Data = new DeviceState()
{
DeviceName = "机车构架及大部件自动化智能清洗设备",
DeviceCode = "942010002",
PowerOnTime = ListDeviceStateStaticModels.Find(a => a.Name == "开机时长").Value,
RunTime = ListDeviceStateStaticModels.Find(a => a.Name == "运行时长").Value,
StandbyTime = ListDeviceStateStaticModels.Find(a => a.Name == "待机时长").Value,
ShutdownTime = ListDeviceStateStaticModels.Find(a => a.Name == "关机时长").Value,
FaultTime = ListDeviceStateStaticModels.Find(a => a.Name == "故障时长").Value,
JobNum = ListDeviceStateStaticModels.Find(a => a.Name == "作业计次").Value,
FaultNum = ListDeviceStateStaticModels.Find(a => a.Name == "故障计次").Value,
//UseRatio = ListDeviceStateStaticModels.Find(a=>a.Name== "故障计次").Value,
};
var Result = FRemoteSqlContext.FDb.Insert<DeviceState>(Data).ExecuteInserted();
if (Result != null && Result.Count > 0)
{
Logger.Error("清洗时间统计数据完成");
BeginInvoke(new Action(() =>
{
MainText.AppendText($"时间:{DateTime.Now}-Msg:清洗时间统计数据完成 {Environment.NewLine}");
}));
}
}
break;
case "清洗完成":
{
// 获取组件信息
var component = CurDBServices.RealtimeData.ListComponentsInfo?.FirstOrDefault();
if (component != null)
{
// 创建CurRunClearState对象并映射数据
var curRunData = new ClearData()
{
// 基本信息
DeviceCode = CurDBServices.RealtimeData.Device_Code,
DeviceName = CurDBServices.RealtimeData.Device_Name,
// 组件信息
part_qrid = component?.part_qrid,
part_num = component?.part_num,
part_position = component?.part_position,
component_name = component?.part_name,
vehicle_model = component?.part_Vehicle_model,
locomotive_number = component?.part_locomotive_number,
repair_process = component?.part_repair_process,
program_process = CurDBServices.RealtimeData.TestData.Test_FrameworkProgramProcess,
// 程序进程
Test_FrameworkProgramProcess = CurDBServices.RealtimeData.TestData.Test_FrameworkProgramProcess,
Test_FrameworkProgramProcessPercentage = CurDBServices.RealtimeData.TestData.Test_FrameworkProgramProcessPercentage,
//// 设备状态
//Test_PartsEquipmentStatus = CurDBServices.RealtimeData.TestData.Test_PartsEquipmentStatus,
// 清洗时长和用量
Test_FrameworkPerModelCleaningDuration = CurDBServices.RealtimeData.TestData.Test_FrameworkPerModelCleaningDuration,
Test_FrameworkPerModelCleaningAgentUsage = CurDBServices.RealtimeData.TestData.Test_FrameworkPerModelCleaningAgentUsage,
Test_FrameworkPerModelWaterUsage = CurDBServices.RealtimeData.TestData.Test_FrameworkPerModelWaterUsage,
// 水箱和清洗剂罐信息
WaterTank_Temp = CurDBServices.RealtimeData.TestData.Test_WaterTankTemperature,
AgentTank_Temp = CurDBServices.RealtimeData.TestData.Test_CleaningAgentTankTemperature,
WaterTank_Level = CurDBServices.RealtimeData.TestData.Test_WaterTankLevel,
AgentTank_Level = CurDBServices.RealtimeData.TestData.Test_CleaningAgentTankLevel,
// 浸泡池温度
SoakingTank1_Temp = CurDBServices.RealtimeData.TestData.Test_SoakingTank1Temperature,
SoakingTank2_Temp = CurDBServices.RealtimeData.TestData.Test_SoakingTank2Temperature,
// 运行模式
Test_WaterTankHeat = CurDBServices.RealtimeData.TestData.Test_WaterTankHeat,
Test_WaterTankAdd = CurDBServices.RealtimeData.TestData.Test_WaterTankAdd,
Test_CleaningAgentTankHeat = CurDBServices.RealtimeData.TestData.Test_CleaningAgentTankHeat,
Test_CleaningAgentTankAdd = CurDBServices.RealtimeData.TestData.Test_CleaningAgentTankAdd,
// 监控信息
Test_ElectricSurveillance = CurDBServices.RealtimeData.TestData.Test_ElectricSurveillance,
Test_SteamSurveillance = CurDBServices.RealtimeData.TestData.Test_SteamSurveillance
};
var Result = FRemoteSqlContext.FDb.Insert<ClearData>(curRunData).ExecuteInserted();
if (Result != null && Result.Count > 0)
{
Logger.Error("清洗当时数据记录完成");
BeginInvoke(new Action(() =>
{
MainText.AppendText($"时间:{DateTime.Now}-Msg:清洗当时数据记录完成 {Environment.NewLine}");
}));
}
}
}
break;
default:
break;
}
}
/// <summary>
/// 报警集合
/// </summary>
public List<AlarmModel> ListAlarmModels { get; set; }
/// <summary>
/// 清洗动作实例
/// </summary>
public ClearAction ClearActionInstance { get; set; }
/// <summary>
/// 运行状态统计模型
/// </summary>
public List<DeviceStateStaticModel> ListDeviceStateStaticModels { get; set; }
/// <summary>
/// 报警结果集合
/// </summary>
private OperateResult<bool[]> OperateResultAlarm { get; set; }
/// <summary>
/// 清洗结束
/// </summary>
private OperateResult<bool> OperateResultClearEnd { get; set; }
/// <summary>
/// 设备关机
/// </summary>
private OperateResult<bool> OperateResultDeviceClose { get; set; }
private OperateResult<short> OperateResultClearAction { get; set; }
/// <summary>
/// 报警结果集合
/// </summary>
private OperateResult<Int16[]> OperateResultDeviceStateStatic { get; set; }
/// <summary>
/// 通信初始化
/// </summary>
private void InitCom()
{
try
{
var PLCIP = ConfigHelper.GetValue("PLCIP");
var PLCPort = 6000;
if (int.TryParse(ConfigHelper.GetValue("PLCPort"), out int PortResult))
{
PLCPort = PortResult;
}
//PLC通信的连接
MelsecMcNetDrive = new MelsecMcNet();
MelsecMcNetDrive.IpAddress = PLCIP;
MelsecMcNetDrive.Port = PLCPort;
MelsecMcNetDrive.ConnectClose();
MelsecMcNetDrive.ConnectTimeOut = 3000; // 连接3秒超时
OperateResult connect = MelsecMcNetDrive.ConnectServer();
if (connect.IsSuccess)//初始连接状态的显示判断
{
//MessageBox.Show(HslCommunication.StringResources.Language.ConnectedSuccess);
//MelsecMcNetDrive.Write("M504", true);
//MessageBox.Show("PLC连接成功");
}
else
{
Logger.Error("初始PLC通信失败");
//MessageBox.Show(connect.Message + Environment.NewLine + "ErrorCode: " + connect.ErrorCode);
}
}
catch (Exception)
{
}
}
/// <summary>
/// PLC扫描线程
/// </summary>
private void RtScanDeviceStart()
{
PLCScanTask = Task.Run(async () =>
{
while (ThreadEnable)
{
//await Task.CompletedTask;
await Task.Delay(50);
try
{
OperateResultAlarm = MelsecMcNetDrive.ReadBool("M150", 50);
if (OperateResultAlarm.IsSuccess)
{
foreach (var item in ListAlarmModels)
{
if (CurDBServices.RealtimeData != null && CurDBServices.RealtimeData.FaultDetails != null)
{
if (int.TryParse(CurDBServices.RealtimeData.FaultDetails.Fault_Code, out int FaultCodeResult))
{
item.DeviceState = FaultCodeResult;
}
}
item.IsActive = OperateResultAlarm.Content[item.Index];
}
}
OperateResultDeviceStateStatic = MelsecMcNetDrive.ReadInt16("D4500", 150);
if (OperateResultDeviceStateStatic.IsSuccess)
{
foreach (var itemDeviceStateStatic in ListDeviceStateStaticModels)
{
itemDeviceStateStatic.Value = OperateResultDeviceStateStatic.Content[itemDeviceStateStatic.Index];
}
}
//OperateResultClearEnd = MelsecMcNetDrive.ReadBool("M210");
//if (OperateResultClearEnd.IsSuccess)
//{
// ClearActionInstance.ClearEnd = OperateResultClearEnd.Content;
//}
OperateResultClearEnd = MelsecMcNetDrive.ReadBool("M211");
if (OperateResultClearEnd.IsSuccess)
{
ClearActionInstance.ClearEnd = OperateResultClearEnd.Content;
}
OperateResultDeviceClose = MelsecMcNetDrive.ReadBool("M210");
if (OperateResultDeviceClose.IsSuccess)
{
ClearActionInstance.DeviceClose = OperateResultDeviceClose.Content;
}
//OperateResultClearAction = MelsecMcNetDrive.ReadInt16("D5010");
//if (OperateResultClearAction.IsSuccess)
//{
// ClearActionInstance.ClearEnd = OperateResultClearAction.Content.GetBoolByIndex(12);
//}
}
catch (Exception ex)
{
//LogService.Info($"时间:{DateTime.Now.ToString()}-【Meter】-{ex.Message}");
}
}
});
}
/// <summary>
/// 信号信息改变事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <exception cref="NotImplementedException"></exception>
private void CurSglModel_SglModelChanged(object sender, string Par)
{
switch (Par)
{
case "CodeReady":
// 处理条码信息OK信号
// 这里可以添加您需要执行的操作
// 获取零件二维码ID
var partQrId = CurDBServices.RealtimeData.ListComponentsInfo.FirstOrDefault()?.part_qrid;
if (string.IsNullOrEmpty(partQrId))
{
Logger.Error("二维码ID为空无法获取部件信息");
//MessageBox.Show("二维码ID为空无法获取部件信息", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Logger.Error("开始进行API请求");
BeginInvoke(new Action(() =>
{
MainText.AppendText($"时间:{DateTime.Now}-Msg:开始进行API请求 {Environment.NewLine}");
}));
Task.Run(async () =>
{
try
{
var partInfoResponse = await ApiHelper.GetPartInfoAsync(CurDBServices.RealtimeData.ListComponentsInfo.FirstOrDefault().part_qrid, "");
BeginInvoke(new Action(() =>
{
materialMultiLineTextBoxEditApi.Text = $"Datetime:{DateTime.Now}-Msg:请求了 Api-{partInfoResponse.Message}";
}));
// 解析响应数据
if (partInfoResponse != null)
{
if (partInfoResponse.Status == 200) // 成功状态码
{
var partInfo = partInfoResponse.Data;
if (partInfo != null)
{
// 这里可以根据需要处理和显示部件信息
string partInfoDetails = $"车型: {partInfo.VehicleModel}\n" +
$"车号: {partInfo.LocomotiveNumber}\n" +
$"修程: {partInfo.RepairProcess}\n" +
$"部件名称: {partInfo.ComponentName}\n" +
$"位别: {partInfo.PartPosition}\n" +
$"部件编号: {partInfo.PartNum}\n" +
$"部件二维码: {partInfo.PartQrCode}";
// 显示或记录部件信息
Logger.Info($"成功获取部件信息: {partInfoDetails}");
if (CurDBServices.RealtimeData.ListComponentsInfo != null && CurDBServices.RealtimeData.ListComponentsInfo.Count > 0)
{
CurDBServices.RealtimeData.ListComponentsInfo.FirstOrDefault().part_Vehicle_model = partInfo.VehicleModel;
CurDBServices.RealtimeData.ListComponentsInfo.FirstOrDefault().part_locomotive_number = partInfo.LocomotiveNumber;
CurDBServices.RealtimeData.ListComponentsInfo.FirstOrDefault().part_repair_process = partInfo.RepairProcess;
CurDBServices.RealtimeData.ListComponentsInfo.FirstOrDefault().part_position = partInfo.PartPosition;
CurDBServices.RealtimeData.ListComponentsInfo.FirstOrDefault().part_name = partInfo.ComponentName;
CurDBServices.RealtimeData.ListComponentsInfo.FirstOrDefault().part_num = partInfo.PartNum;
CurDBServices.RealtimeData.ListComponentsInfo.FirstOrDefault().part_qrid = partInfo.PartQrCode;
}
//写个PLC
FSqlContext.FDb.Update<RTVar>()
.Set(a => a.Val, partInfo.VehicleModel)
.Where(a => a.Name == "part_Vehicle_model")
.ExecuteAffrows();
FSqlContext.FDb.Update<RTVar>()
.Set(a => a.Val, partInfo.LocomotiveNumber)
.Where(a => a.Name == "part_locomotive_number")
.ExecuteAffrows();
FSqlContext.FDb.Update<RTVar>()
.Set(a => a.Val, partInfo.RepairProcess)
.Where(a => a.Name == "part_repair_process")
.ExecuteAffrows();
FSqlContext.FDb.Update<RTVar>()
.Set(a => a.Val, partInfo.PartPosition)
.Where(a => a.Name == "part_position")
.ExecuteAffrows();
FSqlContext.FDb.Update<RTVar>()
.Set(a => a.Val, partInfo.ComponentName)
.Where(a => a.Name == "part_name")
.ExecuteAffrows();
FSqlContext.FDb.Update<RTVar>()
.Set(a => a.Val, partInfo.PartNum)
.Where(a => a.Name == "part_num")
.ExecuteAffrows();
//FSqlContext.FDb.Update<RTVar>()
// .Set(a => a.Val, partInfo.VehicleModel)
// .Where(a => a.Name == "part_qrid")
// .ExecuteAffrows();
// 读取数据后,关闭文件流
BeginInvoke(new Action(() =>
{
MainText.AppendText($"时间:{DateTime.Now}-Msg:{partInfoDetails} {Environment.NewLine}");
}));
// 根据UI设计可以将这些信息显示在相应的控件上
// 例如lblVehicleModel.Text = partInfo.VehicleModel;
// 或者可以将整个部件信息对象保存到某个属性中,供其他地方使用
// this.CurrentPartInfo = partInfo;
}
else
{
Logger.Error("API返回成功但部件信息为空");
//MessageBox.Show($"API返回成功但部件信息为空", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
else
{
Logger.Error($"获取部件信息失败: {partInfoResponse.Message}\n状态码: {partInfoResponse.Status}");
// 处理API返回的错误
//MessageBox.Show($"获取部件信息失败: {partInfoResponse.Message}\n状态码: {partInfoResponse.Status}",
//"API错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("API响应为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
Logger.Error(ex, "异步获取或解析部件信息时发生异常");
}
});
break;
default:
break;
}
}
private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
{
WebSocketClientHelper.Disconnect();
CurDBServices.StopPolling();
ThreadEnable = false;
}
//private Task Async ddd()
//{
// // 初始化
// WebSocketClientHelper.Initialize();
// // 注册消息接收事件
// WebSocketClientHelper.MessageReceived += (sender, e) =>
// {
// // 处理接收到的消息
// if (e.Message.MessageType == MessageType.DeviceStatus)
// {
// // 处理设备状态消息
// }
// };
// // 连接服务器
// WebSocketClientHelper.ConnectAsync();
// // 发送数据
// var statusData = new DeviceStatusData
// {
// DeviceId = "Device001",
// DeviceName = "测试设备",
// Status = "运行中",
// Voltage = 220.5,
// Current = 5.2,
// Temperature = 36.8
// };
// WebSocketClientHelper.SendDeviceStatus(statusData);
//}
//private void Request()
//{
// // 假设您从输入框获取二维码和设备编码
// string qrCode = txtQrCode.Text.Trim();
// string deviceCode = txtDeviceCode.Text.Trim();
// if (string.IsNullOrEmpty(qrCode) || string.IsNullOrEmpty(deviceCode))
// {
// MessageBox.Show("请输入二维码和设备编码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
// return;
// }
// try
// {
// // 显示加载中状态
// SetControlsEnabled(false);
// lblStatus.Text = "正在获取数据...";
// // 调用API获取部件信息
// var result = await ApiHelper.GetPartInfoAsync(qrCode, deviceCode);
// if (result.Status == 200 && result.Data != null)
// {
// // 将获取到的数据填充到表单中
// txtVehicleModel.Text = result.Data.VehicleModel;
// txtLocomotiveNumber.Text = result.Data.LocomotiveNumber;
// txtRepairProcess.Text = result.Data.RepairProcess;
// txtComponentName.Text = result.Data.ComponentName;
// txtPartPosition.Text = result.Data.PartPosition;
// txtPartNum.Text = result.Data.PartNum;
// lblStatus.Text = "数据获取成功";
// }
// else
// {
// // 显示错误信息
// lblStatus.Text = $"获取数据失败:{result.Message}";
// MessageBox.Show($"获取数据失败:{result.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
// }
// }
// catch (Exception ex)
// {
// lblStatus.Text = $"发生异常:{ex.Message}";
// MessageBox.Show($"发生异常:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
// }
// finally
// {
// // 恢复控件状态
// SetControlsEnabled(true);
// }
//}
}
}