|
|
using Custom.Communication.Framework.MyPlc;
|
|
|
using Custom.Utils.Framework;
|
|
|
using Newtonsoft.Json;
|
|
|
using ProductionSystem.Common;
|
|
|
using ProductionSystem_Log;
|
|
|
using ProductionSystem_Model.DbModel;
|
|
|
using ProductionSystem_Model.Enum;
|
|
|
using ProductionSystem_Model.ViewModel.Response.Para;
|
|
|
using ProductionSystem_Service;
|
|
|
using SqlSugar;
|
|
|
using System;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
using System.Threading;
|
|
|
using System.Threading.Tasks;
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
namespace ProductionSystem.Untils
|
|
|
{
|
|
|
public class DoSomething
|
|
|
{
|
|
|
private readonly ResultService _resultService = new ResultService();
|
|
|
private readonly ParaConfigService _paraConfigService = new ParaConfigService();
|
|
|
private static string iniFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SystemConfig.ini");
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 轮询读取PLC数据
|
|
|
/// </summary>
|
|
|
/// <param name="form"></param>
|
|
|
public void ReadPlc()
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
Task.Factory.StartNew(() =>
|
|
|
{
|
|
|
while (true)
|
|
|
{
|
|
|
Thread.Sleep(Program.ReadPlcRate);
|
|
|
// DataChange();
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
var errMsg = ex == null ? "未知异常" : ex.Message;
|
|
|
MessageBox.Show($"DoSomething中ReadPlc方法出发异常:{errMsg},请联系管理员!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private PointKeyValue M249 = Program.CommandPointKeyValues.FirstOrDefault(m => m.Key == "M249");
|
|
|
//是否存数据库
|
|
|
private PointKeyValue M283 = Program.CommandPointKeyValues.FirstOrDefault(m => m.Key == "M283");
|
|
|
//条码值
|
|
|
private PointKeyValue M248 = Program.CommandPointKeyValues.FirstOrDefault(m => m.Key == "M248");
|
|
|
//数据存库反馈点位
|
|
|
private PointKeyValue point = Program.CommandPointKeyValues.FirstOrDefault(m => m.Key == "M284");
|
|
|
|
|
|
///// <summary>
|
|
|
///// 读取PLC数据,并进行处理
|
|
|
///// </summary>
|
|
|
///// <param name="form"></param>
|
|
|
//private void DataChange()
|
|
|
//{
|
|
|
// try
|
|
|
// {
|
|
|
// if (Program.ProtocolDeviceHelper == null)
|
|
|
// {
|
|
|
// Program.ProtocolDeviceHelper = new ProtocolDeviceHelper();
|
|
|
// }
|
|
|
// if (Program.CommandPointKeyValues != null && Program.CommandPointKeyValues.Count > 0)
|
|
|
// {
|
|
|
// var strSaveDb = OmronHelper.GetPlcVal(M283.DataType, M283.Address).val;
|
|
|
// if (strSaveDb == "PLC连接异常")
|
|
|
// {
|
|
|
// strSaveDb = "0";
|
|
|
// }
|
|
|
// //读取配置文件
|
|
|
// var saveDbData = IniHelper.Read("SaveDb", "SaveDbNum", "", iniFilePath);
|
|
|
// // LogHelper.Info($"PLCstrSaveDb={strSaveDb},配置文件SaveDbNum={saveDbData}");
|
|
|
// //strVal值变化时才存库
|
|
|
// if (!string.IsNullOrWhiteSpace(strSaveDb) && Convert.ToInt32(Convert.ToDecimal(strSaveDb)) != Convert.ToInt32(Convert.ToDecimal(saveDbData)) && Program.canSaveDb)
|
|
|
// {
|
|
|
// Program.canSaveDb = false;
|
|
|
// saveDbData = strSaveDb;
|
|
|
// var productCode = OmronHelper.GetPlcVal(M248.DataType, M248.Address).val.TrimEnd('\r');
|
|
|
// if (string.IsNullOrEmpty(productCode))
|
|
|
// {
|
|
|
// Program.canSaveDb = true;
|
|
|
// var errorMsg = "存本地数据库时,条码值为空!";
|
|
|
// LogHelper.Error(null, errorMsg);
|
|
|
// OmronHelper.WriteToPlc(point.DataType, point.Address, "2");
|
|
|
// return;
|
|
|
// }
|
|
|
// //写入配置文件
|
|
|
// IniHelper.Write("SaveDb", "SaveDbNum", strSaveDb, iniFilePath);
|
|
|
// SaveDb(productCode);
|
|
|
// }
|
|
|
|
|
|
// //步序
|
|
|
// var m249Result = OmronHelper.GetPlcVal(M249?.DataType, M249.Address);
|
|
|
// if (!m249Result.ok)
|
|
|
// {
|
|
|
// var errorMsg = $"DoSomething方法中读取步序值异常:{m249Result.val}!";
|
|
|
// LogHelper.Error(null, errorMsg);
|
|
|
// return;
|
|
|
// }
|
|
|
// var intM249 = Convert.ToInt32(Convert.ToDecimal(m249Result.val));
|
|
|
// //如果为待机状态则不往下走了
|
|
|
// if (intM249 == (int)StepEnum.Standby)
|
|
|
// {
|
|
|
// return;
|
|
|
// }
|
|
|
|
|
|
// foreach (var plcPoint in Program.CommandPointKeyValues)
|
|
|
// {
|
|
|
// var plcResult = OmronHelper.GetPlcVal(plcPoint.DataType, plcPoint.Address, (ushort)plcPoint.Length);
|
|
|
// if (plcResult.ok)
|
|
|
// {
|
|
|
// string strVal = plcResult.val;
|
|
|
|
|
|
// switch (plcPoint.Key)
|
|
|
// {
|
|
|
// //空载请求打开电机
|
|
|
// case "M260":
|
|
|
// if (!string.IsNullOrWhiteSpace(strVal) && strVal == "1.000")
|
|
|
// {
|
|
|
// if (intM249 == (int)StepEnum.Idling)
|
|
|
// {
|
|
|
// var para = _paraConfigService.QueryActiveParaConfigsByProductType(Program.ProductType)
|
|
|
// .First(m => m.ParaCategory == StepEnum.Idling.ToString() && m.ParaCode.Contains("SetSpeed"));
|
|
|
|
|
|
// if (para == null)
|
|
|
// {
|
|
|
// LogHelper.Error(null, "空载请求打开电机时,未获取到设定转速!");
|
|
|
// continue;
|
|
|
// }
|
|
|
// var point = Program.CommandPointKeyValues.FirstOrDefault(m => m.Key == "M261");
|
|
|
// if (point == null)
|
|
|
// {
|
|
|
// LogHelper.Error(null, "空载请求打开电机时,未获取到PLC反馈点位!");
|
|
|
// continue;
|
|
|
// }
|
|
|
// ProtocolCommunicate(StepEnum.Idling.ToString(), para.TargetVal.Value, point);
|
|
|
// }
|
|
|
// }
|
|
|
// break;
|
|
|
// //一段速请求打开电机
|
|
|
// case "M262":
|
|
|
// if (!string.IsNullOrWhiteSpace(strVal) && strVal == "1.000")
|
|
|
// {
|
|
|
// Program.sendManualReset.Set();
|
|
|
// Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss fff")}一段速请求打开电机!");
|
|
|
// var para = _paraConfigService.QueryActiveParaConfigsByProductType(Program.ProductType)
|
|
|
// .First(m => m.ParaCategory == StepEnum.SpeedUp1.ToString() && m.ParaCode.Contains("SetSpeed"));
|
|
|
// if (para == null)
|
|
|
// {
|
|
|
// LogHelper.Error(null, "一段速请求打开电机时,未获取到设定转速!");
|
|
|
// continue;
|
|
|
// }
|
|
|
// var point = Program.CommandPointKeyValues.FirstOrDefault(m => m.Key == "M263");
|
|
|
// if (point == null)
|
|
|
// {
|
|
|
// LogHelper.Error(null, "一段速请求打开电机时,未获取到PLC反馈点位!");
|
|
|
// continue;
|
|
|
// }
|
|
|
// ProtocolCommunicate(StepEnum.SpeedUp1.ToString(), para.TargetVal.Value, point);
|
|
|
// }
|
|
|
// break;
|
|
|
// //二段升速请求打开电机
|
|
|
// case "M264":
|
|
|
// if (!string.IsNullOrWhiteSpace(strVal) && strVal == "1.000")
|
|
|
// {
|
|
|
// Program.sendManualReset.Set();
|
|
|
// Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss fff")}二段速请求打开电机!");
|
|
|
|
|
|
// var para = _paraConfigService.QueryActiveParaConfigsByProductType(Program.ProductType)
|
|
|
// .First(m => m.ParaCategory == StepEnum.SpeedUp2.ToString() && m.ParaCode.Contains("SetSpeed"));
|
|
|
|
|
|
// if (para == null)
|
|
|
// {
|
|
|
// LogHelper.Error(null, "二段升速请求打开电机时,未获取到设定转速!");
|
|
|
// continue;
|
|
|
// }
|
|
|
// var point = Program.CommandPointKeyValues.FirstOrDefault(m => m.Key == "M265");
|
|
|
// if (point == null)
|
|
|
// {
|
|
|
// LogHelper.Error(null, "二段升速请求打开电机时,未获取到PLC反馈点位!");
|
|
|
// continue;
|
|
|
// }
|
|
|
// ProtocolCommunicate(StepEnum.SpeedUp2.ToString(), para.TargetVal.Value, point);
|
|
|
|
|
|
// }
|
|
|
// break;
|
|
|
// //三段升速请求打开电机
|
|
|
// case "M266":
|
|
|
// if (!string.IsNullOrWhiteSpace(strVal) && strVal == "1.000")
|
|
|
// {
|
|
|
// Program.sendManualReset.Set();
|
|
|
// var para = _paraConfigService.QueryActiveParaConfigsByProductType(Program.ProductType)
|
|
|
// .First(m => m.ParaCategory == StepEnum.SpeedUp3.ToString() && m.ParaCode.Contains("SetSpeed"));
|
|
|
|
|
|
// if (para == null)
|
|
|
// {
|
|
|
// LogHelper.Error(null, "三段升速请求打开电机时,未获取到设定转速!");
|
|
|
// continue;
|
|
|
// }
|
|
|
// var point = Program.CommandPointKeyValues.FirstOrDefault(m => m.Key == "M267");
|
|
|
// if (point == null)
|
|
|
// {
|
|
|
// LogHelper.Error(null, "三段升速请求打开电机时,未获取到PLC反馈点位!");
|
|
|
// continue;
|
|
|
// }
|
|
|
// ProtocolCommunicate(StepEnum.SpeedUp3.ToString(), para.TargetVal.Value, point);
|
|
|
// }
|
|
|
// break;
|
|
|
// //内漏请求关闭电机
|
|
|
// case "M268":
|
|
|
// if (!string.IsNullOrWhiteSpace(strVal) && strVal == "1.000")
|
|
|
// {
|
|
|
// if (intM249 == (int)StepEnum.Endolead)
|
|
|
// {
|
|
|
// var para = _paraConfigService.QueryActiveParaConfigsByProductType(Program.ProductType)
|
|
|
// .First(m => m.ParaCategory == StepEnum.Endolead.ToString() && m.ParaCode.Contains("SetSpeed"));
|
|
|
// if (para == null)
|
|
|
// {
|
|
|
// LogHelper.Error(null, "内漏请求关闭电机时,未获取到设定转速!");
|
|
|
// continue;
|
|
|
// }
|
|
|
// //串口停止发送报文
|
|
|
// Program.isCanSend = false;
|
|
|
// //发送停止报文
|
|
|
// Program.ProtocolDeviceHelper.SendStop(Program.ProtocolConigCode, (MsgFormatEnum)Enum.Parse(typeof(MsgFormatEnum), Program.MsgFormat));
|
|
|
// var point = Program.CommandPointKeyValues.FirstOrDefault(m => m.Key == "M269");
|
|
|
// if (point == null)
|
|
|
// {
|
|
|
// LogHelper.Error(null, "内漏请求关闭电机时,未获取到PLC反馈点位!");
|
|
|
// continue;
|
|
|
// }
|
|
|
// ProtocolCommunicate(StepEnum.Endolead.ToString(), para.TargetVal.Value, point);
|
|
|
// }
|
|
|
// }
|
|
|
// break;
|
|
|
// //单驱模式请求打开电机
|
|
|
// case "M270":
|
|
|
// if (!string.IsNullOrWhiteSpace(strVal) && strVal == "1.000")
|
|
|
// {
|
|
|
// if (intM249 == (int)StepEnum.SingleModel)
|
|
|
// {
|
|
|
// var para = _paraConfigService.QueryActiveParaConfigsByProductType(Program.ProductType)
|
|
|
// .First(m => m.ParaCategory == StepEnum.SingleModel.ToString() && m.ParaCode.Contains("SetSpeed"));
|
|
|
|
|
|
// if (para == null)
|
|
|
// {
|
|
|
// LogHelper.Error(null, "单驱模式请求打开电机时,未获取到设定转速!");
|
|
|
// continue;
|
|
|
// }
|
|
|
// var point = Program.CommandPointKeyValues.FirstOrDefault(m => m.Key == "M271");
|
|
|
// if (point == null)
|
|
|
// {
|
|
|
// LogHelper.Error(null, "单驱模式请求打开电机时,未获取到PLC反馈点位!");
|
|
|
// continue;
|
|
|
// }
|
|
|
// ProtocolCommunicate(StepEnum.SingleModel.ToString(), para.TargetVal.Value, point);
|
|
|
// }
|
|
|
// }
|
|
|
// break;
|
|
|
// default:
|
|
|
// break;
|
|
|
// }
|
|
|
// plcPoint.Val = strVal;
|
|
|
// }
|
|
|
// else
|
|
|
// {
|
|
|
// //LogHelper.Error(null, $"读取PLC-{plcPoint.Key}-{plcPoint.Address}值失败!");
|
|
|
// }
|
|
|
// }
|
|
|
// }
|
|
|
// }
|
|
|
// catch (Exception ex)
|
|
|
// {
|
|
|
// var errMsg = ex == null ? "未知异常" : ex.Message;
|
|
|
// LogHelper.Error(ex, errMsg);
|
|
|
// }
|
|
|
//}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 调用协议通讯
|
|
|
/// </summary>
|
|
|
/// <param name="paraCategoryCode"></param>
|
|
|
/// <param name="targetVal"></param>
|
|
|
/// <param name="pointKeyValue"></param>
|
|
|
private void ProtocolCommunicate(string paraCategoryCode, decimal targetVal, PointKeyValue pointKeyValue)
|
|
|
{
|
|
|
switch (Program.ProtocolType)
|
|
|
{
|
|
|
case "PWM":
|
|
|
var result = PWMHelper.PWMCommunication(paraCategoryCode, targetVal, point);
|
|
|
if (!result.isOk)
|
|
|
{
|
|
|
LogHelper.Error(null, $"{paraCategoryCode}请求打开电机时,{result.msg}!");
|
|
|
}
|
|
|
break;
|
|
|
case "CAN":
|
|
|
Program.ProtocolDeviceHelper.Run(Program.ProtocolConigCode, (MsgFormatEnum)Enum.Parse(typeof(MsgFormatEnum), Program.MsgFormat), paraCategoryCode, pointKeyValue);
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 保存数据到数据库
|
|
|
/// </summary>
|
|
|
/// <param name="productCode"></param>
|
|
|
private void SaveDb(string productCode)
|
|
|
{
|
|
|
LogHelper.Info($"产品【{productCode}】请求存储数据库!");
|
|
|
try
|
|
|
{
|
|
|
if (Program.type == null)
|
|
|
{
|
|
|
//获取类型
|
|
|
Program.type = typeof(T_Result);
|
|
|
Program.properties = Program.type.GetProperties();
|
|
|
}
|
|
|
//创建对象
|
|
|
var result = Activator.CreateInstance(Program.type);
|
|
|
|
|
|
var saveDbList = Program.TopPointKeyValues.Union(Program.MiddlePointKeyValues).Where(m => m.IsSaveDb).ToList();
|
|
|
|
|
|
if (saveDbList.Count > 0)
|
|
|
{
|
|
|
foreach (var keyValItem in saveDbList)
|
|
|
{
|
|
|
var prop = Program.properties.FirstOrDefault(m => m.Name == keyValItem.ResultFieldName.Replace("column", "Column"));
|
|
|
if (prop != null && !string.IsNullOrWhiteSpace(keyValItem.Val))
|
|
|
{
|
|
|
if (keyValItem.IsTestItem && (keyValItem.KeyDesc.Contains("测试结果") || keyValItem.KeyDesc.Contains("状态")))
|
|
|
{
|
|
|
var valItem = keyValItem?.Val;
|
|
|
if (string.IsNullOrEmpty(valItem))
|
|
|
{
|
|
|
valItem = "0";
|
|
|
}
|
|
|
else if (valItem.Contains("."))
|
|
|
{
|
|
|
valItem = valItem.Substring(0, valItem.LastIndexOf("."));
|
|
|
}
|
|
|
var intVal = Convert.ToInt32(valItem);
|
|
|
|
|
|
var enumVal = EnumHelper.GetEnumByValue(typeof(StatusEnum), intVal);
|
|
|
keyValItem.Val = EnumHelper.GetEnumDescription(enumVal);
|
|
|
}
|
|
|
prop.SetValue(result, Convert.ChangeType(keyValItem.Val, prop.PropertyType));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
var t_Result = result as T_Result;
|
|
|
t_Result.Id = Guid.NewGuid().ToString();
|
|
|
t_Result.ProductType = Program.ProductType;
|
|
|
t_Result.ProductCode = productCode;
|
|
|
t_Result.CreatedBy = CurrentUser.Id;
|
|
|
var addRows = _resultService.AddResult(t_Result);
|
|
|
|
|
|
|
|
|
if (addRows <= 0)
|
|
|
{
|
|
|
LogHelper.Error(null, $"存数据库时,保存结果失败:{JsonConvert.SerializeObject(t_Result)}");
|
|
|
OmronHelper.WriteToPlc(point.DataType, point.Address, "2");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
LogHelper.Info("存数据库时,保存结果成功!");
|
|
|
OmronHelper.WriteToPlc(point.DataType, point.Address, "1");
|
|
|
}
|
|
|
}
|
|
|
Program.canSaveDb = true;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
LogHelper.Error(ex, "DoSomething中的SaveDb方法异常!");
|
|
|
OmronHelper.WriteToPlc(point.DataType, point.Address, "2");
|
|
|
Program.canSaveDb = true;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|