首次上传
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.UPS
|
||||
{
|
||||
public class EATONUPS:UPSBase
|
||||
{
|
||||
public EATONUPS(string module, string name, string scRoot) : base(module, name, scRoot)
|
||||
{
|
||||
Oids.Add("InputVoltage", ".1.3.6.1.4.1.534.1.3.4.1.2.1");//输入电压,输入电压为0说明使用UPS xupsInputVoltage
|
||||
Oids.Add("BatteryVoltage", ".1.3.6.1.4.1.534.1.2.2.0");//电池当前电压V xupsBatVoltage
|
||||
Oids.Add("BatteryRemainsTime", ".1.3.6.1.4.1.534.1.2.1.0");//剩余时间s,要除60 xupsBatTimeRemaining
|
||||
Oids.Add("upsOutputSource", ".1.3.6.1.4.1.534.1.4.5.0");//当前输出源 xupsOutputSource
|
||||
Oids.Add("BatteryUnderResidue", ".1.3.6.1.4.1.534.1.2.4.0");//电量剩余百分比,直接用数值 xupsBatCapacity
|
||||
|
||||
}
|
||||
|
||||
public override void ParseOutputSource(string value)
|
||||
{
|
||||
// xupsOutputSource DESCRIPTION
|
||||
|
||||
// SYNTAX INTEGER {
|
||||
// other(1),
|
||||
//none(2),
|
||||
//normal(3),
|
||||
//bypass(4),
|
||||
//battery(5),
|
||||
//booster(6),
|
||||
//reducer(7),
|
||||
//parallelCapacity(8),
|
||||
//parallelRedundant(9),
|
||||
//highEfficiencyMode(10),
|
||||
//maintenanceBypass(11),
|
||||
//essMode(12)
|
||||
//}
|
||||
//"The present source of output power. The enumeration
|
||||
// none(2) indicates that there is no source of output
|
||||
// power(and therefore no output power), for example,
|
||||
// the system has opened the output breaker.
|
||||
|
||||
// 'normal', 'bypass', and 'battery' indicate those common UPS statuses.
|
||||
|
||||
// 'booster' and 'reducer' indicate boost or buck operation, for
|
||||
|
||||
// line - interactive UPSs only.
|
||||
|
||||
// 'parallelCapacity' and 'parallelRedundant' indicate a normal parallel
|
||||
|
||||
// UPS system, in either Parallel for Capacity or Redundancy configuration.
|
||||
|
||||
// 'highEfficiencyMode' is normal but enhanced by High Efficiency mode.
|
||||
|
||||
// 'maintenanceBypass' indicates that the UPS is in Maintenance / Manual
|
||||
|
||||
// Bypass mode.
|
||||
|
||||
// 'essMode' is normal but enhanced by Energy Saver System.
|
||||
|
||||
// 'other' covers any other, unusual conditions."
|
||||
|
||||
if (value == "2") //None
|
||||
{
|
||||
UPSPowerAlarm = true;
|
||||
}
|
||||
else
|
||||
UPSPowerAlarm = false;
|
||||
}
|
||||
|
||||
public override void ParseBatteryRemainsTime(string value)
|
||||
{
|
||||
BatteryRemainsTime = Convert.ToInt32(value)/60;
|
||||
}
|
||||
|
||||
public override void ParseBatteryVoltage(string value)
|
||||
{
|
||||
BatteryVoltage = Convert.ToSingle(value);
|
||||
}
|
||||
|
||||
public override void ParseInputVoltage(string value)
|
||||
{
|
||||
InputVoltage = Convert.ToSingle(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.UPS
|
||||
{
|
||||
public class ITAUPS : UPSBase
|
||||
{
|
||||
public ITAUPS(string module, string name, string scRoot):base(module, name, scRoot)
|
||||
{
|
||||
Oids.Add("InputVoltage", ".1.3.6.1.4.1.13400.2.54.2.2.1.0");//输入电压,输入电压为0说明使用UPS,要除10
|
||||
Oids.Add("BatteryVoltage", ".1.3.6.1.4.1.13400.2.54.2.5.1.0");//电池当前电压,要除10
|
||||
Oids.Add("BatteryRemainsTime", ".1.3.6.1.4.1.13400.2.54.2.5.7.0");//输入电压,输入电压为0说明使用UPS,要除10
|
||||
Oids.Add("upsOutputSource", ".1.3.6.1.4.1.13400.2.54.2.1.2.0");//当前输出源
|
||||
Oids.Add("BatteryUnderResidue", ".1.3.6.1.4.1.13400.2.54.2.5.10.0");//电量剩余百分比,直接用数值
|
||||
}
|
||||
|
||||
public override void ParseOutputSource(string value)
|
||||
{
|
||||
|
||||
if (value == "0") //UPS No Output
|
||||
{
|
||||
UPSPowerAlarm = true;
|
||||
}
|
||||
else if (value == "1") // UPS On Main
|
||||
{
|
||||
UPSPowerAlarm = false;
|
||||
}
|
||||
else if (value == "2") //UPS On Battery
|
||||
{
|
||||
UPSPowerAlarm = true;
|
||||
}
|
||||
}
|
||||
|
||||
public override void ParseBatteryRemainsTime(string value)
|
||||
{
|
||||
BatteryRemainsTime = Convert.ToInt32(value) / 10;
|
||||
}
|
||||
|
||||
public override void ParseBatteryVoltage(string value)
|
||||
{
|
||||
BatteryVoltage = Convert.ToSingle(value) / 10;
|
||||
}
|
||||
|
||||
public override void ParseInputVoltage(string value)
|
||||
{
|
||||
InputVoltage = Convert.ToSingle(value) / 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,288 @@
|
||||
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 MECF.Framework.Common.Communications;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Net;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.UPS
|
||||
{
|
||||
public class UPSBase :BaseDevice, IConnection, IDevice
|
||||
{
|
||||
#region Properties
|
||||
|
||||
private UPSConnection _connection; //ITAUPSConnection 和 UPS类 是相互绑定的,既然这样应该写到一起才对,自上而下原则
|
||||
private bool _activeMonitorStatus;
|
||||
|
||||
private R_TRIG _trigRetryConnect = new R_TRIG();//断开上升沿
|
||||
private R_TRIG _trigReconnection = new R_TRIG();//重连上升沿
|
||||
private R_TRIG _trigPowerLow = new R_TRIG();//缺电上升沿
|
||||
private PeriodicJob _thread;
|
||||
|
||||
private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
|
||||
|
||||
private object _locker = new object();
|
||||
|
||||
private bool _enableLog = true;
|
||||
|
||||
private string _scRoot;
|
||||
private string portName;
|
||||
|
||||
public bool IsReceive = false;
|
||||
|
||||
public int _connectTimes { get; set; }
|
||||
|
||||
public string Address
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public bool IsConnected
|
||||
{
|
||||
get
|
||||
{
|
||||
return _connection != null && _connection.IsConnected;
|
||||
}
|
||||
}
|
||||
|
||||
public float InputVoltage { get; set; }
|
||||
public float BatteryVoltage { get; set; }
|
||||
public float BatteryRemainsTime { get; set; }
|
||||
public float BatteryUnderResidue { get; set; }
|
||||
public bool UPSPowerAlarm { get; set; }
|
||||
public bool UPSLowerBatteryAlarm { get; set; }
|
||||
|
||||
public Dictionary<string, string> Oids = new Dictionary<string, string>();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
public UPSBase(string module, string name, string scRoot) : base(module, name, name, name)
|
||||
{
|
||||
_scRoot = scRoot;
|
||||
_activeMonitorStatus = true;
|
||||
|
||||
}
|
||||
|
||||
~UPSBase()
|
||||
{
|
||||
_connection.Disconnect();
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Function
|
||||
|
||||
public bool Connect()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Disconnect()
|
||||
{
|
||||
_connection.Disconnect();
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Monitor()
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_connectTimes = 0;
|
||||
_connection.SetCommunicationError(false, "");
|
||||
|
||||
_enableLog = SC.GetValue<bool>($"PM.{Module}.{Name}.EnableLogMessage");
|
||||
_trigRetryConnect.RST = true;
|
||||
_trigReconnection.RST = true;
|
||||
}
|
||||
|
||||
public void Terminate()
|
||||
{
|
||||
_connection.Disconnect();
|
||||
}
|
||||
|
||||
public bool Initialize()
|
||||
{
|
||||
//_connection的底层写死使用161端口号
|
||||
portName = SC.GetStringValue($"PM.{Module}.{Name}.Address");
|
||||
Address = portName;
|
||||
int address = SC.GetValue<int>($"PM.{Module}.{Name}.DeviceAddress");
|
||||
_enableLog = SC.GetValue<bool>($"PM.{Module}.{Name}.EnableLogMessage");
|
||||
_connection = new UPSConnection(portName);
|
||||
_connection.EnableLog(_enableLog);
|
||||
|
||||
_thread = new PeriodicJob(500, OnTimer, $"{Module}.{Name} MonitorHandler", true);
|
||||
|
||||
InitData();
|
||||
|
||||
ConnectionManager.Instance.Subscribe($"{Name}", this);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void InitData()
|
||||
{
|
||||
DATA.Subscribe($"{Module}.{Name}.InputVoltage", () => InputVoltage);
|
||||
DATA.Subscribe($"{Module}.{Name}.BatteryVoltage", () => BatteryVoltage);
|
||||
DATA.Subscribe($"{Module}.{Name}.BatteryRemainsTime", () => BatteryRemainsTime);
|
||||
DATA.Subscribe($"{Module}.{Name}.BatteryUnderResidue", () => BatteryUnderResidue);
|
||||
}
|
||||
|
||||
bool CanConnect()
|
||||
{
|
||||
IPAddress ip = IPAddress.Parse(portName);
|
||||
Ping ping = new Ping();
|
||||
try
|
||||
{
|
||||
PingReply reply = ping.Send(ip);
|
||||
return (reply.Status == IPStatus.Success);
|
||||
}
|
||||
catch (PingException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private bool OnTimer()
|
||||
{
|
||||
try
|
||||
{
|
||||
_connection.MonitorTimeout();
|
||||
|
||||
if (!_connection.IsConnected || _connection.IsCommunicationError)//测试发现这个条件永远不会满足
|
||||
{
|
||||
//lock (_locker)
|
||||
//{
|
||||
// _lstHandler.Clear();
|
||||
//}
|
||||
//if (!_connection.Connect())
|
||||
//{
|
||||
// _trigRetryConnect.CLK = !_connection.IsConnected;
|
||||
// if (_trigRetryConnect.Q)
|
||||
// {
|
||||
// EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
|
||||
// }
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// _trigRetryConnect.RST = true;
|
||||
// EV.PostInfoLog(Module, $"Connected with {_connection.Address}, {Module}.{Name}");
|
||||
//}
|
||||
|
||||
}
|
||||
|
||||
//执行最新连接操作
|
||||
HandlerBase handler = null;
|
||||
lock (_locker)
|
||||
{
|
||||
if (_lstHandler.Count == 0)
|
||||
QueryOids();
|
||||
if (_lstHandler.Count > 0 && !_connection.IsBusy)
|
||||
{
|
||||
handler = _lstHandler.First.Value;
|
||||
_lstHandler.RemoveFirst();
|
||||
if (handler != null)
|
||||
{
|
||||
_connection.Execute(handler); //执行成功_connection会将IsReceive置为true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!IsReceive) //单纯断联重连判断
|
||||
{
|
||||
System.Threading.Thread.Sleep(3000);//不加延时第一次启动时未连接,可能不报警
|
||||
_trigRetryConnect.CLK = true;//断联
|
||||
if (_trigRetryConnect.Q)
|
||||
{
|
||||
InputVoltage = 0;
|
||||
BatteryUnderResidue = 0;
|
||||
BatteryVoltage = 0;
|
||||
BatteryRemainsTime = 0;
|
||||
EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
|
||||
}
|
||||
_trigReconnection.RST = true;//复位重连
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
_trigReconnection.CLK = true;//重连
|
||||
if (_trigReconnection.Q)
|
||||
{
|
||||
EV.PostInfoLog(Module, $"Connection with {_connection.Address}, {Module}.{Name}");
|
||||
}
|
||||
_trigRetryConnect.RST = true;//复位断联
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LOG.Write(ex);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void QueryOids()
|
||||
{
|
||||
IsReceive = false;
|
||||
//UPS二代
|
||||
foreach (var oid in Oids)
|
||||
{
|
||||
if (oid.Key != null)
|
||||
{
|
||||
_lstHandler.AddLast(new UPSGetHandler(this, oid.Key, "Get", oid.Value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void CheckBatteryUnderResidue()
|
||||
{
|
||||
if ((30 >= BatteryUnderResidue) && (BatteryUnderResidue >= 20) || (10 >= BatteryUnderResidue) && (BatteryUnderResidue >= 1))
|
||||
{
|
||||
_trigPowerLow.CLK = true;
|
||||
if (_trigPowerLow.Q)
|
||||
EV.PostAlarmLog(Module, $"Attention {Module}.{Name} Is Power Low, Battery Under Residue {BatteryUnderResidue}%");
|
||||
}
|
||||
else
|
||||
_trigPowerLow.RST = true;//不满足报警条件时自动复位,准备下次报警操作
|
||||
}
|
||||
public virtual void SetBatteryUnderResidue(int data)
|
||||
{
|
||||
BatteryUnderResidue = data;
|
||||
CheckBatteryUnderResidue();
|
||||
|
||||
}
|
||||
|
||||
public virtual void ParseOutputSource(string value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
public virtual void ParseBatteryRemainsTime(string value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
public virtual void ParseBatteryVoltage(string value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
public virtual void ParseInputVoltage(string value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using MECF.Framework.Common.Communications;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.UPS
|
||||
{
|
||||
public class UPSConnection : SNMPBase
|
||||
{
|
||||
private List<byte> _lstCacheBuffer = new List<byte>();
|
||||
private string sendMessage;
|
||||
public UPSConnection(string portName) : base(portName, "public", 1, "\n")
|
||||
{
|
||||
|
||||
}
|
||||
public override bool SendMessage(string message)
|
||||
{
|
||||
_lstCacheBuffer.Clear();
|
||||
sendMessage = message;
|
||||
return base.SendMessage(message);
|
||||
}
|
||||
public override bool SendMessage(List<string> message)
|
||||
{
|
||||
_lstCacheBuffer.Clear();
|
||||
return base.SendMessage(message);
|
||||
}
|
||||
protected override MessageBase ParseResponse(string rawMessage)
|
||||
{
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
protected override MessageBase ParseResponse(string Oid, string Value)
|
||||
{
|
||||
|
||||
UPSMessage msg = new UPSMessage();
|
||||
msg.IsResponse = false;
|
||||
msg.IsAck = false;
|
||||
msg.IsComplete = false;
|
||||
|
||||
if (!sendMessage.Equals(Oid)) return msg;
|
||||
msg.Oid = Oid;
|
||||
msg.Value = Value;
|
||||
msg.IsResponse = true;
|
||||
msg.IsAck = true;
|
||||
msg.IsComplete = true;
|
||||
|
||||
return msg;
|
||||
}
|
||||
protected override MessageBase ParseResponse(Dictionary<string, string> rawMessagelist)
|
||||
{
|
||||
UPSMessage msg = new UPSMessage();
|
||||
msg.IsResponse = false;
|
||||
msg.IsAck = false;
|
||||
msg.IsComplete = false;
|
||||
|
||||
//if (!) return msg;
|
||||
//msg.Oid = Oid;
|
||||
// msg.Value = Value;
|
||||
if (rawMessagelist != null && rawMessagelist.Count == 0)
|
||||
{
|
||||
return msg;
|
||||
}
|
||||
msg.oidic = rawMessagelist;
|
||||
msg.IsResponse = true;
|
||||
msg.IsAck = true;
|
||||
msg.IsComplete = true;
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using Aitex.Core.RT.Log;
|
||||
using MECF.Framework.Common.Communications;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.UPS
|
||||
{
|
||||
public class UPSGetHandler : UPSHandler
|
||||
{
|
||||
public UPSGetHandler(UPSBase device, string name, string messageType, string Oid)
|
||||
: base(device, Oid)
|
||||
{
|
||||
Name = name;
|
||||
MessageType = messageType;
|
||||
}
|
||||
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as UPSMessage;
|
||||
handled = false;
|
||||
if (!result.IsResponse) return true;
|
||||
|
||||
try
|
||||
{
|
||||
foreach (var dic in result.oidic)
|
||||
{
|
||||
var oid = Device.Oids.FirstOrDefault(m => m.Value.Substring(1) == dic.Key);
|
||||
if (oid.Key == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (oid.Key.Contains("InputVoltage"))
|
||||
{
|
||||
Device.ParseInputVoltage(dic.Value);
|
||||
}
|
||||
else if (oid.Key.Contains("BatteryVoltage"))
|
||||
{
|
||||
Device.ParseBatteryVoltage(dic.Value);
|
||||
}
|
||||
else if (oid.Key.Contains("BatteryRemainsTime"))
|
||||
{
|
||||
Device.ParseBatteryRemainsTime(dic.Value);
|
||||
}
|
||||
else if (oid.Key.Contains("BatteryUnderResidue"))
|
||||
{
|
||||
Device.SetBatteryUnderResidue(Convert.ToInt32(dic.Value));
|
||||
}
|
||||
else if (oid.Key.Contains("upsOutputSource"))
|
||||
{
|
||||
Device.ParseOutputSource(dic.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LOG.Write(ex.Message);
|
||||
}
|
||||
Device.IsReceive = true;
|
||||
Device._connectTimes = 0;
|
||||
handled = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using MECF.Framework.Common.Communications;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.UPS
|
||||
{
|
||||
public abstract class UPSHandler : HandlerBase
|
||||
{
|
||||
public UPSBase Device { get; }
|
||||
|
||||
protected UPSHandler(UPSBase device, string Oid)
|
||||
: base(Oid)
|
||||
{
|
||||
Device = device;
|
||||
}
|
||||
protected UPSHandler(ITAUPS device, List<string> Oid)
|
||||
: base(Oid)
|
||||
{
|
||||
Device = device;
|
||||
}
|
||||
public override bool HandleMessage(MessageBase msg, out bool handled)
|
||||
{
|
||||
var result = msg as UPSMessage;
|
||||
ResponseMessage = msg;
|
||||
handled = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using MECF.Framework.Common.Communications;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.UPS
|
||||
{
|
||||
public class UPSMessage : MessageBase
|
||||
{
|
||||
public string Oid;
|
||||
|
||||
public string Value;
|
||||
|
||||
public Dictionary<string, string> oidic;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user