初始化

This commit is contained in:
2025-11-20 16:20:04 +08:00
commit 4230fa4d27
777 changed files with 232488 additions and 0 deletions

189
Commonlib/BLWCommandType.cs Normal file
View File

@@ -0,0 +1,189 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Commonlib
{
public enum BLWCommandType : byte
{
/// <summary>
/// 搜索主机
/// </summary>
= 0x01,
/// <summary>
/// 心跳包
/// </summary>
= 0x02,
/// <summary>
/// 插卡取电
/// </summary>
= 0x03,
/// <summary>
/// 服务请求
/// </summary>
= 0x04,
/// <summary>
/// 报警状态
/// </summary>
= 0x05,
/// <summary>
/// 空调状态
/// </summary>
= 0x06,
/// <summary>
/// 节能模式下发
/// </summary>
= 0x07,
/// <summary>
/// 同步时间
/// </summary>
= 0x08,
/// <summary>
/// 设置命令,客房控制
/// </summary>
= 0x10,
/// <summary>
/// 升级主机固件(局域网使用)
/// </summary>
= 0x0A,
/// <summary>
/// 升级配置文件(局域网使用)
/// </summary>
= 0xA2,
/// <summary>
/// 上传RCU软件当前版本号 RCU -> 服务器
/// </summary>
RCU软件当前版本号 = 0x0B,
/// <summary>
/// 房态变化 服务器 -> RCU
/// </summary>
= 0x0C,
/// <summary>
/// 灯光模式 服务器 -> RCU
/// </summary>
= 0x0D,
/// <summary>
/// 客房状态同步 RCU->服务器
/// </summary>
= 0x0E,
/// <summary>
/// 设备控制 服务器 -> RCU
/// </summary>
= 0xF,
/// <summary>
/// 设置主机网络参数 服务器 -> RCU
/// </summary>
= 0x11,
/// <summary>
/// 服务状态上报及控制
/// </summary>
= 0x12,
/// <summary>
/// 空调单属性控制
/// </summary>
= 0x13,
/// <summary>
/// 文字上报RCU -> 服务器
/// </summary>
= 0x14,
/// <summary>
/// 电视控制 服务器 -> RCU
/// </summary>
= 0x21,
/// <summary>
/// 主机授权 服务器 -> RCU
/// </summary>
= 0x22,
/// <summary>
/// 窗帘控制 服务器 -> RCU
/// </summary>
= 0x23,
/// <summary>
/// 电源控制 服务器 -> RCU
/// </summary>
= 0x24,
/// <summary>
/// 开锁 服务器 -> RCU
/// </summary>
= 0x25,
/// <summary>
/// 音乐控制 服务器 -> RCU
/// </summary>
= 0x26,
/// <summary>
/// 设置MAC地址 服务器 -> RCU
/// </summary>
MAC地址 = 0x27,
/// <summary>
/// 设置设备密钥 服务器 -> RCU
/// </summary>
= 0x28,
/// <summary>
/// 获取主机密钥 服务器 <-> RCU
/// </summary>
= 0x29,
/// <summary>
/// tftp升级
/// </summary>
tftp升级 = 0x68,
/// <summary>
/// ftp升级
/// </summary>
ftp升级 = 0x69,
/// <summary>
/// 读取寄存器信息
/// </summary>
= 0x30,
/// <summary>
/// 设置寄存器信息
/// </summary>
= 0x31,
/// <summary>
/// 读取主机信息
/// </summary>
= 0xB1,
/// <summary>
/// 主机获取指定域名解释的IP
/// </summary>
IP = 0xD6,
/// <summary>
/// 日志服务器域名设置
/// </summary>
= 0xD9,
/// <summary>
/// 日志服务信息查询
/// </summary>
= 0xDA,
/// <summary>
/// 白天起止时间同步
/// </summary>
= 0xDB,
= 0x32,
=0x33,
=0x34,
=0x35,
=0x36
}
public class TypeList
{
public static TypeList Instance = new TypeList();
public List<string> list = new List<string>();
private TypeList()
{
this.list = GetEnum();
this.list.Add("服务器注册命令");
}
public List<string> GetEnum()
{
BLWCommandType[] values = (BLWCommandType[])Enum.GetValues(typeof(BLWCommandType));
return values.Select(x => x.ToString()).ToList();
}
}
}

View File

@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CSRedisCore" Version="3.8.803" />
<PackageReference Include="NPOI" Version="2.7.3" />
</ItemGroup>
</Project>

14
Commonlib/ConstKey.cs Normal file
View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Commonlib
{
public class ConstKey
{
public const string Receive_RX = "主机->服务器";
public const string Send_TX = "服务器->主机";
}
}

235
Commonlib/ExcelHelper.cs Normal file
View File

@@ -0,0 +1,235 @@
using System.Data;
using NPOI.HSSF.UserModel;
using NPOI.POIFS.FileSystem;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
namespace Commonlib
{
public class ExcelHelper
{
public static DataTable ReadExcelToDataTable(string filePath)
{
DataTable dataTable = new DataTable();
// 打开 Excel 文件
IWorkbook workbook;
using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
if (filePath.EndsWith(".xlsx"))
workbook = new XSSFWorkbook(fileStream); // 读取 .xlsx 文件
else if (filePath.EndsWith(".xls"))
workbook = new HSSFWorkbook(fileStream); // 读取 .xls 文件
else
throw new Exception("文件格式不支持,只支持 .xls 和 .xlsx");
}
// 读取第一个工作表
var sheet = workbook.GetSheetAt(0);
if (sheet == null)
throw new Exception("工作表为空!");
// 获取表头
var headerRow = sheet.GetRow(0);
if (headerRow == null)
throw new Exception("未找到表头行!");
for (int i = 0; i < headerRow.LastCellNum; i++)
{
var columnName = headerRow.GetCell(i)?.ToString() ?? $"Column{i}";
dataTable.Columns.Add(columnName);
}
// 读取数据行
for (int i = 1; i <= sheet.LastRowNum; i++) // 从第 1 行(索引为 1开始读取
{
var row = sheet.GetRow(i);
if (row == null) continue;
var dataRow = dataTable.NewRow();
for (int j = 0; j < row.LastCellNum; j++)
{
var cell = row.GetCell(j);
dataRow[j] = cell != null ? GetCellValue(cell) : DBNull.Value;
}
dataTable.Rows.Add(dataRow);
}
return dataTable;
}
static object GetCellValue(ICell cell)
{
switch (cell.CellType)
{
case CellType.Numeric:
return DateUtil.IsCellDateFormatted(cell) ? cell.DateCellValue : cell.NumericCellValue;
case CellType.String:
return cell.StringCellValue;
case CellType.Boolean:
return cell.BooleanCellValue;
case CellType.Formula:
return cell.CellFormula;
case CellType.Blank:
return string.Empty;
default:
return cell.ToString();
}
}
public static void CreateNewExcel(string filePath, DataTable dataTable)
{
IWorkbook workbook;
if (filePath.EndsWith(".xlsx"))
workbook = new XSSFWorkbook(); // 创建 .xlsx 文件
else
workbook = new HSSFWorkbook(); // 创建 .xls 文件
var sheet = workbook.CreateSheet("销售订单");
// 写入表头
var headerRow = sheet.CreateRow(0);
for (int i = 0; i < dataTable.Columns.Count; i++)
{
headerRow.CreateCell(i).SetCellValue(dataTable.Columns[i].ColumnName);
}
// 写入数据
for (int i = 0; i < dataTable.Rows.Count; i++)
{
var dataRow = sheet.CreateRow(i + 1);
for (int j = 0; j < dataTable.Columns.Count; j++)
{
dataRow.CreateCell(j).SetCellValue(dataTable.Rows[i][j].ToString());
}
}
// 设置列宽和行高
SetColumnWidthAndRowHeight(workbook, 0, 0, 1);
SetColumnWidthAndRowHeight(workbook, 0, 1, 1);
SetColumnWidthAndRowHeight(workbook, 0, 2, 1);
SetColumnWidthAndRowHeight(workbook, 0, 3, 1);
SetColumnWidthAndRowHeight(workbook, 0, 4, 1);
// 保存文件
using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
workbook.Write(stream);
}
}
public static void AppendExcel(string filepath)
{
FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);//读取流
POIFSFileSystem ps = new POIFSFileSystem(fs);//需using NPOI.POIFS.FileSystem;
IWorkbook workbook = new HSSFWorkbook(ps);
ISheet sheet = workbook.GetSheetAt(0);//获取工作表
IRow row = sheet.GetRow(0); //得到表头
FileStream fout = new FileStream(filepath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);//写入流
row = sheet.CreateRow((sheet.LastRowNum + 1));//在工作表中添加一行
ICell cell1 = row.CreateCell(0);
cell1.SetCellValue("测试数据");//赋值
fout.Flush();
workbook.Write(fout);//写入文件
workbook = null;
fout.Close();
}
public static void EncryptExcel(string path, string password)
{
// 创建HSSF格式工作簿仅支持.xls
var workbook = new HSSFWorkbook();
var sheet = workbook.CreateSheet("Data");
// 设置工作簿保护密码
workbook.WriteProtectWorkbook(password, "owner");
// 保存加密文件
using (var fs = new FileStream(path, FileMode.Create))
{
workbook.Write(fs);
}
}
public static void SetColumnWidthAndRowHeight(IWorkbook workbook, int sheetIndex, int columnIndex, int rowIndex)
{
var sheet = workbook.GetSheetAt(sheetIndex);
// 设置列宽(单位是 1/256 字符宽度)
sheet.SetColumnWidth(columnIndex, 30 * 256); // 设置第 1 列宽度为 20
// 设置行高(单位是点数)
var row = sheet.GetRow(rowIndex) ?? sheet.CreateRow(rowIndex);
row.HeightInPoints = 25; // 设置行高为 25 点
}
public void SetCellStyle(IWorkbook workbook, int sheetIndex, int rowIndex, int colIndex)
{
var sheet = workbook.GetSheetAt(sheetIndex);
var cell = sheet.GetRow(rowIndex).GetCell(colIndex) ?? sheet.GetRow(rowIndex).CreateCell(colIndex);
var style = workbook.CreateCellStyle();
// 设置字体
var font = workbook.CreateFont();
font.FontHeightInPoints = 12;
font.FontName = "Arial";
font.IsBold = true;
style.SetFont(font);
// 设置边框
style.BorderBottom = BorderStyle.Thin;
style.BorderLeft = BorderStyle.Thin;
style.BorderRight = BorderStyle.Thin;
style.BorderTop = BorderStyle.Thin;
// 设置背景颜色
style.FillForegroundColor = IndexedColors.LightBlue.Index;
style.FillPattern = FillPattern.SolidForeground;
cell.CellStyle = style;
cell.SetCellValue("示例文本");
}
public void FullExample(string filePath, DataTable dataTable)
{
IWorkbook workbook = filePath.EndsWith(".xlsx") ? (IWorkbook)new XSSFWorkbook() : new HSSFWorkbook();
var sheet = workbook.CreateSheet("Sheet1");
// 设置表头
var headerRow = sheet.CreateRow(0);
for (int i = 0; i < dataTable.Columns.Count; i++)
{
headerRow.CreateCell(i).SetCellValue(dataTable.Columns[i].ColumnName);
}
// 写入数据
for (int i = 0; i < dataTable.Rows.Count; i++)
{
var dataRow = sheet.CreateRow(i + 1);
for (int j = 0; j < dataTable.Columns.Count; j++)
{
dataRow.CreateCell(j).SetCellValue(dataTable.Rows[i][j].ToString());
}
}
// 设置列宽和行高
SetColumnWidthAndRowHeight(workbook, 0, 1, 1);
// 设置单元格样式
SetCellStyle(workbook, 0, 1, 1);
// 保存文件
using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
workbook.Write(stream);
}
}
}
}

14
Commonlib/HostMsg.cs Normal file
View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Commonlib
{
public class HostMsg
{
public bool IsSuccess { get; set; }
public List<Dictionary<string, string>> Result { get; set; }
}
}

60
Commonlib/HostNumber.cs Normal file
View File

@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace RCUHost.Protocols
{
/// <summary>
/// 主机编号(房号)
/// </summary>
[StructLayoutAttribute(LayoutKind.Sequential, Pack = 1)]
public struct HostNumber
{
/// <summary>
/// 栋
/// </summary>
public byte NBuild;
/// <summary>
/// 单元
/// </summary>
public byte NUnit;
/// <summary>
/// 层
/// </summary>
public byte NFloor;
/// <summary>
/// 房
/// </summary>
public byte NRoom;
/// <summary>
/// 主机编码
/// </summary>
/// <returns></returns>
public override string ToString()
{
return NBuild.ToString("000") + NUnit.ToString("000") + NFloor.ToString("000") + NRoom.ToString("000");
}
/// <summary>
/// 对应房号
/// </summary>
/// <returns></returns>
public string ToRoomNumber()
{
return NFloor.ToString("000") + NRoom.ToString("000");
}
/// <summary>
/// 酒店编码
/// </summary>
/// <returns></returns>
public Int64 ToHotelCode()
{
return Convert.ToInt64(Convert.ToInt16(NBuild.ToString("000")) + Convert.ToInt16(NUnit.ToString("000")) * 256);
}
}
}

21
Commonlib/MonitorData.cs Normal file
View File

@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Commonlib
{
public class MonitorData
{
public string? Handler { get; set; }
public List<MonitorEndPoint?> DataList { get; set; }
}
public class MonitorEndPoint
{
public string? IPAddress { get; set; }
public int Port { get; set; }
public string AddDateTime { get; set; }
}
}

90
Commonlib/MonitorLog.cs Normal file
View File

@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Commonlib
{
public class MonitorLog
{
public long HotelCode { get; set; }
public int HotelID { get; set; }
public int HostID { get; set; }
public string HostNumber { get; set; }
public string RoomNo { get; set; }
public string LanIP { get; set; }
public int LanPort { get; set; }
public string WWW_IP { get; set; }
public int WWW_Port { get; set; }
public string MAC { get; set; }
public string CommandType { get; set; }
public string SendOrReceive { get; set; }
public string Data { get; set; }
public string CreateTime { get; set; }
public string TiaoMaoCUID { get; set; }
public string XiaoDuCUID { get; set; }
}
public class IOTMonitorData
{
public double Step { get; set; }
public string RequestId { get; set; }
public string Platform { get; set; }
public string TriggerTime { get; set; }
public string HotelId { get; set; }
public int HotelCode { get; set; }
public string HotelName { get; set; }
public string RoomNumber { get; set; }
public string CommandDescription { get; set; }
}
public class TCLMonitorData
{
public string? Step { get; set; }
public string? HotelCode { get; set; }
public string? RoomNumber { get; set; }
public string? RequestTime { get; set; }
public string? RequestCmd { get; set; }
public string? RequestId { get; set; }
public string? CmdType { get; set; }
public string? ResponseMsg { get; set; }
public string? ResponseTime { get; set; }
}
public class SkyWorthMonitorData
{
public string? Step { get; set; }
public string? HotelCode { get; set; }
public string? RoomNumber { get; set; }
public string? RequestTime { get; set; }
public string? RequestCmd { get; set; }
public string? RequestId { get; set; }
public string? CmdType { get; set; }
public string? ResponseMsg { get; set; }
public string? ResponseTime { get; set; }
}
public class IOT_Monitor_Host
{
public string HotelCode { get; set; }
public string HotelID { get; set; }
public string RoomNumber { get; set; }
public string CreateDateTime { get; set; }
}
public class WebAPIMonitorData
{
public string RemoteIP { get; set; }
public string HotelCode { get; set; }
public string RoomNumber { get; set; }
public string InvokStart_Time { get; set; }
public string InvokEnd_Time { get; set; }
public List<string> Parameters { get; set; }
public string MethodName { get; set; }
}
}

40
Commonlib/Monitor_Host.cs Normal file
View File

@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Commonlib
{
public class Monitor_Host
{
public string HostID { get; set; }
public string HotelCode { get; set; }
public string Key_HostNumber { get; set; }
public string RoomNumber { get; set; }
public string MAC { get; set; }
public string CreateDateTime { get; set; }
}
public class Monitor_Host_EX : Monitor_Host
{
public int TX { get; set; }
public int RX { get; set; }
public string? LastData { get; set; }
}
public class Monitor_Host_EX_Cache
{
public string? MAC { get; set; }
public int? TX { get; set; }
public int? RX { get; set; }
public string? LastData { get; set; }
}
public class Monitor_Host_Wrap
{
public List<Monitor_Host_EX> Data { get; set; }
public List<string> CommandList { get; set; }
}
}

View File

@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using RCUHost.Protocols;
namespace RCUHost.Implement
{
public class ReceiverContext
{
private byte[] orginData;
/// <summary>
///
/// </summary>
/// <param name="orginData"></param>
/// <param name="remoteEndPoint"></param>
/// <param name="customer">自定义序号:用于推送通讯命令</param>
public ReceiverContext(byte[] orginData)
{
this.orginData = orginData;
}
public byte[] Data { get { return this.orginData; } }
public SystemHeader? SystemHeader { get; set; }
}
}

View File

@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace Common
{
public sealed class StructConverter
{
public static int SizeOf(object structObj)
{
unsafe { return Marshal.SizeOf(structObj); }
}
public static int SizeOf(Type type)
{
return Marshal.SizeOf(type);
}
public static int SizeOf<T>()
{
return Marshal.SizeOf(typeof(T));
}
/// <summary>
/// 结构体转成字节数组
/// </summary>
/// <param name="structObj"></param>
/// <returns></returns>
public static byte[] StructToBytes(object structObj)
{
int size = SizeOf(structObj);
byte[] bytes = new byte[size];
IntPtr structPtr = Marshal.AllocHGlobal(size);
try
{
Marshal.StructureToPtr(structObj, structPtr, false);
Marshal.Copy(structPtr, bytes, 0, size);
}
finally
{
Marshal.FreeHGlobal(structPtr);
}
return bytes;
}
/// <summary>
/// 字节数组转成结构体
/// </summary>
/// <param name="bytes"></param>
/// <param name="type"></param>
/// <returns></returns>
public static object BytesToStruct(byte[] bytes, Type type)
{
return BytesToStruct(bytes, 0, type);
}
/// <summary>
/// 字节数组转成结构体
/// </summary>
/// <param name="bytes"></param>
/// <param name="startIndex"></param>
/// <param name="type"></param>
/// <returns></returns>
public static object BytesToStruct(byte[] bytes, int startIndex, Type type)
{
object structObj = null;
int size = Marshal.SizeOf(type);
if (startIndex < 0 || startIndex >= bytes.Length || size > bytes.Length)
{
return null;
}
IntPtr structPtr = Marshal.AllocHGlobal(size);
try
{
Marshal.Copy(bytes, startIndex, structPtr, size);
structObj = Marshal.PtrToStructure(structPtr, type);
}
finally
{
Marshal.FreeHGlobal(structPtr);
}
return structObj;
}
}
}

51
Commonlib/SystemHeader.cs Normal file
View File

@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace RCUHost.Protocols
{
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct SystemHeader
{
#if SIMON
public const string SYSTEM_ID = "T3SA";
#else
public const string SYSTEM_ID = "TSA1";
#endif
public const ushort SIGNATURE = 0x55AA;
/// <summary>
/// Magic
/// </summary>
public ushort Signature;
/// <summary>
/// 帧总字节数,包括头部和数据
/// </summary>
public ushort FrameLength;
/// <summary>
/// 系统标识
/// </summary>
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)]
public char[] SystemID;
/// <summary>
/// 指令代码
/// </summary>
public byte CmdType;
/// <summary>
/// 帧序号
/// </summary>
public ushort FrameNo;
/// <summary>
/// 主机编号(房号)
/// </summary>
public HostNumber HostNumber;
}
}

399
Commonlib/Tools.cs Normal file
View File

@@ -0,0 +1,399 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Security.Cryptography;
using System.Web;
using System.Net.NetworkInformation;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace Common
{
public static class Tools
{
/// <summary>
/// 时间戳转本地时间-时间戳精确到秒
/// </summary>
public static DateTime ToLocalTimeDateBySeconds(long unix)
{
var dto = DateTimeOffset.FromUnixTimeSeconds(unix);
return dto.ToLocalTime().DateTime;
}
/// <summary>
/// 时间转时间戳Unix-时间戳精确到秒
/// </summary>
public static long ToUnixTimestampBySeconds(DateTime dt)
{
DateTimeOffset dto = new DateTimeOffset(dt);
return dto.ToUnixTimeSeconds();
}
/// <summary>
/// 时间戳转本地时间-时间戳精确到毫秒
/// </summary>
public static DateTime ToLocalTimeDateByMilliseconds(long unix)
{
var dto = DateTimeOffset.FromUnixTimeMilliseconds(unix);
return dto.ToLocalTime().DateTime;
}
/// <summary>
/// 时间转时间戳Unix-时间戳精确到毫秒
/// </summary>
public static long ToUnixTimestampByMilliseconds(DateTime dt)
{
DateTimeOffset dto = new DateTimeOffset(dt);
return dto.ToUnixTimeMilliseconds();
}
/// <summary>
/// 生成CRC16校验
/// </summary>
/// <param name="buffer"></param>
/// <param name="len"></param>
/// <returns></returns>
public static ushort CRC16(byte[] buffer, int len)
{
uint xda, xdapoly;
byte xdabit;
xda = 0xFFFF;
xdapoly = 0xA001; // (X**16 + X**15 + X**2 + 1)
for (int i = 0; i < len; i++)
{
xda ^= buffer[i];
for (int j = 0; j < 8; j++)
{
xdabit = (byte)(xda & 0x01);
xda >>= 1;
if (xdabit == 1)
{
xda ^= xdapoly;
}
}
}
return Convert.ToUInt16(xda & 0xffff);
}
/// <summary>
/// 计算文件的MD5值
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public static byte[] MD5(string file)
{
using (Stream stream = File.Open(file, FileMode.Open))
{
return MD5(stream);
}
}
/// <summary>
/// 计算流的MD5值
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public static byte[] MD5(Stream stream)
{
using (MD5 md5 = new MD5CryptoServiceProvider())
{
return md5.ComputeHash(stream);
}
}
/// <summary>
/// 计算文件MD5
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public static string ComputeFileHash(Stream stream)
{
byte[] retVal = Tools.MD5(stream);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
}
/// <summary>
/// 将MD5字符串转换成整型数组表示
/// </summary>
/// <param name="md5"></param>
/// <returns></returns>
public static uint[] MD5StringToUIntArray(string md5)
{
if (String.IsNullOrWhiteSpace(md5))
{
throw new ArgumentException("参数不能为空。", "md5");
}
try
{
uint[] md5Arr = new uint[4] { 0, 0, 0, 0 };
md5Arr[0] = Convert.ToUInt32(md5.Substring(0, 8), 16);
md5Arr[1] = Convert.ToUInt32(md5.Substring(8, 8), 16);
md5Arr[2] = Convert.ToUInt32(md5.Substring(16, 8), 16);
md5Arr[3] = Convert.ToUInt32(md5.Substring(24, 8), 16);
return md5Arr;
}
catch (Exception ex)
{
throw new ApplicationException("转换MD5出错。", ex);
}
}
/// <summary>
/// 用MD5加密字符串
/// </summary>
/// <param name="str">待加密的字符串</param>
/// <returns></returns>
public static string MD5Encrypt(string str)
{
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
byte[] hashedDataBytes;
hashedDataBytes = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(str));
StringBuilder tmp = new StringBuilder();
foreach (byte i in hashedDataBytes)
{
tmp.Append(i.ToString("x2"));
}
return tmp.ToString();
}
/// <summary>
/// 获取指定长的的字符串对应的16进制字节码如果长度不够末位自动补0
/// </summary>
/// <param name="s"></param>
/// <param name="length"></param>
/// <returns></returns>
public static byte[] GetBytes(String str, int length)
{
byte[] s = System.Text.Encoding.GetEncoding("gb2312").GetBytes(str);
int fixLength = length - s.Length;
if (s.Length < length)
{
byte[] S_bytes = new byte[length];
Array.Copy(s, 0, S_bytes, 0, s.Length);
for (int x = length - fixLength; x < length; x++)
{
S_bytes[x] = 0x00;
}
return S_bytes;
}
return s;
}
/// 获取一个随机数组
/// </summary>
/// <param name="num">数组个数</param>
/// <param name="minValue"></param>
/// <param name="maxValue"></param>
/// <returns></returns>
public static int[] GetRandomNum(int num, int minValue, int maxValue)
{
Random ra = new Random(unchecked((int)DateTime.Now.Ticks));
int[] arrNum = new int[num];
int tmp = 0;
for (int i = 0; i <= num - 1; i++)
{
tmp = ra.Next(minValue, maxValue); //随机取数
arrNum[i] = tmp;
}
return arrNum;
}
/// <summary>
/// 字节内容转换为字符串
/// </summary>
/// <param name="bytesData"></param>
/// <returns></returns>
public static string ByteToString(byte[] bytesData)
{
StringBuilder result = new StringBuilder();
foreach (byte r in bytesData)
{
result.Append(r.ToString("X2") + " ");
}
return result.ToString().Trim();
}
/// <summary>
/// 把int32类型的数据转存到2个字节的byte数组中
/// </summary>
/// <param name="m">int32类型的数据</param>
/// <param name="arry">2个字节大小的byte数组</param>
/// <returns></returns>
public static byte[] Int32ToByte(Int32 data)
{
byte[] arry = new byte[2];
arry[0] = (byte)((data & 0xFF00) >> 8);
arry[1] = (byte)(data & 0xFF);
return arry;
}
/// <summary>
/// 把int32类型的数据转存到2个字节的byte数组中小端
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] Int32ToByte2(Int32 data)
{
byte[] arry = new byte[2];
arry[0] = (byte)(data & 0xFF);
arry[1] = (byte)((data & 0xFF00) >> 8);
//arry[2] = (byte)((data & 0xFF0000) >> 16);
//arry[3] = (byte)((data >> 24) & 0xFF);
return arry;
}
/// <summary>
/// 2位byte转换为int类型
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static int ByteToInt(byte[] data)
{
return (data[1] & 0xFF) << 8 | data[0];
}
/// <summary>
/// 4位byte转换为long类型
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static long Byte4ToLong(byte[] data)
{
return (data[3] << 24) & 0xFF | (data[2] & 0xFF00) << 16 | (data[1] & 0xFF) << 8 | data[0];
}
/// <summary>
/// long类型转换为4位byte
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static byte[] LongToByte4(long data)
{
byte[] arry = new byte[4];
arry[0] = (byte)(data & 0xFF);
arry[1] = (byte)((data & 0xFF00) >> 8);
arry[2] = (byte)((data & 0xFF0000) >> 16);
arry[3] = (byte)((data >> 24) & 0xFF);
return arry;
}
/// <summary>
/// 得到字符串的长度一个汉字算2个字符
/// </summary>
/// <param name="str">字符串</param>
/// <returns>返回字符串长度</returns>
public static int GetLength(string str)
{
if (str.Length == 0) return 0;
ASCIIEncoding ascii = new ASCIIEncoding();
int tempLen = 0;
byte[] s = ascii.GetBytes(str);
for (int i = 0; i < s.Length; i++)
{
if ((int)s[i] == 63)
{
tempLen += 2;
}
else
{
tempLen += 1;
}
}
return tempLen;
}
/// <summary>
/// HEX编码转换为——>ASCII编码
/// </summary>
/// <param name="Msg"></param>
/// <returns></returns>
public static string HexToASCII(string Msg)
{
byte[] buff = new byte[Msg.Length / 2];
string Message = "";
for (int i = 0; i < buff.Length; i++)
{
buff[i] = byte.Parse(Msg.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber);
}
System.Text.Encoding chs = System.Text.Encoding.ASCII;
Message = chs.GetString(buff);
return Message;
}
/// <summary>
/// HEX编码转换为——>字符串
/// </summary>
/// <param name="Msg"></param>
/// <returns></returns>
public static string HexToStr(string Msg)
{
byte[] buff = new byte[Msg.Length / 2];
string Message = "";
for (int i = 0; i < buff.Length; i++)
{
buff[i] = byte.Parse(Msg.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber);
}
System.Text.Encoding chs = System.Text.Encoding.GetEncoding("gb2312");
Message = chs.GetString(buff);
return Message;
}
/// <summary>
/// 字符串转化为——>HEX编码
/// </summary>
/// <param name="Msg"></param>
/// <returns></returns>
public static string StrToHex(string Msg)
{
byte[] bytes = System.Text.Encoding.Default.GetBytes(Msg);//转换为字节(十进制)
string str = "";
for (int i = 0; i < bytes.Length; i++)
{
str += string.Format("{0:X}", bytes[i]); //转换为十六进制
}
return str;
}
public static string GetCurrentTimeStamp()
{
TimeSpan ts = DateTime.Now.ToLocalTime() - new DateTime(1970, 1, 1, 8, 0, 0, 0, DateTimeKind.Utc);
string current_timestamp = Convert.ToInt64(ts.TotalSeconds).ToString();
return current_timestamp;
}
public static byte[] HEXString2ByteArray(string hexString)
{
char[] hexCharacters = hexString.ToCharArray();
byte[] byteArray = new byte[hexCharacters.Length / 2];
for (int i = 0; i < byteArray.Length; i++)
{
string hexVal = string.Concat(hexCharacters[i * 2], hexCharacters[i * 2 + 1]);
byteArray[i] = Convert.ToByte(hexVal, 16);
}
return byteArray;
}
public static byte CombineBitsToByte(bool bit0, bool bit1, bool bit2, bool bit3)
{
byte result = (byte)((bit0 ? 1 : 0) << 3); // 将bit0移到第4位高位
result |= (byte)((bit1 ? 1 : 0) << 2); // 将bit1移到第3位
result |= (byte)((bit2 ? 1 : 0) << 1); // 将bit2移到第2位
result |= (byte)(bit3 ? 1 : 0); // 将bit3移到第1位低位
return result;
}
}
}

18
Commonlib/Tools_EX.cs Normal file
View File

@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Commonlib
{
public class Tools_EX
{
public DateTime GetSpecificZoneNowDate(string zoneName = "China Standard Time")
{
var utcdate = DateTime.Now.ToUniversalTime();
var beiJingZone = TimeZoneInfo.FindSystemTimeZoneById(zoneName);
return TimeZoneInfo.ConvertTimeFromUtc(utcdate, beiJingZone);
}
}
}

39
Commonlib/TuBiao.cs Normal file
View File

@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Commonlib
{
public class TuBiao
{
public List<TRX> TX { get; set; }
public List<TRX> RX { get; set; }
public List<int> TX_EVERY_Hour { get; set; }
public List<int> RX_EVERY_Hour { get; set; }
public List<int> RTX_EVERY_Hour { get; set; }
}
public class TRX
{
public string name { get; set; }
public int value { get; set; }
}
public class _JieGuo
{
public string CreateTime { get; set; }
public string CommandType { get; set; }
public string SendOrReceive { get; set; }
}
public class JieGuo
{
public int Count { get; set; }
public string CommandType { get; set; }
public string SendOrReceive { get; set; }
}
}