using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
//using System.Text;
using Microsoft.Win32;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using HslCommunication.LogNet;
using MainForm.FaForm;
using Sunny.UI;
using MainForm.ClassFile.XiaomiAPI;
using System.Diagnostics;
using MainForm.Models;
using SqlSugar;
using EasyModbus;
using ModBusClientSimple.Util;
using csharp_networkprotocol_hpsocket;
using MqttnetServerWin;
using Sunny.UI.Win32;
using MainForm.ClassFile.XiaomiAPI_AGV;
using MainForm.ClassFile.XiaomiAPI_RouteCom;
using HslCommunication.Controls;
using EIP_Protocol;
using MainForm.ClassFile.XiaomiAPI_MES;
using NPOI.Util;
using static MainForm.SQLHelper;
using static MainForm.ClassFile.XiaomiAPI_MES.XiaomiMESHttp_StationInbound;
using static MainForm.ClassFile.XiaomiAPI_MES.XiaomiMESHttp_StationOutbound;
using MainForm.ClassFile.ProjectClass;
using CommonLib;
using Org.BouncyCastle.Asn1.IsisMtt;
using System.Web.Services.Description;
using System.Numerics;
using MathNet.Numerics.RootFinding;
using HslCommunication.Enthernet;
using BZFAStandardLib;
using MainForm.ClassFile;
using NPOI.SS.Formula.Functions;
using static MainForm.ClassFile.XiaomiAPI.XiaomiMqttClient_Extend;
using System.Net.Http;
using static MainForm.ClassFile.XiaomiAPI_MES.XiaomiMESHttp_UpLoadFile;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Tab;
using System.Reflection;
using static MainForm.ClassFile.XiaomiAPI_MES.XiaomiMESHttp_StationOutbound.XmMES_StationOutRequest_Body;
using FaFrameUI;
using System.Security.Policy;
using static MainForm.ClassFile.XiaomiClass.MesHelper;
using static MainForm.ClassFile.XiaomiAPI_MES.XiaomiMESHttp_StationOutbound.XmMES_StationOutRequest_Body;
using System.Drawing.Imaging;
using System.Drawing;
using ICSharpCode.SharpZipLib.Zip;
using System.Text.RegularExpressions;
using System.Text;
using SixLabors.ImageSharp;
/*
* 注:本源码对外提供,所以有些地方使用中文命名方法及变量
*/
namespace MainForm
{
///
/// 记录日志的委托
///
/// 日志类型
/// 日志信息
public delegate void HomeMessageHandler(LogType logType, string message);
///
/// 主页窗体
///
public partial class Form_Home : Form
{
#region 常量
//文本常量
private const string Head = "开始采集";
private const string Tail = "采集完成";
private const string Body = "工位出站数据";
private const string BodyCheck = "工位点检数据";
private const string BodyRun = "整线运行数据";
private const string BodyAlarm = "整线报警数据";
#endregion 常量
#region 变量
///
/// 委托-记录日志的方法
///
public event HomeMessageHandler MessageEvent;
///
/// 日志接口
///
ILogNet _PLCLogNet;
///
/// 用于记录IOT MQTT日志
///
ILogNet _IOTMqttLogNet;
///
/// 用于记录AGV MQTT日志
///
ILogNet _AGVMqttLogNet;
//private int DataSwitch = 1; // 1-SQLServer;2-Excel
// 定义信号量,index0给MES(true有信号,false无信号;set()让被控线程运行,Reset()让被控线程停止;WaitOne(等待时间)等待线程运行)
// 间隔时间
private int IntervalReadPLC = 300; //ms 读PLC
private int IntervalMonitorMES = 1000; //ms MES心跳
private int IntervalAlarm = 1000; //ms 数据(报警)查询与设备运行信息
///
/// 设备报警数据
///
uint[] _FaultDatas = { };
uint[] _FaultDatas_Old = { };
uint[] _FaultDatas2 = { };
uint[] _FaultDatas_Old2 = { };
// 软件状态
private bool IsRun = true;
#region PLC 与 TCP对象
// 定义一个字典,存plc对象(通讯)
ModbusClientHelper plc1Alarm; // PLC‘运行数据’与‘报警数据’线程用ModbusTCP
Dictionary Funs = new Dictionary();
// 定义TCPClient对象列表
Dictionary
_HPSocket_TcpClients = new Dictionary();
// 定义MQTTHelper对象
MQTTHelper _MQTTHelper = new MQTTHelper();
#endregion PLC 与 TCP对象
///
/// 上次的设备运行信息
///
private string lineWorkingData1_OldStr = string.Empty;
///
/// 设备报警字典-当前结果
/// Dictionary<工位代码,List<报警信息>>
///
private Dictionary> DicAlarms_Cur = new Dictionary>();
public Dictionary FunsEip = new Dictionary();
///
/// 单机用-设备状态
///
//XiaomiDeviceState xmDeviceState = XiaomiDeviceState.Uninitialized;
XiaomiDeviceStateData xmDeviceStateData = new XiaomiDeviceStateData();
private int test_item_num = 0; //iot 过站数据序号
public static string uuid = ""; //单工位或左工位
public static string uuid2 = ""; //右工位
private bool inpass = false; //保存进站测试状态
public static XiaoMiParm xiaomiParm = new XiaoMiParm();
//记录上传附件的信息
public static FileUpload_FileData fileUploadData = new FileUpload_FileData();
#endregion 变量
#region 窗体基础事件
///
/// 初始化
///
public Form_Home()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false; // 不检查跨线程访问
_PLCLogNet = new LogNetDateTime(GlobalContext.PlcLogDir, GenerateMode.ByEveryDay); // 按天记录日志
_IOTMqttLogNet = new LogNetDateTime(GlobalContext.MqttLogDir, GenerateMode.ByEveryDay); // 按天记录日志
_AGVMqttLogNet = new LogNetDateTime(GlobalContext.MqttLogDir, GenerateMode.ByEveryDay); // 按天记录日志
GlobalContext.Set += new Action(UpdateProductInfo); // 产品信息变化时更新UI
}
///
/// 窗体加载事件
///
public void Form_Home_Load(object sender, EventArgs e)
{
try
{
AddMessage(LogType.Info, "开始初始化程序");
//组建plc对象字典
//plc1Alarm = new ModbusClientHelper(GlobalContext.Machine1Address, GlobalContext.MachinePort);
//plc1Alarm = new Inovance_EIP(GlobalContext.PCAddress, GlobalContext.Machine1Address);
if (GlobalContext.IsUsePLC1)
{
GlobalContext.IsUsePLCNow = 1;
GlobalContext.IsUseStationName = "[OP10]壳体清洁上料";
FunsEip.Add(GlobalContext.IsUsePLCNow,
new Inovance_EIP(GlobalContext.PC1Address, GlobalContext.Machine1Address)); //OP10 壳体清洁上料装备
}
if (GlobalContext.IsUsePLC2)
{
GlobalContext.IsUsePLCNow = 2;
GlobalContext.IsUseStationName = "[OP20]上盖板上料装备";
FunsEip.Add(GlobalContext.IsUsePLCNow,
new Inovance_EIP(GlobalContext.PC2Address, GlobalContext.Machine2Address)); //OP20 顶盖上料设备
}
if (GlobalContext.IsUsePLC3)
{
GlobalContext.IsUsePLCNow = 3;
GlobalContext.IsUseStationName = "[OP30]点散热胶装备";
FunsEip.Add(GlobalContext.IsUsePLCNow,
new Inovance_EIP(GlobalContext.PC3Address, GlobalContext.Machine3Address)); //OP30 点胶设备
}
if (GlobalContext.IsUsePLC4)
{
GlobalContext.IsUsePLCNow = 4;
GlobalContext.IsUseStationName = "[OP40]胶线检测";
FunsEip.Add(GlobalContext.IsUsePLCNow,
new Inovance_EIP(GlobalContext.PC4Address, GlobalContext.Machine4Address)); //OP40 3D胶线检测
}
if (GlobalContext.IsUsePLC5)
{
GlobalContext.IsUsePLCNow = 5;
GlobalContext.IsUseStationName = "[OP50]ADD板上料组装装备";
FunsEip.Add(GlobalContext.IsUsePLCNow,
new Inovance_EIP(GlobalContext.PC5Address, GlobalContext.Machine5Address)); //OP50 ADD PCB板上料
}
if (GlobalContext.IsUsePLC6)
{
GlobalContext.IsUsePLCNow = 6;
GlobalContext.IsUseStationName = "[OP70]组上盖板";
FunsEip.Add(GlobalContext.IsUsePLCNow,
new Inovance_EIP(GlobalContext.PC6Address, GlobalContext.Machine6Address)); //OP60 顶盖装配
}
if (GlobalContext.IsUsePLC7)
{
GlobalContext.IsUsePLCNow = 7;
GlobalContext.IsUseStationName = "[OP80]上盖板锁螺丝";
FunsEip.Add(GlobalContext.IsUsePLCNow,
new Inovance_EIP(GlobalContext.PC7Address, GlobalContext.Machine7Address)); //OP70 锁螺丝
}
if (GlobalContext.IsUsePLC8)
{
GlobalContext.IsUsePLCNow = 8;
GlobalContext.IsUseStationName = "[OP90]NG下料";
FunsEip.Add(GlobalContext.IsUsePLCNow,
new Inovance_EIP(GlobalContext.PC8Address,
GlobalContext.Machine8Address)); //OP80 3D螺丝高度检测,NG出料站
}
if (GlobalContext.IsUsePLC9)
{
GlobalContext.IsUsePLCNow = 9;
GlobalContext.IsUseStationName = "[OP100]半成品下料";
FunsEip.Add(GlobalContext.IsUsePLCNow,
new Inovance_EIP(GlobalContext.PC9Address, GlobalContext.Machine9Address)); //OP90 下料站
}
(bool, string) DicResult = InitalDicAlarm(); // 实例化报警字典
AddMessage(LogType.Info, DicResult.Item2);
foreach (Inovance_EIP plcEIP in FunsEip.Values)
{
if (plcEIP != null)
{
try
{
(int, string) result = plcEIP.Connect();
}
catch (Exception ex)
{
MessageBox.Show($"PLC[{plcEIP._pcIPStr}]连接失败!失败信息:" + ex.Message,
"PLC连接提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
MessageBoxOptions.ServiceNotification);
}
}
}
// 采集任务
Task TaskReadAlarm = new Task(ReadAlarmAllPLC); // 线程-获取线体报警数据
List TaskReadProcess = new List(); // 线程-触发点位(PLC)的线程
//TaskReadProcess.Add(new Task(() => { ReadStation_DownOrderInfo(1); })); // 下发机种
if (GlobalContext.IsUsePLC1)
TaskReadProcess.Add(new Task(() => { ReadStation_S1(1); })); //OP10 壳体清洁上料装备
if (GlobalContext.IsUsePLC2)
TaskReadProcess.Add(new Task(() => { ReadStation_S2(2); })); //OP20 顶盖上料设备
if (GlobalContext.IsUsePLC3)
TaskReadProcess.Add(new Task(() => { ReadStation_S3(3); })); //OP30 点胶设备
if (GlobalContext.IsUsePLC4)
TaskReadProcess.Add(new Task(() => { ReadStation_S4(4); })); //OP40 点胶检测设备
if (GlobalContext.IsUsePLC5)
TaskReadProcess.Add(new Task(() => { ReadStation_S5(5); })); //OP50 ADD PCB板上料设备
if (GlobalContext.IsUsePLC6)
TaskReadProcess.Add(new Task(() => { ReadStation_S6(6); })); //OP60 顶盖装配设备
if (GlobalContext.IsUsePLC7)
TaskReadProcess.Add(new Task(() => { ReadStation_S7(7); })); //OP70 锁螺丝设备
if (GlobalContext.IsUsePLC8)
TaskReadProcess.Add(new Task(() => { ReadStation_S8(8); })); //OP80 3D螺丝高度检测设备
if (GlobalContext.IsUsePLC9)
TaskReadProcess.Add(new Task(() => { ReadStation_S9(9); }));
#region 初始化
try
{
// 开启MES(Http)
if (GlobalContext.IsUseMES)
{
bool mesret = HttpUitls.PingIP(GlobalContext.ServerIp);
if (mesret)
{
picMESStatus.Image = imageListState.Images[1];
GlobalContext.MESIsConnect = true;
AddMessage(LogType.Info, "小米MES初始连接成功!");
}
else
{
picMESStatus.Image = imageListState.Images[0];
GlobalContext.MESIsConnect = false;
AddMessage(LogType.Info, $"小米MES[{GlobalContext.ServerHost}]初始连接失败!");
}
}
// 开启IOT(MQTT)
if (GlobalContext.IsUseIot)
{
string addr = GlobalContext.MQTTServerHost;
int port = GlobalContext.MQTTServerPort;
//生产环境需要修改
(int, string) qmttResult = XiaomiMqttClient_Extend.OpenWithMqttServer("127.0.0.1", 6666,
GlobalContext.MqttServerPath, GlobalContext.MqttServerName);
XiaomiMqttResponse_ErrCode response_ErrCode = (XiaomiMqttResponse_ErrCode)qmttResult.Item1;
if (response_ErrCode == XiaomiMqttResponse_ErrCode.OK)
{
picIot.Image = imageListState.Images[1];
AddMessage(LogType.Info, "小米IOT MQTT初始连接成功!");
// 设置回调函数
//XiaomiMqttClient_Extend.SetCallbackWithDataId(CallbackWithDataId);
// 配置参数
XiaomiMqttLoginFunAndParam param = new XiaomiMqttLoginFunAndParam();
// fds
param.parameter.fds.address = GlobalContext.address;
param.parameter.fds.appId = GlobalContext.appId;
param.parameter.fds.appKey = GlobalContext.appKey;
// mes
param.parameter.mes.address = GlobalContext.ServerIp;
param.parameter.mes.appId = GlobalContext.MESAppId;
param.parameter.mes.appKey = GlobalContext.MESAppKey;
// mqtt
param.parameter.mqtt.address = GlobalContext.MQTTServerHost;
param.parameter.mqtt.port = GlobalContext.MQTTServerPort;
param.parameter.mqtt.username = GlobalContext.MQTTAppId;
param.parameter.mqtt.password = GlobalContext.MQTTAppPwd;
// 设备配置
param.parameter.equipment.factoryCode = GlobalContext.Factory_Code;
if (GlobalContext.IsUsePLC1)
{
param.parameter.equipment.deviceCode = GlobalContext.S1_device_code; // 装备编码
param.parameter.equipment.stationCode = GlobalContext.S1_station; // ⼯位Id
xiaomiParm.workstation = GlobalContext.S1_work_station; //工站
}
if (GlobalContext.IsUsePLC2)
{
param.parameter.equipment.deviceCode = GlobalContext.S2_device_code; // 装备编码
param.parameter.equipment.stationCode = GlobalContext.S2_station; // ⼯位Id
xiaomiParm.workstation = GlobalContext.S2_work_station; //工站
}
if (GlobalContext.IsUsePLC3)
{
param.parameter.equipment.deviceCode = GlobalContext.s3_1_device_code; // 装备编码
//param.parameter.equipment.stationCode = GlobalContext.s3_1_station; // ⼯位Id
}
if (GlobalContext.IsUsePLC4)
{
param.parameter.equipment.deviceCode = GlobalContext.s4_device_code; // 装备编码
param.parameter.equipment.stationCode = GlobalContext.s4_station; // ⼯位Id
xiaomiParm.workstation = GlobalContext.s4_work_station; //工站
}
if (GlobalContext.IsUsePLC5)
{
param.parameter.equipment.deviceCode = GlobalContext.s5_device_code; // 装备编码
param.parameter.equipment.stationCode = GlobalContext.s5_station; // ⼯位Id
xiaomiParm.workstation = GlobalContext.s5_work_station; //工站
}
if (GlobalContext.IsUsePLC6)
{
param.parameter.equipment.deviceCode = GlobalContext.s6_device_code; // 装备编码
param.parameter.equipment.stationCode = GlobalContext.s6_station; // ⼯位Id
xiaomiParm.workstation = GlobalContext.s6_work_station; //工站
}
if (GlobalContext.IsUsePLC7)
{
param.parameter.equipment.deviceCode = GlobalContext.s7_1_device_code; // 装备编码
//param.parameter.equipment.stationCode = GlobalContext.s7_1_station; // ⼯位Id
xiaomiParm.stationCode = GlobalContext.s7_1_station;
}
if (GlobalContext.IsUsePLC8)
{
param.parameter.equipment.deviceCode = GlobalContext.s8_device_code; // 装备编码
param.parameter.equipment.stationCode = GlobalContext.s8_station; // ⼯位Id
xiaomiParm.workstation = GlobalContext.s8_work_station; //工站
}
if (GlobalContext.IsUsePLC9)
{
param.parameter.equipment.deviceCode = GlobalContext.s9_device_code; // 装备编码
param.parameter.equipment.stationCode = GlobalContext.s9_station; // ⼯位Id
xiaomiParm.workstation = GlobalContext.s9_work_station; //工站
}
param.parameter.equipment.project = GlobalContext.Project_Code;
param.parameter.equipment.productMode = "debug";
param.parameter.other.logLevel = 0;
param.parameter.other.LogPath = GlobalContext.MqttLogDir;
XiaomiMqttClient_Extend.ParameterConfig(param);
//保存全局变量
xiaomiParm.stationCode = param.parameter.equipment.stationCode;
xiaomiParm.deviceCode = param.parameter.equipment.deviceCode;
}
else
{
picIot.Image = imageListState.Images[0];
AddMessage(LogType.Info,
$"小米IOT MQTT[{GlobalContext.MQTTServerHost}:{GlobalContext.MQTTServerPort}]初始连接失败!--- {response_ErrCode.ToString()}");
}
}
// 开启AGV(Http与MQTT)
if (GlobalContext.IsUseAGV)
{
// AGV HTTP
bool mesret1 = HttpUitls.PingIP(GlobalContext.AGVHttpIp);
if (mesret1)
{
picAgvHttp.Image = imageListState.Images[1];
AddMessage(LogType.Info, "AGV Http初始连接成功!");
}
else
{
picAgvHttp.Image = imageListState.Images[0];
AddMessage(LogType.Info, $"AGV Http[{GlobalContext.AGVHttpHost}]初始连接失败!");
}
string agvMqttIp = GlobalContext.MQTTServerHost;
int agvMqttPort = GlobalContext.MQTTServerPort;
Action callback = AGVMqttShowLog;
ResultData_MQTT result_MQTT = _MQTTHelper
.CreateMQTTClientAndStart(agvMqttIp, agvMqttPort, null, null, callback).Result; // 连接MQTT服务器
// AGV MQTT
if (result_MQTT.ResultCode == 1)
{
picAgvMqtt.Image = imageListState.Images[1];
GlobalContext.AGVMQTTIsConnect = true;
AddMessage(LogType.Info, "小米AGV MQTT初始连接成功!");
ResultData_MQTT result =
XiaomiAGVMQTT_Base.DeviceTopicAGV(ref _MQTTHelper, GlobalContext.AGVMQTTDeviceCode);
AddMessage(LogType.Info,
$"小米AGV MQTT订阅{GlobalContext.AGVMQTTDeviceCode}--- [{result.ResultCode}]{result.ResultMsg}!");
}
else
{
picAgvMqtt.Image = imageListState.Images[0];
GlobalContext.AGVMQTTIsConnect = false;
AddMessage(LogType.Info,
$"小米AGV MQTT[{GlobalContext.AGVMQTTHost}:{GlobalContext.AGVMQTTPort}]初始连接失败!--- [{result_MQTT.ResultCode}]{result_MQTT.ResultMsg}");
}
}
// 持续监视MES、IOT、AGV HTTP、AGV MQTT连接状态
Task.Run(MonitorMESConnect);
// 查询PLC连接状态
foreach (int plcNo in FunsEip.Keys)
{
bool connected = FunsEip[plcNo].IsConnected;
if (connected)
{
string msg = plcNo.ToString() + "工位初始连接成功---" + FunsEip[plcNo]._pcIPStr;
AddMessage(LogType.Info, msg);
UpdatePLCMonitor(1, plcNo, 1); // 更新PLC状态的UI
}
else
{
string msg = plcNo.ToString() + "工位初始连接失败---" + FunsEip[plcNo]._pcIPStr;
AddMessage(LogType.Info, msg);
UpdatePLCMonitor(1, plcNo, 0); // 更新PLC状态的UI
}
}
// PLC4时 初始化扫码器TCP
//if (GlobalContext.IsUsePLC4)
// HpTCPClientInit();
// 开启PLC的业务处理线程-监听PLC点位+状态
foreach (Task task in TaskReadProcess)
{
if (task != null)
task.Start();
}
//// 开启iot的线程
TaskReadAlarm.Start();
////下传MES信息给1工位(先判断下plc对象数量)
//if (Funs.Count > 1)
// DownLoadProductInfo(1);
if (GlobalContext.IsUsePLC3 || GlobalContext.IsUsePLC7)
{
//state_l.Text = "设备状态(左):";
//state_r.Text = "设备状态(右):";
//state_r.Visible = true;
//lblDeviceStates2.Visible = true;
}
//上传操作记录
operateToIot("startup", "开启");
//上传装备配置
deviceConfigToIot("project_name", GlobalContext.Project_Code);
AddMessage(LogType.Info, "程序初始化完成");
}
catch (Exception ex)
{
string str = ex.StackTrace;
this.BeginInvoke(new Action(() =>
{
AddMessage(LogType.Error,
"初始化PLC连接失败!异常信息:" + ex.Message + ";异常位置:" + str.Substring(str.LastIndexOf("\\") + 1,
str.Length - str.LastIndexOf("\\") - 1));
}));
}
#endregion
}
catch (Exception ex)
{
string str = ex.StackTrace;
OnMessage(LogType.Info,
"主窗体的首页初始化出错!异常位置:" +
str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1) + ";异常信息:" +
ex.Message.ToString());
if (ex.Message != null && ex.Message.Contains("timed out"))
MessageBox.Show("主窗体的首页初始化出错!异常信息:PLC连接超时!" + ex.Message);
else
MessageBox.Show("主窗体的首页初始化出错!异常信息:" + ex.Message);
}
}
///
/// 窗体关闭事件
///
private void Form_Home_FormClosed(object sender, FormClosedEventArgs e)
{
Closed2();
}
public void Closed2()
{
try
{
IsRun = false;
Thread.Sleep(IntervalReadPLC);
// 断开TCP
int count = _HPSocket_TcpClients.Count();
for (int i = 0; i < count; i++)
{
try
{
if (_HPSocket_TcpClients[i] != null && _HPSocket_TcpClients[i]._client.IsConnected)
{
_HPSocket_TcpClients[i].Stop();
_HPSocket_TcpClients[i]._client.OnPrepareConnect -= OnPrepareConnect; // 准备连接了事件
_HPSocket_TcpClients[i]._client.OnConnect -= OnConnect; // 连接事件
_HPSocket_TcpClients[i]._client.OnSend -= OnSend; // 数据包发送事件
_HPSocket_TcpClients[i]._client.OnReceive -= OnReceive; // 数据包到达事件
_HPSocket_TcpClients[i]._client.OnClose -= OnClose; // TCP连接关闭事件
}
}
catch
{
}
}
// 关闭Iot
try
{
XiaomiMqttClient_Extend.CloseWithMqttServer(GlobalContext.MqttServerPath,
GlobalContext.MqttServerName);
}
catch
{
}
// 关闭AGV Mqtt
try
{
_MQTTHelper.DisconnectAsync_Client().Wait();
}
catch
{
}
}
catch
{
}
}
#endregion 窗体基础事件
#region 监控MES状态
///
/// 监控MES连接状态
///
private void MonitorMESConnect()
{
while (IsRun) // 运行被控线程
{
try
{
// 开启MES(Http)
if (GlobalContext.IsUseMES)
{
bool mesret = HttpUitls.PingIP(GlobalContext.ServerIp);
if (mesret)
{
picMESStatus.Image = imageListState.Images[1];
GlobalContext.MESIsConnect = true;
}
else
{
picMESStatus.Image = imageListState.Images[0];
GlobalContext.MESIsConnect = false;
OnMessage(LogType.Info, $"小米MES[{GlobalContext.ServerHost}]连接失败");
}
}
// 开启IOT(MQTT)
if (GlobalContext.IsUseIot)
{
bool iIot = XiaomiMqttClient.IsOpen;
if (iIot)
picIot.Image = imageListState.Images[1];
else
{
picIot.Image = imageListState.Images[0];
OnMessage(LogType.Info,
$"小米IOT MQTT[{GlobalContext.MQTTServerHost}:{GlobalContext.MQTTServerPort}]连接失败");
}
}
// 开启AGV(Http与MQTT)
if (GlobalContext.IsUseAGV)
{
// AGV Http
bool mesret1 = HttpUitls.PingIP(GlobalContext.AGVHttpIp);
if (mesret1)
picAgvHttp.Image = imageListState.Images[1];
else
{
picAgvHttp.Image = imageListState.Images[0];
OnMessage(LogType.Info, $"小米AGV Http[{GlobalContext.AGVHttpHost}]连接失败");
}
// AGV MQTT
if (GlobalContext.AGVMQTTIsConnect)
picAgvMqtt.Image = imageListState.Images[1];
else
{
picAgvMqtt.Image = imageListState.Images[0];
OnMessage(LogType.Info,
$"小米AGV MQTT[{GlobalContext.AGVMQTTHost}:{GlobalContext.AGVMQTTPort}]连接失败");
}
}
}
catch (Exception ex)
{
string str = ex.StackTrace;
AddMessage(LogType.Error,
"监控MES心跳失败!异常信息:" + ex.Message + ";异常位置:" + str.Substring(str.LastIndexOf("\\") + 1,
str.Length - str.LastIndexOf("\\") - 1));
}
Thread.Sleep(IntervalMonitorMES);
}
}
#endregion 监控MES连接状态
#region 采集设备状态、运行数据、报警数据
///
/// 请求设备状态 5000
///
/// 1
///
/// 0:证明未连接到PLC;1,代表设备控制状态处于运行状态;2,代表设备控制状态处于故障状态;3,代表设备控制状态处于缺料状态;4,代表设备控制状态处于待机状态;5,代表设备控制状态处于维修状态;
public int GetDeviceStatus(int plcNo, string stationNameStr = "[S0]壳体上料")
{
try
{
if (Funs[plcNo].IsConnected) // 检查PLC是否已连接上
{
short result = Funs[plcNo].ReadHoldingRegisters(5000); // 5000
return result;
}
else
{
return 0;
}
}
catch (Exception ex)
{
string str = ex.StackTrace;
AddMessage_Station(stationNameStr, LogType.Error,
"请求设备状态失败!" + ex.Message.ToString() + ";异常位置:" + str.Substring(str.LastIndexOf("\\") + 1,
str.Length - str.LastIndexOf("\\") - 1));
return 0;
}
}
///
/// 检查是否可采集点检数据 - 不取新值
/// 5000不为1时可点检
///
///
public bool CheckCanSpotcheck1(int deviceState)
{
//return true;
//D5000 = 1,代表设备控制状态处于运行状态
//D5000 = 2, 代表设备控制状态处于故障状态
//D5000 = 3,代表设备控制状态处于缺料状态
//D5000 = 4, 代表设备控制状态处于待机状态
//D5000 = 5,代表设备控制状态处于维修状态
return deviceState != 1;
}
///
/// 检查是否可采集产品数据 - 不取新值
///
///
public bool CheckCanCollData(int deviceState)
{
return deviceState == 0; // 点检时该值不为0
}
///
/// 采集到的设备状态
///
private string _DeviceStates = "未知状态";
private string _DeviceStates_Old = "未知状态";
private string _DeviceStates2 = "未知状态";
private string _DeviceStates_Old2 = "未知状态";
private static string AlarmStateCode=""; //记录出发故障时的其中一笔警报编码,上传设备状态用
private static string AlarmStateName = ""; //记录出发故障时的其中一笔警报名称,上传设备状态用
private static string AlarmStateTime = ""; //记录出发故障时的其中一笔警报时间,上传设备状态用
///
/// 获取设备报警数据与获取设备运行信息
///
private async void ReadAlarmAllPLC()
{
/// 获取设备报警数据与状态信息
string stationNameStr = "获取设备报警数据与状态信息";
// 已连接到PLC
while (IsRun)
{
try
{
#region 报警数据
try
{
//_FaultDatas = new uint[] { 4, 0, 30, 10 };
if (_FaultDatas.Length > 0)
{
ReadPLCAlarmToIot(_FaultDatas, _FaultDatas_Old, stationNameStr,"left");
}
else
{
AddMessage_Station(stationNameStr, LogType.Error, $"【报警日志】{stationNameStr}_获取报警数据出错!");
}
if (_FaultDatas2.Length > 0)
{
ReadPLCAlarmToIot(_FaultDatas2, _FaultDatas_Old2, stationNameStr,"right");
}
else
{
AddMessage_Station(stationNameStr, LogType.Error, $"【报警日志】{stationNameStr}_获取报警数据出错!");
}
}
catch (Exception ex)
{
string str = ex.StackTrace;
AddMessage_Station(stationNameStr, LogType.Error,
$"【报警日志】{stationNameStr}_获取报警数据出错!错误信息:" + ex.Message.ToString() + "异常位置:" +
str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1));
}
#endregion 报警数据
#region 设备状态
//if (!GlobalContext._IsCon_plc1Alarm)
//{
// UpdatePLCMonitor(1, -2, 0);
// continue;
//}
foreach (Inovance_EIP plcEIP in FunsEip.Values)
{
if (plcEIP != null)
{
if (plcEIP.IsConnected)
{
#region 主页展示设备运行状态并上传到IOT中,有双工位left就是左工位,没有双工位left就是单工位
switch (xmDeviceStateData.left)
{
case XiaomiDeviceState.Uninitialized: // 未初始化状态(未初始状态,需先初始化装备才能运行)
_DeviceStates = "未初始化状态";
lblDeviceStates.Text = _DeviceStates;
break;
case XiaomiDeviceState.Initializing: // 初始化状态(初始化进行中)
_DeviceStates = "初始化状态";
lblDeviceStates.Text = _DeviceStates;
break;
case XiaomiDeviceState.Initialized: // 初始化完成状态(初始化完成)
_DeviceStates = "初始化完成状态";
lblDeviceStates.Text = _DeviceStates;
break;
case XiaomiDeviceState.Running: // 运行状态(正常运行中)
_DeviceStates = "运行状态";
lblDeviceStates.Text = _DeviceStates;
break;
case XiaomiDeviceState.Paused: // 暂停状态(设备运行中人工操作暂停,进入此状态)
_DeviceStates = "暂停状态";
lblDeviceStates.Text = _DeviceStates;
break;
case XiaomiDeviceState.Fault: // 故障状态(发生故障后进入此状态,同时停止运行)
_DeviceStates = "故障状态";
lblDeviceStates.Text = _DeviceStates;
break;
case XiaomiDeviceState.Alarm: // 警报状态(产生报警后进入此状态,同时停止运行)
_DeviceStates = "警报状态";
lblDeviceStates.Text = _DeviceStates;
break;
}
if (!_DeviceStates.Equals(_DeviceStates_Old))
{
var iotResult =
SaveDeviceStateData(stationNameStr, xmDeviceStateData.left, "left"); // 上传+保存
if (iotResult.Item1 == 1)
{
_DeviceStates_Old = _DeviceStates;
AddMessage_Station(stationNameStr, LogType.Info,
"【设备状态】" + stationNameStr + $"_上传设备状态到Iot成功!");
}
else
AddMessage_Station(stationNameStr, LogType.Info,
"【设备状态】" + stationNameStr +
$"_上传设备状态到Iot失败!报错信息:[{iotResult.Item1}]_{iotResult.Item2}");
}
#endregion 主页展示设备运行状态并上传到IOT中
#region 右工位
//if (GlobalContext.IsUsePLC3 || GlobalContext.IsUsePLC7)
//{
// switch (xmDeviceStateData.right)
// {
// case XiaomiDeviceState.Uninitialized: // 未初始化状态(未初始状态,需先初始化装备才能运行)
// _DeviceStates2 = "未初始化状态";
// lblDeviceStates2.Text = _DeviceStates2;
// break;
// case XiaomiDeviceState.Initializing: // 初始化状态(初始化进行中)
// _DeviceStates2 = "初始化状态";
// lblDeviceStates2.Text = _DeviceStates2;
// break;
// case XiaomiDeviceState.Initialized: // 初始化完成状态(初始化完成)
// _DeviceStates2 = "初始化完成状态";
// lblDeviceStates2.Text = _DeviceStates2;
// break;
// case XiaomiDeviceState.Running: // 运行状态(正常运行中)
// _DeviceStates2 = "运行状态";
// lblDeviceStates2.Text = _DeviceStates2;
// break;
// case XiaomiDeviceState.Paused: // 暂停状态(设备运行中人工操作暂停,进入此状态)
// _DeviceStates2 = "暂停状态";
// lblDeviceStates2.Text = _DeviceStates2;
// break;
// case XiaomiDeviceState.Fault: // 故障状态(发生故障后进入此状态,同时停止运行)
// _DeviceStates2 = "故障状态";
// lblDeviceStates2.Text = _DeviceStates2;
// break;
// case XiaomiDeviceState.Alarm: // 警报状态(产生报警后进入此状态,同时停止运行)
// _DeviceStates2 = "警报状态";
// lblDeviceStates2.Text = _DeviceStates2;
// break;
// }
// if (!_DeviceStates2.Equals(_DeviceStates_Old2))
// {
// var iotResult = SaveDeviceStateData(stationNameStr, xmDeviceStateData.left,
// "right"); // 上传+保存
// if (iotResult.Item1 == 1)
// {
// _DeviceStates_Old2 = _DeviceStates2;
// AddMessage_Station(stationNameStr, LogType.Info,
// "【设备状态】" + stationNameStr + $"_上传Iot成功!");
// }
// else
// AddMessage_Station(stationNameStr, LogType.Info,
// "【设备状态】" + stationNameStr +
// $"_上传Iot失败!报错信息:[{iotResult.Item1}]_{iotResult.Item2}");
// }
//}
#endregion 右工位
}
}
}
#endregion
}
catch (Exception ex)
{
//AddMessage_Station(stationNameStr, LogType.Error, $"PLC1_{stationNameStr}运行出错!错误信息:" + ex.Message.ToString());
AddMessage_Station(stationNameStr, LogType.Error,
$"PLC1_{stationNameStr}_采集运行数据与报警数据出错!错误信息:" + ex.Message.ToString());
}
Thread.Sleep(IntervalAlarm);
}
}
#endregion 轮询PLC
#region 下发订单信息
/////
///// 壳体上料(下发工单)的交互逻辑
/////
/////
/////
//private void ReadStation_DownOrderInfo(int plcNo)
//{
// // [S1] Tray盘上料装备(板测)
// // [S2] FCT(板测)
// // [S3] 值板机
// // [S4] 取放桁架
// // [S5] Tray盘下料装备
// /// 上位机心跳
// /// 获取设备报警数据与状态信息
// string stationNameStr = "[S0]壳体上料";
// while (IsRun)
// {
// try
// {
// if (!GlobalContext._IsCon_Funs1)
// {
// UpdatePLCMonitor(plcNo, 0);
// continue;
// }
// if (Funs[plcNo].isConnected) // 检查PLC是否已连接上
// {
// #region 壳体上料(下发工单)
// try
// {
// Funs[plcNo].Read_Int_Tag("500", 1, out short[] iiMes0);
// Funs[plcNo].Read_Int_Tag("501", 1, out short[] iiPlc0);
// bool mES_FLAG_1 = iiMes0[0] == 1 ? true : false; // MES_FLAG_1
// bool pLC_Flag_1 = iiPlc0[0] == 1 ? true : false; // PLC_FLAG_1
// // 重置数据和信号
// if (mES_FLAG_1 && pLC_Flag_1) // 1 1
// {
// // 清空写给PLC的数据
// int[] i497 = new int[1] { 0 };
// Funs[plcNo].Write_DInt_Tag("497", 1, i497); // SN号(数字部分)重置信号
// // MES_Flag重置为0
// int[] i500 = new int[1] { 0 };
// Funs[plcNo].Write_DInt_Tag("500", 1, i500); // MES_FLAG_1
// }
// }
// catch (Exception ex)
// {
// string str = ex.StackTrace;
// AddMessage_Station(stationNameStr, LogType.Error, $"PLC{plcNo}_{stationNameStr}下发订单信息运行出错!错误信息:" + ex.Message.ToString() + "异常位置:" + str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1));
// }
// #endregion 壳体上料(下发工单)
// UpdatePLCMonitor(plcNo, 1); // 更新PLC状态的UI // 更新PLC状态的UI
// }
// else
// {
// UpdatePLCMonitor(plcNo, 0); // 更新PLC状态的UI
// AddMessage_Station(stationNameStr, LogType.Info, "PLC" + plcNo.ToString() + "_" + stationNameStr + "连接失败!");
//
// Funs[plcNo].Connect();
// }
// }
// catch (Exception ex)
// {
// UpdatePLCMonitor(plcNo, 0); // 更新PLC状态的UI
// AddMessage_Station(stationNameStr, LogType.Error, $"PLC{plcNo}_{stationNameStr}运行出错!错误信息:" + ex.Message.ToString());
//
// Funs[plcNo].ReConnect();
// }
// Thread.Sleep(IntervalReadPLC);
// }
//}
/////
///// 下发订单信息到PLC
/////
///// PLC编号
//private void DownLoadProductInfo(int plcNo, string stationNameStr = "[S0]壳体上料")
//{
// try
// {
// if (!string.IsNullOrEmpty(GlobalContext.Mtltmrk))
// {
// Funs[plcNo].Write_String_Tag("568", 1, GlobalContext.Mtltmrk); // 产品型号(mtltmrk)
// WritePLCLog(LogType.Debug, GlobalContext.Mtltmrk);
// }
// Funs[plcNo].Write_DInt_Tag("500", 1, new Int32[1] { 1 }); // MES_FLAG_1
// }
// catch (Exception ex)
// {
// string str = ex.StackTrace;
// AddMessage_Station(stationNameStr, LogType.Error, "下发订单信息到PLC失败!" + ex.Message.ToString() + ";异常位置:" + str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1));
// }
//}
///
/// 下发清料信号
///
/// PLC编号
public bool ClearProducts(int plcNo, string stationNameStr = "[S0]壳体上料")
{
try
{
//Funs[plcNo].ReadHoldingRegisters(496); //
AddMessage_Station(stationNameStr, LogType.Info, "下发了清料信号!");
return true;
}
catch (Exception ex)
{
string str = ex.StackTrace;
AddMessage_Station(stationNameStr, LogType.Error,
"下发清料信号失败!" + ex.Message.ToString() + ";异常位置:" + str.Substring(str.LastIndexOf("\\") + 1,
str.Length - str.LastIndexOf("\\") - 1));
return false;
}
}
#endregion 下发订单信息
#region Xiaomi 贲流
#region 公共方法
private static bool ProgressState = false;
private static readonly object lockObj = new object(); // 锁对象
private static bool isCollectingFlagLeft;
private static bool isCollectingFlagRight;
public static bool OpenDailogFalg = true; //是否开启扫码弹窗标识
private static int res = 0;
public static bool StopWhile =false; //二维码弹窗后停止循环
public static bool inStationPass = true; //过站结果,保存进站结果
public static string _strProductBarcode = "";
public static string _strCarrierBarcode = "";
public void ConnectToIOT()
{
XiaomiMqttLoginFunAndParam param = new XiaomiMqttLoginFunAndParam();
// fds
param.parameter.fds.address = GlobalContext.address;
param.parameter.fds.appId = GlobalContext.appId;
param.parameter.fds.appKey = GlobalContext.appKey;
// mes
param.parameter.mes.address = GlobalContext.ServerIp;
param.parameter.mes.appId = GlobalContext.MESAppId;
param.parameter.mes.appKey = GlobalContext.MESAppKey;
// mqtt
param.parameter.mqtt.address = GlobalContext.MQTTServerHost;
param.parameter.mqtt.port = GlobalContext.MQTTServerPort;
param.parameter.mqtt.username = GlobalContext.MQTTAppId;
param.parameter.mqtt.password = GlobalContext.MQTTAppPwd;
// 设备配置
param.parameter.equipment.factoryCode = GlobalContext.Factory_Code;
if (GlobalContext.IsUsePLC1)
{
param.parameter.equipment.deviceCode = GlobalContext.S1_device_code; // 装备编码
param.parameter.equipment.stationCode = GlobalContext.S1_station; // ⼯位Id
xiaomiParm.workstation = GlobalContext.S1_work_station; //工站
}
if (GlobalContext.IsUsePLC2)
{
param.parameter.equipment.deviceCode = GlobalContext.S2_device_code; // 装备编码
param.parameter.equipment.stationCode = GlobalContext.S2_station; // ⼯位Id
xiaomiParm.workstation = GlobalContext.S2_work_station; //工站
}
if (GlobalContext.IsUsePLC3)
{
param.parameter.equipment.deviceCode = GlobalContext.s3_1_device_code; // 装备编码
//param.parameter.equipment.stationCode = GlobalContext.s3_1_station; // ⼯位Id
}
if (GlobalContext.IsUsePLC4)
{
param.parameter.equipment.deviceCode = GlobalContext.s4_device_code; // 装备编码
param.parameter.equipment.stationCode = GlobalContext.s4_station; // ⼯位Id
xiaomiParm.workstation = GlobalContext.s4_work_station; //工站
}
if (GlobalContext.IsUsePLC5)
{
param.parameter.equipment.deviceCode = GlobalContext.s5_device_code; // 装备编码
param.parameter.equipment.stationCode = GlobalContext.s5_station; // ⼯位Id
xiaomiParm.workstation = GlobalContext.s5_work_station; //工站
}
if (GlobalContext.IsUsePLC6)
{
param.parameter.equipment.deviceCode = GlobalContext.s6_device_code; // 装备编码
param.parameter.equipment.stationCode = GlobalContext.s6_station; // ⼯位Id
xiaomiParm.workstation = GlobalContext.s6_work_station; //工站
}
if (GlobalContext.IsUsePLC7)
{
param.parameter.equipment.deviceCode = GlobalContext.s7_1_device_code; // 装备编码
//param.parameter.equipment.stationCode = GlobalContext.s7_1_station; // ⼯位Id
xiaomiParm.stationCode = GlobalContext.s7_1_station;
}
if (GlobalContext.IsUsePLC8)
{
param.parameter.equipment.deviceCode = GlobalContext.s8_device_code; // 装备编码
param.parameter.equipment.stationCode = GlobalContext.s8_station; // ⼯位Id
xiaomiParm.workstation = GlobalContext.s8_work_station; //工站
}
if (GlobalContext.IsUsePLC9)
{
param.parameter.equipment.deviceCode = GlobalContext.s9_device_code; // 装备编码
param.parameter.equipment.stationCode = GlobalContext.s9_station; // ⼯位Id
xiaomiParm.workstation = GlobalContext.s9_work_station; //工站
}
param.parameter.equipment.project = GlobalContext.Project_Code;
param.parameter.equipment.productMode = "debug";
param.parameter.other.logLevel = 0;
param.parameter.other.LogPath = GlobalContext.MqttLogDir;
XiaomiMqttClient_Extend.ParameterConfig(param);
}
///
/// float[]转为string
///
public string FloatArrayToString(float[] nScrewResults)
{
// 使用 "R" 格式说明符来保证浮点数的往返精度,不添加 'f' 后缀
return string.Join(",", Array.ConvertAll(nScrewResults, element => element.ToString("R")));
}
///
/// short[]转为string
///
public string ShortArrayToString(short[] nScrewResults)
{
// 使用 string.Join 来连接数组元素,并使用逗号作为分隔符
return string.Join(",", nScrewResults);
}
///
/// 写入PLC重复三次
///
public (int, string) WriteResultToPlc(int plcNo, string stationNameStr, string strTagName, int nCount,
T inObj)
{
int i = 0;
int nRet = 0;
string strRet = "";
try
{
while (i < 3) // 最多上传三次
{
(nRet, strRet) = FunsEip[plcNo].Write_SingleTag(strTagName, nCount, inObj);
if (nRet == 0) //成功
{
break;
}
else
{
AddMessage_Station(stationNameStr, LogType.Error,
$"PLC{plcNo}_{stationNameStr} 进站结果写入PLC出错!错误信息:" + strRet);
i++;
}
}
return (nRet, strRet);
}
catch (Exception ex)
{
return (1, ex.Message);
}
}
public (int, string) SaveScrewDataToTxt(string direction, string ProductBarcode, float[] fScrewTimes,
short[] nScrewOrders, short[] nScrewResults)
{
try
{
// 获取当前日期
string dateFolder = DateTime.Now.ToString("yyyyMMdd");
// 构建保存路径
string basePath = AppDomain.CurrentDomain.BaseDirectory; // 获取执行文件的目录
string savePath = Path.Combine(basePath, "screw", dateFolder, ProductBarcode, direction, "螺丝Mes数据");
// 确保目录存在
Directory.CreateDirectory(savePath);
// 文件名
string fileName = $"{ProductBarcode}_{DateTime.Now.ToString("HHmmss")}.txt";
string filePath = Path.Combine(savePath, fileName);
// 确保不会超出数组长度,只取前14个或数组的实际长度
int count = Math.Min(14, fScrewTimes.Length);
using (StreamWriter sw = new StreamWriter(filePath))
{
for (int i = 0; i < count; i++)
{
sw.WriteLine($"{ProductBarcode}{"_"}{i + 1}");
sw.WriteLine($"锁附时间:{fScrewTimes[i]}");
sw.WriteLine($"锁附顺序:{nScrewOrders[i]}");
sw.WriteLine($"锁附结果:{nScrewResults[i]}");
sw.WriteLine(); // 空行分隔不同螺丝的信息
}
}
return (0, "");
}
catch (Exception ex)
{
return (1, ex.Message);
}
}
///
/// 解析三点激光存储的文本
///
/// 文本路径
///
public string GetLastLineCompensation(string path)
{
string result = "";
try
{
// 获取当前日期并格式化为 "yyyy-MM-dd" 格式
string currentDate = DateTime.Now.ToString("yyyy-MM-dd");
string filename = $"Laser-{currentDate}-W0.txt";
// 拼接完整路径
string fullPath = Path.Combine(path, filename);
string lastNonEmptyLine = "";
// 判断文件是否存在
if (File.Exists(fullPath))
{
//读取文件内容
string[] lines = File.ReadAllLines(fullPath);
// 获取最后一行数据(忽略标题行)
if (lines.Length > 1)
{
string lastLine = "";
for (int i = lines.Length - 1; i > 0; i--)
{
if (!string.IsNullOrEmpty(lines[i]))
{
lastLine = lines[i];
break;
}
}
// 将最后一行按逗号分隔
string[] values = lastLine.Split(',');
// 跳过前两个元素并转换为新的数组
string[] newValues = values.Skip(0).Skip(1).ToArray();
string value = string.Join(",", newValues); // 获取值并去除多余空格
value = value.Replace(" ", "").Replace("\t", "").TrimStart(',');
if (value.StartsWith(","))
{
value = value.Substring(1);
}
result = value;
}
}
else
{
Console.WriteLine($"文件不存在: {fullPath}");
}
}
catch (Exception ex)
{
// 捕获异常并输出错误信息
Console.WriteLine($"发生错误: {ex.Message}");
}
return result;
}
public int PCBStationOutData(BarcodeSet_t Barcode, IoT_DataSet_t iotData)
{
int res = 0;
string jsonstr1 = "";
try
{
XmMES_StationOutRequest_Body outRequest_Body = new XmMES_StationOutRequest_Body();
outRequest_Body.machineId = GlobalContext.S5_MachineId; // 装备id(可配置)
outRequest_Body.stationId = GlobalContext.s5_station; // ⼯位ID(可配置)
outRequest_Body.clientMac = GlobalContext.MacStr; // 客⼾端本机MAC地址,格式:XX-XX-XX-XX-XX-XX
outRequest_Body.clientTime =
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); // 客⼾端请求时间,格式yyyy-MM-dd HH:mm:ss.fff
outRequest_Body.unitSn = Barcode.strPCBBarcode; // 产品SN
int a1Result = (int)iotData.testStatus;
//bool pass = a1Result == 1;
//outRequest_Body.state = pass ? "PASS" : "FAIL"; ; // 出站条件 PASS或FAIL
outRequest_Body.state = "PASS";
outRequest_Body.userId = GlobalContext.MESUserId; // ⽤⼾ID
outRequest_Body.factoryId = GlobalContext.Factory_Code; // ⼯⼚id
XmStationOut_KeyMaterial keyMaterial = new XmStationOut_KeyMaterial();
keyMaterial.bindSort = 1;
keyMaterial.materialSn = Barcode.strProductBarcode;
outRequest_Body.unitData.keyMaterial.Add(keyMaterial); // 产品码
jsonstr1 = JsonConvert.SerializeObject(outRequest_Body);
XmMES_StationOutResponse response = new XmMES_StationOutResponse();
response = XiaomiMESHttp_StationOutbound.StationOut(outRequest_Body);
if (response != null && response.header.code == "200")
{
res = 1;
AddMessage(LogType.Info,
"上传PCB出站数据到MES服务器---成功!请求信息:" + jsonstr1 + ",返回信息:" +
JsonConvert.SerializeObject(response.body));
}
else
{
res = 0;
AddMessage(LogType.Error,
"上传PCB出站数据到MES服务器---失败!错误信息:" + response.header.desc + ",请求信息:" + jsonstr1 + ",返回信息:" +
JsonConvert.SerializeObject(response.body));
}
}
catch (Exception e)
{
res = 0;
AddMessage(LogType.Info, "上传PCB出站数据到MES服务器---失败!请求信息:" + jsonstr1 + ",返回信息:" + e.Message);
}
return res;
}
public int PCBStationInData(BarcodeSet_t Barcode, IoT_DataSet_t iotData)
{
int res = 0;
string json_Body = "";
try
{
XmMES_StationInRequest_Body inRequest_Body = new XmMES_StationInRequest_Body();
inRequest_Body.machineId = GlobalContext.S5_MachineId; // 装备ID(可配置)
inRequest_Body.stationId = GlobalContext.s5_station; // ⼯位ID(可配置)
inRequest_Body.clientMac = GlobalContext.MacStr; // 客⼾端本机MAC地址,格式:XX-XX-XX-XX-XX-XX
inRequest_Body.clientTime =
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); // 客⼾端请求时间,格式yyyy-MM-dd HH:mm:ss.fff
inRequest_Body.unitSn = Barcode.strPCBBarcode; // 产品SN
inRequest_Body.userId = GlobalContext.MESUserId; // 用户ID;
inRequest_Body.factoryId = GlobalContext.Factory_Code; // 工厂ID;
json_Body = JsonConvert.SerializeObject(inRequest_Body);
var response = XiaomiMESHttp_StationInbound.StationIn(inRequest_Body);
string resultJson = JsonConvert.SerializeObject(response);
if (response != null && response.header.code == "200")
{
res = 1;
AddMessage(LogType.Info,
"上传PCB进站数据到MES服务器---成功!请求信息:" + json_Body + ",返回信息:" +
JsonConvert.SerializeObject(response.body));
}
else
{
res = 0;
AddMessage(LogType.Error,
"上传PCB进站数据到MES服务器---失败!错误信息:" + response.header.desc + ",请求信息:" + json_Body + ",返回信息:" +
JsonConvert.SerializeObject(response.body));
}
}
catch (Exception e)
{
res = 0;
AddMessage(LogType.Info, "上传PCB进站数据到MES服务器---失败!请求信息:" + json_Body + ",返回信息:" + e.Message);
}
return res;
}
///
/// 调用进站接口并保存进站数据
///
/// 工站信息
/// 工单号
/// 型号(物料号)
/// 产品SN
/// 进站数据
/// 1成功;5MES报警;6上位机报警
public int SaveStationInData(string stationNameStr, string workorder_code, string mtltmrk, string sn,
List items, string MachineId, string StationId, bool pass, string slot)
{
int result = 0;
XmMES_StationInRequest_Body inRequest_Body = new XmMES_StationInRequest_Body();
inRequest_Body.machineId = MachineId; // 装备ID(可配置)
inRequest_Body.stationId = StationId; // ⼯位ID(可配置)
inRequest_Body.clientMac = GlobalContext.MacStr; // 客⼾端本机MAC地址,格式:XX-XX-XX-XX-XX-XX
inRequest_Body.clientTime =
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); // 客⼾端请求时间,格式yyyy-MM-dd HH:mm:ss.fff
inRequest_Body.unitSn = sn; // 产品SN
inRequest_Body.uuidInspection = uuid;
inRequest_Body.state = pass ? "PASS" : "FAIL";
inRequest_Body.userId = GlobalContext.MESUserId; // 用户ID;
inRequest_Body.factoryId = GlobalContext.Factory_Code; // 工厂ID;
string json_Body = JsonConvert.SerializeObject(inRequest_Body);
StationIn stationIn = new StationIn()
{
Workorder_code = workorder_code, // 车间订单号
Mtltmrk = mtltmrk, // 产品型号(物料号)
Sn = sn, // SN
StationIn_body = json_Body, // 进站接口Json数据 - Body
Parameter_values = items, // 进站数据
Write_user = inRequest_Body.userId, // 员工Id
Test_time = inRequest_Body.clientTime // 进站时间
};
// 本地数据
string sql = stationIn.ToStringInsert(0);
string ret = SQLHelper_New.ExecuteNonQuery(sql, null);
result = ret == "成功" ? 1 : 6;
#region MES
if (GlobalContext.IsSendStationIn)
{
try
{
XmMES_StationInResponse response = new XmMES_StationInResponse();
string resultJson = "";
string mesRet = string.Empty;
int i = 0;
while (i < 2) // 1009会多次尝试上传
{
response = XiaomiMESHttp_StationInbound.StationIn(inRequest_Body);
resultJson = JsonConvert.SerializeObject(response);
if (response != null && response.header.code == "200")
break;
else if (!mesRet.Contains("1009")) // 1009是未知错误
i++;
i++;
mesRet = $"[{response?.header?.code}]{response?.header?.desc}";
// 记录失败原因
OnMessage(LogType.Error, $"上传出站数据到MES服务器---失败!正在重新上传!接口报错信息:{mesRet},请求参数:{json_Body}");
}
if (response?.header?.code == "200")
{
inStationPass = true;//上传iot进站结果
string sql_Upd = stationIn.ToStringUpdateStatusByID(1);
string ret_Upd = SQLHelper_New.ExecuteNonQuery(sql_Upd, null);
result = ret_Upd == "成功" ? 1 : 6;
AddMessage(LogType.Info,
$"【进站数据 SN {stationIn.Sn}】上传MES服务器---成功,返回参数:{resultJson},请求参数:{json_Body}");
}
else
{
inStationPass = false;//上传iot进站结果
result = 5;
AddMessage(LogType.Info,
$"【进站数据 SN {stationIn.Sn}】上传MES服务器---失败!接口报错信息: {mesRet},请求参数:{json_Body}");
}
string sql_response =
stationIn.ToStringUpdateStationInReturn_body(JsonConvert.SerializeObject(response));
SQLHelper_New.ExecuteNonQuery(sql_response, null);
}
catch (Exception ex)
{
inStationPass = false;//上传iot进站结果
result = 6;
string str = ex.StackTrace;
AddMessage_Station(stationNameStr, LogType.Error,
$"PLC上传进站数据MES报错!错误信息:" + ex.Message.ToString() + ";异常位置:" +
str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1));
}
}
#endregion
#region IOT
//过站结果,出站传结果,这里注释掉
//if (GlobalContext.IsMqttSendProcessData)
//{
// PassStationResultRequest request = new PassStationResultRequest();
// request.project_code = GlobalContext.Project_Code; // 项⽬编码
// request.factory_code = GlobalContext.Factory_Code; // ⼯⼚Id
// request.process_section_code = GlobalContext.Process_Section_Code; // ⼯段编码
// request.line_code = GlobalContext.LineCode; // 线体编码
// request.work_station = xiaomiParm.workstation; // ⼯站ID
// request.device_code = xiaomiParm.deviceCode; // 装备编码
// request.station = xiaomiParm.stationCode;
// request.process_time =
// DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); // 节拍发⽣时间(2022-06-01 14:27:57.283)
// request.slot = slot; // 槽位编码
// request.sn = sn; // 产品SN
// // request.enter_status = inpass ? "PASS" : "FAIL"; // 进站状态
// request.enter_status = pass ? "PASS" : "FAIL"; // 进站状态
// request.result = ""; // 出站条件 PASS或FAIL; // 过站结果
// request.work_type = "OUT_STATION"; // 作业类型
// // 上传过站结果
// (int, string) iotResult = XiaomiMqttClient_Extend.Write_PassStationResult(request);
// if (iotResult.Item1 != 0)
// {
// OnMessage(LogType.Info,
// $"【IOT过站结果】上传失败!错误原因:{iotResult.Item2},过站结果;产品码[{sn}] 进站结果[{request.enter_status}] 出站结果[{request.result}]");
// }
//}
#endregion
return result;
}
///
/// 选择如何记录出站数据
///
/// 出站数据
/// 设备编号
/// 测试项目
/// 车间订单号
/// 批次号
/// 型号
/// 日期
/// 供应商代码
/// 产品序列号的 数字序列部分
/// 上传成功时返回1;失败返回0
private int SwitctProcessData(string stationNameStr, List items, string equipmentCode,
string processItem,
string workorder_code, string batch_num, string mtltmrk, string proDate,
string supplierCode, string sn, bool pass, string vehicleSn, string vehicleSlot, string MachineId,
string StationId, string PartBarcode, string detailjson, XmStationOut_InspectionItemData inspectionItemData, string direction = "")
{
return SaveProcessDataByDB(stationNameStr, items, equipmentCode, processItem, workorder_code, batch_num,
mtltmrk,
proDate, supplierCode, sn, pass, vehicleSn, vehicleSlot, MachineId, StationId, PartBarcode, detailjson, inspectionItemData,
direction);
}
///
/// 添加出站数据(提交到MES+本地保存到数据库)
///
/// 出站数据
/// 设备编号
/// 测试项目
/// 车间订单号
/// 批次号
/// 型号
/// 日期
/// 供应商代码
/// 产品序列号的 数字序列部分
/// 上传成功时返回1;失败返回0
public int SaveProcessDataByDB(string stationNameStr, List items, string equipmentCode,
string processItem, string workorder_code, string batch_num, string mtltmrk,
string proDate, string supplierCode, string sn, bool pass, string vehicleSn, string vehicleSlot,
string machineId, string stationId, string partBarcode, string detailjson, XmStationOut_InspectionItemData inspectionItemData, string direction = "")
{
int upload = 0;
int result = 0;
ProcessData processData = new ProcessData()
{
Equipment_code = equipmentCode,
Workorder_code = workorder_code,
Batch_number = batch_num,
Sn = sn, // SN
Testitem = processItem,
Parameter_values = items,
Write_user = GlobalContext.CurrentUser,
Test_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")
};
// 本地数据
string sql = processData.ToStringInsert(upload);
string ret = SQLHelper_New.ExecuteNonQuery(sql, null);
//AddMessage_Station(stationNameStr, LogType.Info, string.Concat("[[", equipmentCode, "]", stationNameStr, "]保存本地出站数据---" + ret));
#region MES
if (GlobalContext.IsSendProcessData)
{
try
{
string id = processData.ID.Copy();
XmMES_StationOutRequest_Body outRequest_Body = new XmMES_StationOutRequest_Body();
if (direction == "Right")
outRequest_Body.uuidInspection = uuid2;
else
outRequest_Body.uuidInspection = uuid;
outRequest_Body.machineId = machineId; // 装备id(可配置)
outRequest_Body.stationId = stationId; // ⼯位ID(可配置)
outRequest_Body.clientMac = GlobalContext.MacStr; // 客⼾端本机MAC地址,格式:XX-XX-XX-XX-XX-XX
outRequest_Body.clientTime = processData.Test_time; // 客⼾端请求时间,格式yyyy-MM-dd HH:mm:ss.fff
outRequest_Body.unitSn = sn; // 产品SN
outRequest_Body.state = pass ? "PASS" : "FAIL"; // 出站条件 PASS或FAIL
outRequest_Body.userId = GlobalContext.MESUserId; // ⽤⼾ID
outRequest_Body.factoryId = GlobalContext.Factory_Code; // ⼯⼚id
outRequest_Body.unitData.vehicleData.vehicleSn = vehicleSn;
outRequest_Body.unitData.vehicleData.vehicleType = string.Empty;
outRequest_Body.unitData.vehicleData.slot = vehicleSlot;
if (!string.IsNullOrEmpty(partBarcode))
{
outRequest_Body.unitData.keyMaterial.Add(
new XmMES_StationOutRequest_Body.XmStationOut_KeyMaterial()
{
bindSort = 1,
materialSn = partBarcode
}); // 设备数据 - 部件码
}
#region 过站明细
if (GlobalContext.IsSendProcessDetail)
{
if (detailjson.Length > 0)
{
//detailjson = "{\"fGlueSupplySpeed\":0.63,\"fAB_AirPress\":0.0,\"fAB_AirPressDiff\":0.0,\"fMesHeightInfos\":[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0],\"fIntervalWeights\":[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0],\"fRemainGlues\":[600.0,600.0]}";
// 解析JSON字符串为字典
detailjson = detailjson.Replace("[", "\"[");
detailjson = detailjson.Replace("]", "]\"");
var dictionary = JsonConvert.DeserializeObject>(detailjson);
foreach (var kvp in dictionary)
{
outRequest_Body.unitData.processData.Add(
new XmMES_StationOutRequest_Body.XmStationOut_ProcessData()
{
dataName = kvp.Key.ToString(),
dataValue = string.IsNullOrEmpty(kvp.Value) ? "" : kvp.Value.ToString()
});
}
}
}
#endregion
#region 测试项
if (GlobalContext.IsSendTestData)
{
if (stationNameStr.Contains("OP31") || stationNameStr.Contains("OP32") || stationNameStr.Contains("OP40") || stationNameStr.Contains("OP50"))
{
outRequest_Body.unitData.inspectionItemData.Add(
new XmStationOut_InspectionItemData()
{
childUnitSn = inspectionItemData.childUnitSn,
childUnitState = inspectionItemData.childUnitState,
toolVersion = inspectionItemData.toolVersion,
dataItem = inspectionItemData.dataItem,
}
);
}
}
#endregion
#region OP30站读三点测高
if (stationNameStr.Contains("OP31") || stationNameStr.Contains("OP32"))
{
string path = "";
if (direction == "Left")
{
string pathStr = GlobalContext.MESLaserLPath;
path = DecFileName(pathStr);
}
else
{
string pathStr = GlobalContext.MESLaserRPath;
path = DecFileName(pathStr);
}
var item = outRequest_Body.unitData.processData.FirstOrDefault(data => data.dataName == "fMesHeightInfos");
if (item != null)
{
item.dataValue = GetLastLineCompensation(path);
}
//outRequest_Body.unitData.processData.Add(
// new XmMES_StationOutRequest_Body.XmStationOut_ProcessData()
// {
// dataName = "fMesHeightInfos",
// dataValue = GetLastLineCompensation(path)
// });
}
#endregion
#region 上传文件,添加附件uuid
if (GlobalContext.MESIsSendUpFile)
{
foreach (var item in fileUploadData.fileData)
{
if (!string.IsNullOrEmpty(item.FileName))
{
outRequest_Body.unitData.fileData.Add(
new XmStationOut_FileData()
{
fileName = item.FileName,
fileId = item.FileId,
bucket = item.Bucket
});
}
}
}
#endregion
#region 上传MES
string jsonstr1 = JsonConvert.SerializeObject(outRequest_Body);
if (GlobalContext.IsSendProcessData)
{
XmMES_StationOutResponse response = new XmMES_StationOutResponse();
string mesRet = string.Empty;
int i = 0;
while (i < 2) // 1009会多次尝试上传
{
response = XiaomiMESHttp_StationOutbound.StationOut(outRequest_Body);
if (response != null && response.header.code == "200")
{
OnMessage(LogType.Info,
$"【出站数据 SN {sn}】上传出站数据到MES服务器---成功!请求信息:" + jsonstr1 + "返回信息:" +
JsonConvert.SerializeObject(response.body));
break;
}
else if (!mesRet.Contains("1009")) // 1009是未知错误
i++;
i++;
mesRet = $"[{response?.header?.code}]{response?.header?.desc}";
// 记录失败原因
OnMessage(LogType.Error,
$"【出站数据 SN {sn}】上传出站数据到MES服务器---失败!正在重新上传!接口报错信息:" + mesRet + "参数:" + jsonstr1);
}
if (response?.header?.code == "200")
{
string sql_Upd = ProcessData.ToStringUpdateStatusByID(1, id);
string ret_Upd = SQLHelper_New.ExecuteNonQuery(sql_Upd, null);
result = 1;
//AddMessage_Station(stationNameStr, LogType.Info, $"更新【出站数据 id {id}】上传状态---" + ret_Upd);
AddMessage(LogType.Info, $"【出站数据 SN {sn}】上传MES服务器---成功");
}
else
{
AddMessage(LogType.Info, $"【出站数据 SN {sn}】上传MES服务器---失败!接口报错信息:" + mesRet);
}
fileUploadData.fileData.Clear();
string sql_UpStationout = ProcessData.ToStringUpdateStationOut_body(
JsonConvert.SerializeObject(outRequest_Body),
JsonConvert.SerializeObject(response), id);
SQLHelper_New.ExecuteNonQuery(sql_UpStationout, null);
}
#endregion
}
catch (Exception ex)
{
string str = ex.StackTrace;
AddMessage_Station(stationNameStr, LogType.Error,
$"PLC上传出站数据MES报错!错误信息:" + ex.Message.ToString() + ";异常位置:" +
str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1));
}
}
#endregion
#region IOT
//过站明细
if (GlobalContext.IsMqttSendProcessData)
{
test_item_num = 0;
PassStationDetailRequest request = new PassStationDetailRequest();
if (direction == "Right")
request.pass_station_id = uuid2;
else
request.pass_station_id = uuid;
request.sn = sn; // 产品SN
request.slot = vehicleSlot; // 槽位编码
request.function_name = "Machine_加⼯模块";
request.status = "PASS";
(int, string) iotResult = (0, "");
if (detailjson.Length > 0)
{
// 解析JSON字符串为字典
var dictionary = JsonConvert.DeserializeObject>(detailjson);
foreach (var kvp in dictionary)
{
test_item_num += 1;
request.test_item_num = test_item_num.ToString();
request.test_item = kvp.Key.ToString();
request.result_val = string.IsNullOrEmpty(kvp.Value) ? "" : kvp.Value.ToString();
request.description = request.test_item;
// 上传过站明细
iotResult = XiaomiMqttClient_Extend.Write_PassStationDetail(request);
if (iotResult.Item1 != 0)
{
OnMessage(LogType.Info,
$"【IOT过站明细】上传失败!错误原因:{iotResult.Item2},过站明细;产品码[{sn}] 测试项目[{request.test_item}] 测试值[{request.result_val}]");
}
}
}
}
//过站结果
if (GlobalContext.IsMqttSendProcessData)
{
PassStationResultRequest request = new PassStationResultRequest();
if (direction == "Right")
request.pass_station_id = uuid2;
else
request.pass_station_id = uuid;
request.project_code = GlobalContext.Project_Code; // 项⽬编码
request.factory_code = GlobalContext.Factory_Code; // ⼯⼚Id
request.process_section_code = GlobalContext.Process_Section_Code; // ⼯段编码
request.line_code = GlobalContext.LineCode; // 线体编码
request.work_station = xiaomiParm.workstation; // ⼯站ID
request.device_code = xiaomiParm.deviceCode; // 装备编码
request.station = xiaomiParm.stationCode;
request.process_time =
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); // 节拍发⽣时间(2022-06-01 14:27:57.283)
request.slot = vehicleSlot; // 槽位编码
request.sn = sn; // 产品SN
// request.enter_status = inpass ? "PASS" : "FAIL"; // 进站状态
request.enter_status = inStationPass ? "PASS" : "FAIL";// 进站状态
request.result = pass ? "PASS" : "FAIL"; // 出站条件 PASS或FAIL; // 过站结果
request.work_type = "OUT_STATION"; // 作业类型
// 上传过站结果
(int, string) iotResult = XiaomiMqttClient_Extend.Write_PassStationResult(request);
if (iotResult.Item1 != 0)
{
OnMessage(LogType.Info,
$"【IOT过站结果】上传失败!错误原因:{iotResult.Item2},过站结果;产品码[{sn}] 进站结果[{request.enter_status}] 出站结果[{request.result}]");
}
}
#endregion
return result;
}
//private void CollectAndProcessDataLeft(string sn, string direction, string ip, string port, int connectTimeOut, int sendDataTimeOut)
//{
// Stopwatch stopwatch = new Stopwatch();
// stopwatch.Start();
// try
// {
// // 初始化 AtlasScrew 实例
// AtlasScrew atlasScrew1 = new AtlasScrew(ip, port, connectTimeOut, sendDataTimeOut);
// atlasScrew1.Initial();
// // 存储结果的列表
// List<(double Angle, double Torque, double StartTorque, double MaxTorque)> results = new List<(double, double, double, double)>();
// // 存储角度和扭力的字符串列表
// List angleStrs = new List();
// List torqueStrs = new List();
// // 上一次获取的数据
// (double JD_MEAN, double NL_MEAN) lastResult = (-1, -1);
// while (isExitAtlasLeft) // 检查是否收集数据
// {
// // 获取当前数据
// var currentResult = atlasScrew1.GetResults();
// // 判断是否为新数据
// if (currentResult.JD_MEAN != lastResult.JD_MEAN || currentResult.NL_MEAN != lastResult.NL_MEAN)
// {
// OnMessage(LogType.Info, $"检测到新数据: JD_MEAN={currentResult.JD_MEAN}, NL_MEAN={currentResult.NL_MEAN}");
// // 更新角度和扭力的字符串列表
// angleStrs.Add(currentResult.JD_MEAN.ToString());
// torqueStrs.Add(currentResult.NL_MEAN.ToString());
// // 计算角度、扭力、起始扭力和最大扭力
// double[] angles = angleStrs.Select(a => double.Parse(a)).ToArray();
// double[] torques = torqueStrs.Select(a => double.Parse(a)).ToArray();
// double startTorque = torques.Length > 0 ? torques[0] : -1; // 起始扭力
// double maxTorque = torques.Length > 0 ? torques.Max() : -1; // 最大扭力
// // 将新数据添加到结果列表
// results.Add((angles.Last(), torques.Last(), startTorque, maxTorque));
// // 更新上一次获取的数据
// lastResult = currentResult;
// }
// // 等待一段时间后再次检查
// Thread.Sleep(20); // 轮询间隔时间
// // 如果触发了出站,则退出循环
// if (!isExitAtlasLeft)
// {
// break;
// }
// }
// // 生成文件名
// string fileName = $"{sn}_{direction}_{DateTime.Now:yyyyMMddHHmmss}.txt";
// // 写入数据到文件
// using (StreamWriter writer = new StreamWriter(fileName))
// {
// // 写入标题行
// writer.WriteLine("角度, 扭力, 起始扭力, 最大扭力");
// // 写入每一行数据
// foreach (var result in results)
// {
// writer.WriteLine($"{result.Angle}, {result.Torque}, {result.StartTorque}, {result.MaxTorque}");
// }
// }
// stopwatch.Stop();
// AddMessage(LogType.Info, $"数据采集完成并保存到文件 {fileName}; 总用时 {stopwatch.ElapsedMilliseconds}ms");
// }
// catch (Exception ex)
// {
// AddMessage(LogType.Error, $"数据采集过程中发生错误: {ex.Message}");
// }
// finally
// {
// // 重置标志变量
// isExitAtlasLeft = false;
// }
//}
//private void CollectAndProcessDataRight(string sn, string direction, string ip, string port, int connectTimeOut, int sendDataTimeOut)
//{
// Stopwatch stopwatch = new Stopwatch();
// stopwatch.Start();
// try
// {
// // 初始化 AtlasScrew 实例
// AtlasScrew atlasScrew2 = new AtlasScrew(ip, port, connectTimeOut, sendDataTimeOut);
// atlasScrew2.Initial();
// // 存储结果的列表
// List<(double JD_MEAN, double NL_MEAN)> results = new List<(double JD_MEAN, double NL_MEAN)>();
// // 上一次获取的数据
// (double JD_MEAN, double NL_MEAN) lastResult = (-1, -1);
// while (isExitAtlasRight) // 检查是否收集数据
// {
// // 获取当前数据
// var currentResult = atlasScrew2.GetResults();
// // 判断是否为新数据
// if (currentResult.JD_MEAN != lastResult.JD_MEAN || currentResult.NL_MEAN != lastResult.NL_MEAN)
// {
// AddMessage(LogType.Info, $"检测到新数据: JD_MEAN={currentResult.JD_MEAN}, NL_MEAN={currentResult.NL_MEAN}");
// // 将新数据写入PLC
// //WriteDataToPlc(plcNo, stationNameStr, tagMesCommName, currentResult.JD_MEAN, currentResult.NL_MEAN);
// // 将新数据添加到结果列表
// results.Add(currentResult);
// // 更新上一次获取的数据
// lastResult = currentResult;
// }
// // 等待一段时间后再次检查
// Thread.Sleep(20); // 轮询间隔时间
// // 如果触发了出站,则退出循环
// if (!isExitAtlasRight)
// {
// break;
// }
// }
// // 将所有数据写入文件
// //WriteDataToFile(sn, direction, results);
// stopwatch.Stop();
// OnMessage(LogType.Info, $"螺丝数据采集完成;总用时{stopwatch.ElapsedMilliseconds}ms");
// }
// catch (Exception ex)
// {
// OnMessage(LogType.Error, $"螺丝数据采集过程中发生错误: {ex.Message}");
// }
// finally
// {
// // 重置标志变量
// isExitAtlasRight = false;
// }
//}
///
///字符串中文乱码解决
///
///
///
public static string DecFileName(string str)
{
Encoding utf8 = Encoding.GetEncoding("gbk");
byte[] btArr = utf8.GetBytes(str);
return Encoding.UTF8.GetString(btArr);
}
private void CollectAndProcessDataLeft(AtlasScrew atlasScrew, string sn, string direction)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
int nRet = 0;
string strRet = "";
try
{
int fileCounter = 1; // 文件计数器,用于生成文件名中的序号
while (isCollectingFlagLeft)
{
// 从缓存中获取所有未处理的数据
var cachedData = atlasScrew.GetCachedDataLeft();
foreach (var currentResult in cachedData)
{
if (currentResult.JD_MEAN == 0 || currentResult.NL_MEAN == 0)
{
continue; // 跳过无效数据
}
OnMessage(LogType.Info,
$"检测到新数据: JD_MEAN={currentResult.JD_MEAN}, NL_MEAN={currentResult.NL_MEAN}");
// 写入PLC
OP70_ScrewDataSet_t resultToPlC = new OP70_ScrewDataSet_t
{
fTorque = float.Parse(currentResult.NL_MEAN.ToString()),
fCircles = float.Parse(currentResult.JD_MEAN.ToString())
};
(nRet, strRet) = WriteResultToPlc(7, "", $"g_OP70_MES.{direction}.screwDriver", 1, resultToPlC);
if (nRet != 0)
{
OnMessage(LogType.Info,
$"SN:{sn}, JD_MEAN={currentResult.JD_MEAN}, NL_MEAN={currentResult.NL_MEAN} 写入PLC失败");
}
else
{
OnMessage(LogType.Info,
$"SN:{sn}, JD_MEAN={currentResult.JD_MEAN}, NL_MEAN={currentResult.NL_MEAN} 写入PLC成功");
}
// 构建保存路径
string dateFolder = DateTime.Now.ToString("yyyyMMdd");
string basePath = AppDomain.CurrentDomain.BaseDirectory;
string savePath = Path.Combine(basePath, "screw", dateFolder, sn, direction, "曲线");
Directory.CreateDirectory(savePath); // 确保目录存在
// 构建文件名(以 SN + 序号命名)
string fileName = $"{sn}_{fileCounter}.txt";
string filePath = Path.Combine(savePath, fileName);
// 写入文件
using (StreamWriter writer = new StreamWriter(filePath))
{
writer.WriteLine("精度, 扭力");
// 遍历 Pearkdegree 和 PearkTorque 的所有值
for (int i = 0;
i < currentResult.Pearkdegree.Length && i < currentResult.PearkTorque.Length;
i++)
{
double precision = Math.Round(currentResult.Pearkdegree[i]); // 精度
double torque = Math.Round(currentResult.PearkTorque[i], 2); // 扭力
writer.WriteLine($"{precision}, {torque}");
}
}
OnMessage(LogType.Info, $"保存文件成功: {filePath}");
// 增加文件计数器
fileCounter++;
}
// 如果没有更多数据,则短暂休眠以节省资源
if (!cachedData.Any())
{
Thread.Sleep(10); // 根据需要调整休眠时间
}
// 如果触发了出站,则退出循环
if (!isCollectingFlagLeft)
{
break;
}
}
stopwatch.Stop();
OnMessage(LogType.Info, $"数据采集完成; 总用时 {stopwatch.ElapsedMilliseconds}ms");
}
catch (Exception ex)
{
OnMessage(LogType.Error, $"数据采集过程中发生错误: {ex.Message}");
}
finally
{
isCollectingFlagLeft = false;
}
}
private void CollectAndProcessDataRight(AtlasScrew atlasScrew, string sn, string direction)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
int nRet = 0;
string strRet = "";
try
{
int fileCounter = 1; // 文件计数器,用于生成文件名中的序号
while (isCollectingFlagRight)
{
// 从缓存中获取所有未处理的数据
var cachedData = atlasScrew.GetCachedDataLeft();
foreach (var currentResult in cachedData)
{
if (currentResult.JD_MEAN == 0 || currentResult.NL_MEAN == 0)
{
continue; // 跳过无效数据
}
OnMessage(LogType.Info,
$"检测到新数据: JD_MEAN={currentResult.JD_MEAN}, NL_MEAN={currentResult.NL_MEAN}");
// 写入PLC
OP70_ScrewDataSet_t resultToPlC = new OP70_ScrewDataSet_t
{
fTorque = float.Parse(currentResult.NL_MEAN.ToString()),
fCircles = float.Parse(currentResult.JD_MEAN.ToString())
};
(nRet, strRet) = WriteResultToPlc(7, "", $"g_OP70_MES.{direction}.screwDriver", 1, resultToPlC);
if (nRet != 0)
{
OnMessage(LogType.Info,
$"SN:{sn}, JD_MEAN={currentResult.JD_MEAN}, NL_MEAN={currentResult.NL_MEAN} 写入PLC失败");
}
else
{
OnMessage(LogType.Info,
$"SN:{sn}, JD_MEAN={currentResult.JD_MEAN}, NL_MEAN={currentResult.NL_MEAN} 写入PLC成功");
}
// 构建保存路径
string dateFolder = DateTime.Now.ToString("yyyyMMdd");
string basePath = AppDomain.CurrentDomain.BaseDirectory;
string savePath = Path.Combine(basePath, "screw", dateFolder, sn, direction, "曲线");
Directory.CreateDirectory(savePath); // 确保目录存在
// 构建文件名(以 SN + 序号命名)
string fileName = $"{sn}_{fileCounter}.txt";
string filePath = Path.Combine(savePath, fileName);
// 写入文件
using (StreamWriter writer = new StreamWriter(filePath))
{
writer.WriteLine("精度, 扭力");
// 遍历 Pearkdegree 和 PearkTorque 的所有值
for (int i = 0;
i < currentResult.Pearkdegree.Length && i < currentResult.PearkTorque.Length;
i++)
{
double precision = Math.Round(currentResult.Pearkdegree[i]); // 精度
double torque = Math.Round(currentResult.PearkTorque[i], 2); // 扭力
writer.WriteLine($"{precision}, {torque}");
}
}
OnMessage(LogType.Info, $"保存文件成功: {filePath}");
// 增加文件计数器
fileCounter++;
}
// 如果没有更多数据,则短暂休眠以节省资源
if (!cachedData.Any())
{
Thread.Sleep(10); // 根据需要调整休眠时间
}
// 如果触发了出站,则退出循环
if (!isCollectingFlagRight)
{
break;
}
}
stopwatch.Stop();
OnMessage(LogType.Info, $"数据采集完成; 总用时 {stopwatch.ElapsedMilliseconds}ms");
}
catch (Exception ex)
{
OnMessage(LogType.Error, $"数据采集过程中发生错误: {ex.Message}");
}
finally
{
isCollectingFlagRight = false;
}
}
private void CopyFileToTempPath(string morepath,string temppath) {
string tempPath = GlobalContext.MqttFileTempDir; //临时存放图片的文件夹
var morePath = morepath.Split(",");
try
{
if (Directory.Exists(tempPath))
{
Directory.Delete(tempPath, recursive: true); // 清理临时目录
}
foreach (var item in morePath)
{
foreach (var file in Directory.GetFiles(item, "*.*", SearchOption.AllDirectories))
{
var relativePath = file.Substring(item.Length + 1);
var destFile = Path.Combine(temppath, relativePath);
Directory.CreateDirectory(Path.GetDirectoryName(destFile));
File.Copy(file, destFile, overwrite: true);
//删除文件
(bool, string) newResult = DeleteFile(file);
}
}
}
catch (Exception ex){
throw ex;
}
}
///
/// 压缩文件
///
/// 源文件地址
/// 目标地址
///
public static (int, string) CompressFolder(string folderPath, string zipFilePath)
{
try
{
// 创建zip文件,将指定文件夹内的所有内容压缩到这个zip文件中
System.IO.Compression.ZipFile.CreateFromDirectory(folderPath, zipFilePath);
return (1, "文件夹已成功压缩!");
}
catch (Exception ex)
{
return (0, "文件压缩出错!" + ex.Message);
}
}
///
/// 上传文件
///
/// 条码集合
/// 工站编号
/// 工站名称
/// 文件路径
public async Task<(int, string)> SaveDBbyFileInfo(BarcodeSet_t BarcodeSet, string stationCode,
string stationName, int pass, string path, string guid = "")
{
string sql, filename = "";
int result = 0;
var formData = new MultipartFormDataContent();
string msg = "";
string file_category = "IMAGE"; //IMAGE 、TEXT 、UNKNOWN
string file_type = "IMAGE";
string project = GlobalContext.ProgramName;
string run_mode = GlobalContext.run_mode;
string product_mode = GlobalContext.product_mode;
string pass_result = (pass == 1 ? "PASS" : "Fail");
string device_code = xiaomiParm.deviceCode;
string sn = BarcodeSet.strProductBarcode;
string staion_id = xiaomiParm.stationCode;
string bucket =
$"{file_category}/{file_type}/{project}/{product_mode}/{run_mode}/{pass_result}/{device_code}/{sn}/{staion_id}";
// 获取所有图片文件
if (guid == "")
{
guid = Guid.NewGuid().ToString();
}
try
{
if (GlobalContext.MESIsSendUpFile)
{
List imageFiles = GetAllImageFiles(path);
string toPath = GlobalContext.MqttFileBackupLogDir;
filename =
$"{xiaomiParm.workstation}_{file_type}_{sn}_{DateTime.Now.ToString("yyyyMMddHHmmss")}.zip";
//string directoryPath = Path.GetDirectoryName(path);
string strFilePath = toPath + "\\" + filename;
if (imageFiles.Count > 0)
{
var r = CompressFolder(path, strFilePath);
if (r.Item1 == 0)
{
return (0, r.Item2);
}
else
{
msg = r.Item2 + "\r\n";
}
}
else
{
return (0, "文件不存在!");
}
FileUpload_X5 fileUpload_X5 = new FileUpload_X5();
fileUpload_X5.bucket = bucket;
fileUpload_X5.name = filename;
fileUpload_X5.uuid = guid.ToString();
///需要上传文件
FileInfo file = new FileInfo(strFilePath);
string fileMd5Hex = GetMD5Hex(file);
fileUpload_X5.md5 = fileMd5Hex;
fileUpload_X5.uploadCloud = true;
fileUpload_X5.informMqtt = true;
FileMqttPayload fileMqttPayload = new FileMqttPayload();
fileMqttPayload.factory = GlobalContext.Factory_Code;
fileMqttPayload.project_name = GlobalContext.Project_Code;
fileMqttPayload.product_mode = GlobalContext.product_mode;
fileMqttPayload.line_no = GlobalContext.LineCode;
fileMqttPayload.work_station_no = xiaomiParm.workstation;
fileMqttPayload.equipment_no = xiaomiParm.deviceCode;
fileMqttPayload.station_no = xiaomiParm.stationCode;
fileMqttPayload.file_id = guid;
fileMqttPayload.file_name = filename;
fileMqttPayload.sn = BarcodeSet.strProductBarcode;
//fileMqttPayload.opt_time = "";
//fileMqttPayload.file_type = "";
//fileMqttPayload.file_category = "";
//fileMqttPayload.tag = "";
fileMqttPayload.reference_info.pass_station_id = uuid;
var fileresult = await
XiaomiMESHttp_UpLoadFile.FileUoladToMes(strFilePath, fileUpload_X5, fileMqttPayload);
if (fileresult.Item1 != 1)
{
return (0, msg + fileresult.Item2 + "\r\n");
}
else
{
msg = msg + fileresult.Item2 + "\r\n";
}
}
return (1, msg);
}
catch (Exception e)
{
return (0,
filename + $"文件上传错误!载具码:{BarcodeSet.strCarrierBarcode}产品码{BarcodeSet.strProductBarcode},错误原因:" +
e.Message);
}
}
///
/// 获取路径下的所有图片
///
///
///
public List GetAllImageFiles(string directoryPath)
{
var imageExtensions = new HashSet(StringComparer.OrdinalIgnoreCase)
{ ".jpg", ".jpeg", ".png", ".bmp", ".gif", ".tiff" };
var imageFiles = new List();
try
{
// 遍历目录及子目录中的所有文件
foreach (string file in Directory.EnumerateFiles(directoryPath, "*.*", SearchOption.AllDirectories))
{
// 获取文件扩展名并检查是否为图片格式
string extension = Path.GetExtension(file);
if (imageExtensions.Contains(extension))
{
imageFiles.Add(file);
}
}
}
catch (Exception ex)
{
OnMessage(LogType.Error, $"图片查询发生错误: {ex.Message}");
}
return imageFiles;
}
///
/// 实例化报警字典
///
private (bool, string) InitalDicAlarm()
{
#region 加载报警表
string excelPath = "";
switch (GlobalContext.IsUsePLCNow)
{
case 1:
excelPath = AppDomain.CurrentDomain.BaseDirectory + "DBFile\\小米-IoT报警字典_10_壳体清洁上料.xlsx";
break;
case 2:
excelPath = AppDomain.CurrentDomain.BaseDirectory + "DBFile\\小米-IoT报警字典_20_上盖板上料装备.xlsx";
break;
case 3:
excelPath = AppDomain.CurrentDomain.BaseDirectory + "DBFile\\小米-IoT报警字典_30_点散热胶装备.xlsx";
break;
case 4:
excelPath = AppDomain.CurrentDomain.BaseDirectory + "DBFile\\小米-IoT报警字典_40_胶线检测.xlsx";
break;
case 5:
excelPath = AppDomain.CurrentDomain.BaseDirectory + "DBFile\\小米-IoT报警字典_50_ADD板上料组装装备.xlsx";
break;
case 6:
excelPath = AppDomain.CurrentDomain.BaseDirectory + "DBFile\\小米-IoT报警字典_60_组上盖板.xlsx";
break;
case 7:
excelPath = AppDomain.CurrentDomain.BaseDirectory + "DBFile\\小米-IoT报警字典_70_上盖板锁螺丝.xlsx";
break;
case 8:
excelPath = AppDomain.CurrentDomain.BaseDirectory + "DBFile\\小米-IoT报警字典_80_NG下料.xlsx";
break;
case 9:
excelPath = AppDomain.CurrentDomain.BaseDirectory + "DBFile\\小米-IoT报警字典_90_半成品下料.xlsx";
break;
default:
return (false, $"不支持当前PLC[{GlobalContext.IsUsePLCNow}]的报警点位表!请在设置页切换到正确的plc后重启该软件!");
}
if (!File.Exists(excelPath))
return (false, $"未找到当前PLC[{GlobalContext.IsUsePLCNow}]的报警点位表!请检查文件路径:'{excelPath}'。");
DataTable DtModel = NPOIHelper.ReadExcel(excelPath);
if (DtModel == null || DtModel.Rows.Count < 1)
return (false, $"报警点位表未包含任何报警数据!请检查‘{excelPath}’文件!");
// 检查列名
List isNeedParms = new List()
{
"设备分类名称", "设备分类编码", "分类层级1", "分类层级2", "分类层级3", "分类层级4", "事件名称", "事件ID", "描述", "标签", "PLC地址", "工位"
}; // 必须要有的列
// 指定列名 + 检查列是否完整
DataRow dtRowName = DtModel.Rows[0];
int count = DtModel.Columns.Count;
for (int i = 0; i < count; i++)
{
string columeName = dtRowName[i]?.ToString();
if (!string.IsNullOrEmpty(columeName))
DtModel.Columns[i].ColumnName = columeName;
if ((!string.IsNullOrEmpty(columeName)) && isNeedParms.Count > 0 && isNeedParms.Contains(columeName))
isNeedParms.Remove(columeName);
}
DtModel.Rows.RemoveAt(0);
if (isNeedParms.Count > 0)
{
string msg1 = string.Join(",", isNeedParms);
return (false, $"报警点位表未包含列:{msg1}!请检查‘{excelPath}’文件!");
}
#endregion 加载报警表
List keyValues1 = new List();
for (int i = 0; i < DtModel.Rows.Count; i++)
{
Alarm alarm = new Alarm();
alarm.plcName = DtModel.Rows[i]["设备分类名称"]?.ToString(); // 设备名;
string type1Str = DtModel.Rows[i]["分类层级1"]?.ToString(); // 分类层级1;
alarm.type1CH = type1Str; // 分类层级1 中文
alarm.type1 = type1Str; // 分类层级1
if (!string.IsNullOrEmpty(type1Str) && type1Str.Contains("\n"))
{
string[] type1Strs = type1Str.Split('\n');
if (type1Strs.Length >= 2)
{
alarm.type1CH = type1Strs[0].Trim(); // 分类层级1 中文
alarm.type1 = type1Strs[1].Trim(); // 分类层级1
if (string.IsNullOrEmpty(alarm.type1))
alarm.type1 = alarm.type1CH;
}
}
string type2Str = DtModel.Rows[i]["分类层级2"]?.ToString(); // 分类层级2;电气控制 electric_control
alarm.type2CH = type2Str; // 分类层级2 中文
alarm.type2 = type2Str; // 分类层级2
if (!string.IsNullOrEmpty(type2Str) && type2Str.Contains("\n"))
{
string[] type2Strs = type2Str.Split('\n');
if (type2Strs.Length >= 2)
{
alarm.type2CH = type2Strs[0].Trim(); // 分类层级2 中文
alarm.type2 = type2Strs[1].Trim(); // 分类层级2
if (string.IsNullOrEmpty(alarm.type2))
alarm.type2 = alarm.type2CH;
}
}
string type3Str = DtModel.Rows[i]["分类层级3"]?.ToString(); // 分类层级3;故障 Fault
alarm.type3CH = type3Str; // 分类层级3 中文
alarm.type3 = type3Str; // 分类层级3
if (!string.IsNullOrEmpty(type3Str) && type3Str.Contains("\n"))
{
string[] type3Strs = type3Str.Split('\n');
if (type3Strs.Length >= 2)
{
alarm.type3CH = type3Strs[0].Trim(); // 分类层级3 中文
alarm.type3 = type3Strs[1].Trim(); // 分类层级3
if (string.IsNullOrEmpty(alarm.type3))
alarm.type3 = alarm.type3CH;
}
}
string faultStr = DtModel.Rows[i]["分类层级4"]?.ToString(); // 故障部件;overall_module
alarm.type4 = faultStr; // 分类层级4 中文
alarm.type4CH = faultStr; // 分类层级4
if (!string.IsNullOrEmpty(faultStr) && faultStr.Contains("\n"))
{
string[] faultStrs = faultStr.Split('\n');
if (faultStrs.Length >= 2)
{
alarm.type4CH = faultStrs[0].Trim(); // 故障部件;overall_module
alarm.type4 = faultStrs[1].Trim(); // 故障部件
if (string.IsNullOrEmpty(alarm.type4))
alarm.type4 = alarm.type4CH;
}
}
alarm.fault_code = DtModel.Rows[i]["事件ID"]?.ToString(); // 故障编码;A40001
alarm.fault_name = DtModel.Rows[i]["事件名称"]?.ToString(); // 故障名称;AL[1000]_系统_HMI急停故障
alarm.fault_desc = alarm.fault_name; // 故障描述;AL[1000]_系统_HMI急停故障
string plcAdress = DtModel.Rows[i]["PLC地址"]?.ToString();
alarm.PLC地址 = plcAdress;
plcAdress = plcAdress.Replace("fault_codes[", "");
plcAdress = plcAdress.Replace("]", "");
alarm.group_index = Convert.ToInt32(plcAdress.Split(".")[0]);
keyValues1.Add(alarm);
}
DicAlarms_Cur.Add(GlobalContext.IsUseStationName, keyValues1); // 这里使用线体代替工位
return (true, "报警字典表初始化成功!");
}
private async void ReadPLCAlarmToIot(uint[] FaultData, uint[] FaultDataOld, string stationNameStr,string flag)
{
DateTime dtNow = DateTime.Now;
try
{
List deviceAlarm_Curs = new List(); // 同步到报警页面用传输载体
List<(int, int)> AlarmIndexList = new List<(int, int)>(); //收集所有报警信息的位置
AlarmData alarmData = new AlarmData();
bool isNeedUpdUI = false; // 是否需要更新历史报警UI
bool isNoAlarm = false; //是否有报警
string binaryString = "";
bool isNoNewAlarm = false; //是否有新的报警
if (FaultData.Length > 0)
{
//foreach (var item in FaultData)
//{
// if (item > 0)
// {
// isNoAlarm = true;
// break;
// }
// else {
// isNoAlarm = false;
// }
//}
//如果警报都是0,则跳过解析
if (FaultData?.All(x => x == 0) ?? true)
{
if (flag == "left")
{
_FaultDatas_Old = FaultData.ToArray();
}
else
{
_FaultDatas_Old2 = FaultData.ToArray();
}
return;
}
if (!FaultData.SequenceEqual(FaultDataOld))
{
isNoNewAlarm = true;
isNeedUpdUI = true;
}
//if (FaultData.Length > 0 && isNoAlarm && isNoNewAlarm)
if (FaultData.Length > 0 && isNoNewAlarm)
{
//解析报警信息,分析当前报警在字典中的定位
for (int i = 0; i <= FaultData.Length - 1; i++)
{
var num = 0;
if (FaultData[i] > 0)
{
//转换二进制
binaryString = Convert.ToString(FaultData[i], 2);
for (int j = binaryString.Length - 1; j >= 0; j--)
{
num++;
char s = binaryString[j];
if (binaryString[j] == '1')
{
//记录1的位置
AlarmIndexList.Add((i, num - 1));
}
}
}
}
// 同步“设备报警信息”到“设备报警临时字典DicAlarms_Cur”
var dicAlarms_Cur_PLC1 = DicAlarms_Cur[GlobalContext.IsUseStationName];
foreach ((int index, int row) in AlarmIndexList)
{
var tempDic = dicAlarms_Cur_PLC1.Where(x => x.group_index == index).ToList();
for (int i = 0; i < tempDic.Count - 1; i++) // 读取
{
//若报警字典第[group_index]个数据,第[i]行与AlarmIndexList中的定位匹配,则添加进对应行的报警数据
if (tempDic[i].group_index == index && i == row)
{
dicAlarms_Cur_PLC1.Where(x => x.group_index == index).ToList()[row].报警数据 =
new AlarmData()
{
GUID = Guid.NewGuid().ToString(),
LineName = GlobalContext.IsUseStationName, // 工站
PlcStation = tempDic[i].plcName, // 工站全称;[S1]
Type1 = tempDic[i].type1, // 故障层级1
Type2 = tempDic[i].type2, // 故障层级2
Type3 = tempDic[i].type3, // 故障层级3
Type4 = tempDic[i].type4, // 故障层级4
AlarmType = tempDic[i].fault_code, // 报警类型
AlarmDesc = tempDic[i].fault_name, // 报警内容
StartTime = dtNow // 开始时间
};
// 传输到页面
deviceAlarm_Curs.Add(new DeviceAlarm_Cur()
{
线体名称 = tempDic[i].plcName,
报警类型 = tempDic[i].fault_code,
报警内容 = tempDic[i].fault_name,
开始时间 = dtNow
});
// 新增到数据库
//var data1 = dicAlarms_Cur_PLC1.Where(x => x.group_index == index).ToList()[row].报警数据;
//SaveAlarmDataByDB(stationNameStr, data1, false);
//OnMessage(LogType.Info, $"更新{BodyAlarm}完毕!");
}
}
}
//筛选含报警数据的字典
var dicAlarms_Cur = dicAlarms_Cur_PLC1.Where(x => x.报警数据 != null).ToList();
if (dicAlarms_Cur.Count > 0)
{
string stationId = "";
foreach (var item in dicAlarms_Cur)
{
if (!string.IsNullOrEmpty(xiaomiParm.stationCode))
{
stationId = xiaomiParm.stationCode;
//上传
SaveAlarmDataByDB(GlobalContext.IsUseStationName, stationId, item.报警数据, false);
}
else {
if (flag == "left")
{
if (GlobalContext.IsUsePLC3)
stationId = GlobalContext.s3_1_station;
if (GlobalContext.IsUsePLC7)
stationId = GlobalContext.s7_1_station;
//上传
SaveAlarmDataByDB(GlobalContext.IsUseStationName, stationId, item.报警数据, false);
}
else {
if (GlobalContext.IsUsePLC3)
stationId = GlobalContext.s3_2_station;
if (GlobalContext.IsUsePLC7)
stationId = GlobalContext.s7_2_station;
//上传
SaveAlarmDataByDB(GlobalContext.IsUseStationName, stationId, item.报警数据, false);
}
}
}
}
// 有新报警则更新
if (isNeedUpdUI)
// UI展示 - 展示到设备状态页
await Task.Run(() =>
{
try
{
if (Form_Main.formDevAlarm != null && !Form_Main.formDevAlarm.IsDisposed)
{
Form_Main.formDevAlarm.UpdDeviceAlarm_Cur(deviceAlarm_Curs); // 报警UI 更新
if (Form_Main.formDevAlarm.Visible)
Form_Main.formDevAlarm.UpdDeviceAlarm_History_48H(); // 历史报警UI 更新
}
}
catch
{
}
});
if (flag == "left")
{
_FaultDatas_Old = FaultData.ToArray();
}
else {
_FaultDatas_Old2 = FaultData.ToArray();
}
}
}
//FaultLogRequest request = new FaultLogRequest();
//request.station = mesStation; // 工位
//request.fault_name = xmFaultName; // 故障名称(同数据字典中的事件名称)
//request.fault_code = xmFaultCode2; // 故障编码(A,B,C,D,E)
//request.fault_cmpnt = xmFaultCmpnt; // 故障部件
//request.fault_desc = xmFaultDesc; // 故障描述
//request.fault_tm = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); // 故障发生时间 2022-06-01 14:27:57.283
// // 上传
//(int, string) iotResult = XiaomiMqttClient_Extend.Write_FaultLog(request, type1, type2, type3, request.fault_cmpnt, deciveCode);
}
catch (Exception ex)
{
string str = ex.StackTrace;
AddMessage_Station(stationNameStr, LogType.Error,
$"{stationNameStr}_获取报警数据出错!错误信息:" + ex.Message.ToString() + "异常位置:" +
str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1));
}
}
public void operateToIot(string action, string stationNameStr)
{
OperateLogRequest request = new OperateLogRequest();
request.software_version = "V" + Application.ProductVersion; // 软件版本号;如:V1.2.4
request.operate_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); // 操作时间(2022-06-01 14:27:57.283)
request.operate_action = action; // 操作动作(对应软件开启/关闭/重新加载项⽬;startup、shutdown、reload)
//request.current_process = Process.GetCurrentProcess()?.Id.ToString(); // 当前进程;进程ID
request.current_process = Application.ProductName;
request.operate_desc = stationNameStr; // 操作描述;如:供应商软件开启/关闭/重新加载项⽬
request.operate_result = "success"; // 操作结果
request.operator_name = "default"; // 操作账号名;填当前操作⽤⼾,如⽆则填default
// 上传
(int, string) iotResult = XiaomiMqttClient_Extend.Write_OperateLog(request);
if (iotResult.Item1 != 0)
{
AddMessage(LogType.Info, "【操作记录】上传失败!错误原因:" + iotResult.Item2 + "操作状态:" + stationNameStr);
}
}
public void deviceConfigToIot(string id, string value)
{
DeviceConfigRequest request = new DeviceConfigRequest();
request.id = id; // 操作时间(2022-06-01 14:27:57.283)
request.value = value; // 操作动作(对应软件开启/关闭/重新加载项⽬;startup、shutdown、reload)
// 上传
(int, string) iotResult = XiaomiMqttClient_Extend.Write_deviceConfig(request);
if (iotResult.Item1 != 0)
{
AddMessage(LogType.Info, "【装备配置】上传失败!错误原因:" + iotResult.Item2 );
}
}
private static readonly object _oeeLock = new object();
private void 通用节拍接口(int plcNo, string stationNameStr, string tagMesCommName, string CarrierBarcode,
IoT_DataSet_t iot_data, out int res)
{
lock (_oeeLock) // 保证同一时间只有一个任务执行
{
Stopwatch stopwatch1 = new Stopwatch();
Stopwatch stopwatch2 = new Stopwatch();
string resultStr = string.Empty;
try
{
string oEEType = iot_data.beatAction.ToString(); // 节拍类型(plc写入)
string strProductBarcode = SQLHelper.GetProductBarcodeByCarrierCode(CarrierBarcode); //产品SN
bool actionBool = Enum.TryParse(oEEType, out XiaomiDeviceOEE deviceOEE);
(short, string) result = (0, "");
// 上传开始节拍 节拍只需要PLC传1、3、4、5
result = SaveOEEData(plcNo, stationNameStr, deviceOEE, strProductBarcode, CarrierBarcode);
//上传结束节拍
switch (oEEType)
{
case "1":
Enum.TryParse("2", out deviceOEE);
result = SaveOEEData(plcNo, stationNameStr, deviceOEE, strProductBarcode, CarrierBarcode);
break;
case "5":
Enum.TryParse("6", out deviceOEE);
result = SaveOEEData(plcNo, stationNameStr, deviceOEE, strProductBarcode, CarrierBarcode);
break;
}
if (result.Item1 == 1)
{
OnMessage(LogType.Info, $"PLC{plcNo}_{stationNameStr} 节拍[{deviceOEE}]上传成功!");
//写入PLC
IoT_DataSet_t iotData = new IoT_DataSet_t();
iotData.machineState = iot_data.machineState;
iotData.work_type = iot_data.work_type;
iotData.testStatus = iot_data.testStatus;
iotData.beatAction = 0;
iotData.beatReturn = 1; //OK
iotData.fault_codes = iot_data.fault_codes;
(int, string) PLCresult = WriteResultToPlc(plcNo, stationNameStr, tagMesCommName, 1, iotData);
if (PLCresult.Item1 == 0)
{
res = 1;
OnMessage(LogType.Info, $"PLC{plcNo}_{stationNameStr} 节拍[{deviceOEE}]写入成功!");
}
else
{
res = 0;
}
}
else
{
res = 0;
OnMessage(LogType.Info, $"PLC{plcNo}_{stationNameStr} 节拍[{deviceOEE}]接口出错!错误信息:" + result.Item2);
//写入PLC
IoT_DataSet_t iotData = new IoT_DataSet_t();
iotData.machineState = iot_data.machineState;
iotData.work_type = iot_data.work_type;
iotData.testStatus = iot_data.testStatus;
iotData.beatAction = 0;
iotData.beatReturn = 2; //NG
iotData.fault_codes = iot_data.fault_codes;
(int, string) PLCresult = WriteResultToPlc(plcNo, stationNameStr, tagMesCommName, 1, iotData);
}
}
catch (Exception ex)
{
res = 0;
string str = ex.StackTrace;
AddMessage_Station(stationNameStr, LogType.Error,
$"PLC{plcNo}_{stationNameStr} 节拍接口出错!错误信息:" + ex.Message + "异常位置:" +
str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1));
//写入PLC
IoT_DataSet_t iotData = new IoT_DataSet_t();
iotData.machineState = iot_data.machineState;
iotData.work_type = iot_data.work_type;
iotData.testStatus = iot_data.testStatus;
iotData.beatAction = 0;
iotData.beatReturn = 2; //NG
iotData.fault_codes = iot_data.fault_codes;
(int, string) PLCresult = WriteResultToPlc(plcNo, stationNameStr, tagMesCommName, 1, iotData);
}
}
}
///
/// 去除常见特殊字符,如 \r, \n, \t 等
///
///
///
private static string FormatStrbyPLC(string format)
{
string cleanedString = "";
if (!string.IsNullOrEmpty(format))
{
cleanedString = Regex.Replace(format, @"[\r\n\t]", "");
}
return cleanedString;
}
private void S1_OpenDialog(int plcNo,string stationNameStr,string tagBaseName,string tagMesCommName) {
Task.Run(() =>
{
using (var dialog = new Dialog_BandBarode_S1())
{
var rs = dialog.ShowDialog();
if (rs == DialogResult.OK)
{
//绑定载具和产品
ResponseMessage message = new ResponseMessage();
message = SQLHelper.InsertCarrierBind(_strCarrierBarcode, _strProductBarcode);
if (message.result == false)
{
AddMessage(LogType.Error, stationNameStr + "_载具码与产品码绑定失败,错误:" + message.text);
CommandFromPLC CommandToPlC = new CommandFromPLC();
CommandToPlC.cmd = 0;
CommandToPlC.cmdParam = 5;
CommandToPlC.cmdResult = 0;
WriteResultToPlc(plcNo, stationNameStr, tagBaseName + "." + tagMesCommName, 1, CommandToPlC);
return;
}
else
{
AddMessage(LogType.Error, stationNameStr + "_载具码与产品码绑定成功");
CommandFromPLC CommandToPlC = new CommandFromPLC();
CommandToPlC.cmd = 0;
CommandToPlC.cmdParam = 4;
CommandToPlC.cmdResult = 0;
WriteResultToPlc(plcNo, stationNameStr, tagBaseName + "." + tagMesCommName, 1, CommandToPlC);
}
AddMessage(LogType.Info, $"载具码:{_strCarrierBarcode},产品码:{_strProductBarcode}");
//StopWhile = false;//开启while循环
}
}
});
}
///
/// 连接数组元素
///
///
///
private static string ArrayToString(Array array)
{
// 使用 string.Join 连接数组元素
return string.Join(", ", array.Cast