using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static IoTIntegrationPlatform.Model.Enum.EnumDevice;
namespace IoTIntegrationPlatform.Common
{
///
/// 设备公共类
///
public static class DeviceHelper
{
///
/// 计算时间差及返回当前状态
///
///
///
///
///
///
public static bool CalcTimeDifference(EnumDeviceStatus oldState, DateTime currentTime, DateTime value, ref EnumDeviceStatus changeState)
{
bool isTrue = false;
try
{
if (currentTime != null && value != null)
{
double minutes = TimeHelper.DiffMinutes(value, currentTime);
if (minutes >= 2 && oldState == EnumDeviceStatus.Open)
{
isTrue = true;
changeState = 0;//离线
}
else if (minutes < 3 && oldState == 0)
{
isTrue = true;
changeState = EnumDeviceStatus.Open;//在线
}
}
}
catch (Exception ex)
{
Logging.Error(ex, ex.Message);
}
return isTrue;
}
///
/// 生成网关设备的mac地址
///
/// 设备序列号 32位
///
public static string GenerateDeviceMacAddress(string serialNumber)
{
string macAddress = string.Empty;
try
{
// 制造商标识符
string manufacturerId = "0123456789ABCDEF"; // 24位
// 将字符串转换为字节数组
byte[] manufacturerBytes = new byte[6];
byte[] serialBytes = new byte[6];
for (int i = 0; i < 6; i++)
{
manufacturerBytes[i] = Convert.ToByte(manufacturerId.Substring(i * 2, 2), 16);
serialBytes[i] = Convert.ToByte(serialNumber.Substring(i * 2, 2), 16);
}
// 按位或(|)制造商标识符和序列号,得到完整的MAC地址
byte[] macBytes = new byte[6];
for (int i = 0; i < 6; i++)
{
macBytes[i] = (byte)(manufacturerBytes[i] | serialBytes[i]);
}
// 将MAC地址转换为16进制字符串并打印出来
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 6; i++)
{
sb.Append(macBytes[i].ToString("X2")); // 使用"X2"格式将字节转换为两位的16进制数字,不足两位的会在前面补0
if (i < 5) sb.Append(":"); // 每两个字节之间添加一个短横线"-"
}
macAddress = sb.ToString();
}
catch (Exception ex)
{
Logging.Error(ex, ex.Message);
}
return macAddress;
}
}
}