|
|
|
@ -93,14 +93,14 @@ namespace FBeeService
|
|
|
|
|
public void X81(string sn)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"refresh {sn}");
|
|
|
|
|
this.Clients.TryGetValue(sn, out TcpClientWrapper client);
|
|
|
|
|
var list = new List<byte>() { 0x08, 0x00 };
|
|
|
|
|
list.AddRange(sn.HexToBytes().Reverse());
|
|
|
|
|
list.Add(0xfe);
|
|
|
|
|
list.Add(0x81);
|
|
|
|
|
this.Write(sn, list.ToArray());
|
|
|
|
|
this.Write(sn, RequestType.x81, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 接收当前连接的设备信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sn"></param>
|
|
|
|
|
/// <param name="data"></param>
|
|
|
|
|
private void X01(string sn, byte[] data)
|
|
|
|
|
{
|
|
|
|
|
using (var ms = new MemoryStream(data))
|
|
|
|
@ -166,16 +166,13 @@ namespace FBeeService
|
|
|
|
|
var repo = scope.ServiceProvider.GetService<IRepository<FBeeDevice>>();
|
|
|
|
|
var device = repo.ReadOnlyTable().FirstOrDefault(o => o.Online != 0 && o.IEEE == ieee);
|
|
|
|
|
var address = device.Address;
|
|
|
|
|
var list = new List<byte>() { 0x15, 0x00 };
|
|
|
|
|
list.AddRange(sn.HexToBytes().Reverse());
|
|
|
|
|
list.Add(0xfe);
|
|
|
|
|
list.Add(0x95);
|
|
|
|
|
var list = new List<byte>();
|
|
|
|
|
list.Add(0x0c);
|
|
|
|
|
list.Add(0x02);
|
|
|
|
|
list.AddRange(address.HexToBytes());
|
|
|
|
|
list.AddRange(ieee.HexToBytes());
|
|
|
|
|
list.Add(0x01);
|
|
|
|
|
this.Write(sn, list.ToArray());
|
|
|
|
|
this.Write(sn, RequestType.x95, list);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -197,8 +194,8 @@ namespace FBeeService
|
|
|
|
|
var address = device.Address;
|
|
|
|
|
var list = new List<byte>() { 0x16, 0x00 };
|
|
|
|
|
list.AddRange(sn.HexToBytes().Reverse());
|
|
|
|
|
list.Add(0xfe);
|
|
|
|
|
list.Add(0x82);
|
|
|
|
|
list.Add(RequestType.xfe);
|
|
|
|
|
list.Add(RequestType.x82);
|
|
|
|
|
list.Add(0x0d);
|
|
|
|
|
list.Add(0x02);
|
|
|
|
|
list.AddRange(address.HexToBytes());
|
|
|
|
@ -206,7 +203,7 @@ namespace FBeeService
|
|
|
|
|
list.Add(0x01);
|
|
|
|
|
list.AddRange(new byte[] { 0x00, 0x00 });
|
|
|
|
|
list.Add(status);
|
|
|
|
|
this.Write(sn, list.ToArray());
|
|
|
|
|
this.Write(sn, RequestType.x82, list);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -214,6 +211,36 @@ namespace FBeeService
|
|
|
|
|
//0x83
|
|
|
|
|
//0x84
|
|
|
|
|
//0x85
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 0x07接收指定设备的开关状态
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sn"></param>
|
|
|
|
|
/// <param name="data"></param>
|
|
|
|
|
private void X07(string sn, byte[] data)
|
|
|
|
|
{
|
|
|
|
|
using (var ms = new MemoryStream(data))
|
|
|
|
|
{
|
|
|
|
|
var responseType = ms.ReadByte();
|
|
|
|
|
var dataLength = ms.ReadByte();
|
|
|
|
|
var address = ms.ReadHexString(2);
|
|
|
|
|
var endpoint = ms.ReadByte();
|
|
|
|
|
using (var scope = _applicationServices.CreateScope())
|
|
|
|
|
{
|
|
|
|
|
var deviceRepo = scope.ServiceProvider.GetService<IRepository<FBeeDevice>>();
|
|
|
|
|
var device = deviceRepo.Table().FirstOrDefault(o => o.Sn == sn && o.Address == address);
|
|
|
|
|
if (device != null)
|
|
|
|
|
{
|
|
|
|
|
device.Power = ms.ReadInt();
|
|
|
|
|
deviceRepo.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"device {address} hasn't save in database");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//0x86
|
|
|
|
|
//0x87
|
|
|
|
|
//0x88
|
|
|
|
@ -237,13 +264,14 @@ namespace FBeeService
|
|
|
|
|
//0x9d获取网关信息
|
|
|
|
|
public void X9d(string sn)
|
|
|
|
|
{
|
|
|
|
|
var list = new List<byte>() { 0x08, 0x00 };
|
|
|
|
|
list.AddRange(sn.HexToBytes().Reverse());
|
|
|
|
|
list.Add(0xfe);
|
|
|
|
|
list.Add(0x9d);
|
|
|
|
|
this.Write(sn, list.ToArray());
|
|
|
|
|
this.Write(sn, RequestType.x9d, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 0x15接收网关信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sn"></param>
|
|
|
|
|
/// <param name="data"></param>
|
|
|
|
|
private void X15(string sn, byte[] data)
|
|
|
|
|
{
|
|
|
|
|
using (var ms = new MemoryStream(data))
|
|
|
|
@ -293,7 +321,69 @@ namespace FBeeService
|
|
|
|
|
//0xac
|
|
|
|
|
//0xaf
|
|
|
|
|
//0xb0
|
|
|
|
|
//0x70
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 0x70设备上报信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sn"></param>
|
|
|
|
|
/// <param name="data"></param>
|
|
|
|
|
private void X70(string sn, byte[] data)
|
|
|
|
|
{
|
|
|
|
|
using (var ms = new MemoryStream(data))
|
|
|
|
|
{
|
|
|
|
|
var responseType = ms.ReadByte();
|
|
|
|
|
var dataLength = ms.ReadByte();
|
|
|
|
|
var address = ms.ReadHexString(2);
|
|
|
|
|
var endpoint = ms.ReadByte();
|
|
|
|
|
using (var scope = _applicationServices.CreateScope())
|
|
|
|
|
{
|
|
|
|
|
var deviceRepo = scope.ServiceProvider.GetService<IRepository<FBeeDevice>>();
|
|
|
|
|
var device = deviceRepo.Table().FirstOrDefault(o => o.Sn == sn && o.Address == address);
|
|
|
|
|
var clusterId = ms.ReadInt();
|
|
|
|
|
if (device != null)
|
|
|
|
|
{
|
|
|
|
|
var reportCount = ms.ReadByte();
|
|
|
|
|
var props = new Dictionary<int, byte[]>();
|
|
|
|
|
for (int i = 0; i < reportCount; i++)
|
|
|
|
|
{
|
|
|
|
|
var propId = ms.ReadInt();
|
|
|
|
|
var propDataTypeValue = ms.ReadByte();
|
|
|
|
|
int propDataLength;
|
|
|
|
|
if (Enum.IsDefined(typeof(DataType), propDataTypeValue))
|
|
|
|
|
{
|
|
|
|
|
var propDataType = (DataType)propDataTypeValue;
|
|
|
|
|
if (propDataType == DataType.bitstring || propDataType == DataType.characterstring)
|
|
|
|
|
{
|
|
|
|
|
propDataLength = ms.ReadByte();
|
|
|
|
|
}
|
|
|
|
|
else if (propDataType == DataType.longbitstring || propDataType == DataType.longcharacterstring)
|
|
|
|
|
{
|
|
|
|
|
propDataLength = ms.ReadInt();
|
|
|
|
|
}
|
|
|
|
|
else if (propDataType == DataType.sequence || propDataType == DataType.set || propDataType == DataType.bag)
|
|
|
|
|
{
|
|
|
|
|
propDataLength = ms.ReadInt();
|
|
|
|
|
}
|
|
|
|
|
else if (propDataType == DataType.unknown)
|
|
|
|
|
{
|
|
|
|
|
propDataLength = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
propDataLength = Convert.ToInt32(Regex.Match(propDataType.GetName(), @"\d+").Groups[1].Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
propDataLength = 1;
|
|
|
|
|
}
|
|
|
|
|
var propData = ms.Read(propDataLength);
|
|
|
|
|
props.Add(propId, propData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//0x72
|
|
|
|
|
//0xb1
|
|
|
|
|
//0xb2
|
|
|
|
@ -476,83 +566,30 @@ namespace FBeeService
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"read:{BitConverter.ToString(data).Replace("-", " ")}");
|
|
|
|
|
var responseType = data[0];
|
|
|
|
|
if (responseType == MessageType.x15)
|
|
|
|
|
if (responseType == ResponseType.x15)
|
|
|
|
|
{
|
|
|
|
|
this.X15(sn, data);
|
|
|
|
|
}
|
|
|
|
|
else if (responseType == MessageType.x01)
|
|
|
|
|
else if (responseType == ResponseType.x01)
|
|
|
|
|
{
|
|
|
|
|
this.X01(sn, data);
|
|
|
|
|
}
|
|
|
|
|
//using (var ms = new MemoryStream(data))
|
|
|
|
|
//{
|
|
|
|
|
// var dataLength = ms.ReadByte();
|
|
|
|
|
// var address = ms.ReadHexString(2);
|
|
|
|
|
// var endpoint = ms.ReadByte();
|
|
|
|
|
// using (var scope = _applicationServices.CreateScope())
|
|
|
|
|
// {
|
|
|
|
|
// var deviceRepo = scope.ServiceProvider.GetService<IRepository<FBeeDevice>>();
|
|
|
|
|
// var device = deviceRepo.Table().FirstOrDefault(o => o.Address == address);
|
|
|
|
|
// if (device == null)
|
|
|
|
|
// {
|
|
|
|
|
// Console.WriteLine($"device {address} hasn't save in database");
|
|
|
|
|
// }
|
|
|
|
|
// else if (responseType == MessageType.x70 || responseType == MessageType.x72)//设备上报
|
|
|
|
|
// {
|
|
|
|
|
// var clusterId = ms.ReadInt();
|
|
|
|
|
// if (device != null)
|
|
|
|
|
// {
|
|
|
|
|
// var reportCount = ms.ReadByte();
|
|
|
|
|
// var props = new Dictionary<int, byte[]>();
|
|
|
|
|
// for (int i = 0; i < reportCount; i++)
|
|
|
|
|
// {
|
|
|
|
|
// var propId = ms.ReadInt();
|
|
|
|
|
// var propDataTypeValue = ms.ReadByte();
|
|
|
|
|
// int propDataLength;
|
|
|
|
|
// if (Enum.IsDefined(typeof(DataType), propDataTypeValue))
|
|
|
|
|
// {
|
|
|
|
|
// var propDataType = (DataType)propDataTypeValue;
|
|
|
|
|
// if (propDataType == DataType.bitstring || propDataType == DataType.characterstring)
|
|
|
|
|
// {
|
|
|
|
|
// propDataLength = ms.ReadByte();
|
|
|
|
|
// }
|
|
|
|
|
// else if (propDataType == DataType.longbitstring || propDataType == DataType.longcharacterstring)
|
|
|
|
|
// {
|
|
|
|
|
// propDataLength = ms.ReadInt();
|
|
|
|
|
// }
|
|
|
|
|
// else if (propDataType == DataType.sequence || propDataType == DataType.set || propDataType == DataType.bag)
|
|
|
|
|
// {
|
|
|
|
|
// propDataLength = ms.ReadInt();
|
|
|
|
|
// }
|
|
|
|
|
// else if (propDataType == DataType.unknown)
|
|
|
|
|
// {
|
|
|
|
|
// propDataLength = 0;
|
|
|
|
|
// }
|
|
|
|
|
// else
|
|
|
|
|
// {
|
|
|
|
|
// propDataLength = Convert.ToInt32(Regex.Match(propDataType.GetName(), @"\d+").Groups[1].Value);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// else
|
|
|
|
|
// {
|
|
|
|
|
// propDataLength = 1;
|
|
|
|
|
// }
|
|
|
|
|
// var propData = ms.Read(propDataLength);
|
|
|
|
|
// props.Add(propId, propData);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// else if (responseType == MessageType.x07)
|
|
|
|
|
// {
|
|
|
|
|
// var powerStatus = ms.ReadByte();
|
|
|
|
|
// if (device != null)
|
|
|
|
|
// {
|
|
|
|
|
// device.Power = powerStatus;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// deviceRepo.SaveChanges();
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
else if (responseType == ResponseType.x07)
|
|
|
|
|
{
|
|
|
|
|
this.X07(sn, data);
|
|
|
|
|
}
|
|
|
|
|
else if (responseType == ResponseType.x70)
|
|
|
|
|
{
|
|
|
|
|
this.X70(sn, data);
|
|
|
|
|
}
|
|
|
|
|
else if (responseType == ResponseType.x72)
|
|
|
|
|
{
|
|
|
|
|
//this.X70(sn, data);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"{responseType} hasn't handle");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private byte[] Command(byte[] data)
|
|
|
|
@ -582,15 +619,36 @@ namespace FBeeService
|
|
|
|
|
return command.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Write(string sn, byte[] command)
|
|
|
|
|
private void Write(string sn, byte commandType, List<byte> command)
|
|
|
|
|
{
|
|
|
|
|
this.Clients.TryGetValue(sn, out TcpClientWrapper client);
|
|
|
|
|
if (!client.Client.Connected)
|
|
|
|
|
{
|
|
|
|
|
this.Connect(client);
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine($"write:{BitConverter.ToString(command).Replace("-", " ")}");
|
|
|
|
|
client.Client.GetStream().Write(command);
|
|
|
|
|
if (command == null)
|
|
|
|
|
{
|
|
|
|
|
command = new List<byte>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var data = new List<byte>();
|
|
|
|
|
data.AddRange(sn.HexToBytes().Reverse());
|
|
|
|
|
data.Add(RequestType.xfe);
|
|
|
|
|
data.Add(commandType);
|
|
|
|
|
data.AddRange(command);
|
|
|
|
|
|
|
|
|
|
var head = BitConverter.GetBytes(data.Count() + 2).ToList();
|
|
|
|
|
if (BitConverter.IsLittleEndian)
|
|
|
|
|
{
|
|
|
|
|
head.Reverse();
|
|
|
|
|
}
|
|
|
|
|
head = head.Skip(2).Reverse().ToList();
|
|
|
|
|
var list = new List<byte>();
|
|
|
|
|
list.AddRange(head);
|
|
|
|
|
list.AddRange(data);
|
|
|
|
|
var bytes = list.ToArray();
|
|
|
|
|
Console.WriteLine($"write:{BitConverter.ToString(bytes).Replace("-", " ")}");
|
|
|
|
|
client.Client.GetStream().Write(bytes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private NotifyModel CreateModel(string name, string number, string path, string icon)
|
|
|
|
|