Sic03-8inch/FrameworkLocal/RTEquipmentLibrary/HardwareUnits/Temps/AKunTemp/AKunTemp.cs

291 lines
8.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Aitex.Core.RT.DataCenter;
using Aitex.Core.RT.Device;
using Aitex.Core.RT.Event;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.SCCore;
using Aitex.Core.Util;
using Global;
using MECF.Framework.Common.Communications;
using RTOverEthernetDevelopmentKit;
using SessionLayer;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Temps
{
public class AKunTemp : BaseDevice, IDevice, IConnection, ITempData
{
#region Variables
private readonly IROverEthernet _ir;
private PeriodicJob _thread;//工作线程
private readonly R_TRIG _trigConnService = new R_TRIG();//连接昂坤软件服务器
private readonly R_TRIG _trigDataError = new R_TRIG();//机箱USB断联
private readonly R_TRIG _trigDataOK = new R_TRIG();//机箱USB重连
private int _lastIntegTime;
public double TempData1 { get; set; }
public double TempData2 { get; set; }
public double TempData3 { get; set; }
public double TempData4 { get; set; }
bool _enableLog;
#endregion
#region Constructors
public AKunTemp(string module, string name) : base(module, name, name, name)
{
string[] data = SC.GetStringValue($"{Name}.Address").Split(':');
Address = data[0];
port = Convert.ToInt32(data[1]);
_enableLog = SC.GetValue<bool>($"{Name}.EnableLogMessage");
_ir = new IROverEthernet();
_ir.CommunicationBroken += RT_CommunicationBroken;//测试下来这个事件不是很靠谱,先留着
_trigDataOK.CLK = true;//初始化重连信号
DATA.Subscribe($"PM1.{Name}.Middle", () => TempData1);
DATA.Subscribe($"PM1.{Name}.Outer", () => TempData2);
DATA.Subscribe($"PM2.{Name}.Middle", () => TempData3);
DATA.Subscribe($"PM2.{Name}.Outer", () => TempData4);
}
~AKunTemp()
{
Terminate();
}
#endregion
#region Properties
public string Address
{
get;
private set;
}
private int port { get; set; }
public bool IsConnected => _ir.IsConnected;
#endregion
#region Methods
/// <summary>
/// 获取系统配置中的积分时间。
/// </summary>
/// <returns></returns>
private int GetScIntegrationTime()
{
var integrationTime = SC.GetValue<int>($"{Name}.IntegrationTime"); // the minimum value should be 10ms.
if (integrationTime < 10)
integrationTime = 10;
return integrationTime;
}
public bool Connect()
{
try
{
_ir.Init(Address, port);
_lastIntegTime = GetScIntegrationTime();
_ir.SetIntegrationTime(_lastIntegTime);
_ir.StartIR();
return true;
}
catch (Exception ex)
{
return false;
}
}
private void Reconnect()
{
try
{
_ir.Close();
}
catch
{
}
finally { Connect(); }
}
private bool OnTimer()
{
try
{
//if (_ir.Current.Status != 0)//昂坤提供的SUB断联属性测试发现大约半分钟才会提示断联
//{
// _trigDataError.CLK = true;
// if (_trigDataError.Q)
// {
// ConnectMesError();
// }
// return true;
//}
//_trigDataError.RST = true;//复位USB线断联信号
if (_ir != null && _ir.IsConnected)
{
GetValues();
return true;
}
Reconnect();
_trigConnService.CLK = _ir.IsConnected;
if (!_trigConnService.Q)
{
ConnectMesError();
}
System.Threading.Thread.Sleep(2000);
return true;
}
catch (Exception ex)
{
if (_trigConnService.Q)
{
EV.PostAlarmLog(Module, $"Unable to read data from viper RTC, {ex.Message}");
}
return true;
}
}
public bool Initialize()
{
try
{
_thread = new PeriodicJob(300, OnTimer, "AKunTemp", true);
return true;
}
catch (Exception ex)
{
LOG.Error(ex.Message, ex);
return false;
}
}
public void Monitor()
{
}
public void Terminate()
{
try
{
_ir.Close();
}
catch (Exception ex)
{
LOG.Write(ex);
}
}
public void Reset()
{
}
int CountNum = 0;//接受数据失败次数
private void GetValues()
{
try
{
var all = _ir.GetAllChannel();
//多次接收到空数据开始报警
if ((all[0].Count == 0 && all[1].Count == 0 && all[2].Count == 0 && all[3].Count == 0))
{
_trigDataOK.CLK = false;//准备重连信号
CountNum++;
if (CountNum > 10)
{
_trigConnService.CLK = true;
if (_trigConnService.Q)
ConnectMesError();
_ir.StartIR();//再次发送给数据请求
CountNum = 0;
return;
}
return;
}
_trigDataOK.CLK = true;
if (_trigDataOK.Q)
ConnectMesOK();
for (var i = 0; i < all.Count; i++)
{
switch (i)
{
case 0: //PM1 Inner
TempData1 = all[i].Average();
break;
case 1: //PM1 Middle
TempData2 = all[i].Average();
break;
case 2: //PM2 Inner
TempData3 = all[i].Average();
break;
case 3: //PM2 Middle
TempData4 = all[i].Average();
break;
default:
break;
}
}
_trigConnService.RST = true;
LOG.Write($"PM1 Middle{TempData1} Outer{TempData2} PM2 Middle{TempData3} Outer{TempData4}");
}
catch (Exception ex)
{
LOG.Write($"AKunTemp GetValues Error {ex}");
}
}
#endregion
#region Events
private void RT_CommunicationBroken(object sender, object e)
{
}
private void ConnectMesError()
{
if (SC.GetValue<bool>($"System.SetUp.IsPM1Installed"))
EV.PostAlarmLog("PM1", $"Can not connect with {Address}, PM1.{Name}");
if (SC.GetValue<bool>($"System.SetUp.IsPM2Installed"))
EV.PostAlarmLog("PM2", $"Can not connect with {Address}, PM2.{Name}");
}
private void ConnectMesOK()
{
if (SC.GetValue<bool>($"System.SetUp.IsPM1Installed"))
EV.PostInfoLog("PM1", $"Connect OK with {Address}, PM1.{Name}");
if (SC.GetValue<bool>($"System.SetUp.IsPM2Installed"))
EV.PostInfoLog("PM2", $"Connect OK with {Address}, PM2.{Name}");
}
public bool Disconnect()
{
Terminate();
return true;
}
//
#endregion
}
}