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.DataCollector/DataCollectorFactory.cs

153 lines
5.5 KiB
C#

using Admin.Core.IService.IService_New;
using Admin.Core.Model.Model_New;
using Aucma.Core.DataCollector.Factory;
using Aucma.Core.HwPLc;
using log4net;
using NPOI.SS.Formula.Functions;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Aucma.Core.DataCollector
{
/// <summary>
/// 设备数据采集
/// </summary>
public abstract class DataCollectorFactory
{
public readonly log4net.ILog _logger = LogManager.GetLogger(typeof(DataCollectorFactory));
public readonly IBaseDeviceParamServices _deviceParamServices;
public readonly IRecordDeviceAlarmInfoServices _deviceAlarmInfoServices;
public readonly IRecordDeviceElectricityServices _deviceElectricityServices;
public int AlarmReadTimer = (1000 * 5);
public int EleReadTimer = (1000 * 60 * 5);
public DataCollectorFactory(IBaseDeviceParamServices deviceParamServices, IRecordDeviceAlarmInfoServices deviceAlarmInfoServices, IRecordDeviceElectricityServices deviceElectricityServices)
{
_deviceParamServices = deviceParamServices;
_deviceAlarmInfoServices = deviceAlarmInfoServices;
_deviceElectricityServices = deviceElectricityServices;
}
/// <summary>
/// 采集设备报警信息
/// </summary>
/// <param name="deviceAlarmInfos"></param>
public abstract void CollectDeviceAlarmInfo(out List<Record_DeviceAlarmInfo> deviceAlarmInfos);
/// <summary>
/// 采集设备用电
/// </summary>
/// <param name="deviceElectricity"></param>
public abstract void CollectDeviceElectricity(out List<Record_DeviceElectricity> deviceElectricitys);
/// <summary>
/// 根据PLC地址读取设备用电
/// </summary>
/// <param name="address"></param>
/// <param name="plc"></param>
/// <param name="deviceElectricity"></param>
public void ReadDeviceElectricity(string[] address,IPlc plc,ref Record_DeviceElectricity deviceElectricity)
{
var ground = plc.ReadRandomInt32(address);
if (ground != null)
{
if (ground.Length >= 10)
{
var info = ParseAndReverseByteArray(ground, 0, address.Length * 2);
if(info != null)
{
if(info.Length >= 10)
{
//deviceElectricity.Glys = ground[0];
deviceElectricity.Va = (decimal)info[0]/100;
deviceElectricity.Vb = (decimal)info[1]/100;
deviceElectricity.Vc = (decimal)info[2]/100;
deviceElectricity.Ia = (decimal)info[6]/100;
deviceElectricity.Ib = (decimal)info[7]/100;
deviceElectricity.Ic = (decimal)info[8]/100;
deviceElectricity.Zxyg = (decimal)info[9]/100;
deviceElectricity.CollectType = 0;
deviceElectricity.CollectTime = DateTime.Now;
deviceElectricity.RecordTime = DateTime.Now;
}
}
}
}
else
{
deviceElectricity = null;
}
}
/// <summary>
/// 读取PLC数据
/// </summary>
/// <param name="_plc"></param>
/// <param name="addr"></param>
/// <param name="dataType"></param>
/// <param name="paramValue"></param>
public void ReadParamValueByPlc(IPlc _plc, string addr, string dataType, out int paramValue)
{
object result = null;
switch (dataType)
{
case "int":
result = _plc.ReadInt16(addr);
break;
case "float":
result = _plc.ReadFloat(addr);
break;
case "bool":
result = _plc.ReadBool(addr);
break;
default:
result = _plc.ReadInt16(addr);
break;
}
if (result == null)
{
result = "0";
}
paramValue = Convert.ToInt32(result);
}
/// <summary>
/// 电表数据解析
/// </summary>
/// <param name="data"></param>
/// <param name="startIndex"></param>
/// <param name="length"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
private int[] ParseAndReverseByteArray(byte[] data, int startIndex, int length)
{
if (data == null || data.Length < startIndex + length)
{
throw new ArgumentException("Invalid data or startIndex/length parameter.");
}
int[] resultArray = new int[length / 2];
for (int i = 0; i < resultArray.Length; i++)
{
byte[] subset = new byte[2];
Array.Copy(data, startIndex + (i * 2), subset, 0, 2);
Array.Reverse(subset);
string hexString = BitConverter.ToString(subset).Replace("-", "");
resultArray[i] = Convert.ToInt32(hexString, 16);
}
return resultArray;
}
}
}