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.
ProductionSystem/Production_Oil/DoSomething.cs

83 lines
3.9 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Custom.Communication.Framework.MyPlc;
using Newtonsoft.Json;
using Production_Oil.DbModel;
using Production_Oil.Services;
using Production_Oil.ViewModel;
using ProductionSystem_Log;
using System.Linq;
using System.Threading;
namespace Production_Oil
{
public class DoSomething
{
private readonly OilService _oilService = new OilService();
public void ReadPlc(Station station)
{
//PLC请求数据点位
var M0 = station.Points.FirstOrDefault(m => m.PointCode == "M0");
//产品条码点位
var M1 = station.Points.FirstOrDefault(m => m.PointCode == "M1");
//压缩机空重点位
var M2 = station.Points.FirstOrDefault(m => m.PointCode == "M2");
//压缩机空重反馈点位地址
var M3 = station.Points.FirstOrDefault(m => m.PointCode == "M3");
while (true)
{
Thread.Sleep(200);
switch (station.StationCode)
{
case "Oiling"://注油工位
var oilingM0Read = OmronHelper.GetPlcVal(M0.DataType, M0.PointAddress);
//PLC请求数据上传
if (oilingM0Read.ok && oilingM0Read.val == "1")
{
T_Oil t_Oil = new T_Oil();
var productCode = OmronHelper.GetPlcVal(M1.DataType, M1.PointAddress).val.TrimEnd('\r');
var emptyWeight = OmronHelper.GetPlcVal(M2.DataType, M2.PointAddress).val;
decimal.TryParse(emptyWeight, out var weight);
t_Oil.ProductCode = productCode;
t_Oil.Weight = weight;
var addRow = _oilService.AddOil(t_Oil);
if (addRow <= 0)
{
LogHelper.Error(null, $"条码:【{productCode}】新增空重数据失败:{JsonConvert.SerializeObject(t_Oil)}");
}
else
{
LogHelper.Info(null, $"条码:【{productCode}】新增空重数据成功!");
}
}
break;
case "RepairOil"://补油工位
var repairOilM0Read = OmronHelper.GetPlcVal(M0.DataType, M0.PointAddress);
//PLC请求空重数据
if (repairOilM0Read.ok && repairOilM0Read.val == "1")
{
var productCode = OmronHelper.GetPlcVal(M1.DataType, M1.PointAddress).val.TrimEnd('\r');
var data = _oilService.QueryOilByProductCode(productCode);
if (data == null)
{
LogHelper.Error(null, $"根据条码:【{productCode}】,未查询到空重数据!");
}
else
{
var writeResult = OmronHelper.WriteToPlc(M3.DataType, M3.PointAddress, data.Weight.ToString());
if (writeResult.ok)
{
LogHelper.Info(null, $"条码:【{productCode}】的空重数据反馈点位【{M3.PointAddress}】写入成功,值={data.Weight}");
}
else
{
LogHelper.Error(null, $"条码:【{productCode}】的空重数据反馈点位【{M3.PointAddress}】写入失败!");
}
}
}
break;
}
}
}
}
}