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.

191 lines
7.9 KiB
C#

using Admin.Core.Common;
using Admin.Core.IService;
using Admin.Core.Model;
1 year ago
using Aucma.Core.HwPLc;
using Microsoft.Extensions.DependencyInjection;
using NetTaste;
using System;
12 months ago
using System.Collections.Generic;
1 year ago
using System.Linq;
using System.Text;
using System.Threading;
12 months ago
using System.Threading.Tasks;
1 year ago
namespace Aucma.Core.SheetMetal.Business
{
/// <summary>
/// 同步PLC处理
/// </summary>
public class SyncModelHandle
{
protected readonly ISmSyncModelServices? _smSyncModelServices;
12 months ago
public HwPLc.PlcModel obj_backPanel = null;
public HwPLc.PlcModel obj_sidePanel = null;
1 year ago
public SyncModelHandle() {
_smSyncModelServices = App.ServiceProvider.GetService<ISmSyncModelServices>();
}
12 months ago
/// <summary>
/// 获取所有设置的钣金前板型号
/// </summary>
/// <returns></returns>
public async Task QuerySidePanelMode()
1 year ago
{
12 months ago
Thread.Sleep(5000);
obj_sidePanel = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("SidePanelPlc"));
if (obj_sidePanel == null) { return; }
if (obj_sidePanel.plc.IsConnected)
1 year ago
{
12 months ago
Console.WriteLine("===============采集前板型号开始===============");
byte[] info = obj_sidePanel.plc.Read("D7000", 3000);
if (info == null)
1 year ago
{
12 months ago
Thread.Sleep(1000);
return;
}
int j = 1;
string mode = string.Empty;
List<SmSyncModel> addList = new List<SmSyncModel>();
List<SmSyncModel> updateList = new List<SmSyncModel>();
var smSyncModelList = _smSyncModelServices.QueryAsync().Result;
for (int i = 1; i <= 600; i++)
{
if (i % 2 == 0)
1 year ago
{
12 months ago
//设置型号
mode = Encoding.ASCII.GetString(info.Skip(i * 10).Take(10).ToArray()).Replace("\0", "").Trim();
if (string.IsNullOrEmpty(mode)) return;
if (smSyncModelList.Count == 0 || smSyncModelList == null)
{
SmSyncModel sm = new SmSyncModel();
sm.SmProductId = j.ToString();
sm.SmModel = mode;
sm.SyncTime = DateTime.Now;
sm.CreateTime = DateTime.Now;
sm.Category = "0";
addList.Add(sm);
}
else
1 year ago
{
12 months ago
var obj = smSyncModelList.First(d => d.SmProductId == j.ToString()&& d.Category=="0" );
if (obj == null)
{
SmSyncModel sm = new SmSyncModel();
sm.SmProductId = j.ToString();
sm.SmModel = mode;
sm.SyncTime = DateTime.Now;
sm.Category = "0";
addList.Add(sm);
}
if (string.IsNullOrEmpty(obj.SmModel) && string.IsNullOrEmpty(mode.Trim())) continue;
if (obj.SmModel.Equals(mode)) continue;
else
{
if (obj.SmModel.Equals(mode))
{
obj.SmProductId = j.ToString();
obj.SmModel = mode;
obj.SyncTime = DateTime.Now;
obj.Category = "0";
updateList.Add(obj);
}
}
1 year ago
}
12 months ago
Console.WriteLine($"型号{j}{mode}");
j++;
}
}
if (addList != null || addList.Count > 0) await _smSyncModelServices.AddAsync(addList);
if (updateList != null || updateList.Count > 0) await _smSyncModelServices.UpdateAsync(updateList);
Console.WriteLine("===============采集结束===============");
1 year ago
}
12 months ago
}
/// <summary>
/// 获取所有设置的钣金后板型号
/// </summary>
/// <returns></returns>
public async Task QueryBackPanelMode()
{
Thread.Sleep(5000);
obj_backPanel = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("BackPanelPLC"));
if (obj_backPanel == null) { return; }
if (obj_backPanel.plc.IsConnected)
{
Console.WriteLine("===============采集后板开始===============");
byte[] info = obj_sidePanel.plc.Read("D7000", 3000);
if (info == null)
{
Thread.Sleep(1000);
return;
}
int j = 1;
string mode = string.Empty;
List<SmSyncModel> addList = new List<SmSyncModel>();
List<SmSyncModel> updateList = new List<SmSyncModel>();
var smSyncModelList = _smSyncModelServices.QueryAsync().Result;
1 year ago
12 months ago
for (int i = 1; i <= 600; i++)
{
if (i % 2 == 0)
{
//设置型号
mode = Encoding.ASCII.GetString(info.Skip(i * 10).Take(10).ToArray()).Replace("\0","").Trim();
if (smSyncModelList.Count == 0 || smSyncModelList == null)
{
SmSyncModel sm = new SmSyncModel();
sm.SmProductId = j.ToString();
sm.SmModel = mode.Trim();
sm.SyncTime = DateTime.Now;
sm.CreateTime = DateTime.Now;
sm.Category = "1";
addList.Add(sm);
}
else
{
var obj = smSyncModelList.First(d => d.SmProductId == j.ToString() && d.Category == "1");
if (obj == null)
{
SmSyncModel sm = new SmSyncModel();
sm.SmProductId = j.ToString();
sm.SmModel = mode.Trim();
sm.SyncTime = DateTime.Now;
sm.Category ="1";
addList.Add(sm);
}
else
{
if (string.IsNullOrEmpty(obj.SmModel) && string.IsNullOrEmpty(mode)) continue;
if (obj.SmModel.Equals(mode)) continue;
else
{
obj.SmProductId = j.ToString();
obj.SmModel = mode.Trim();
obj.SyncTime = DateTime.Now;
obj.Category = "1";
updateList.Add(obj);
}
}
}
Console.WriteLine($"型号{j}{mode}");
j++;
}
}
if (addList.Count > 0) await _smSyncModelServices.AddAsync(addList);
if (updateList.Count > 0) await _smSyncModelServices.UpdateAsync(updateList);
Console.WriteLine("===============采集后板结束===============");
}
1 year ago
}
12 months ago
1 year ago
}
}