/********************************
*
* 作者:徐贞卫
* 创建时间: 2019-10-16 13:38:05
*
********************************/
using System;
using System.Diagnostics;
using System.Linq;
using System.Net.NetworkInformation;
namespace Server {
///
/// 监控服务应用信息
///
public class MonitoringAppInfo : IEquatable {
static DateTime unixbase { get; } = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime();
///
/// 网卡硬件地址
///
public string[] MacAddress { get; set; }
///
/// 系统已启动时间(单位:秒)
///
public int SysStartTime { get; set; }
///
/// 进程PID
///
public int Pid { get; set; }
///
/// 服务应用完整路径
///
public string FullFileName { get; set; }
///
/// 模块ID标识符
///
public string UniqueId { get; set; }
///
/// 模块ID
///
public int ModuleId { get; set; }
///
/// 节点编号
///
public int NodeNumber { get; set; }
///
/// GetListViewItem
///
/// GetListViewItem
public System.Windows.Forms.ListViewItem GetListViewItem() {
if(viewItem == null) {
viewItem = new System.Windows.Forms.ListViewItem(
new string[] { ModuleId.ToString(), NodeNumber.ToString(), UniqueId, FullFileName }) { Tag = this };
}
return viewItem;
}
private System.Windows.Forms.ListViewItem viewItem;
///
/// 当前应用信息
///
/// 模块ID标识符
/// 模块ID
/// 节点编号
/// 当前进程的应用信息
public static MonitoringAppInfo GetCurrentApplicateInfo(string uniqueId, int moduleId, int nodeNumber) {
//时间戳
var now = (int)DateTime.Now.Subtract(unixbase).TotalSeconds;
//获取当前进程
var process = Process.GetCurrentProcess();
var mainModule = process.MainModule;
var macAddress = GetNetcardMacaddress();
var sysStartTime = now - (Environment.TickCount / 1000);
var pId = process.Id;
var fullFileName = mainModule.FileName;
return new MonitoringAppInfo {
MacAddress = macAddress,
SysStartTime = sysStartTime,
Pid = pId,
FullFileName = fullFileName,
UniqueId = uniqueId,
ModuleId = moduleId,
NodeNumber = nodeNumber
};
}
///
/// 获取本机所有工作的以太网卡物理地址
///
///
public static string[] GetNetcardMacaddress() {
if (MacAddresss == null) {
var networkAdapters = NetworkInterface.GetAllNetworkInterfaces().Where(p => p.NetworkInterfaceType == NetworkInterfaceType.Ethernet && p.OperationalStatus == OperationalStatus.Up);
MacAddresss = networkAdapters.Select(p => p.GetPhysicalAddress()).Select(p => p.ToString()).ToArray();
}
return MacAddresss;
}
static string[] MacAddresss = null;
///
/// 指示当前对象是否等于同一类型的另一个对象
///
/// 一个与此对象进行比较的对象。
/// 如果当前对象等于 other 参数,则为 true;否则为 false。
public bool Equals(MonitoringAppInfo other) {
if(other == null) {
return false;
}
if(Pid != other.Pid) {
return false;
}
if(ModuleId != other.ModuleId) {
return false;
}
if(NodeNumber != other.NodeNumber) {
return false;
}
if(!string.Equals(FullFileName, other.FullFileName, StringComparison.InvariantCultureIgnoreCase)) {
return false;
}
return true;
}
///
/// ToString
///
///
public override string ToString() {
return $"模块ID:{ModuleId}, 节点ID:{NodeNumber},路径:{FullFileName}";
}
}
}