using Admin.Core.Common.Helper;
using Aucma.Core.BoxFoam.Models;
using Aucma.Core.HwPLc;
using NetTaste;
using NPOI.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.RightsManagement;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Documents;
namespace Aucma.Core.BoxFoam.Business
{
///
/// 采集发泡线数据
///
public class CollectionFoamLine
{
public delegate Task RefreshBoxFoamDataDelegate(List list);
public static event RefreshBoxFoamDataDelegate RefreshBoxFoamDataDelegateEvent;
private static System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
public static List listFixtureStatus = new List();
public static List BoxTypeList = new List();
public static List HolderStatusList = new List();
public static List HolderOutPutList = new List();
public static List SolidifyTimeList = new List();
public static List ActualTimeList = new List();
public static List MoldTemperatureList = new List();
Semaphore semaphore = new Semaphore(1, 1);
public CollectionFoamLine()
{
startCollect();
}
public void startCollect()
{
Task.Run(() =>
{
while (true)
{
semaphore.WaitOne();
try
{
Thread.Sleep(3000);
var obj = PlcHelper.siemensList.FirstOrDefault(d => d.EquipName.Equals("foamLine1Plc"));
if (obj != null)
{
BoxTypeList.Clear();
HolderStatusList.Clear();
HolderOutPutList.Clear();
SolidifyTimeList.Clear();
ActualTimeList.Clear();
listFixtureStatus.Clear();
ReadMoldTemperature(obj,MoldTemperatureList);
ReadBoxType(obj, BoxTypeList); // ok
ReadFixtureStatus(obj, HolderStatusList); //ok
ReadFixtureProduction(obj, HolderOutPutList); //ok
ReadPlanCuringTime(obj, SolidifyTimeList); //ok
ReadDisplayCuringTime(obj, ActualTimeList); //ok
//ReadMoldTemperature(obj,MoldTemperatureList);
//ReadLineStatus(obj); //ok
// });
for (int i = 0; i < 12; i++)
{
FixtureStatus model = new FixtureStatus();
model.ObjId = i + 1;
if (BoxTypeList.Count > 0 && BoxTypeList != null)
{
model.FixtureBoxType = BoxTypeList[i].ToString();
}
else
{
model.FixtureBoxType = "";
}
if (HolderStatusList.Count > 0 && HolderStatusList != null)
{
model.Status = HolderStatusList[i];
}
else
{
model.Status = HolderStatusList[i];
}
if (HolderOutPutList.Count > 0 && HolderOutPutList != null)
{
model.Yield = HolderOutPutList[i];
}
else
{
model.Yield = 0;
}
if (SolidifyTimeList.Count > 0 && HolderOutPutList != null)
{
model.PlanCuringTime = SolidifyTimeList[i];
}
else
{
model.PlanCuringTime = 0;
}
if (MoldTemperatureList.Count > 0 && MoldTemperatureList != null)
{
model.InternalTemperature = MoldTemperatureList[i].ToString();
model.OutsideTemperature = MoldTemperatureList[i+12].ToString();
}
else
{
model.PlanCuringTime = 0;
}
listFixtureStatus.Add(model);
}
RefreshBoxFoamDataDelegateEvent?.Invoke(listFixtureStatus);
}
}
catch (Exception)
{
throw;
}
finally
{
semaphore.Release();
}
}
});
}
///
/// 读取夹具箱型
///
static void ReadBoxType(HwPLc.PlcModel obj, List boxTypeList)
{
if (obj == null) return;
else
{
if (obj.plc.IsConnected)
{
byte[] result = obj.plc.Read("DB8.0", 124);
byte[] info = result;
if (result == null) return;
int str0 = int.Parse(StringChange.bytesToHexStr(info.Skip(0).Take(2).ToArray(), 2), System.Globalization.NumberStyles.HexNumber);
string str1 = asciiEncoding.GetString(result.Skip(4).Take(10).ToArray()).Trim('\n');
string str2 = asciiEncoding.GetString(result.Skip(14).Take(10).ToArray()).Trim('\n');
string str3 = asciiEncoding.GetString(result.Skip(24).Take(10).ToArray()).Trim('\n');
string str4 = asciiEncoding.GetString(result.Skip(34).Take(10).ToArray()).Trim('\n');
string str5 = asciiEncoding.GetString(result.Skip(44).Take(10).ToArray()).Trim('\n');
string str6 = asciiEncoding.GetString(result.Skip(54).Take(10).ToArray()).Trim('\n');
string str7 = asciiEncoding.GetString(result.Skip(64).Take(10).ToArray()).Trim('\n');
string str8 = asciiEncoding.GetString(result.Skip(74).Take(10).ToArray()).Trim('\n');
string str9 = asciiEncoding.GetString(result.Skip(84).Take(10).ToArray()).Trim('\n');
string str10 = asciiEncoding.GetString(result.Skip(94).Take(10).ToArray()).Trim('\n');
string str11 = asciiEncoding.GetString(result.Skip(104).Take(10).ToArray()).Trim('\n');
string str12 = asciiEncoding.GetString(result.Skip(114).Take(10).ToArray()).Trim('\n');
boxTypeList.Add(str1);
boxTypeList.Add(str2);
boxTypeList.Add(str3);
boxTypeList.Add(str4);
boxTypeList.Add(str5);
boxTypeList.Add(str6);
boxTypeList.Add(str7);
boxTypeList.Add(str8);
boxTypeList.Add(str9);
boxTypeList.Add(str10);
boxTypeList.Add(str11);
boxTypeList.Add(str12);
}
}
}
///
/// 读取夹具状态
///
static void ReadFixtureStatus(HwPLc.PlcModel obj, List HolderStatusList)
{
byte[] result = obj.plc.Read("DB8.122", 23);
byte[] info = result;
int temp1 = judgeStatus(ByteArrayToBinary(info.Skip(0).Take(1).ToArray()));
int temp2 = judgeStatus(ByteArrayToBinary(info.Skip(2).Take(1).ToArray()));
int temp3 = judgeStatus(ByteArrayToBinary(info.Skip(4).Take(1).ToArray()));
int temp4 = judgeStatus(ByteArrayToBinary(info.Skip(6).Take(1).ToArray()));
int temp5 = judgeStatus(ByteArrayToBinary(info.Skip(8).Take(1).ToArray()));
int temp6 = judgeStatus(ByteArrayToBinary(info.Skip(10).Take(1).ToArray()));
int temp7 = judgeStatus(ByteArrayToBinary(info.Skip(12).Take(1).ToArray()));
int temp8 = judgeStatus(ByteArrayToBinary(info.Skip(14).Take(1).ToArray()));
int temp9 = judgeStatus(ByteArrayToBinary(info.Skip(16).Take(1).ToArray()));
int temp10 = judgeStatus(ByteArrayToBinary(info.Skip(18).Take(1).ToArray()));
int temp11 = judgeStatus(ByteArrayToBinary(info.Skip(20).Take(1).ToArray()));
int temp12 = judgeStatus(ByteArrayToBinary(info.Skip(22).Take(1).ToArray()));
HolderStatusList.Add(temp1);
HolderStatusList.Add(temp2);
HolderStatusList.Add(temp3);
HolderStatusList.Add(temp4);
HolderStatusList.Add(temp5);
HolderStatusList.Add(temp6);
HolderStatusList.Add(temp7);
HolderStatusList.Add(temp8);
HolderStatusList.Add(temp9);
HolderStatusList.Add(temp10);
HolderStatusList.Add(temp11);
HolderStatusList.Add(temp11);
Console.WriteLine($"读取夹具状态,1#:{temp1};2#:{temp2};3#:{temp3};4#:{temp4};5#:{temp5};6#:{temp6}");
Console.WriteLine($"读取夹具状态,7#:{temp7};8#:{temp8};9#:{temp9};10#:{temp10};11#:{temp11};12#:{temp12}");
}
///
/// 读取夹具产量
///
static void ReadFixtureProduction(HwPLc.PlcModel obj, List HolderOutPutList)
{
if (obj == null) return;
if (obj.plc.IsConnected)
{
byte[] result = obj.plc.Read("DB8.146", 48);
byte[] info = result;
int temp1 = int.Parse(StringChange.bytesToHexStr(info.Skip(0).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp2 = int.Parse(StringChange.bytesToHexStr(info.Skip(4).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp3 = int.Parse(StringChange.bytesToHexStr(info.Skip(8).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp4 = int.Parse(StringChange.bytesToHexStr(info.Skip(12).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp5 = int.Parse(StringChange.bytesToHexStr(info.Skip(16).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp6 = int.Parse(StringChange.bytesToHexStr(info.Skip(20).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp7 = int.Parse(StringChange.bytesToHexStr(info.Skip(24).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp8 = int.Parse(StringChange.bytesToHexStr(info.Skip(28).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp9 = int.Parse(StringChange.bytesToHexStr(info.Skip(32).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp10 = int.Parse(StringChange.bytesToHexStr(info.Skip(36).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp11 = int.Parse(StringChange.bytesToHexStr(info.Skip(40).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp12 = int.Parse(StringChange.bytesToHexStr(info.Skip(44).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
HolderOutPutList.Add(temp1);
HolderOutPutList.Add(temp2);
HolderOutPutList.Add(temp3);
HolderOutPutList.Add(temp4);
HolderOutPutList.Add(temp5);
HolderOutPutList.Add(temp6);
HolderOutPutList.Add(temp7);
HolderOutPutList.Add(temp8);
HolderOutPutList.Add(temp9);
HolderOutPutList.Add(temp10);
HolderOutPutList.Add(temp11);
HolderOutPutList.Add(temp12);
Console.WriteLine($"读取夹具产量,1#:{temp1};2#:{temp2};3#:{temp3};4#:{temp4};5#:{temp5};6#:{temp6}");
Console.WriteLine($"读取夹具产量,7#:{temp7};8#:{temp8};9#:{temp9};10#:{temp10};11#:{temp11};12#:{temp12}");
}
}
///
/// 固化时间设定值
///
static void ReadPlanCuringTime(HwPLc.PlcModel obj, List SolidifyTimeList)
{
if (obj == null) return;
if (obj.plc.IsConnected)
{
byte[] result = obj.plc.Read("DB8.194", 48);
byte[] info = result;
int temp1 = int.Parse(StringChange.bytesToHexStr(info.Skip(0).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp2 = int.Parse(StringChange.bytesToHexStr(info.Skip(4).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp3 = int.Parse(StringChange.bytesToHexStr(info.Skip(8).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp4 = int.Parse(StringChange.bytesToHexStr(info.Skip(12).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp5 = int.Parse(StringChange.bytesToHexStr(info.Skip(16).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp6 = int.Parse(StringChange.bytesToHexStr(info.Skip(20).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp7 = int.Parse(StringChange.bytesToHexStr(info.Skip(24).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp8 = int.Parse(StringChange.bytesToHexStr(info.Skip(28).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp9 = int.Parse(StringChange.bytesToHexStr(info.Skip(32).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp10 = int.Parse(StringChange.bytesToHexStr(info.Skip(36).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp11 = int.Parse(StringChange.bytesToHexStr(info.Skip(40).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp12 = int.Parse(StringChange.bytesToHexStr(info.Skip(44).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
SolidifyTimeList.Add(temp1);
SolidifyTimeList.Add(temp2);
SolidifyTimeList.Add(temp3);
SolidifyTimeList.Add(temp4);
SolidifyTimeList.Add(temp5);
SolidifyTimeList.Add(temp6);
SolidifyTimeList.Add(temp7);
SolidifyTimeList.Add(temp8);
SolidifyTimeList.Add(temp9);
SolidifyTimeList.Add(temp10);
SolidifyTimeList.Add(temp11);
SolidifyTimeList.Add(temp12);
Console.WriteLine($"读取固化时间设定值,1#:{temp1};2#:{temp2};3#:{temp3};4#:{temp4};5#:{temp5};6#:{temp6}");
Console.WriteLine($"读取固化时间设定值,7#:{temp7};8#:{temp8};9#:{temp9};10#:{temp10};11#:{temp11};12#:{temp12}");
}
}
///
/// 固化时间显示值
///
static void ReadDisplayCuringTime(HwPLc.PlcModel obj, List ActualTimeList)
{
if (obj == null) return;
if (obj.plc.IsConnected)
{
byte[] result = obj.plc.Read("DB8.242", 48);
byte[] info = result;
int temp1 = int.Parse(StringChange.bytesToHexStr(info.Skip(0).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp2 = int.Parse(StringChange.bytesToHexStr(info.Skip(4).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp3 = int.Parse(StringChange.bytesToHexStr(info.Skip(8).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp4 = int.Parse(StringChange.bytesToHexStr(info.Skip(12).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp5 = int.Parse(StringChange.bytesToHexStr(info.Skip(16).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp6 = int.Parse(StringChange.bytesToHexStr(info.Skip(20).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp7 = int.Parse(StringChange.bytesToHexStr(info.Skip(24).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp8 = int.Parse(StringChange.bytesToHexStr(info.Skip(28).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp9 = int.Parse(StringChange.bytesToHexStr(info.Skip(32).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp10 = int.Parse(StringChange.bytesToHexStr(info.Skip(36).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp11 = int.Parse(StringChange.bytesToHexStr(info.Skip(40).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
int temp12 = int.Parse(StringChange.bytesToHexStr(info.Skip(44).Take(4).ToArray(), 4), System.Globalization.NumberStyles.HexNumber);
ActualTimeList.Add(temp1);
ActualTimeList.Add(temp2);
ActualTimeList.Add(temp3);
ActualTimeList.Add(temp4);
ActualTimeList.Add(temp5);
ActualTimeList.Add(temp6);
ActualTimeList.Add(temp7);
ActualTimeList.Add(temp8);
ActualTimeList.Add(temp9);
ActualTimeList.Add(temp10);
ActualTimeList.Add(temp11);
ActualTimeList.Add(temp12);
Console.WriteLine($"读取固化时间显示值,1#:{temp1};2#:{temp2};3#:{temp3};4#:{temp4};5#:{temp5};6#:{temp6}");
Console.WriteLine($"读取固化时间显示值,7#:{temp7};8#:{temp8};9#:{temp9};10#:{temp10};11#:{temp11};12#:{temp12}");
}
}
///
/// 模具内外温度
///
static void ReadMoldTemperature(HwPLc.PlcModel obj, List MoldTemperatureList)
{
if (obj == null) return;
if (obj.plc.IsConnected)
{
byte[] info = obj.plc.Read("DB8.294", 284);
float temp1 = byteToFloat(info.Skip(0).Take(4).ToArray());
float temp2 = byteToFloat(info.Skip(6).Take(4).ToArray());
float temp3 = byteToFloat(info.Skip(24).Take(4).ToArray());
float temp4 = byteToFloat(info.Skip(40).Take(4).ToArray());
float temp5 = byteToFloat(info.Skip(48).Take(4).ToArray());
float temp6 = byteToFloat(info.Skip(64).Take(4).ToArray());
float temp7 = byteToFloat(info.Skip(72).Take(4).ToArray());
float temp8 = byteToFloat(info.Skip(88).Take(4).ToArray());
float temp9 = byteToFloat(info.Skip(96).Take(4).ToArray());
float temp10 = byteToFloat(info.Skip(112).Take(4).ToArray());
float temp11 = byteToFloat(info.Skip(120).Take(4).ToArray());
float temp12 = byteToFloat(info.Skip(136).Take(4).ToArray());
float temp13 = byteToFloat(info.Skip(144).Take(4).ToArray());
float temp14 = byteToFloat(info.Skip(160).Take(4).ToArray());
float temp15 = byteToFloat(info.Skip(168).Take(4).ToArray());
float temp16 = byteToFloat(info.Skip(184).Take(4).ToArray());
float temp17 = byteToFloat(info.Skip(192).Take(4).ToArray());
float temp18 = byteToFloat(info.Skip(208).Take(4).ToArray());
float temp19 = byteToFloat(info.Skip(216).Take(4).ToArray());
float temp20 = byteToFloat(info.Skip(232).Take(4).ToArray());
float temp21 = byteToFloat(info.Skip(240).Take(4).ToArray());
float temp22 = byteToFloat(info.Skip(256).Take(4).ToArray());
float temp23 = byteToFloat(info.Skip(264).Take(4).ToArray());
float temp24 = byteToFloat(info.Skip(280).Take(4).ToArray());
MoldTemperatureList.Add(temp1);
MoldTemperatureList.Add(temp3);
MoldTemperatureList.Add(temp5);
MoldTemperatureList.Add(temp7);
MoldTemperatureList.Add(temp9);
MoldTemperatureList.Add(temp11);
MoldTemperatureList.Add(temp13);
MoldTemperatureList.Add(temp15);
MoldTemperatureList.Add(temp17);
MoldTemperatureList.Add(temp19);
MoldTemperatureList.Add(temp21);
MoldTemperatureList.Add(temp23);
MoldTemperatureList.Add(temp2);
MoldTemperatureList.Add(temp4);
MoldTemperatureList.Add(temp6);
MoldTemperatureList.Add(temp8);
MoldTemperatureList.Add(temp10);
MoldTemperatureList.Add(temp12);
MoldTemperatureList.Add(temp14);
MoldTemperatureList.Add(temp16);
MoldTemperatureList.Add(temp18);
MoldTemperatureList.Add(temp20);
MoldTemperatureList.Add(temp22);
MoldTemperatureList.Add(temp24);
}
}
///
/// 发泡线状态
///
static void ReadLineStatus(HwPLc.PlcModel obj)
{
byte[] result = obj.plc.Read("DB8.630", 1);
byte[] info = result;
int temp1 = judgeStatus(ByteArrayToBinary(info.Skip(0).Take(1).ToArray()));
Console.WriteLine($"发泡线状态:{temp1}");
}
#region 字符工具转换方法
///
/// byte数组转换为float
///
static float byteToFloat(byte[] list)
{
float result = 0.00f;
Array.Reverse(list);
result = BitConverter.ToSingle(list, 0);
return result;
}
///
/// byte[]转二进制
///
///
static string ByteArrayToBinary(byte[] bytes)
{
StringBuilder binaryString = new StringBuilder();
foreach (byte b in bytes)
{
binaryString.Append(Convert.ToString(b, 2).PadLeft(8, '0'));
}
// Console.WriteLine(binaryString);
string reversedStr = new string(binaryString.ToString().Reverse().ToArray());
// Console.WriteLine(reversedStr);
return reversedStr;
}
///
/// 根据二进制字符串确定夹具状态 1正常生产 2暂停生产
///
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;
}
}
#endregion
}