初始化提交
This commit is contained in:
@@ -0,0 +1,882 @@
|
||||
|
||||
using JinYuan.Helper;
|
||||
using JinYuan.MES.Models;
|
||||
using JinYuan.Models;
|
||||
using JinYuan.VirtualDataLibrary;
|
||||
using PLCCommunication;
|
||||
using PLCCommunication.Common;
|
||||
using PLCCommunication.Modbus;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JinYuan.ControlCenters
|
||||
{
|
||||
public partial class ControlCenter
|
||||
{
|
||||
#region 刷新12小时产能
|
||||
//public void Refresh12HourProdQty()
|
||||
//{
|
||||
// List<Chart12HourData> chart12Hours = new List<Chart12HourData>();
|
||||
// int j = 0;
|
||||
// int m = 0;
|
||||
// int n = 0;
|
||||
// DateTime dtTime = DateTime.Now;
|
||||
// string strDate = dtTime.ToString("yyyy-MM-dd");
|
||||
// int Hour = GetHour();
|
||||
// var listTotal = CommonMethods.db.QueryWhereList<Hourprod>(t => t.FDate == strDate);// 查询稼动率表中total数据
|
||||
// if (listTotal != null && listTotal.Count > 0)
|
||||
// {
|
||||
// for (int i = 1; i < 13; i++)
|
||||
// {
|
||||
// #region X轴数据
|
||||
|
||||
// if (CommonMethods.strClass != "白班")
|
||||
// {
|
||||
// j = i + 21;
|
||||
|
||||
// if (j >= 25 && j < 26)
|
||||
// {
|
||||
// m = j - 2;
|
||||
// n = j - 25;
|
||||
// }
|
||||
// else if (j >= 26)
|
||||
// {
|
||||
// m = j - 26;
|
||||
// n = j - 25;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// m = j - 2;
|
||||
// n = j - 1;
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// j = i + 9;
|
||||
// m = j - 2;
|
||||
// n = j - 1;
|
||||
// }
|
||||
// #endregion
|
||||
|
||||
// int Fhour = Convert.ToInt32(m);
|
||||
// var hour1 = listTotal.Where(p => p.FHour == Fhour).FirstOrDefault();
|
||||
// int ProdALLQty = 0;
|
||||
// int ProdOKQty = 0;
|
||||
// int ProdNGQty = 0;
|
||||
// string strOkRatio = "0.00";
|
||||
|
||||
// if (hour1 != null)
|
||||
// {
|
||||
// ProdALLQty += hour1.ProdIn;
|
||||
// ProdOKQty += hour1.ProdOut;
|
||||
// }
|
||||
|
||||
// ProdNGQty = ProdALLQty - ProdOKQty;
|
||||
// if (ProdOKQty >= 0 && ProdALLQty > 0)
|
||||
// {
|
||||
// strOkRatio = (Math.Round(decimal.Parse(ProdOKQty.ToString()) / decimal.Parse(ProdALLQty.ToString()), 4)).ToString();
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// strOkRatio = "0.00";
|
||||
// }
|
||||
|
||||
// chart12Hours.Add(new Chart12HourData()
|
||||
// {
|
||||
// DisplayTime = m + ":30~" + (n) + ":30",
|
||||
// ProdIn = ProdALLQty,
|
||||
// ProdOut = ProdOKQty,
|
||||
// ProdNg = ProdNGQty,
|
||||
// OKRatio = strOkRatio
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// for (int i = 1; i < 13; i++)
|
||||
// {
|
||||
// #region X轴数据
|
||||
|
||||
// if (CommonMethods.strClass != "白班")
|
||||
// {
|
||||
// j = i + 21;
|
||||
|
||||
// if (j >= 25 && j < 26)
|
||||
// {
|
||||
// m = j - 2;
|
||||
// n = j - 25;
|
||||
// }
|
||||
// else if (j >= 26)
|
||||
// {
|
||||
// m = j - 26;
|
||||
// n = j - 25;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// m = j - 2;
|
||||
// n = j - 1;
|
||||
// }
|
||||
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// j = i + 9;
|
||||
// m = j - 2;
|
||||
// n = j - 1;
|
||||
// }
|
||||
// #endregion
|
||||
|
||||
// Random rand = new Random();
|
||||
// int ranOK = rand.Next(1000, 3000);
|
||||
// int ranNG = rand.Next(400, 1200);
|
||||
// int total = ranOK + ranNG;
|
||||
|
||||
// chart12Hours.Add(new Chart12HourData()
|
||||
// {
|
||||
// DisplayTime = m + ":30~" + n + ":30",
|
||||
// ProdIn = ranOK + ranNG,
|
||||
// ProdOut = ranOK,
|
||||
// ProdNg = ranNG,
|
||||
// OKRatio = (Math.Round(decimal.Parse(ranOK.ToString()) / decimal.Parse(total.ToString()) * 0.01m, 2) * 100).ToString()
|
||||
// });
|
||||
// }
|
||||
|
||||
// }
|
||||
|
||||
|
||||
// if (chart12Hours != null)
|
||||
// {
|
||||
// CommonMethods.Lst_Prod12HourData = chart12Hours;
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
public int GetHour()
|
||||
{
|
||||
DateTime now = DateTime.Now;
|
||||
int nowHour = Convert.ToInt32($"{now.Hour}30");
|
||||
int hour = 0;
|
||||
if (CommonMethods.LastHour != nowHour)
|
||||
{
|
||||
hour = Convert.ToInt32($"{now.Hour - 1}30");
|
||||
if (now.Hour == 0)
|
||||
{
|
||||
hour = 2330;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
hour = Convert.ToInt32($"{now.Hour}30");
|
||||
}
|
||||
return hour;
|
||||
}
|
||||
|
||||
|
||||
public void Refresh12HourProdQty()
|
||||
{
|
||||
List<Chart12HourData> chart12Hours = new List<Chart12HourData>();
|
||||
DateTime dtTime = DateTime.Now;
|
||||
string strDate = dtTime.ToString("yyyy-MM-dd");
|
||||
string nextDate = dtTime.AddDays(1).ToString("yyyy-MM-dd");
|
||||
int Hour = GetHour();
|
||||
|
||||
var listTotal = CommonMethods.db.QueryWhereList<Hourprod>(t =>
|
||||
t.FDate == strDate || t.FDate == nextDate);// 查询两天的数据
|
||||
|
||||
if (listTotal != null && listTotal.Count > 0)
|
||||
{
|
||||
for (int i = 0; i < 24; i++) // 改为24小时
|
||||
{
|
||||
int currentHour = (i + 8) % 24; // 从8点开始
|
||||
int nextHour = (currentHour + 1) % 24;
|
||||
string currentDate = currentHour >= 8 ? strDate : nextDate;
|
||||
|
||||
var hour1 = listTotal.Where(p =>
|
||||
p.FDate == currentDate &&
|
||||
p.FHour == currentHour).FirstOrDefault();
|
||||
|
||||
int ProdALLQty = 0;
|
||||
int ProdOKQty = 0;
|
||||
int ProdNGQty = 0;
|
||||
string strOkRatio = "0.00";
|
||||
if (hour1 != null)
|
||||
{
|
||||
ProdALLQty = hour1.ProdIn;
|
||||
ProdOKQty = hour1.ProdOut;
|
||||
}
|
||||
ProdNGQty = ProdALLQty - ProdOKQty;
|
||||
if (ProdOKQty >= 0 && ProdALLQty > 0)
|
||||
{
|
||||
strOkRatio = (Math.Round(decimal.Parse(ProdOKQty.ToString()) / decimal.Parse(ProdALLQty.ToString()), 4)).ToString();
|
||||
}
|
||||
string displayTime = $"{currentHour:D2}:30~{nextHour:D2}:29";
|
||||
|
||||
chart12Hours.Add(new Chart12HourData()
|
||||
{
|
||||
DisplayTime = displayTime,
|
||||
ProdIn = ProdALLQty,
|
||||
ProdOut = ProdOKQty,
|
||||
ProdNg = ProdNGQty,
|
||||
OKRatio = strOkRatio
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < 24; i++)
|
||||
{
|
||||
int currentHour = (i + 8) % 24;
|
||||
int nextHour = (currentHour + 1) % 24;
|
||||
|
||||
Random rand = new Random();
|
||||
int ranOK = rand.Next(1000, 3000);
|
||||
int ranNG = rand.Next(400, 1200);
|
||||
int total = ranOK + ranNG;
|
||||
string displayTime = $"{currentHour:D2}:30~{nextHour:D2}:29";
|
||||
chart12Hours.Add(new Chart12HourData()
|
||||
{
|
||||
DisplayTime = displayTime,
|
||||
ProdIn = ranOK + ranNG,
|
||||
ProdOut = ranOK,
|
||||
ProdNg = ranNG,
|
||||
OKRatio = (Math.Round(decimal.Parse(ranOK.ToString()) / decimal.Parse(total.ToString()) * 0.01m, 2) * 100).ToString()
|
||||
});
|
||||
}
|
||||
}
|
||||
if (chart12Hours != null)
|
||||
{
|
||||
CommonMethods.Lst_Prod12HourData = chart12Hours;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region 检测项不良
|
||||
/// <summary>
|
||||
/// 读取检测项不良
|
||||
/// </summary>
|
||||
private void GetDefectTypeQty()
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
if (CommonMethods.Lst_DefectTypeQty.Count <= 0)
|
||||
{
|
||||
|
||||
CommonMethods.Lst_DefectTypeQty.Add(new ChartDataType { DataType = "腔体真空NG", DataCount = 0 });
|
||||
CommonMethods.Lst_DefectTypeQty.Add(new ChartDataType { DataType = "清腔漏率检测NG", DataCount = 0 });
|
||||
|
||||
//CommonMethods.Lst_DefectTypeQty.Add(new ChartDataType { DataType = "厚度不良", DataCount = 0 });
|
||||
//CommonMethods.Lst_DefectTypeQty.Add(new ChartDataType { DataType = "焊前CCD正极耳顶面不良", DataCount = 0 });
|
||||
}
|
||||
else //读取PLC数据
|
||||
{
|
||||
if (CommonMethods.plcDevices[0] != null && CommonMethods.plcDevices[0].IsConnected)
|
||||
{
|
||||
Task.Run(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
int ProdDQTZKNgQty = CommonMethods.plcDevices[0].ReadInt32("D18068");//腔体真空NG
|
||||
int ProdDQTLLJCNgQty = CommonMethods.plcDevices[0].ReadInt32("D18072");//清腔漏率检测NG
|
||||
|
||||
//從PLC讀取數據賦值給到NGList
|
||||
CommonMethods.Lst_DefectTypeQty.Where(p => p.DataType == "腔体真空NG").FirstOrDefault().DataCount = ProdDQTZKNgQty;
|
||||
CommonMethods.Lst_DefectTypeQty.Where(p => p.DataType == "清腔漏率检测NG").FirstOrDefault().DataCount = ProdDQTLLJCNgQty;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Instance.WriteEX(ex);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Instance.WriteEX(ex);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 智能电表 ---默认D1300开始
|
||||
public void GetEnergyData()
|
||||
{
|
||||
ModbusRtu busRtuClient = new ModbusRtu();
|
||||
if (CommonMethods.plcDevices[0] != null && CommonMethods.plcDevices[0].IsConnected)
|
||||
{
|
||||
|
||||
List<ElectricEnergy> listM = new List<ElectricEnergy>();
|
||||
List<float> ListF = new List<float>();
|
||||
List<string> listAddre = new List<string>()
|
||||
{
|
||||
"D1100","D1102","D1104","D1106","D1108","D1110","D1112","D1114","D1116","D1118","D1120","D1122","D1124","D1126"
|
||||
};
|
||||
try
|
||||
{
|
||||
|
||||
ElectricEnergy m = new ElectricEnergy();
|
||||
for (int i = 0; i < listAddre.Count; i++)
|
||||
{
|
||||
ListF.Add(CommonMethods.plcDevices[0].ReadFloat(listAddre[i]));
|
||||
}
|
||||
|
||||
m.AccumulatedElectricity = (Decimal)ListF[0]; //有功电能(累计电能)D1100
|
||||
m.PhaseVoltageA = (Decimal)ListF[1]; ; //A相电压 D1102
|
||||
m.PhaseVoltageB = (Decimal)ListF[2]; ;//B相电压 D1104
|
||||
m.PhaseVoltageC = (Decimal)ListF[3]; ;//C相电压 D1106
|
||||
m.PhaseCurrentA = (Decimal)ListF[4]; ; //电流IA D1108
|
||||
m.PhaseCurrentB = (Decimal)ListF[5]; ;//电流IB D1110
|
||||
m.PhaseCurrentC = (Decimal)ListF[6]; ;//电流IC D1112
|
||||
m.PhasePowerA = (Decimal)ListF[7]; ; //A相有功功率 D1114
|
||||
m.PhasePowerB = (Decimal)ListF[8]; ; //B相有功功率 D1116
|
||||
m.PhasePowerC = (Decimal)ListF[9]; ;//C相有功功率 D1118
|
||||
m.ActivePower = (Decimal)ListF[10]; ; //总有功功率 D1120
|
||||
m.PowerFactor = (Decimal)ListF[11]; ; //总功率因数 D1122
|
||||
m.pt = (Decimal)ListF[12]; ; //电压变比 D1124
|
||||
m.ct = (Decimal)ListF[13]; ; //电流变比 D1126
|
||||
|
||||
listM.Add(m);
|
||||
CommonMethods.listElectricEnergy = listM;
|
||||
return;
|
||||
|
||||
busRtuClient.SerialPortInni(sp =>
|
||||
{
|
||||
sp.PortName = "COM1";
|
||||
sp.BaudRate = 9600;
|
||||
sp.DataBits = 8;
|
||||
sp.StopBits = System.IO.Ports.StopBits.One;
|
||||
sp.Parity = System.IO.Ports.Parity.None;
|
||||
});
|
||||
busRtuClient.DataFormat = (PLCCommunication.Common.DataFormat)DataFormat.CDAB;
|
||||
busRtuClient.Open(); // 打开
|
||||
if (busRtuClient.IsOpen())
|
||||
{
|
||||
// 读取数据
|
||||
JYResult<byte[]> readResult = busRtuClient.Read("35", 30);
|
||||
JYResult<byte[]> readResult1 = busRtuClient.Read("3", 2);
|
||||
if (readResult.IsSuccess && readResult1.IsSuccess)
|
||||
{
|
||||
//DPT
|
||||
Byte DptH = busRtuClient.ByteTransform.TransByte(readResult.Content, 0);//高8位
|
||||
Byte DptL = busRtuClient.ByteTransform.TransByte(readResult.Content, 1);//低8位
|
||||
//DPQ
|
||||
Byte DpqH = busRtuClient.ByteTransform.TransByte(readResult.Content, 2);//高8位
|
||||
Byte DpqL = busRtuClient.ByteTransform.TransByte(readResult.Content, 3);//低8位
|
||||
|
||||
m = new ElectricEnergy();
|
||||
Decimal q = busRtuClient.ByteTransform.TransUInt16(readResult.Content, 56);
|
||||
Decimal w = busRtuClient.ByteTransform.TransUInt16(readResult.Content, 58);
|
||||
|
||||
m.pt = Convert.ToDecimal(busRtuClient.ByteTransform.TransInt16(readResult1.Content, 0)); //电压变比 D1124
|
||||
m.ct = Convert.ToDecimal(busRtuClient.ByteTransform.TransInt16(readResult1.Content, 2)); //电流变比 D1126
|
||||
Decimal Power = (q * 65536 + w) / 1000;
|
||||
m.AccumulatedElectricity = Convert.ToDecimal(Power * m.pt * m.ct); //有功电能(累计电能)D1100
|
||||
ListF.Add((float)m.AccumulatedElectricity.Value);
|
||||
//电压
|
||||
m.PhaseVoltageA = Convert.ToDecimal(busRtuClient.ByteTransform.TransUInt16(readResult.Content, 4) * Math.Pow(10, DptH - 4)); //A相电压 D1102
|
||||
m.PhaseVoltageB = Convert.ToDecimal(busRtuClient.ByteTransform.TransUInt16(readResult.Content, 6) * Math.Pow(10, DptH - 4));//B相电压 D1104
|
||||
m.PhaseVoltageC = Convert.ToDecimal(busRtuClient.ByteTransform.TransUInt16(readResult.Content, 8) * Math.Pow(10, DptH - 4));//C相电压 D1106
|
||||
ListF.Add((float)m.PhaseVoltageA.Value);
|
||||
ListF.Add((float)m.PhaseVoltageB.Value);
|
||||
ListF.Add((float)m.PhaseVoltageC.Value);
|
||||
|
||||
//电流
|
||||
m.PhaseCurrentA = Convert.ToDecimal(busRtuClient.ByteTransform.TransUInt16(readResult.Content, 16) * Math.Pow(10, DptL - 4)); //电流IA D1108
|
||||
m.PhaseCurrentB = Convert.ToDecimal(busRtuClient.ByteTransform.TransUInt16(readResult.Content, 18) * Math.Pow(10, DptL - 4));//电流IB D1110
|
||||
m.PhaseCurrentC = Convert.ToDecimal(busRtuClient.ByteTransform.TransUInt16(readResult.Content, 20) * Math.Pow(10, DptL - 4));//电流IC D1112
|
||||
ListF.Add((float)m.PhaseCurrentA.Value);
|
||||
ListF.Add((float)m.PhaseCurrentB.Value);
|
||||
ListF.Add((float)m.PhaseCurrentC.Value);
|
||||
|
||||
//有功功率
|
||||
m.PhasePowerA = Convert.ToDecimal(busRtuClient.ByteTransform.TransInt16(readResult.Content, 22) * Math.Pow(10, DpqH - 4)); //A相有功功率 D1114
|
||||
m.PhasePowerB = Convert.ToDecimal(busRtuClient.ByteTransform.TransInt16(readResult.Content, 24) * Math.Pow(10, DpqH - 4)); //B相有功功率 D1116
|
||||
m.PhasePowerC = Convert.ToDecimal(busRtuClient.ByteTransform.TransInt16(readResult.Content, 26) * Math.Pow(10, DpqH - 4));//C相有功功率 D1118
|
||||
m.ActivePower = Convert.ToDecimal(busRtuClient.ByteTransform.TransInt16(readResult.Content, 28) * Math.Pow(10, DpqH - 4)); //总有功功率 D1120
|
||||
ListF.Add((float)m.PhasePowerA.Value);
|
||||
ListF.Add((float)m.PhasePowerB.Value);
|
||||
ListF.Add((float)m.PhasePowerC.Value);
|
||||
ListF.Add((float)m.ActivePower.Value);
|
||||
m.PowerFactor = Convert.ToDecimal(busRtuClient.ByteTransform.TransInt16(readResult.Content, 44)) / 1000; //总功率因数 D1122
|
||||
ListF.Add((float)m.PowerFactor.Value);
|
||||
ListF.Add((float)m.pt.Value);
|
||||
ListF.Add((float)m.ct.Value);
|
||||
|
||||
for (int i = 0; i < ListF.Count; i++)
|
||||
{
|
||||
CommonMethods.plcDevices[0].WriteFloat(listAddre[i], ListF[i]);
|
||||
}
|
||||
listM.Add(m);
|
||||
CommonMethods.listElectricEnergy = listM;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
TxtHelper.WriteTxt($@"D:\APILog\Logs\智能电表信息\{DateTime.Now.ToString("yyyy-MM-dd")}.txt", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "智能电表:读取数据失败");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
TxtHelper.WriteTxt($@"D:\APILog\Logs\智能电表信息\{DateTime.Now.ToString("yyyy-MM-dd")}.txt", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "智能电表:串口打开失败");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
TxtHelper.WriteTxt($@"D:\APILog\Logs\智能电表信息\{DateTime.Now.ToString("yyyy-MM-dd")}.txt", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + $"智能电表:{ex}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (busRtuClient.IsOpen())
|
||||
{
|
||||
busRtuClient.Close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void GetEnergyDataFormSerialPort()
|
||||
{
|
||||
if (CommonMethods.plcDevices[0] != null && CommonMethods.plcDevices[0].IsConnected)
|
||||
{
|
||||
if (CommonMethods.EM_modbusRtus != null && CommonMethods.EM_modbusRtus.Count > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
List<ElectricEnergy> listM = new List<ElectricEnergy>();
|
||||
decimal[] writPLC = new decimal[14];
|
||||
for (int i = 0; i < CommonMethods.EM_modbusRtus.Count; i++)
|
||||
{
|
||||
ModbusRtu busRtuClient = CommonMethods.EM_modbusRtus[i];
|
||||
if (!busRtuClient.IsOpen())
|
||||
busRtuClient.Open();
|
||||
if (busRtuClient.IsOpen())
|
||||
{
|
||||
// 读取数据
|
||||
JYResult<byte[]> readResult = busRtuClient.Read("35", 60); // 35是十进制,实际对应0x23,22个寄存器,1个寄存器2个字节
|
||||
JYResult<byte[]> readResult1 = busRtuClient.Read("3", 4);
|
||||
if (readResult.IsSuccess && readResult1.IsSuccess)
|
||||
{
|
||||
//DPT
|
||||
Byte DptH = busRtuClient.ByteTransform.TransByte(readResult.Content, 0);//高8位 实际对应0x23
|
||||
Byte DptL = busRtuClient.ByteTransform.TransByte(readResult.Content, 1);//低8位
|
||||
//DPQ
|
||||
Byte DpqH = busRtuClient.ByteTransform.TransByte(readResult.Content, 2);//高8位 实际对应0x24
|
||||
Byte DpqL = busRtuClient.ByteTransform.TransByte(readResult.Content, 3);//低8位
|
||||
|
||||
ElectricEnergy m = new ElectricEnergy();
|
||||
Decimal q = busRtuClient.ByteTransform.TransUInt16(readResult.Content, 56);
|
||||
Decimal w = busRtuClient.ByteTransform.TransUInt16(readResult.Content, 58);
|
||||
|
||||
m.pt = writPLC[12] = Convert.ToDecimal(busRtuClient.ByteTransform.TransInt16(readResult1.Content, 0)); //电压变比 D1124
|
||||
m.ct = writPLC[13] = Convert.ToDecimal(busRtuClient.ByteTransform.TransInt16(readResult1.Content, 2)); //电流变比 D1126
|
||||
Decimal Power = (q * 65536 + w) / 1000;
|
||||
m.AccumulatedElectricity = writPLC[0] = Convert.ToDecimal(Power * m.pt * m.ct); //有功电能(累计电能)D1100
|
||||
|
||||
//电压
|
||||
m.PhaseVoltageA = writPLC[1] = Convert.ToDecimal(busRtuClient.ByteTransform.TransUInt16(readResult.Content, 4) * Math.Pow(10, DptH - 4)); //A相电压 实际对应0x25
|
||||
m.PhaseVoltageB = writPLC[2] = Convert.ToDecimal(busRtuClient.ByteTransform.TransUInt16(readResult.Content, 6) * Math.Pow(10, DptH - 4));//B相电压 实际对应0x26
|
||||
m.PhaseVoltageC = writPLC[3] = Convert.ToDecimal(busRtuClient.ByteTransform.TransUInt16(readResult.Content, 8) * Math.Pow(10, DptH - 4));//C相电压 实际对应0x27
|
||||
|
||||
//电流
|
||||
m.PhaseCurrentA = writPLC[4] = Convert.ToDecimal(busRtuClient.ByteTransform.TransUInt16(readResult.Content, 16) * Math.Pow(10, DptL - 4)); //电流IA 实际对应0x2B
|
||||
m.PhaseCurrentB = writPLC[5] = Convert.ToDecimal(busRtuClient.ByteTransform.TransUInt16(readResult.Content, 18) * Math.Pow(10, DptL - 4)); //电流IB 实际对应0x2C
|
||||
m.PhaseCurrentC = writPLC[6] = Convert.ToDecimal(busRtuClient.ByteTransform.TransUInt16(readResult.Content, 20) * Math.Pow(10, DptL - 4)); //电流IC 实际对应0x2D
|
||||
|
||||
//有功功率
|
||||
m.PhasePowerA = writPLC[7] = Convert.ToDecimal(busRtuClient.ByteTransform.TransInt16(readResult.Content, 22) * Math.Pow(10, DpqH - 4)); //A相有功功率 实际对应0x2E
|
||||
m.PhasePowerB = writPLC[8] = Convert.ToDecimal(busRtuClient.ByteTransform.TransInt16(readResult.Content, 24) * Math.Pow(10, DpqH - 4)); //B相有功功率 实际对应0x2F
|
||||
m.PhasePowerC = writPLC[9] = Convert.ToDecimal(busRtuClient.ByteTransform.TransInt16(readResult.Content, 26) * Math.Pow(10, DpqH - 4)); //C相有功功率 实际对应0x30 D1118
|
||||
m.ActivePower = writPLC[10] = Convert.ToDecimal(busRtuClient.ByteTransform.TransInt16(readResult.Content, 28) * Math.Pow(10, DpqH - 4)); //总有功功率 实际对应0x31 D1120
|
||||
|
||||
m.PowerFactor = writPLC[11] = Convert.ToDecimal(busRtuClient.ByteTransform.TransInt16(readResult.Content, 44)) / 1000; //总功率因数 实际对应0x39 D1122
|
||||
|
||||
listM.Add(m);
|
||||
CommonMethods.listElectricEnergy = listM;
|
||||
//写入PLC
|
||||
CommonMethods.plcDevices[0].WriteValue($"D{(1300 + i * 28)}", Array.ConvertAll(writPLC, d => (float)d), PLC.DataType.ArrFloat);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
TxtHelper.WriteTxt($@"D:\APILog\Logs\智能电表信息\{DateTime.Now.ToString("yyyy-MM-dd")}.txt", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "智能电表:读取数据失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
TxtHelper.WriteTxt($@"D:\APILog\Logs\智能电表信息\{DateTime.Now.ToString("yyyy-MM-dd")}.txt", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + $"智能电表:{ex}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void GetEnergyDataFormPlc()
|
||||
{
|
||||
//if (CommonMethods.plcDevices.Count <= 0) { return; }
|
||||
if (CommonMethods.plcDevices[0] != null && CommonMethods.plcDevices[0].IsConnected && CommonMethods.energyMeter.Connected)
|
||||
{
|
||||
|
||||
List<ElectricEnergy> listM = new List<ElectricEnergy>();
|
||||
List<float> ListF = new List<float>();
|
||||
List<string> listAddre = new List<string>()
|
||||
{
|
||||
//"D1100","D1102","D1104","D1106","D1108","D1110","D1112","D1114","D1116","D1118","D1120","D1122","D1124","D1126"
|
||||
"D1300","D1302","D1304","D1306","D1308","D1310","D1312","D1314","D1316","D1318","D1320","D1322","D1324","D1326"
|
||||
};
|
||||
//00 00 00 00 00 06 报文头
|
||||
//01 04 00 00 00 3E 不需要校验位
|
||||
byte[] bytes = new byte[12];
|
||||
bytes[0] = Convert.ToByte(01 >> 8); // Slave id high byte//事务元标识符,高字节在前,低字节在后
|
||||
bytes[1] = Convert.ToByte(01);// Slave id low byte
|
||||
bytes[2] = 0x00;//协议标识符,高字节在前,低字节在后 (TCP 00)
|
||||
bytes[3] = 0x00;
|
||||
bytes[4] = 0x00;//后续字节长度,高字节在前,低字节在后
|
||||
bytes[5] = 0x06; // Message size
|
||||
bytes[6] = 0x01; // Slave address
|
||||
bytes[7] = 0x04;//功能码04H // Function code
|
||||
bytes[8] = Convert.ToByte((00 >> 8) & 0xFF); // Start address//寄存器起始地址高字节m_unBeginWord
|
||||
bytes[9] = Convert.ToByte(00 & 0xFF); // Start address//寄存器起始地址低字节m_unBeginWord
|
||||
bytes[10] = Convert.ToByte((0x58 >> 8) & 0xFF); // Number of data to read//寄存器数量高字节m_unWordsCount
|
||||
bytes[11] = Convert.ToByte(0x58 & 0xFF); // Number of data to read//寄存器数量低字节m_unWordsCount
|
||||
|
||||
try
|
||||
{
|
||||
CommonMethods.energyMeter.SendAndGetRes(bytes, ref listM);
|
||||
#region 读取×
|
||||
//for (int i = 0; i < listAddre.Count; i++)
|
||||
//{
|
||||
// float val = CommonMethods.plcDevices[0].ReadFloat(listAddre[i]);
|
||||
// ListF.Add(val);
|
||||
//}
|
||||
|
||||
//ElectricEnergy m = new ElectricEnergy();
|
||||
//m.AccumulatedElectricity = Convert.ToDecimal(ListF[0]);
|
||||
//m.PhaseVoltageA = Convert.ToDecimal(ListF[1]);
|
||||
//m.PhaseVoltageB = Convert.ToDecimal(ListF[2]);
|
||||
//m.PhaseVoltageC = Convert.ToDecimal(ListF[3]);
|
||||
//m.PhaseCurrentA = Convert.ToDecimal(ListF[4]);
|
||||
//m.PhaseCurrentB = Convert.ToDecimal(ListF[5]);
|
||||
//m.PhaseCurrentC = Convert.ToDecimal(ListF[6]);
|
||||
//m.PhasePowerA = Convert.ToDecimal(ListF[7]);
|
||||
//m.PhasePowerB = Convert.ToDecimal(ListF[8]);
|
||||
//m.PhasePowerC = Convert.ToDecimal(ListF[9]);
|
||||
//m.ActivePower = Convert.ToDecimal(ListF[10]);
|
||||
//m.PowerFactor = Convert.ToDecimal(ListF[11]);
|
||||
//m.pt = Convert.ToDecimal(ListF[12]);
|
||||
//m.ct = Convert.ToDecimal(ListF[13]);
|
||||
|
||||
//listM.Add(m);
|
||||
#endregion
|
||||
foreach (ElectricEnergy energy in listM)
|
||||
{
|
||||
CommonMethods.plcDevices[0].WriteFloat(listAddre[0], (float)energy.AccumulatedElectricity);
|
||||
CommonMethods.plcDevices[0].WriteFloat(listAddre[1], (float)energy.PhaseVoltageA);
|
||||
CommonMethods.plcDevices[0].WriteFloat(listAddre[2], (float)energy.PhaseVoltageB);
|
||||
CommonMethods.plcDevices[0].WriteFloat(listAddre[3], (float)energy.PhaseVoltageC);
|
||||
CommonMethods.plcDevices[0].WriteFloat(listAddre[4], (float)energy.PhaseCurrentA);
|
||||
CommonMethods.plcDevices[0].WriteFloat(listAddre[5], (float)energy.PhaseCurrentB);
|
||||
CommonMethods.plcDevices[0].WriteFloat(listAddre[6], (float)energy.PhaseCurrentC);
|
||||
CommonMethods.plcDevices[0].WriteFloat(listAddre[7], (float)energy.PhasePowerA);
|
||||
CommonMethods.plcDevices[0].WriteFloat(listAddre[8], (float)energy.PhasePowerB);
|
||||
CommonMethods.plcDevices[0].WriteFloat(listAddre[9], (float)energy.PhasePowerC);
|
||||
|
||||
CommonMethods.plcDevices[0].WriteFloat(listAddre[10], (float)energy.ActivePower);
|
||||
CommonMethods.plcDevices[0].WriteFloat(listAddre[11], (float)energy.PowerFactor);
|
||||
CommonMethods.plcDevices[0].WriteFloat(listAddre[12], (float)energy.pt);
|
||||
CommonMethods.plcDevices[0].WriteFloat(listAddre[13], (float)energy.ct);
|
||||
}
|
||||
CommonMethods.listElectricEnergy = listM;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
TxtHelper.WriteTxt($@"D:\APILog\Logs\智能电表信息\{DateTime.Now.ToString("yyyy-MM-dd")}.txt", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + $"智能电表:{ex}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 能耗上传MES
|
||||
/// </summary>
|
||||
public async void UploadEquEnergy()
|
||||
{
|
||||
if (CommonMethods.listElectricEnergy.Count > 0)
|
||||
{
|
||||
if (CommonMethods.mesConfig.isUpMes)
|
||||
{
|
||||
string msg = string.Empty;
|
||||
DateTime nowDate = DateTime.Now;
|
||||
string filePath = string.Empty;
|
||||
string fileName = string.Empty;
|
||||
string fileTitle = string.Empty;
|
||||
string fileContent = string.Empty;
|
||||
List<WattrMeter> listP = new List<WattrMeter>();
|
||||
int i = 0;
|
||||
foreach (ElectricEnergy m in CommonMethods.listElectricEnergy)
|
||||
{
|
||||
WattrMeter param = new WattrMeter();
|
||||
param.siteCode = CommonMethods.mesConfig.siteCode;
|
||||
param.lineCode = CommonMethods.mesConfig.lineCode;
|
||||
param.equipNum = CommonMethods.mesConfig.equipNum;
|
||||
param.userName = CommonMethods.mesConfig.mesUserName;
|
||||
param.electricMeterNum = CommonMethods.mesConfig.electricMeterNum[i];
|
||||
param.recordDate = nowDate.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
param.accumulatedElectricity = (decimal)m.AccumulatedElectricity;
|
||||
param.phaseVoltageA = (decimal)m.PhaseVoltageA;
|
||||
param.phaseVoltageB = (decimal)m.PhaseVoltageB;
|
||||
param.phaseVoltageC = (decimal)m.PhaseVoltageC;
|
||||
param.phaseCurrentA = (decimal)m.PhaseCurrentA;
|
||||
param.phaseCurrentB = (decimal)m.PhaseCurrentB;
|
||||
param.phaseCurrentC = (decimal)m.PhaseCurrentC;
|
||||
param.phasePowerA = (decimal)m.PhasePowerA;
|
||||
param.phasePowerB = (decimal)m.PhasePowerB;
|
||||
param.phasePowerC = (decimal)m.PhasePowerC;
|
||||
param.activePower = (decimal)m.ActivePower;
|
||||
param.powerFactor = (decimal)m.PowerFactor;
|
||||
param.pt = (decimal)m.pt;
|
||||
param.ct = (decimal)m.ct;
|
||||
|
||||
listP.Add(param);
|
||||
i++;
|
||||
|
||||
// 写csv文件
|
||||
string productName = string.Empty;
|
||||
|
||||
filePath = $@"{CommonMethods.strWattrMeterpath}\{nowDate.Year}\{nowDate.Month}";
|
||||
fileName = $@"{nowDate.Day}.csv";
|
||||
fileTitle = "工序名称,生产机台编号,拉线,型号,物料编号,操作人员,班次,电表编号,采集时间,有功电能(累计电能)KwH,A相电压 V,B相电压 V,C相电压 V,A相电流 A,B相电流 A,C相电流 A,A相功率 Kw,B相功率 Kw,C相功率 Kw,总有功功率 Kw,总功率因数 %,电压变比,电流变比";
|
||||
fileContent = $"入壳A工序,{CommonMethods.mesConfig.equipNum},{CommonMethods.mesConfig.lineCode},{productName},{CommonMethods.mesConfig.MaterialCode},{CommonMethods.mesConfig.mesUserName},{CommonMethods.strClass},{param.electricMeterNum},{nowDate.ToString("yyyy-MM-dd HH:mm:ss")}," +
|
||||
$"{param.accumulatedElectricity},{param.phaseVoltageA},{param.phaseVoltageB},{param.phaseVoltageC},{param.phaseCurrentA},{param.phaseCurrentB},{param.phaseCurrentC}," +
|
||||
$"{param.phasePowerA},{param.phasePowerB},{param.phasePowerC},{param.activePower},{param.powerFactor},{param.pt},{param.ct}";
|
||||
CSVHelper<object>.WriterCSV(filePath, fileName, fileTitle, fileContent);
|
||||
}
|
||||
//上传MES
|
||||
var (success, mesg) = await CommonMethods.hbgMes.UploadEquEnergyAsync(CommonMethods.mesConfig.EnergyConsumption, CommonMethods.mesConfig.equipNum, CommonMethods.mesConfig.siteCode, CommonMethods.mesConfig.lineCode,
|
||||
CommonMethods.mesConfig.mesUserName, CommonMethods.mesConfig.electricMeterNum, listP);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] CalculateCRCTemp(ref byte[] messageArray, int dataLength)
|
||||
{
|
||||
byte usCRCHi = 0xFF;
|
||||
byte usCRCLo = 0xFF;
|
||||
byte[] returnResult = { 0x00, 0x00 };
|
||||
int index = 0;
|
||||
int messageIndex = 0;
|
||||
while (dataLength > 0)
|
||||
{
|
||||
index = usCRCLo ^ messageArray[messageIndex];
|
||||
usCRCLo = Convert.ToByte(usCRCHi ^ crcHi[index]);
|
||||
usCRCHi = crcLo[index];
|
||||
messageIndex++;
|
||||
dataLength--;
|
||||
}
|
||||
//0th item is crcLo
|
||||
returnResult[0] = usCRCLo;
|
||||
//1st item is crcHi
|
||||
returnResult[1] = usCRCHi;
|
||||
//2nd item is the total CRC16.
|
||||
//returnResult[2] = Convert.ToByte((usCRCHi << 8 | usCRCLo));
|
||||
return returnResult;
|
||||
}
|
||||
|
||||
static byte[] crcHi =
|
||||
{
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
|
||||
0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
|
||||
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
|
||||
0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81,
|
||||
0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
|
||||
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
|
||||
0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
|
||||
0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
|
||||
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
|
||||
0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
|
||||
0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
|
||||
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
|
||||
0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
|
||||
0x40
|
||||
};
|
||||
|
||||
static byte[] crcLo =
|
||||
{
|
||||
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4,
|
||||
0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
|
||||
0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD,
|
||||
0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
|
||||
0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7,
|
||||
0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
|
||||
0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE,
|
||||
0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
|
||||
0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2,
|
||||
0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
|
||||
0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB,
|
||||
0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
|
||||
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91,
|
||||
0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
|
||||
0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88,
|
||||
0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
|
||||
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80,
|
||||
0x40
|
||||
};
|
||||
#endregion
|
||||
|
||||
#region 定期清理本地保存下料数据
|
||||
/// <summary>
|
||||
/// 定期清理本地保存下料数据
|
||||
/// </summary>
|
||||
public void DeleteFileOfLocal()
|
||||
{
|
||||
try
|
||||
{
|
||||
//string filePath = string.Format(@"{0}\Data\{1}", Application.StartupPath, DateTime.Now.AddDays(-365).Year);
|
||||
string filePath = string.Format(@"{0}\{1}", @"D:\LocalData", DateTime.Now.AddYears(-1).Year);
|
||||
if (Directory.Exists(filePath))
|
||||
{
|
||||
//判断是否有子文件夹,循环删除
|
||||
for (int i = 1; i <= DateTime.Now.Month; i++)
|
||||
{
|
||||
string tempPath = string.Format(@"{0}\{1}", filePath, i);
|
||||
if (Directory.Exists(tempPath))
|
||||
{
|
||||
LogCLear_DirOrFile("下料本地文件", DateTime.Now, tempPath, 365);
|
||||
}
|
||||
}
|
||||
//没有子文件夹,删除目录
|
||||
DirectoryInfo[] DirList = GetSubcatalog(filePath);//获取文件路径
|
||||
if (DirList != null)//目录存在
|
||||
{
|
||||
if (DirList.Length == 0)
|
||||
{
|
||||
Directory.Delete(filePath, true);
|
||||
Thread.Sleep(10);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Instance.WriteLog($"定期清理本地保存下料数据异常:{ex.ToString()}", "Info");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>清理指定目录下的文件夹或文件 </summary>
|
||||
/// <param name="strName">清理文件注释</param>
|
||||
/// <param name="CurrentTime">当前时间</param>
|
||||
/// <param name="FilePath">文件目录</param>
|
||||
/// <param name="RtnDayNums">保留天数</param>
|
||||
public void LogCLear_DirOrFile(string strName, DateTime CurrentTime, string FilePath, uint RtnDayNums)
|
||||
{
|
||||
long time = 0;
|
||||
Stopwatch stopWatch = new Stopwatch();
|
||||
stopWatch.Start();
|
||||
bool b_OK = true;
|
||||
LogHelper.Instance.WriteLog($"清理{strName}开始", "Info");
|
||||
try
|
||||
{
|
||||
DirectoryInfo[] DirList = GetSubcatalog(FilePath);//获取文件路径
|
||||
if (DirList != null)//目录不存在
|
||||
{
|
||||
if (DirList.Length != 0)//目录是否有子文件夹:如 文件夹“2021-10-31”
|
||||
{
|
||||
#region 删文件夹
|
||||
foreach (var dirInfo in DirList)//循环删除文件夹
|
||||
{
|
||||
DateTime itemDateTime = dirInfo.CreationTime;//获取文件夹创建日期
|
||||
if (CurrentTime.AddDays(-RtnDayNums) > itemDateTime)
|
||||
{
|
||||
if (dirInfo.Exists)
|
||||
{
|
||||
dirInfo.Delete(true);
|
||||
Thread.Sleep(10);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
else//目录下无子文件夹存在时,判断是否有文件
|
||||
{
|
||||
string[] fileList = null;
|
||||
if (Directory.Exists(FilePath))
|
||||
{
|
||||
//获取文件列表
|
||||
fileList = Directory.GetFiles(FilePath);
|
||||
if (fileList != null)//目录不存在
|
||||
{
|
||||
if (fileList.Length != 0)//目录下是否存在文件
|
||||
{
|
||||
#region 删除文件
|
||||
foreach (var filePath in fileList)//循环删除文件
|
||||
{
|
||||
DateTime itemDateTime = new FileInfo(filePath).LastWriteTime;//获取文件修改日期
|
||||
if (CurrentTime.AddDays(-RtnDayNums) > itemDateTime)
|
||||
{
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
File.Delete(filePath);
|
||||
Thread.Sleep(2);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
else
|
||||
{
|
||||
#region 删除文件夹
|
||||
Directory.Delete(FilePath, true);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
b_OK = true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
b_OK = false;
|
||||
}
|
||||
stopWatch.Stop();
|
||||
time = stopWatch.ElapsedMilliseconds;
|
||||
LogHelper.Instance.WriteLog($"{FilePath},清理结束,结果;{b_OK},耗时:{time}", "Info");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定目录中所有文件列表
|
||||
/// </summary>
|
||||
/// <param name="directoryPath">指定目录的绝对路径</param>
|
||||
public DirectoryInfo[] GetSubcatalog(string directoryPath)
|
||||
{
|
||||
DirectoryInfo dir = new DirectoryInfo(directoryPath);
|
||||
//如果目录存在
|
||||
if (dir.Exists)
|
||||
{
|
||||
//获取文件列表
|
||||
DirectoryInfo[] dirs = dir.GetDirectories();
|
||||
return dirs;
|
||||
}
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user