You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AUCMA_SCADA/Aucma.Core.Tasks/AucamTaskService.cs

427 lines
20 KiB
C#

using Admin.Core.Common;
10 months ago
using Admin.Core.Common.Helper;
using Admin.Core.IService;
using Admin.Core.Model;
using Admin.Core.Model.Model_New;
using Admin.Core.Model.ViewModels;
1 year ago
using Aucma.Core.HwPLc;
1 year ago
using Aucma.Core.Tasks.Business;
using log4net;
using NPOI.HSSF.Record;
10 months ago
using StackExchange.Profiling.Internal;
using System.Text;
1 year ago
namespace Aucma.Core.Tasks
{
/// <summary>
1 year ago
/// 任务列表
1 year ago
/// </summary>
public class AucamTaskService : IAucamTaskService
{
1 year ago
private static readonly log4net.ILog logHelper = LogManager.GetLogger(typeof(AucamTaskService));
10 months ago
Semaphore semaphore = new Semaphore(1, 1);
protected readonly IPerfusionRecordServices? _perfusionRecordServices;
protected readonly IPerfusionAlarmServices? _perfusionAlarmServices;
protected readonly IPerfusionDeviceStatusServices? _perfusionDeviceStatusServices;
private readonly IMaterialCompletionServices _materialCompletionServices;
private readonly IPrintBarCodeServices _printBarCodeServices;
private readonly IReportQualityInspectionServices _reportQualityInspectionServices;
10 months ago
bool flag = true;
private static System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
private Perfusion_Record record;
private Perfusion_Record temprRecord;
private Perfusion_DeviceStatus perfusion_DeviceStatus = new Perfusion_DeviceStatus();
private Perfusion_Alarm perfusion_Alarm = new Perfusion_Alarm();
System.Timers.Timer timer1 = new System.Timers.Timer(5000);
10 months ago
public AucamTaskService(IPerfusionRecordServices perfusionRecordServices, IPerfusionAlarmServices perfusionAlarmServices,
IPerfusionDeviceStatusServices perfusionDeviceStatusServices, IMaterialCompletionServices materialCompletionServices,
IPrintBarCodeServices printBarCodeServices, IReportQualityInspectionServices reportQualityInspectionServices)
1 year ago
{
10 months ago
_perfusionRecordServices = perfusionRecordServices;
_perfusionAlarmServices = perfusionAlarmServices;
_perfusionDeviceStatusServices = perfusionDeviceStatusServices;
_materialCompletionServices = materialCompletionServices;
_printBarCodeServices = printBarCodeServices;
_reportQualityInspectionServices = reportQualityInspectionServices;
}
#region 更新灌注工位计划数
bool qFlay = true;//完成
10 months ago
public void AucamTaskAsync()
{
startCollect();
}
#endregion
#region 灌注
public void startCollect()
{
Task.Run(() =>
{
Thread.Sleep(3000);
Console.WriteLine($"【{DateTime.Now}】》灌注数据采集服务开启");
var obj = PlcHelper.siemensList.FirstOrDefault(d => d.EquipName.Equals("PerfusionPlc"));
if (obj != null)
{
while (true)
{
if (flag)
{
flag = false;
try
{
int signal = obj.plc.ReadInt16("DB55.84.0");
if (signal == 0)
{
Thread.Sleep(1000);
continue;
}
Console.WriteLine($"【{DateTime.Now}】》当前灌注采集信号:{signal}");
Console.WriteLine($"【{DateTime.Now}】》灌注数据开始采集");
//采集数据
record = ReadEquipSystem(obj, record, perfusion_DeviceStatus, perfusion_Alarm);
if (record == null) continue;
if (string.IsNullOrEmpty(record.PerfusionBoxCode))
{
Console.WriteLine($"【{DateTime.Now}】》条码为空!");
continue;
}
Console.WriteLine($"【{DateTime.Now}】》灌注数据条码:{record.PerfusionBoxCode}》灌注结果:{record.PerfusionResult}》设定灌注量:{record.PerfusionSetVolume}》灌注量:{record.PerfusionActualVolume}");
var tempRecord = _perfusionRecordServices.FirstAsync(d => d.PerfusionBoxCode.Equals(record.PerfusionBoxCode) && d.PerfusionResult == record.PerfusionResult).Result;
if (tempRecord != null)
{
Console.WriteLine($"【{DateTime.Now}】》灌注数据条码:[{record.PerfusionBoxCode}],灌注结果:[{record.PerfusionResult}],已存在系统中");
bool fw = obj.plc.WriteInt16("DB55.84.0", "0");//存盘成功写入bool
Console.WriteLine($"【{DateTime.Now}】》写入复位信号{fw}");
Thread.Sleep(1000);
continue;
}
#region 更新设备状态
Perfusion_DeviceStatus device = _perfusionDeviceStatusServices.FirstAsync().Result;
if (device == null)
{
_ = _perfusionDeviceStatusServices.AddAsync(perfusion_DeviceStatus).Result;
}
else
{
device.PerfusionStatus = perfusion_DeviceStatus.PerfusionStatus;
var rsult = _perfusionDeviceStatusServices.UpdateAsync(device).Result;
}
#endregion
#region 更新报警信息
if (perfusion_Alarm.PerfusionStatus > 0)
{
int a = _perfusionAlarmServices.AddAsync(perfusion_Alarm).Result;
}
#endregion
record.IsFlag = 0;
//充注结束
int result = _perfusionRecordServices.AddAsync(record).Result;
if (result == 0)
{
//添加过点数
Console.WriteLine($"【{DateTime.Now}】》灌注结果:{record.PerfusionResult}");
if (record.PerfusionResult == 8)
{
var insert = InsertToCompleteAndCheck(record);
if (insert)
{
Console.WriteLine($"【{DateTime.Now}】》保存过点数成功!");
}
}
Task.Run(async () =>
{
await ToMes();//灌注
});
Console.WriteLine(record.ToJson());
}
bool r = obj.plc.WriteInt16("DB55.84.0", "0");//存盘成功写入bool
Console.WriteLine($"【{DateTime.Now}】》写入复位信号为{r}");
Console.WriteLine($"【{DateTime.Now}】》灌注数据采集结束");
10 months ago
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
flag = true;
}
}
}
}
});
}
/// <summary>
/// 采集灌注参数
/// </summary>
/// <returns></returns>
static Perfusion_Record ReadEquipSystem(Aucma.Core.HwPLc.PlcModel obj, Perfusion_Record record, Perfusion_DeviceStatus deviceStatus, Perfusion_Alarm alarmInfo)
{
record = new Perfusion_Record();
byte[] info = obj.plc.Read("DB55.0", 84);
byte[] codeInfo = obj.plc.Read("DB60.0", 46);
if (info == null) return null;
#region 灌注记录赋值
record.PerfusionActualVolume = byteToDouble(info.Skip(0).Take(8).ToArray()).ToString().Replace("\0", "");
record.PerfusionR = byteToFloat(info.Skip(8).Take(4).ToArray()).ToString().ToString().Replace("\0", "");
record.PerfusionL = byteToFloat(info.Skip(54).Take(4).ToArray()).ToString().Replace("\0", "");
record.PerfusionDuration = int.Parse(StringChange.bytesToHexStr(info.Skip(12).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber).ToString().Replace("\0", "");
record.Yield = int.Parse(StringChange.bytesToHexStr(info.Skip(16).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber).ToString().Replace("\0", "");
record.PerfusionBoxCode = asciiEncoding.GetString(codeInfo.Skip(2).Take(22).ToArray()).Trim('\n').Replace("\0", "").Replace("\r","");
record.PerfusionRefrigerantTypeLeft = asciiEncoding.GetString(codeInfo.Skip(28).Take(7).ToArray()).Trim('\n').Replace("\0", "");
record.PerfusionRefrigerantTypeRight = asciiEncoding.GetString(codeInfo.Skip(38).Take(7).ToArray()).Trim('\n').Replace("\0", "");
record.PerfusionSetVolume = byteToFloat(info.Skip(78).Take(4).ToArray()).ToString().Replace("\0", "");
record.PerfusionSystem = short.Parse(StringChange.bytesToHexStr(info.Skip(82).Take(2).ToArray(), 2), System.Globalization.NumberStyles.HexNumber).ToString().Replace("\0", "");
///灌注完成状态字
record.PerfusionFinishStatus = short.Parse(StringChange.bytesToHexStr(info.Skip(46).Take(2).ToArray(), 2), System.Globalization.NumberStyles.HexNumber).ToString().Replace("\0", "");
record.PerfusionResult = short.Parse(StringChange.bytesToHexStr(info.Skip(48).Take(2).ToArray(), 2), System.Globalization.NumberStyles.HexNumber);
record.CreatedBy = "equip";
record.CreatedTime = DateTime.Now;
record.UpdatedBy = "equip";
record.UpdatedTime = DateTime.Now;
#endregion
// Console.WriteLine("灌注记录:" + record.ToJson());
#region 设备状态赋值
deviceStatus.PerfusionDevicetype = "灌注";
deviceStatus.PerfusionStatus = short.Parse(StringChange.bytesToHexStr(info.Skip(50).Take(2).ToArray(), 2), System.Globalization.NumberStyles.HexNumber);
deviceStatus.CreatedBy = "equip";
deviceStatus.CreatedTime = DateTime.Now;
deviceStatus.UpdatedBy = "equip";
deviceStatus.UpdatedTime = DateTime.Now;
#endregion
// Console.WriteLine("设备状态:" + deviceStatus.ToJson());
#region 设备报警赋值
alarmInfo.PerfusionBoxcode = record.PerfusionBoxCode;
alarmInfo.PerfusionStatus = short.Parse(StringChange.bytesToHexStr(info.Skip(52).Take(2).ToArray(), 2), System.Globalization.NumberStyles.HexNumber);
alarmInfo.PerfusionAlarm = AlarmTrans(alarmInfo.PerfusionStatus);
alarmInfo.PerfusionCompleted = "1";
alarmInfo.CreatedBy = "equip";
alarmInfo.CreatedTime = DateTime.Now;
alarmInfo.UpdatedBy = "equip";
alarmInfo.UpdatedTime = DateTime.Now;
#endregion
// Console.WriteLine("报警信息:" + alarmInfo.ToJson());
// Console.WriteLine("\n");
return record;
}
static string AlarmTrans(short code)
1 year ago
{
10 months ago
string result = "";
switch (code)
{
case 2: result = "充注枪真空不合格"; break;
case 4: result = "充注故障"; break;
case 8: result = "充注时真空度超限定"; break;
case 16: result = "回升检测不合格"; break;
case 32: result = "工件真空不合格"; break;
case 64: result = "平衡缸接近开关异常"; break;
case 128: result = "增压超时"; break;
case 256: result = "风压不足"; break;
}
return result;
1 year ago
}
10 months ago
#region 字符工具转换方法
/// byte[]转十进制ascii码
/// </summary>
/// <param name="bytes"></param>
static string ByteArrayToString(byte[] bytes)
{
string result = string.Empty;
foreach (byte b in bytes)
{
int decimalValue = Convert.ToInt32(b); // 将字节转换为十进制值
result += decimalValue;
}
return result;
}
/// <summary>
/// byte[]转二进制
/// </summary>
/// <param name="bytes"></param>
static string ByteArrayToBinary(byte[] bytes)
{
10 months ago
StringBuilder binaryString = new StringBuilder();
foreach (byte b in bytes)
{
10 months ago
binaryString.Append(Convert.ToString(b, 2).PadLeft(8, '0'));
1 year ago
}
10 months ago
// Console.WriteLine(binaryString);
string reversedStr = new string(binaryString.ToString().Reverse().ToArray());
// Console.WriteLine(reversedStr);
return reversedStr;
}
/// <summary>
/// 根据二进制字符串确定夹具状态 1正常生产 2暂停生产
/// </summary>
static int judgeStatus(string str1)
{
int item1 = 0;
if (str1.Substring(0, 1) == "1")
{
item1 = 1; //
}
else if (str1.Substring(1, 1) == "1")
{
item1 = 2; //
}
return item1;
}
/// <summary>
/// byte数组转换为float
/// </summary>
static float byteToFloat(byte[] list)
{
float result = 0.00f;
Array.Reverse(list);
result = BitConverter.ToSingle(list, 0);
// 只保留两位小数
// string str = result.ToString("0.00");
return result;
}
/// <summary>
/// byte数组转换为double
/// </summary>
static double byteToDouble(byte[] list)
{
double result = 0;
Array.Reverse(list);
result = BitConverter.ToDouble(list, 0);
return result;
}
#endregion
#region 保存过点数
/// <summary>
/// 保存过点数
/// </summary>
/// <param name="record"></param>
/// <returns></returns>
public bool InsertToCompleteAndCheck(Perfusion_Record record)
{
var barcode = _printBarCodeServices.FirstAsync(d => d.MaterialBarcode == record.PerfusionBoxCode).Result;
if (barcode == null) return false;
#region 更新过点数据
MaterialCompletion completion = new MaterialCompletion();
completion.OrderCode = barcode.OrderCode;
completion.MaterialBarcode = record.PerfusionBoxCode;
completion.MaterialCode = barcode.MaterialCode;
completion.MaterialName = barcode.MaterialName;
completion.StationName = "2015";
completion.ProductLineCode = "CX_02";
completion.CompleteDate = DateTime.Now;
#endregion
int r = _materialCompletionServices.AddAsync(completion).Result;
if (r > 0) return true;
else return false;
}
#endregion
#region 发送质检请求
/// <summary>
/// 发送质检请求
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public async Task ToMes()
{
try
{
var list = await _perfusionRecordServices.QueryAsync(d => d.IsFlag == 0 && d.CreatedTime.ToString("yyyy-MM-dd").Equals(DateTime.Now.ToString("yyyy-MM-dd")));
if (list == null || list.Count == 0) return;
Console.WriteLine($"【{DateTime.Now}】开始向质检发送灌注数据");
list.ForEach(async k =>
{
var r = await _reportQualityInspectionServices.FirstAsync(d => d.BarCode == k.PerfusionBoxCode && d.TreatmentMeasure == "3" && d.StationCode == "2015");
if (r != null)
{
var checkList = _perfusionRecordServices.GetPerfusionRecord(k.ObjId).Result;
if (checkList != null && checkList.Count != 0)
{
var check = checkList.First();
ReportQualityInsPection report = new ReportQualityInsPection();
if (check.TreatmentMeasure == "3")
{
report.FinishTime = null;
report.UpdatedTime = null;
}
else
{
report.FinishTime = check.FinishTime;
report.UpdatedTime = null;
}
report.BarCode = check.BarCode;
report.MaterialName = check.MaterialName;
report.ProcessCode = check.ProcessCode;
report.TestItemCode = check.TestItemCode;
report.QualityDefectCode = check.QualityDefectCode;
report.QualityDefectName = check.QualityDefectName;
report.TreatmentMeasure = check.TreatmentMeasure;
report.ProcessResult = check.ProcessResult;
report.GroupCode = check.GroupCode;
report.InspectorCode = check.InspectorCode;
report.InspectorTime = check.InspectorTime;
report.ReworkNumber = check.ReworkNumber;
report.IsFlag = check.IsFlag;
report.StationCode = check.StationCode;
report.UpdatedTime = DateTime.Now;
int reportResult = await _reportQualityInspectionServices.AddAsync(report);
if (reportResult > 0)
{
k.IsFlag = 1;
_ = _perfusionRecordServices.UpdateAsync(k).Result;
}
}
}
});
Console.WriteLine($"【{DateTime.Now}】更新灌注质检数据成功!");
}
catch (Exception ex)
{
Console.WriteLine($"【{DateTime.Now}】更新灌注质检数据失败!原因:{ex.Message}");
}
}
#endregion
#endregion
1 year ago
}
}