You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

326 lines
12 KiB

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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Monitor.Models;
using DataBase.MySql.Tool;
namespace Module.EMeter.ModbusTcp
{
/// <summary>
/// 电表管理
/// </summary>
public class ElecMeterEqmManger
{
#region 定义锁
private object lockObj = new object(); //线程同步锁
#endregion 定义锁
#region 字段属性
/// <summary>
/// 决定去读取参数
/// </summary>
private bool _is_read = true;
/// <summary>
/// 决定去读取参数
/// </summary>
public bool F_IsRead
{
get
{
return _is_read;
}
set
{
lock (lockObj)
{
_is_read = value;
}
}
}
#endregion 字段属性
/// <summary>
/// 电表通讯连接线程
/// </summary>
public void ElecMeterNetConnectThread()
{
Thread emeterConnectThread = new Thread(ElecMeterNetConnectFunc);
emeterConnectThread.IsBackground = true;
emeterConnectThread.Start();
Thread.Sleep(10 * 1000);
Thread eMeterEnergyThread = new Thread(GetEMeterDayEnergyCalcResult);
eMeterEnergyThread.IsBackground = true;
eMeterEnergyThread.Start();
Thread staDayEnergyThread = new Thread(GetStationDayEnergyCalcResult);
staDayEnergyThread.IsBackground = true;
staDayEnergyThread.Start();
}
/// <summary>
/// 电表通讯连接
/// </summary>
private void ElecMeterNetConnectFunc()
{
while (true)
{
Thread.Sleep(200);
if (!CmnElecMeterBaseInfo.EBC01.F_NetConnected)
{
CmnElecMeterBaseInfo.EBC01.Connect();
}
Thread.Sleep(200);
if (!CmnElecMeterBaseInfo.EBC02.F_NetConnected)
{
CmnElecMeterBaseInfo.EBC02.Connect();
}
Thread.Sleep(200);
if (!CmnElecMeterBaseInfo.EBC03.F_NetConnected)
{
CmnElecMeterBaseInfo.EBC03.Connect();
}
Thread.Sleep(200);
if (!CmnElecMeterBaseInfo.EBC04.F_NetConnected)
{
CmnElecMeterBaseInfo.EBC04.Connect();
}
Thread.Sleep(100);
if (!CmnElecMeterBaseInfo.EBC05.F_NetConnected)
{
CmnElecMeterBaseInfo.EBC05.Connect();
}
Thread.Sleep(100);
if (!CmnElecMeterBaseInfo.EBC06.F_NetConnected)
{
CmnElecMeterBaseInfo.EBC06.Connect();
}
Thread.Sleep(100);
if (!CmnElecMeterBaseInfo.EBC07.F_NetConnected)
{
CmnElecMeterBaseInfo.EBC07.Connect();
}
Thread.Sleep(100);
Thread.Sleep(100);
}
}
#region 电表每天电能统计
/// <summary>
/// 上一次采集电表总有功电能二次侧的时间点,精确到小时
/// </summary>
private DateTime _emeter_pre_time = DateTime.Now;
/// <summary>
/// 上一次采集到的总有功电能二次侧
/// </summary>
private Dictionary<string, UInt32> _pre_total_active_energys = new Dictionary<string, uint>();
/// <summary>
/// 读取参数值方法
/// </summary>
private void GetEMeterDayEnergyCalcResult()
{
while (_is_read)
{
DateTime checkTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:00:00"));
Dictionary<string, UInt32> total_active_energys = GetCurrentTotalActiveEnergys();
if (checkTime > _emeter_pre_time)
{
foreach (var q in total_active_energys)
{
string currEqmNo = q.Key;
UInt32 currEnergy = q.Value;
if (_pre_total_active_energys.ContainsKey(currEqmNo))
{
UInt32 preEnergy = _pre_total_active_energys[currEqmNo];
//存储仪表累计值参数结果
new ElecMeterParamToMySQL().AddTotalEnergyToMySql(currEqmNo, currEnergy, DateTime.Now);
Thread.Sleep(500);
//计算小时能耗变化值
UInt32 hourValue = currEnergy - preEnergy;
new ElecMeterParamToMySQL().AddHourEnergyToMySql(currEqmNo, hourValue, _emeter_pre_time, checkTime);
_pre_total_active_energys[currEqmNo] = currEnergy;
Thread.Sleep(500);
}
else
{
_pre_total_active_energys.Add(currEqmNo, currEnergy);
//存储仪表累计值参数结果
new ElecMeterParamToMySQL().AddTotalEnergyToMySql(currEqmNo, currEnergy, DateTime.Now);
Thread.Sleep(500);
}
Thread.Sleep(500);
}
_emeter_pre_time = checkTime;
}
else if (checkTime < _emeter_pre_time)
{
foreach (var q in total_active_energys)
{
string currEqmNo = q.Key;
UInt32 currEnergy = q.Value;
//存储仪表累计值参数结果
new ElecMeterParamToMySQL().AddTotalEnergyToMySql(currEqmNo, currEnergy, DateTime.Now);
if (!_pre_total_active_energys.ContainsKey(currEqmNo))
{
_pre_total_active_energys.Add(currEqmNo, currEnergy);
}
Thread.Sleep(500);
}
_emeter_pre_time = checkTime;
}
Thread.Sleep(5000);
}
}
/// <summary>
/// 获取当前所有电表的累计能耗值
/// </summary>
/// <returns></returns>
private Dictionary<string, UInt32> GetCurrentTotalActiveEnergys()
{
Dictionary<string, UInt32> total_active_energys = new Dictionary<string, uint>();
if (CmnElecMeterBaseInfo.EBC01.F_NetConnected)
{
total_active_energys.Add(CmnElecMeterBaseInfo.EBC01.F_EqmCode, Convert.ToUInt32(CmnElecMeterBaseInfo.EBC01.F_TotalActiveEnergy));
}
if (CmnElecMeterBaseInfo.EBC02.F_NetConnected)
{
total_active_energys.Add(CmnElecMeterBaseInfo.EBC02.F_EqmCode, Convert.ToUInt32(CmnElecMeterBaseInfo.EBC02.F_TotalActiveEnergy));
}
if (CmnElecMeterBaseInfo.EBC03.F_NetConnected)
{
total_active_energys.Add(CmnElecMeterBaseInfo.EBC03.F_EqmCode, Convert.ToUInt32(CmnElecMeterBaseInfo.EBC03.F_TotalActiveEnergy));
}
return total_active_energys;
}
#endregion 电表每天电能统计
#region 换电站每天电能统计
/// <summary>
/// 上一次统计的时间点
/// </summary>
private DateTime _station_pre_time = DateTime.Now;
/// <summary>
/// 换电站每天能耗值统计,M001:主控柜电表M002:水冷电表M003~M010充电机电表
/// </summary>
private async void GetStationDayEnergyCalcResult()
{
while (_is_read)
{
DateTime checkTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:00:00")).AddMinutes(5);
if (checkTime > _station_pre_time)
{
List<TEgEmeterDayEnergyValue> emeterDayResult = await new SEgEMeterDayEnergyValue().AddEMeterDayEnergyValue();
if(emeterDayResult!= null)
{
if(emeterDayResult.Count>0)
{
TEgElecDayStatistic dayResult = new TEgElecDayStatistic();
dayResult.FDayChargingValue = 0;
dayResult.FDaySwitchingValue = 0;
dayResult.FDayOtherValue = 0;
dayResult.FDayTotalValue = 0;
for (int i=0;i<emeterDayResult.Count;i++)
{
switch(emeterDayResult[i].FEmeterNo)
{
case "M001":
{
dayResult.FDayTotalValue += emeterDayResult[i].FDayValue;
break;
}
case "M002":
{
dayResult.FDayTotalValue += emeterDayResult[i].FDayValue;
break;
}
case "M003":
{
dayResult.FDayTotalValue += emeterDayResult[i].FDayValue;
break;
}
}
}
dayResult.FDayTotalValue = dayResult.FDaySwitchingValue + dayResult.FDayChargingValue + dayResult.FDayOtherValue;
dayResult.FStartTime = emeterDayResult[0].FStartTime;
dayResult.FEndTime = emeterDayResult[0].FEndTime;
await new SEgElecDayStatistics().AddElecDayStatistic(dayResult);
Thread.Sleep(1000);
}
}
_station_pre_time = checkTime;
}
Thread.Sleep(10000);
}
}
#endregion 换电站每天电能统计
/// <summary>
/// 获取电表采集的常用参数结果列表
/// </summary>
/// <returns></returns>
public List<MEMeterParamResult> GetEMeterParamResultList()
{
List<MEMeterParamResult> result = new List<MEMeterParamResult>();
if (CmnElecMeterBaseInfo.EBC01.F_NetConnected)
{
result.Add(CmnElecMeterBaseInfo.EBC01.F_EMeterParamResult);
}
if (CmnElecMeterBaseInfo.EBC02.F_NetConnected)
{
result.Add(CmnElecMeterBaseInfo.EBC02.F_EMeterParamResult);
}
if (CmnElecMeterBaseInfo.EBC03.F_NetConnected)
{
result.Add(CmnElecMeterBaseInfo.EBC03.F_EMeterParamResult);
}
result.Add(CmnElecMeterBaseInfo.EBC04.F_EMeterParamResult);
result.Add(CmnElecMeterBaseInfo.EBC05.F_EMeterParamResult);
result.Add(CmnElecMeterBaseInfo.EBC06.F_EMeterParamResult);
result.Add(CmnElecMeterBaseInfo.EBC07.F_EMeterParamResult);
return result;
}
}
}