123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- using System;
- using System.Collections.Generic;
- 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.Util;
- using MECF.Framework.Common.Communications;
- using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.FFUs.MayAir;
- using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts;
- using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts.TDK;
- namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.FlowMeters
- {
- public class Usf500N : BaseDevice, IConnection, IDevice
- {
- private Usf500NConnection _connection;
- public byte DeviceAddress { get; set; }
-
- public FlowMeterData[] FlowMeterDataSet;
- public FlowMeterData GetFlowMeterData(string flowname)
- {
- foreach(var fdata in FlowMeterDataSet)
- {
- if (fdata.FlowName == flowname)
- return fdata;
- }
- return new FlowMeterData();
- }
- private R_TRIG _trigCommunicationError = new R_TRIG();
- private R_TRIG _trigRetryConnect = new R_TRIG();
- private PeriodicJob _thread;
- private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
- private object _locker = new object();
- private bool _enableLog = true;
- 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 Usf500N(string module, string name, Usf500NConnection conn, Tuple<string,int, int>[] addressChannel) : base(module, name, name, name)
- {
- _connection = conn;
- List<FlowMeterData> tempset = new List<FlowMeterData>();
- foreach (var addCh in addressChannel)
- {
- FlowMeterData data = new FlowMeterData(addCh.Item1, addCh.Item2,addCh.Item3);
- tempset.Add(data);
- }
- FlowMeterDataSet = tempset.ToArray();
- }
- public Usf500N(string module, string name, string comPort, Tuple<string, int, int>[] addressChannel) : base(module, name, name, name)
- {
- _connection = new Usf500NConnection(comPort);
- List<FlowMeterData> tempset = new List<FlowMeterData>();
- foreach (var addCh in addressChannel)
- {
- FlowMeterData data = new FlowMeterData(addCh.Item1, addCh.Item2, addCh.Item3);
- tempset.Add(data);
- }
- FlowMeterDataSet = tempset.ToArray();
- }
- public void QuerySpeed()
- {
- foreach(var flowdata in FlowMeterDataSet)
- {
- _lstHandler.AddLast(new Usf500NQueryFlowRateHandler(this,(byte)flowdata.Address,flowdata.Channel));
- _lstHandler.AddLast(new Usf500NQueryFlowVolumeHandler(this, (byte)flowdata.Address, flowdata.Channel));
- }
- //_lstHandler.AddLast(new FfuAAFQuerySpeedHandler(this, DeviceAddress, GroupAddress));
- }
- internal void ParseFlowRate(byte address, byte channel, byte[] readOutData)
- {
- try
- {
- for (int i = 0; i < FlowMeterDataSet.Length; i++)
- {
- if (FlowMeterDataSet[i].Address == address && FlowMeterDataSet[i].Channel == channel)
- {
- int nValue = 0;
- for(int j=0;j< readOutData.Length;j++)
- {
- nValue += readOutData[j] << (8* (readOutData.Length-j-1));
- }
- FlowMeterDataSet[i].FlowRate = nValue;
- }
- }
- }
- catch(Exception ex)
- {
- LOG.Write(ex);
- }
- }
- internal void ParseFlowVolume(byte address, byte channel, byte[] readOutData)
- {
- try
- {
- for (int i = 0; i < FlowMeterDataSet.Length; i++)
- {
- if (FlowMeterDataSet[i].Address == address && FlowMeterDataSet[i].Channel == channel)
- {
- int nValue = 0;
- for (int j = 0; j < readOutData.Length; j++)
- {
- nValue += readOutData[j] << (8 * (readOutData.Length - j - 1));
- }
- FlowMeterDataSet[i].TotalFlowVolume = nValue;
- }
- }
- }
- catch (Exception ex)
- {
- LOG.Write(ex);
- }
- }
- public bool Initialize()
- {
- //DATA.Subscribe($"{Module}.{Name}.Power", () => _power);
- //DATA.Subscribe($"{Module}.{Name}.ErrorCode", () => _errorCode);
- //DATA.Subscribe($"{Module}.{Name}.IsAtSpeed", () => _isAtSpeed);
- //DATA.Subscribe($"{Module}.{Name}.IsAccelerate", () => _isAccelerate);
-
- DATA.Subscribe($"{Name}.DeviceAddress", () => DeviceAddress);
- DATA.Subscribe($"{Name}.FlowMeterNames", () => GetFlowMeterNames());
- if (_connection.Connect())
- {
- EV.PostInfoLog(Module, $"{Module}.{Name} connected");
- }
- _thread = new PeriodicJob(1000, OnTimer, $"{Name} MonitorHandler", true);
- //DATA.Subscribe($"{Name}.Speed", () => _ffuSpeed);
- OP.Subscribe($"{Name}.ResetVolume", (cmd, param) =>
- {
- if (param.Length <1)
- {
- EV.PostWarningLog(Module, "Wrong parameter.");
- return false;
- }
- ResetFlowVolumn(param[0].ToString());
- EV.PostInfoLog(Module, $"{Name} reset flow volume:{param[0].ToString()}");
- return true;
- });
- ConnectionManager.Instance.Subscribe($"{Name}", this);
- return true;
- }
- private List<string> GetFlowMeterNames()
- {
- List<string> ret = new List<string>();
- if (FlowMeterDataSet == null) return ret;
- foreach (var flow in FlowMeterDataSet)
- {
- ret.Add($"{flow.FlowName},{flow.Address},{flow.Channel},{flow.FlowRate},{flow.TotalFlowVolume}");
- }
- return ret;
- }
- public void ResetFlowVolumn(int address, int channel)
- {
- lock(_locker)
- {
- _lstHandler.AddLast(new Usf500NResetFlowVolumeHandler(this, (byte)address, channel));
- }
- }
- public void ResetFlowVolumn(string flowName)
- {
- foreach(var fdata in FlowMeterDataSet)
- {
- if(flowName == fdata.FlowName)
- {
- lock (_locker)
- {
- _lstHandler.AddLast(new Usf500NResetFlowVolumeHandler(this, (byte)fdata.Address, fdata.Channel));
- }
- }
-
- }
-
- }
- private bool OnTimer()
- {
- try
- {
- _connection.MonitorTimeout();
- if (!_connection.IsConnected)// || _connection.IsCommunicationError
- {
- lock (_locker)
- {
- _lstHandler.Clear();
- }
- //_trigRetryConnect.CLK = !_connection.IsConnected;
- //if (_trigRetryConnect.Q)
- //{
- // //_connection.SetPortAddress(SC.GetStringValue($"{_scRoot}.{Name}.Address"));
- // if (!_connection.Connect())
- // {
- // EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
- // }
- //}
- return true;
- }
- HandlerBase handler = null;
- if (!_connection.IsBusy)
- {
- lock (_locker)
- {
- if (_lstHandler.Count == 0 )
- {
- QuerySpeed();
- }
- if (_lstHandler.Count > 0)
- {
- handler = _lstHandler.First.Value;
- _lstHandler.RemoveFirst();
- }
- }
- if (handler != null)
- {
- _connection.Execute(handler);
- }
- }
- return true;
- }
- catch (Exception ex)
- {
- LOG.Write(ex);
- }
- return true;
- }
- internal void NoteError()
- {
- }
- public void Monitor()
- {
- try
- {
- _connection.EnableLog(_enableLog);
- _trigCommunicationError.CLK = _connection.IsCommunicationError;
- if (_trigCommunicationError.Q)
- {
- EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
- }
- }
- catch (Exception ex)
- {
- LOG.Write(ex);
- }
- }
- public void Terminate()
- {
- try
- {
- if (_connection != null)
- {
- _connection.Disconnect();
- _connection = null;
- }
- }
- catch (Exception ex)
- {
- LOG.Write(ex);
- }
-
- }
- public void Reset()
- {
- _connection.SetCommunicationError(false, "");
- _trigCommunicationError.RST = true;
-
- _trigRetryConnect.RST = true;
- }
- }
- public struct FlowMeterData
- {
- public FlowMeterData(string Name,int address,int channel)
- {
- FlowName = Name;
- Address = address;
- Channel = channel;
- FlowRate = 0;
- TotalFlowVolume = 0;
- }
- public string FlowName { get; set; }
- public int Address { get; }
- public int Channel { get; }
- public int FlowRate { get; set; }
- public int TotalFlowVolume { get; set; }
- }
- }
|