初始化
This commit is contained in:
125
LogCap/Common/CSRedisCacheHelper.cs
Normal file
125
LogCap/Common/CSRedisCacheHelper.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Configuration;
|
||||
using CSRedis;
|
||||
using LogCap.Common;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Common
|
||||
{
|
||||
/// <summary>
|
||||
/// Redis缓存辅助类
|
||||
/// </summary>
|
||||
public class CSRedisCacheHelper
|
||||
{
|
||||
public static CSRedisClient redis;
|
||||
private static int SessionExpireMinutes = int.Parse(ReadConfig.Instance.get_session_expire_minutes);
|
||||
//private static int RedisMaxReadPool = int.Parse(ConfigurationManager.AppSettings["redis_max_read_pool"]);
|
||||
//private static int RedisMaxWritePool = int.Parse(ConfigurationManager.AppSettings["redis_max_write_pool"]);
|
||||
|
||||
//private const string ip = "127.0.0.1";
|
||||
//private const string port = "6379";
|
||||
//private const string preheat = "100"; // 设置预热连接数
|
||||
//private const string connectTimeout = "100"; // 设置连接超时时间
|
||||
//private const string tryit = "1"; // 设置重试次数
|
||||
//private const string prefix = "CSRedisTest."; // 设置前缀
|
||||
//private static readonly string _connectString = $"{ip}:{port},preheat={preheat},connectTimeout={connectTimeout},tryit={tryit},prefix={prefix}";
|
||||
|
||||
static CSRedisCacheHelper()
|
||||
{
|
||||
//var redisHostStr = ConfigurationManager.AppSettings["redis_server_session"];
|
||||
var redisHostStr = ReadConfig.Instance.get_redis_server_session;
|
||||
if (!string.IsNullOrEmpty(redisHostStr))
|
||||
{
|
||||
//redis = new CSRedisClient(redisHostStr);//+ ",password=,defaultDatabase=0,poolsize=500,ssl=false,writeBuffer=10240,prefix=");
|
||||
//RedisHelper.Initialization(redis);
|
||||
//redis = new CSRedisClient[2];
|
||||
redis = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=1");
|
||||
//redis[1] = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=1");
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 添加缓存
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
public static void Set<T>(string key, T value)
|
||||
{
|
||||
redis.Set(key, value, SessionExpireMinutes * 60);
|
||||
}
|
||||
|
||||
public static T Get<T>(string key)
|
||||
{
|
||||
return redis.Get<T>(key);
|
||||
}
|
||||
|
||||
public static void Forever<T>(string key, T value)
|
||||
{
|
||||
redis.Set(key, value, -1);
|
||||
}
|
||||
public static void Del(string key)
|
||||
{
|
||||
redis.Del(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断是否存在
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="mac"></param>
|
||||
/// <returns></returns>
|
||||
public static bool Contains(string key, string mac)
|
||||
{
|
||||
bool result = redis.Exists(mac);
|
||||
if (!result)
|
||||
{
|
||||
result = redis.Exists(key);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static bool Contains(string key)
|
||||
{
|
||||
bool result = redis.Exists(key);
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// 添加Hash缓存
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
public static void HMSet<T>(string key, T value)
|
||||
{
|
||||
redis.HMSet(key, value);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取Hash缓存
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static T[] HMGet<T>(string key)
|
||||
{
|
||||
return redis.HMGet<T>(key);
|
||||
}
|
||||
/// <summary>
|
||||
/// 删除Hash缓存
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static long HDel<T>(string key)
|
||||
{
|
||||
return redis.HDel(key);
|
||||
}
|
||||
|
||||
public static void Publish(string Key,string Message)
|
||||
{
|
||||
redis.Publish(Key, Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
22
LogCap/Common/CacheKey.cs
Normal file
22
LogCap/Common/CacheKey.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LogCap.Common
|
||||
{
|
||||
public class CacheKey
|
||||
{
|
||||
public static string RoomIP_Port_Prefix = "Log_IP_Port";
|
||||
|
||||
public static string Key = "monitor_host";
|
||||
|
||||
public static string MonitorLogPrefix = "AllLogKey";
|
||||
|
||||
|
||||
public static string MonitorEndPointList= "MonitorEndPointList";
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
547
LogCap/Common/DealWithData.cs
Normal file
547
LogCap/Common/DealWithData.cs
Normal file
@@ -0,0 +1,547 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Common;
|
||||
using Commonlib;
|
||||
using LogCap.Entity;
|
||||
using MathNet.Numerics.Distributions;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
using RCUHost.Implement;
|
||||
using RCUHost.Protocols;
|
||||
using RestSharp;
|
||||
|
||||
namespace LogCap.Common
|
||||
{
|
||||
public class DealWithData
|
||||
{
|
||||
private static Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
public static void HandleData(List<byte> data, string source_ip, ushort source_port, string dest_ip, ushort dest_port)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
string fg = Tools.ByteToString(data.ToArray());
|
||||
Program.CurrentTime = DateTime.Now;
|
||||
List<byte> Header = data.Take(2).ToList();
|
||||
List<byte> Len = data.Skip(2).Take(2).ToList();
|
||||
|
||||
int Len_int = BitConverter.ToInt16(Len.ToArray(), 0);
|
||||
|
||||
//系统 ID 的四个字节。固定“T3SA”
|
||||
List<byte> ID = data.Skip(4).Take(4).ToList();
|
||||
|
||||
//命令字
|
||||
List<byte> CMD = data.Skip(8).Take(1).ToList();
|
||||
|
||||
//Frame
|
||||
List<byte> Frame = data.Skip(9).Take(2).ToList();
|
||||
|
||||
|
||||
//Project_Code
|
||||
List<byte> Project_Code = data.Skip(11).Take(2).ToList();
|
||||
|
||||
List<byte> IP3 = data.Skip(13).Take(1).ToList();
|
||||
List<byte> IP4 = data.Skip(14).Take(1).ToList();
|
||||
|
||||
List<byte> MsgData = data.Skip(15).Take(Len_int - 15 - 2).ToList();
|
||||
List<byte> CRC_byte = data.TakeLast(2).ToList();
|
||||
|
||||
|
||||
string Key = string.Format("{0}_{1}_{2}", CacheKey.RoomIP_Port_Prefix, source_ip, source_port);
|
||||
string Dest_Key = string.Format("{0}_{1}_{2}", CacheKey.RoomIP_Port_Prefix, dest_ip, dest_port);
|
||||
|
||||
//Console.WriteLine("Key is: " + Key);
|
||||
|
||||
byte cmdType_byte = CMD[0];
|
||||
BLWCommandType cmdtype = (BLWCommandType)cmdType_byte;
|
||||
string CommandType = cmdtype.ToString();
|
||||
|
||||
string? iii = ReadConfig.Instance.monitor_server_ip;
|
||||
int ppp = ReadConfig.Instance.monitor_server_port;
|
||||
|
||||
|
||||
var QQQSSS = Program.Cache.Get<List<MonitorEndPoint>>(CacheKey.MonitorEndPointList);
|
||||
if (QQQSSS.Count > 0)
|
||||
{
|
||||
|
||||
string S_R_Type = "";
|
||||
if (source_port == ppp && source_ip.Equals(iii))
|
||||
{
|
||||
S_R_Type = ConstKey.Send_TX;
|
||||
}
|
||||
else
|
||||
{
|
||||
S_R_Type = ConstKey.Receive_RX;
|
||||
}
|
||||
foreach (var item in QQQSSS)
|
||||
{
|
||||
if (item.Port == 0 && item.IPAddress.Equals(source_ip))
|
||||
{
|
||||
EndPoint_Monitor(data, source_ip, source_port, CommandType, S_R_Type);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (item.Port == source_port && item.IPAddress.Equals(source_ip))
|
||||
{
|
||||
EndPoint_Monitor(data, source_ip, source_port, CommandType, S_R_Type);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//主机注册
|
||||
if (cmdType_byte == 0x01)
|
||||
{
|
||||
|
||||
|
||||
//00 00 00 00 服务器 IP 地址
|
||||
//FF FF FC 00 子网掩码
|
||||
//C0 A8 64 01 网关
|
||||
//0D 0D 端口
|
||||
//34 D0 B8 11 38 89--MAC
|
||||
|
||||
List<byte> IP = MsgData.Take(4).ToList();
|
||||
List<byte> MaskNet = MsgData.Skip(4).Take(4).ToList();
|
||||
List<byte> GetWay = MsgData.Skip(8).Take(4).ToList();
|
||||
List<byte> Port = MsgData.Skip(12).Take(2).ToList();
|
||||
List<byte> MAC_byte = MsgData.Skip(14).Take(6).ToList();
|
||||
|
||||
string MACstring = Tools.ByteToString(MAC_byte.ToArray());
|
||||
MACstring = MACstring.Trim().Replace(" ", "-");
|
||||
|
||||
|
||||
if (Frame.All(A => A == 0xFF))
|
||||
{
|
||||
CommandType = "服务器注册命令";
|
||||
}
|
||||
|
||||
|
||||
if (source_port == ppp && source_ip.Equals(iii))
|
||||
{
|
||||
ReallyDealWith(true, data, dest_ip, dest_port, CommandType, ConstKey.Send_TX);
|
||||
}
|
||||
else
|
||||
{
|
||||
ReallyDealWith(true, data, source_ip, source_port, CommandType);
|
||||
}
|
||||
}
|
||||
//心跳包,其实会维持 连接
|
||||
else if (cmdType_byte == 0x02)
|
||||
{
|
||||
|
||||
//心跳包
|
||||
//心跳包 里面有这个 MAC地址
|
||||
//AA 55 17 00 54 33 53 41
|
||||
//02
|
||||
//F9 FF
|
||||
//A4 06
|
||||
//2C 69 --这个是IP
|
||||
//34 D0 B8 11 2C 69
|
||||
//91 F5
|
||||
//BLWCommandType cmdtype = (BLWCommandType)cmdType_byte;
|
||||
|
||||
//心跳包可能大于17个
|
||||
//有一些项目的 心跳包是 这样的
|
||||
// aa 55 17 00 54 33 53 41 02 26 ae e9 03 46 b6 34 d0 b8 11 46 b6 29 7e
|
||||
if (source_port == ppp && source_ip.Equals(iii))
|
||||
{
|
||||
ReallyDealWith(true, data, dest_ip, dest_port, CommandType, ConstKey.Send_TX);
|
||||
}
|
||||
else
|
||||
{
|
||||
ReallyDealWith(true, data, source_ip, source_port, CommandType);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
//有时候经常上报这样的数据:
|
||||
//AA 55 11 00 54 33 53 41 0E FF FF BF 06 0A C8 E7 72
|
||||
//AA 55 11 00 54 33 53 41 0E FF FF BF 06 0A C6 66 B6
|
||||
//设备状态上报
|
||||
if (cmdType_byte == 0x0E)
|
||||
{
|
||||
if (MsgData != null && MsgData.Count > 0)
|
||||
{
|
||||
//设备的数量
|
||||
byte[] couont = MsgData.Skip(7).Take(1).ToArray();
|
||||
int Count = couont[0];
|
||||
|
||||
var MMN = MsgData.Skip(8);
|
||||
for (int j = 0; j < Count; j++)
|
||||
{
|
||||
var DeviceData = MMN.Skip(j * 5).Take(5).ToArray();
|
||||
if (DeviceData[0] == 0x0A)
|
||||
{
|
||||
byte n1 = DeviceData[0];
|
||||
byte n2 = DeviceData[1];
|
||||
|
||||
byte[] vvv = new byte[] { DeviceData[4] };
|
||||
|
||||
|
||||
|
||||
BitArray bitArray = new BitArray(vvv);
|
||||
|
||||
|
||||
|
||||
//卡身份
|
||||
var qqq1 = bitArray.Cast<bool>().Take(1).ToArray();
|
||||
|
||||
var qqq2 = bitArray.Cast<bool>().Skip(1).Take(3).ToArray();
|
||||
|
||||
var qqq3 = qqq2.ToArray().Prepend(false).ToArray();
|
||||
//节能状态
|
||||
byte hhhg1 = Tools.CombineBitsToByte(qqq3[3], qqq3[2], qqq3[1], qqq3[0]);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if (source_port == ppp && source_ip.Equals(iii))
|
||||
{
|
||||
ReallyDealWith(false, data, dest_ip, dest_port, CommandType, ConstKey.Send_TX);
|
||||
}
|
||||
else
|
||||
{
|
||||
ReallyDealWith(false, data, source_ip, source_port, CommandType);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error(ex.Message);
|
||||
_logger.Error(ex.StackTrace);
|
||||
Console.WriteLine(ex.Message + " \r\n" + ex.StackTrace);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// IP 和端口 端结点监控
|
||||
/// </summary>
|
||||
/// <param name="isRegister"></param>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="source_ip"></param>
|
||||
/// <param name="source_port"></param>
|
||||
/// <param name="cmdtype"></param>
|
||||
/// <param name="Send_Or_Receive"></param>
|
||||
private static void EndPoint_Monitor(List<byte> data, string source_ip, ushort source_port, string cmdtype, string Send_Or_Receive = ConstKey.Receive_RX)
|
||||
{
|
||||
try
|
||||
{
|
||||
////系统 ID 的四个字节。固定“T3SA”
|
||||
//List<byte> ID = data.Skip(4).Take(4).ToList();
|
||||
|
||||
////命令字
|
||||
//List<byte> CMD = data.Skip(8).Take(1).ToList();
|
||||
|
||||
////Frame
|
||||
//List<byte> Frame = data.Skip(9).Take(2).ToList();
|
||||
|
||||
|
||||
//Project_Code
|
||||
List<byte> Project_Code = data.Skip(11).Take(2).ToList();
|
||||
ushort hotel_code_o = BitConverter.ToUInt16(Project_Code.ToArray(), 0);
|
||||
|
||||
////MAC
|
||||
//List<byte> IP3 = data.Skip(13).Take(1).ToList();
|
||||
//List<byte> IP4 = data.Skip(14).Take(1).ToList();
|
||||
|
||||
long hotel_code = hotel_code_o;
|
||||
string hostnumber = "";
|
||||
string hexdata = Tools.ByteToString(data.ToArray());
|
||||
HttpSend.SendLog_2(cmdtype, hotel_code, 0, "", hostnumber, source_ip, source_port, "", 0, "", Send_Or_Receive, hexdata);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private static void ReallyDealWith(bool isRegister, List<byte> data, string source_ip, ushort source_port, string cmdtype, string Send_Or_Receive = ConstKey.Receive_RX)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (data == null || data.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
ReceiverContext context = new ReceiverContext(data.ToArray());
|
||||
context.SystemHeader = DecodeSystemHeader(context.Data);
|
||||
|
||||
string Ghostnumber = context.SystemHeader.Value.HostNumber.ToString() ?? "";
|
||||
if (string.IsNullOrEmpty(Ghostnumber))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var list = Program.Cache.Get<List<Monitor_Host>>(CacheKey.Key);
|
||||
if (list == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (list.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (isRegister)
|
||||
{
|
||||
if (list != null && list.Count > 0)
|
||||
{
|
||||
#region 删除过期的数据
|
||||
try
|
||||
{
|
||||
|
||||
list.RemoveAll((Item) =>
|
||||
{
|
||||
if (Item != null)
|
||||
{
|
||||
var ddt = Item.CreateDateTime;
|
||||
if (!string.IsNullOrEmpty(ddt))
|
||||
{
|
||||
DateTime dta = DateTime.Parse(ddt);
|
||||
|
||||
DateTime nnn = dta.AddDays(30);
|
||||
if (DateTime.Now >= nnn)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
}
|
||||
#endregion
|
||||
foreach (Monitor_Host item in list)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (item == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
string HostNNN = item.Key_HostNumber;
|
||||
if (HostNNN == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
string room_num = item.RoomNumber;
|
||||
string TakeOut_MAC = item.MAC;
|
||||
string hotelcode = item.HotelCode;
|
||||
|
||||
//如果 要监控的MAC 和 当前上传的MAC相等
|
||||
if (HostNNN.Equals(Ghostnumber))
|
||||
{
|
||||
|
||||
//心跳包可以注册
|
||||
if (isRegister)
|
||||
{
|
||||
var llg = Tools.ByteToString(data.ToArray());
|
||||
string KeyHeartBeat = string.Format("{0}_{1}_{2}", CacheKey.RoomIP_Port_Prefix, source_ip, source_port);
|
||||
//Console.WriteLine("TakeOut_HostNUMBER: " + HostNNN + " Key: " + KeyHeartBeat + " MAC: " + TakeOut_MAC + " data:" + llg);
|
||||
Program.Cache.Set<string>(KeyHeartBeat, HostNNN);
|
||||
}
|
||||
|
||||
string Key = string.Format("{0}_{1}", CacheKey.RoomIP_Port_Prefix, HostNNN);
|
||||
var takeMM = Program.Cache.Get<MonitorRedis>(Key);
|
||||
//如果房间为空
|
||||
if (takeMM == null)
|
||||
{
|
||||
#region 查询房间信息
|
||||
//从数据库里面查询得
|
||||
Dictionary<string, string> dic = new Dictionary<string, string>();
|
||||
dic.Add("key", "blw_ws@2015");
|
||||
dic.Add("roomnumber", room_num);
|
||||
dic.Add("hotelcode", hotelcode);
|
||||
|
||||
string sttr = "";
|
||||
string KKK = "ChaXunData_" + hotelcode + "_" + room_num;
|
||||
string GGG = Program.Cache.Get<string>(KKK);
|
||||
if (!string.IsNullOrEmpty(GGG))
|
||||
{
|
||||
sttr = GGG;
|
||||
}
|
||||
else
|
||||
{
|
||||
sttr = Send_Http_Request_Params("api/GetHostByMAC_POST", dic);
|
||||
Program.Cache.Set<string>(KKK, GGG, new MemoryCacheEntryOptions()
|
||||
{
|
||||
SlidingExpiration = TimeSpan.FromMinutes(10)
|
||||
});
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(sttr))
|
||||
{
|
||||
|
||||
//Console.WriteLine( "查询的数据:"+sttr);
|
||||
var msghost = JsonConvert.DeserializeObject<HostMsg>(sttr);
|
||||
var dtrow = msghost.Result.FirstOrDefault();
|
||||
if (dtrow != null)
|
||||
{
|
||||
|
||||
string? hotelid = dtrow["HotelID"].ToString();
|
||||
string? hostid = dtrow["ID"].ToString();
|
||||
string? roomno = dtrow["RoomNumber"].ToString();
|
||||
string? hostnumber = dtrow["HostNumber"].ToString();
|
||||
string? LanIP = dtrow["LanIP"] ?? "";
|
||||
string? LanPort = dtrow["LanPort"].ToString();
|
||||
string? HotelID = dtrow["HotelID"].ToString();
|
||||
//string? XiaoDuCUID = dtrow["XiaoDuCUID"].ToString();
|
||||
//string? TiaoMaoCUID = dtrow["TiaoMaoCUID"].ToString();
|
||||
|
||||
string? XiaoDuCUID = "";
|
||||
string? TiaoMaoCUID = "";
|
||||
MonitorRedis mm = new MonitorRedis();
|
||||
long hhh = 0;
|
||||
long.TryParse(hotelcode, out hhh);
|
||||
|
||||
int hid = 0;
|
||||
int.TryParse(HotelID, out hid);
|
||||
|
||||
mm.TiaoMaoCUID = TiaoMaoCUID;
|
||||
mm.XiaoDuCUID = XiaoDuCUID;
|
||||
mm.HotelCode = hhh;
|
||||
mm.HotelID = hid;
|
||||
|
||||
mm.HostID = int.Parse(hostid);
|
||||
|
||||
mm.RoomNo = roomno;
|
||||
mm.HostNumber = hostnumber;
|
||||
|
||||
mm.WWW_IP = source_ip;
|
||||
mm.WWW_Port = source_port;
|
||||
|
||||
mm.LanIP = LanIP;
|
||||
mm.LanPort = int.Parse(LanPort);
|
||||
|
||||
mm.MAC = TakeOut_MAC;
|
||||
|
||||
mm.CommandType = cmdtype.ToString();
|
||||
mm.SendOrReceive = Send_Or_Receive;
|
||||
|
||||
mm.RemoteEndPointIP = source_ip;
|
||||
mm.RemoteEndPointPort = source_port;
|
||||
Program.Cache.Set<MonitorRedis>(Key, mm);
|
||||
|
||||
string dataHex = Tools.ByteToString(data.ToArray());
|
||||
|
||||
HttpSend.SendLog(mm.XiaoDuCUID, mm.TiaoMaoCUID, mm.CommandType, hhh, mm.HostID, mm.RoomNo, mm.HostNumber, source_ip, source_port, mm.LanIP, mm.LanPort, mm.MAC, Send_Or_Receive, dataHex);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
else
|
||||
{
|
||||
long hotel_code = takeMM.HotelCode;
|
||||
int host_id = takeMM.HostID;
|
||||
|
||||
string hostnumber = takeMM.HostNumber;
|
||||
string roomnumber = takeMM.RoomNo;
|
||||
|
||||
string lan_ip = takeMM.LanIP;
|
||||
int lan_port = takeMM.LanPort;
|
||||
|
||||
string www_ip = takeMM.WWW_IP;
|
||||
int www_port = takeMM.WWW_Port;
|
||||
|
||||
string mac_take_out = takeMM.MAC;
|
||||
string dataHex = Tools.ByteToString(data.ToArray());
|
||||
HttpSend.SendLog(takeMM.XiaoDuCUID, takeMM.TiaoMaoCUID, cmdtype, hotel_code, host_id, roomnumber, hostnumber, www_ip, www_port, lan_ip, lan_port, mac_take_out, Send_Or_Receive, dataHex);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (list != null && list.Count > 0)
|
||||
{
|
||||
string KeyHeartBeat = string.Format("{0}_{1}_{2}", CacheKey.RoomIP_Port_Prefix, source_ip, source_port);
|
||||
string Hostnumber_TTT = Program.Cache.Get<string>(KeyHeartBeat);
|
||||
if (!string.IsNullOrEmpty(Hostnumber_TTT))
|
||||
{
|
||||
string Key = string.Format("{0}_{1}", CacheKey.RoomIP_Port_Prefix, Hostnumber_TTT);
|
||||
//Console.WriteLine("KeyHeartBeat is " + KeyHeartBeat);
|
||||
if (!string.IsNullOrEmpty(Key))
|
||||
{
|
||||
//string bbbaaa=Tools.ByteToString(data.ToArray());
|
||||
//Console.WriteLine("111111111111111111: "+Key+" data: "+bbbaaa);
|
||||
var takeMM = Program.Cache.Get<MonitorRedis>(Key);
|
||||
if (takeMM != null)
|
||||
{
|
||||
//Console.WriteLine("22222222222");
|
||||
long hotel_code = takeMM.HotelCode;
|
||||
int host_id = takeMM.HostID;
|
||||
|
||||
string hostnumber = takeMM.HostNumber;
|
||||
string roomnumber = takeMM.RoomNo;
|
||||
|
||||
string lan_ip = takeMM.LanIP;
|
||||
int lan_port = takeMM.LanPort;
|
||||
|
||||
string www_ip = takeMM.WWW_IP;
|
||||
int www_port = takeMM.WWW_Port;
|
||||
|
||||
string mac_take_out = takeMM.MAC;
|
||||
string dataHex = Tools.ByteToString(data.ToArray());
|
||||
HttpSend.SendLog("", "", cmdtype, hotel_code, host_id, roomnumber, hostnumber, www_ip, www_port, lan_ip, lan_port, mac_take_out, Send_Or_Receive, dataHex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("出错了" + ex.Message);
|
||||
Console.WriteLine("出错了" + ex.StackTrace);
|
||||
_logger.Error(ex.Message);
|
||||
_logger.Error(ex.StackTrace);
|
||||
}
|
||||
}
|
||||
private static SystemHeader? DecodeSystemHeader(byte[] data)
|
||||
{
|
||||
return StructConverter.BytesToStruct(data, typeof(SystemHeader)) as SystemHeader?;
|
||||
}
|
||||
public static string Send_Http_Request_Params(string Url, Dictionary<string, string> Params)
|
||||
{
|
||||
string BaseUrl = ReadConfig.Instance.CRICS_URL;
|
||||
var options = new RestClientOptions(BaseUrl);
|
||||
var client = new RestClient(options);
|
||||
|
||||
var request = new RestRequest(Url);
|
||||
|
||||
foreach (var item in Params)
|
||||
{
|
||||
request.AddParameter(item.Key, item.Value);
|
||||
}
|
||||
//RoomNumber
|
||||
// The cancellation token comes from the caller. You can still make a call without it.
|
||||
string? hostlist = client.Post(request).Content;
|
||||
return hostlist;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
108
LogCap/Common/HttpSend.cs
Normal file
108
LogCap/Common/HttpSend.cs
Normal file
@@ -0,0 +1,108 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using LogCap.Entity;
|
||||
using Microsoft.VisualBasic;
|
||||
using MQTTnet;
|
||||
using RestSharp;
|
||||
|
||||
namespace LogCap.Common
|
||||
{
|
||||
public class HttpSend
|
||||
{
|
||||
|
||||
public static readonly string debug_log_report_url = ReadConfig.Instance.get_debug_log_report_url;
|
||||
public static readonly string debug_log_report_mqtt_topic = ReadConfig.Instance.get_debug_log_report_mqtt_topic;
|
||||
|
||||
public static readonly string debug_log_report_mqtt_topic_new = "blw/rcu/monitor/endpoint";
|
||||
//public static RestClient client1 = new RestClient(debug_log_report_url);
|
||||
public static async void SendLog(string xiaodu, string tianmao, string MyCommandType, long hotel_code, int host_id, string roomnumber, string hostnumber, string WWW_IP, int WWW_Port, string lan_ip, int lan_port, string mac, string send_or_receive, string dataHex)
|
||||
{
|
||||
try
|
||||
{
|
||||
//if (hostnumber.Equals("061004043027"))
|
||||
//{
|
||||
// Console.WriteLine("研发发布mqtt");
|
||||
//}
|
||||
string ti = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
||||
MonitorLog mm = new MonitorLog();
|
||||
mm.HotelCode = hotel_code;
|
||||
mm.HostID = host_id;
|
||||
mm.HostNumber = hostnumber;
|
||||
mm.RoomNo = roomnumber;
|
||||
|
||||
mm.LanIP = lan_ip;
|
||||
mm.LanPort = lan_port;
|
||||
mm.WWW_IP = WWW_IP;
|
||||
mm.WWW_Port = WWW_Port;
|
||||
|
||||
mm.MAC = mac;
|
||||
mm.SendOrReceive = send_or_receive;
|
||||
mm.CommandType = MyCommandType;
|
||||
mm.Data = dataHex;
|
||||
mm.CreateTime = ti;
|
||||
mm.XiaoDuCUID = xiaodu;
|
||||
mm.TiaoMaoCUID = tianmao;
|
||||
|
||||
|
||||
string str = Newtonsoft.Json.JsonConvert.SerializeObject(mm);
|
||||
//Console.WriteLine("发送MQTT的数据为:" + str);
|
||||
//var request1 = new RestRequest("/", Method.Post);
|
||||
|
||||
////注意方法是POST
|
||||
////两个参数名字必须是 topic 和payload ,区分大小写的
|
||||
////Console.WriteLine("Topic: " + debug_log_report_mqtt_topic);
|
||||
//request1.AddParameter("topic", debug_log_report_mqtt_topic);
|
||||
//request1.AddParameter("payload", str);
|
||||
|
||||
//await HttpSend.client1.ExecuteAsync(request1);
|
||||
//Console.WriteLine("当前时间为:" + ti+" Data: "+dataHex);
|
||||
|
||||
//var payload_data= Encoding.UTF8.GetBytes(str);
|
||||
|
||||
await Program.mqttClient.PublishStringAsync(debug_log_report_mqtt_topic, str);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("发送MQTT数据出错:" + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static async void SendLog_2(string MyCommandType, long hotel_code, int host_id, string roomnumber, string hostnumber, string WWW_IP, int WWW_Port, string lan_ip, int lan_port, string mac, string send_or_receive, string dataHex)
|
||||
{
|
||||
try
|
||||
{
|
||||
string ti = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
||||
MonitorLog mm = new MonitorLog();
|
||||
mm.HotelCode = hotel_code;
|
||||
mm.HostID = host_id;
|
||||
mm.HostNumber = hostnumber;
|
||||
mm.RoomNo = roomnumber;
|
||||
|
||||
mm.LanIP = lan_ip;
|
||||
mm.LanPort = lan_port;
|
||||
mm.WWW_IP = WWW_IP;
|
||||
mm.WWW_Port = WWW_Port;
|
||||
|
||||
mm.MAC = mac;
|
||||
mm.SendOrReceive = send_or_receive;
|
||||
mm.CommandType = MyCommandType;
|
||||
mm.Data = dataHex;
|
||||
mm.CreateTime = ti;
|
||||
mm.XiaoDuCUID = "";
|
||||
mm.TiaoMaoCUID = "";
|
||||
|
||||
string str = Newtonsoft.Json.JsonConvert.SerializeObject(mm);
|
||||
await Program.mqttClient.PublishStringAsync(debug_log_report_mqtt_topic_new, str);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("发送MQTT数据出错:" + ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
110
LogCap/Common/LogExecute.cs
Normal file
110
LogCap/Common/LogExecute.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
using System.Configuration;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Runtime.Intrinsics.Arm;
|
||||
using System.Text;
|
||||
using PacketDotNet;
|
||||
using SharpPcap;
|
||||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
|
||||
namespace LogCap.Common
|
||||
{
|
||||
public class LogExecute
|
||||
{
|
||||
public static string FilterStr = ReadConfig.Instance.get_monitor_filter;
|
||||
public Task Execute(int i)
|
||||
{
|
||||
Console.WriteLine("过滤的: " + FilterStr);
|
||||
var device2 = CaptureDeviceList.Instance[i];
|
||||
|
||||
PubRepository.liveDevice = device2;
|
||||
int readTimeoutMilliseconds = 2000;
|
||||
|
||||
|
||||
// Register our handler function to the 'packet arrival' event
|
||||
device2.OnPacketArrival +=
|
||||
new PacketArrivalEventHandler(device_OnPacketArrival);
|
||||
|
||||
// Open the devices for capturing
|
||||
device2.Open(DeviceModes.Promiscuous, readTimeoutMilliseconds);
|
||||
|
||||
// set the filters
|
||||
device2.Filter = FilterStr;
|
||||
|
||||
Console.WriteLine("device2.Filter {0} ", device2.Filter);
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("-- Listening on {0} {1}, hit 'Enter' to stop...",
|
||||
device2.Name, device2.Description);
|
||||
|
||||
// Start the capturing process
|
||||
//device1.StartCapture();
|
||||
device2.StartCapture();
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public static long TotalCountGlobal = 0;
|
||||
private static void device_OnPacketArrival(object sender, PacketCapture e)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
var rawPacket = e.GetPacket();
|
||||
var time = rawPacket.Timeval.Date;
|
||||
var len = rawPacket.Data.Length;
|
||||
var udd = rawPacket.Data;
|
||||
//Console.WriteLine("{0}:{1}:{2},{3} Len={4}",
|
||||
// time.Hour, time.Minute, time.Second, time.Millisecond, len);
|
||||
Packet p = Packet.ParsePacket(rawPacket.LinkLayerType, rawPacket.Data);
|
||||
|
||||
//var ethernetPacket = (EthernetPacket)p;
|
||||
|
||||
//// 获取源MAC和目标MAC地址
|
||||
//string sourceMac = ethernetPacket.SourceHardwareAddress.ToString();
|
||||
//[IPv4Packet: SourceAddress= 172.16.4.152,
|
||||
//DestinationAddress = 172.16.4.152,
|
||||
//HeaderLength = 5, Protocol = Udp, TimeToLive = 128][UDPPacket: SourcePort= 55966, DestinationPort = 1314]
|
||||
|
||||
var q = p.PayloadData;
|
||||
var pff = p.PayloadPacket;
|
||||
IPAddress SourceAddress = ((PacketDotNet.IPv4Packet)pff).SourceAddress;
|
||||
ushort SourcePort = ((PacketDotNet.UdpPacket)((PacketDotNet.IPPacket)pff).PayloadPacket).SourcePort;
|
||||
var DestinationAddress = ((PacketDotNet.IPv4Packet)pff).DestinationAddress;
|
||||
ushort DestinationPort = ((PacketDotNet.UdpPacket)((PacketDotNet.IPPacket)pff).PayloadPacket).DestinationPort;
|
||||
|
||||
int pport = ReadConfig.Instance.monitor_server_port;
|
||||
string iip = ReadConfig.Instance.monitor_server_ip;
|
||||
|
||||
|
||||
if (SourcePort != pport && !iip.Equals(SourceAddress.ToString()))
|
||||
{
|
||||
Interlocked.Increment(ref TotalCountGlobal);
|
||||
}
|
||||
//上行和下行如何区分
|
||||
|
||||
//如果目标IP和端口是 3339和服务器IP的话 就是上行,其它都是下行
|
||||
byte[] Data = ((PacketDotNet.IPPacket)pff).PayloadPacket.PayloadData;
|
||||
//ReceiverContext context = new ReceiverContext(Data);
|
||||
//context.SystemHeader = DecodeSystemHeader(context.Data);
|
||||
//string hostnumber= context.SystemHeader.Value.HostNumber.ToString();
|
||||
//028006045045
|
||||
//253007014192
|
||||
if (Data.Length > 0)
|
||||
{
|
||||
//Console.WriteLine(p.ToString());
|
||||
Tuple<byte[], IPAddress, ushort, IPAddress, ushort> ddd = new Tuple<byte[], IPAddress, ushort, IPAddress, ushort>(Data, SourceAddress, SourcePort, DestinationAddress, DestinationPort);
|
||||
Task.Factory.StartNew((State) =>
|
||||
{
|
||||
var tf = State as Tuple<byte[], IPAddress, ushort, IPAddress, ushort>;
|
||||
DealWithData.HandleData(tf.Item1.ToList(), tf.Item2.ToString(), tf.Item3, tf.Item4.ToString(), tf.Item5);
|
||||
}, ddd);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
73
LogCap/Common/PubRepository.cs
Normal file
73
LogCap/Common/PubRepository.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using SharpPcap;
|
||||
|
||||
namespace LogCap.Common
|
||||
{
|
||||
public class PubRepository
|
||||
{
|
||||
public static LogExecute log { get; set; }
|
||||
public static ILiveDevice liveDevice { get; set; }
|
||||
|
||||
|
||||
public static List<DDD> GetDeviceList()
|
||||
{
|
||||
var ver = Pcap.SharpPcapVersion;
|
||||
|
||||
if (CaptureDeviceList.Instance.Count < 1)
|
||||
{
|
||||
Console.WriteLine("No devices were found on this machine");
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
List<DDD> list = new List<DDD>();
|
||||
foreach (ILiveDevice dev in CaptureDeviceList.Instance)
|
||||
{
|
||||
DDD dd = new DDD();
|
||||
dd.Name = dev.Name;
|
||||
dd.Description = dev.Description;
|
||||
|
||||
list.Add(dd);
|
||||
Console.WriteLine("{2} <> {0} {1}", dev.Name, dev.Description, i);
|
||||
i++;
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public bool GetMonitor_Status()
|
||||
{
|
||||
return PubRepository.liveDevice.Started;
|
||||
}
|
||||
public static void StartMonitor(int i)
|
||||
{
|
||||
PubRepository.log = new LogExecute();
|
||||
PubRepository.log.Execute(i);
|
||||
|
||||
}
|
||||
public static void StopMonitor()
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
PubRepository.liveDevice.StopCapture();
|
||||
PubRepository.liveDevice.Dispose();
|
||||
PubRepository.log = null;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public string Test()
|
||||
{
|
||||
return "hello";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public class DDD
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
}
|
||||
}
|
||||
67
LogCap/Common/ReadConfig.cs
Normal file
67
LogCap/Common/ReadConfig.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace LogCap.Common
|
||||
{
|
||||
public class ReadConfig
|
||||
{
|
||||
public static ReadConfig Instance = new ReadConfig();
|
||||
public IConfiguration? Configuration { get; set; }
|
||||
public string? MySQLConnectionString { get; set; }
|
||||
public string? get_monitor_filter { get; set; }
|
||||
public string? get_session_expire_minutes { get; set; }
|
||||
public string? get_redis_server_session { get; set; }
|
||||
public string? get_redis_max_read_pool { get; set; }
|
||||
public string? get_redis_max_write_pool { get; set; }
|
||||
public string? get_debug_log_report_url { get; set; }
|
||||
public string? get_debug_log_report_mqtt_topic { get; set; }
|
||||
|
||||
public string? monitor_log_expire_minutes { get; set; }
|
||||
|
||||
public string? monitor_server_ip { get; set; }
|
||||
public int monitor_server_port { get; set; }
|
||||
public string? CRICS_URL { get; set; }
|
||||
public string? MQTT_ServerIP { get; set; }
|
||||
public int? MQTT_ServerPort { get; set; }
|
||||
public string? MQTT_User { get; set; }
|
||||
public string? MQTT_PassWord { get; set; }
|
||||
|
||||
|
||||
public ReadConfig()
|
||||
{
|
||||
Configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("app.json").Build();
|
||||
MySQLConnectionString = Configuration.GetConnectionString("SQLServerConnectionString");
|
||||
get_monitor_filter = Configuration.GetSection("monitor_filter").Value;
|
||||
|
||||
get_session_expire_minutes = Configuration.GetSection("session_expire_minutes").Value;
|
||||
get_redis_server_session = Configuration.GetSection("redis_server_session").Value;
|
||||
get_redis_max_read_pool = Configuration.GetSection("redis_max_read_pool").Value;
|
||||
get_redis_max_write_pool = Configuration.GetSection("redis_max_write_pool").Value;
|
||||
|
||||
monitor_log_expire_minutes = Configuration.GetSection("monitor_log_expire_minutes").Value;
|
||||
|
||||
get_debug_log_report_url = Configuration.GetSection("debug_log_report_url").Value;
|
||||
get_debug_log_report_mqtt_topic = Configuration.GetSection("debug_log_report_mqtt_topic").Value;
|
||||
|
||||
monitor_server_ip = Configuration.GetSection("monitor_server_ip").Value;
|
||||
|
||||
int i = 3339;
|
||||
int.TryParse(Configuration.GetSection("monitor_server_port").Value, out i);
|
||||
monitor_server_port = i;
|
||||
|
||||
MQTT_ServerIP = Configuration.GetSection("MQTT_ServerIP").Value;
|
||||
|
||||
int p = 1883;
|
||||
int.TryParse(Configuration.GetSection("MQTT_ServerPort").Value, out p);
|
||||
MQTT_ServerPort = p;
|
||||
|
||||
CRICS_URL = Configuration.GetSection("CRICS_URL").Value;
|
||||
MQTT_User = Configuration.GetSection("MQTT_User").Value;
|
||||
MQTT_PassWord = Configuration.GetSection("MQTT_PWD").Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
LogCap/Common/TCache.cs
Normal file
12
LogCap/Common/TCache.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LogCap.Common
|
||||
{
|
||||
internal class TCache
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user