1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- 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
- {
- /// <summary>
- /// 设备公共类
- /// </summary>
- public static class DeviceHelper
- {
- /// <summary>
- /// 计算时间差及返回当前状态
- /// </summary>
- /// <param name="oldState"></param>
- /// <param name="currentTime"></param>
- /// <param name="value"></param>
- /// <param name="changeState"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 生成网关设备的mac地址
- /// </summary>
- /// <param name="serialNumber">设备序列号 32位 </param>
- /// <returns></returns>
- 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;
- }
- }
- }
|