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 { /// /// 电表管理 /// public class ElecMeterEqmManger { #region 定义锁 private object lockObj = new object(); //线程同步锁 #endregion 定义锁 #region 字段属性 /// /// 决定去读取参数 /// private bool _is_read = true; /// /// 决定去读取参数 /// public bool F_IsRead { get { return _is_read; } set { lock (lockObj) { _is_read = value; } } } #endregion 字段属性 /// /// 电表通讯连接线程 /// 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(); } /// /// 电表通讯连接 /// 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 电表每天电能统计 /// /// 上一次采集电表总有功电能二次侧的时间点,精确到小时 /// private DateTime _emeter_pre_time = DateTime.Now; /// /// 上一次采集到的总有功电能二次侧 /// private Dictionary _pre_total_active_energys = new Dictionary(); /// /// 读取参数值方法 /// private void GetEMeterDayEnergyCalcResult() { while (_is_read) { DateTime checkTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:00:00")); Dictionary 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); } } /// /// 获取当前所有电表的累计能耗值 /// /// private Dictionary GetCurrentTotalActiveEnergys() { Dictionary total_active_energys = new Dictionary(); 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 换电站每天电能统计 /// /// 上一次统计的时间点 /// private DateTime _station_pre_time = DateTime.Now; /// /// 换电站每天能耗值统计,M001:主控柜电表,M002:水冷电表;M003~M010充电机电表 /// 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 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 /// 获取电表采集的常用参数结果列表 /// /// public List GetEMeterParamResultList() { List result = new List(); 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; } } }