Sic.Framework/MECF.Framework.RT.Equipment.../HardwareUnits/Temps/AE/AETemp.cs

288 lines
9.7 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.OperationCenter;
using Aitex.Core.RT.SCCore;
using Aitex.Core.RT.IOCore;
using Aitex.Core.Util;
using MECF.Framework.Common.Communications;
using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Temps.Omron;
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts.TDK;
using System.Runtime.InteropServices;
using System.Xml;
using System.Net;
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Temps
{
public class AETemp : BaseDevice, IConnection, IDevice, ITempData
{
private AETempConnection _connection;
private R_TRIG _trigCommunicationError = new R_TRIG();
private R_TRIG _trigFewChannels = new R_TRIG();
private PeriodicJob _thread;
TempBasFunction tempBasFunction;
/// <summary>
/// 温度不发生变化
/// </summary>
private readonly R_TRIG _trigTempNoChange = new R_TRIG();
private int TempInvariantCount;
private int TempInvariantCountMax = 5;
private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
private object _locker = new object();
private bool _enableLog = true;
public int TempMin { get; set; } = 600;
public int NumberOfChannels { get; set; } = 4;
public float[] TempDatasArray { get; set; }
/// <summary>
/// 设备通道启用
/// </summary>
bool[] ChannelInstalled { get; set; }
public string Address
{
get; set;
}
public bool IsConnected
{
get
{
return _connection != null && _connection.IsConnected;
}
}
public bool Connect()
{
return true;
}
public bool Disconnect()
{
return true;
}
public AETemp(string module, XmlElement node, string ioModule = "") : base(module, node, ioModule)
{
}
public void QueryTemp()
{
_lstHandler.AddLast(new AETempReadCommandHandler(this, "OUT", "1"));
}
public bool Initialize()
{
if (!SC.GetValue<bool>($"{ScBasePath}.EnableDevice"))
return true;
IniData();
IniTemp();
return true;
}
private void IniData()
{
Address = SC.GetStringValue($"{ScBasePath}.Address");
_enableLog = SC.GetValue<bool>($"TempDevice.EnableLogMessage");
_connection = new AETempConnection(Address);
_connection.EnableLog(_enableLog);
var scOnTime = SC.GetValue<int>($"TempDevice.OnTimer");
_thread = new PeriodicJob(scOnTime, OnTimer, $"{Module}.{Name} MonitorHandler", true);
SC.RegisterValueChangedCallback($"TempDevice.OnTimer", (obj) => { _thread.ChangeInterval((int)obj); });
SC.RegisterValueChangedCallback($"TempDevice.EnableLogMessage", (obj) => { _connection.EnableLog((bool)obj); });
tempBasFunction = new TempBasFunction(Name, TempMin, NumberOfChannels);
tempBasFunction.SetPm1Pm2IoForInterlock(false);//复位PM DO_PyroCommunicationError信号
}
private void IniTemp()
{
TempDatasArray = new float[NumberOfChannels];
ChannelInstalled = new bool[NumberOfChannels];
for (int i = 0; i < NumberOfChannels ; i++)//初始化温度下限 初始化获取配置的通道是否禁用
{
int indexer = i;
TempDatasArray[indexer] = TempMin;
ChannelInstalled[indexer] = SC.GetValue<bool>($"TempDevice.{Name}.Channel{indexer + 1}");
DATA.Subscribe($"Temp.{Name}.t{indexer + 1}", () => TempDatasArray[indexer]);
}
}
private bool OnTimer()
{
try
{
_connection.MonitorTimeout();
//
if (!_connection.IsConnected || _connection.IsCommunicationError)
{
lock (_locker)
{
_lstHandler.Clear();
}
//只要断开,就进行连接
if (!_connection.Connect())
{
_trigCommunicationError.CLK = !_connection.IsConnected;
if (_trigCommunicationError.Q)
{
tempBasFunction.PM1PM2PostLog($"Can not connect with {_connection.Address}, {Module}.{Name}",EV.PostAlarmLog);
tempBasFunction.SetPm1Pm2IoForInterlock(true);
}
_connection.ForceClear();
Thread.Sleep(1000);//重连延时
return true;
}
else
{
//连接成功后不会再进入所以也只是写入一次LOG
LOG.Write($"{Module} {Name} Connected");
EV.PostInfoLog(Module, $"{Name} Connected");
tempBasFunction.PM1PM2PostLog($"{Name} Connected", EV.PostInfoLog);
tempBasFunction.SetPm1Pm2IoForInterlock(false);
}
_trigCommunicationError.CLK = !_connection.IsConnected;
_connection.ForceClear();
return true;
}
//
HandlerBase handler = null;
lock (_locker)
{
if (_lstHandler.Count == 0)
QueryTemp();
if (_lstHandler.Count > 0 && !_connection.IsBusy)
{
handler = _lstHandler.First.Value;
_lstHandler.RemoveFirst();
if (handler != null)
{
_connection.Execute(handler);
}
}
}
}
catch (Exception ex)
{
LOG.Write(ex);
}
return true;
}
internal void NoteError()
{
}
public void ParseCommandInfo(string command, string Message)
{
try
{
switch (command)
{
case "OUT":
{
if (Message != null)
{
if (Message.Contains(" "))
{
var strs = Message.Split(' ');
if (strs.Length > 0 )
{
//判断AE温度通道启用和上位机设定通道启用是否相同
if (strs.Length != ChannelInstalled.Count(c => c == true))
{
_trigFewChannels.CLK = true;
if (_trigFewChannels.Q)
{
tempBasFunction.PM1PM2PostLog("AE Error: Too few channels.", EV.PostAlarmLog);
}
return;
}
//温度数组,根据通道是否屏蔽来赋值,
_trigFewChannels.RST = true;
for (int i = 0; i < strs.Length; i++)
{
if (ChannelInstalled[i] == true)
{
TempDatasArray[i] = float.Parse(strs[i]);
}
}
//温度连续多次不变化侦测
//TempInvariantCount = tempBasFunction.TempInvariantCount(TempDatasArray, out string tempInvariantData);
//_trigTempNoChange.CLK = TempInvariantCount >= TempInvariantCountMax ? true : false;
//if (_trigTempNoChange.Q)
//{
// tempBasFunction.PM1PM2PostLog($"{Name} {TempInvariantCount} temp no changeCount {tempInvariantData}", EV.PostWarningLog);
//}
}
}
}
}
break;
}
}
catch (Exception ex)
{
}
}
public void Reset()
{
_connection?.SetCommunicationError(false, "");
//_enableLog = SC.GetValue<bool>($"AETemp.EnableLogMessage");
_trigCommunicationError.RST = true;
//_trigRetryConnect.RST = true;
_trigFewChannels.RST = true;
_trigTempNoChange.RST = true;
}
public void Terminate()
{
_connection.Disconnect();
}
}
}