|
|
|
@ -86,6 +86,10 @@ namespace FBeeService
|
|
|
|
|
|
|
|
|
|
#region api
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 0x81获取当前连接所有设备
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sn"></param>
|
|
|
|
|
public void X81(string sn)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"refresh {sn}");
|
|
|
|
@ -97,6 +101,62 @@ namespace FBeeService
|
|
|
|
|
this.Write(sn, list.ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void X01(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 profileId = ms.ReadInt();
|
|
|
|
|
var deviceId = ms.ReadInt();
|
|
|
|
|
var deviceRepo = scope.ServiceProvider.GetService<IRepository<FBeeDevice>>();
|
|
|
|
|
var device = deviceRepo.Table().FirstOrDefault(o => o.Sn == sn && o.Address == address);
|
|
|
|
|
if (device == null)
|
|
|
|
|
{
|
|
|
|
|
var deviceType = DeviceId.List.FirstOrDefault(o => o.RawDeviceId == deviceId);
|
|
|
|
|
device = new FBeeDevice
|
|
|
|
|
{
|
|
|
|
|
Sn = sn,
|
|
|
|
|
Name = deviceType.Name,
|
|
|
|
|
EName = deviceType.EName,
|
|
|
|
|
Icon = deviceType.Icon,
|
|
|
|
|
CategoryNumber = deviceType.RawDeviceId,
|
|
|
|
|
CategoryName = deviceType.Category
|
|
|
|
|
};
|
|
|
|
|
deviceRepo.Add(device);
|
|
|
|
|
}
|
|
|
|
|
device.DataType = responseType;
|
|
|
|
|
device.DataLength = dataLength;
|
|
|
|
|
device.Address = address;
|
|
|
|
|
device.Endpoint = endpoint;
|
|
|
|
|
device.ProfileId = profileId;
|
|
|
|
|
device.DeviceId = deviceId;
|
|
|
|
|
device.Status = ms.ReadByte();
|
|
|
|
|
device.NameLength = ms.ReadByte();
|
|
|
|
|
device.CName = ms.ReadASIIString(device.NameLength);
|
|
|
|
|
device.Online = ms.ReadByte();
|
|
|
|
|
device.IEEE = ms.ReadHexString(8);
|
|
|
|
|
device.SNLength = ms.ReadByte();
|
|
|
|
|
device.RawSN = ms.ReadHexString(device.SNLength);
|
|
|
|
|
device.ZoneType = ms.ReadByte();
|
|
|
|
|
device.Power = ms.ReadByte();
|
|
|
|
|
device.Data = ms.ReadHexString(4);
|
|
|
|
|
device.Safe = ms.ReadHexString(2);
|
|
|
|
|
device.RawValue = BitConverter.ToString(data);
|
|
|
|
|
deviceRepo.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 0x95删除指定设备
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sn"></param>
|
|
|
|
|
/// <param name="ieee"></param>
|
|
|
|
|
public void X95(string sn, string ieee)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"delete {ieee} from {sn}");
|
|
|
|
@ -119,6 +179,13 @@ namespace FBeeService
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//0x94更改指定设备名
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 0x82设置指定设备的开关状态
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sn"></param>
|
|
|
|
|
/// <param name="ieee"></param>
|
|
|
|
|
/// <param name="status"></param>
|
|
|
|
|
public void X82(string sn, string ieee, byte status)
|
|
|
|
|
{
|
|
|
|
|
using (var scope = _applicationServices.CreateScope())
|
|
|
|
@ -139,12 +206,104 @@ namespace FBeeService
|
|
|
|
|
list.Add(0x01);
|
|
|
|
|
list.AddRange(new byte[] { 0x00, 0x00 });
|
|
|
|
|
list.Add(status);
|
|
|
|
|
var client = this.Clients[sn];
|
|
|
|
|
this.Write(sn, list.ToArray());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//0x83
|
|
|
|
|
//0x84
|
|
|
|
|
//0x85
|
|
|
|
|
//0x86
|
|
|
|
|
//0x87
|
|
|
|
|
//0x88
|
|
|
|
|
//0x8d
|
|
|
|
|
//0x96
|
|
|
|
|
//0x8e
|
|
|
|
|
//0x8f
|
|
|
|
|
//0x97
|
|
|
|
|
//0x98
|
|
|
|
|
//0x90
|
|
|
|
|
//0x91
|
|
|
|
|
//0x92
|
|
|
|
|
//0x93
|
|
|
|
|
//0x8a
|
|
|
|
|
//0x8b
|
|
|
|
|
//0x8c
|
|
|
|
|
//0x99
|
|
|
|
|
//0x9a
|
|
|
|
|
//0x9b
|
|
|
|
|
//0x9c
|
|
|
|
|
//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());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void X15(string sn, byte[] data)
|
|
|
|
|
{
|
|
|
|
|
using (var ms = new MemoryStream(data))
|
|
|
|
|
{
|
|
|
|
|
var responseType = ms.ReadByte();
|
|
|
|
|
var dataLength = ms.ReadByte();
|
|
|
|
|
var version = ms.ReadASIIString(5);
|
|
|
|
|
var snid = ms.ReadHexString(4);
|
|
|
|
|
using (var scope = _applicationServices.CreateScope())
|
|
|
|
|
{
|
|
|
|
|
var gatewayRepo = scope.ServiceProvider.GetService<IRepository<Gateway>>();
|
|
|
|
|
var gateway = gatewayRepo.Table().FirstOrDefault(o => o.Sn == sn);
|
|
|
|
|
if (gateway == null)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"gateway {sn} hasn't save in database");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
gateway.Version = version;
|
|
|
|
|
gateway.UserName = ms.ReadASIIString(20);
|
|
|
|
|
gateway.Password = ms.ReadASIIString(20);
|
|
|
|
|
gateway.DeviceCount = ms.ReadByte();
|
|
|
|
|
gateway.GroupCount = ms.ReadByte();
|
|
|
|
|
gateway.TimerCount = ms.ReadByte();
|
|
|
|
|
gateway.SceneCount = ms.ReadByte();
|
|
|
|
|
gateway.TaskCount = ms.ReadByte();
|
|
|
|
|
var hex = ms.ReadHexString(5);
|
|
|
|
|
gateway.CompileVersion = ms.ReadHexString(5);
|
|
|
|
|
}
|
|
|
|
|
gatewayRepo.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//0x9e
|
|
|
|
|
//0x9f
|
|
|
|
|
//0xa0
|
|
|
|
|
//0xa1
|
|
|
|
|
//0xa2
|
|
|
|
|
//0xa3
|
|
|
|
|
//0xa4
|
|
|
|
|
//0xa5
|
|
|
|
|
//0xa8
|
|
|
|
|
//0xa9
|
|
|
|
|
//0xab
|
|
|
|
|
//0xa7
|
|
|
|
|
//0xac
|
|
|
|
|
//0xaf
|
|
|
|
|
//0xb0
|
|
|
|
|
//0x70
|
|
|
|
|
//0x72
|
|
|
|
|
//0xb1
|
|
|
|
|
//0xb2
|
|
|
|
|
//0xc1
|
|
|
|
|
//0xc2
|
|
|
|
|
//0xc3
|
|
|
|
|
//0xc4
|
|
|
|
|
//0xc5
|
|
|
|
|
//0x75
|
|
|
|
|
|
|
|
|
|
#endregion api
|
|
|
|
|
|
|
|
|
|
private void CheckConnection()
|
|
|
|
@ -292,6 +451,7 @@ namespace FBeeService
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}).Start();
|
|
|
|
|
this.X9d(client.Sn);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
@ -315,103 +475,84 @@ namespace FBeeService
|
|
|
|
|
private void HandleInternal(string sn, byte[] data)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"read:{BitConverter.ToString(data).Replace("-", " ")}");
|
|
|
|
|
using (var ms = new MemoryStream(data))
|
|
|
|
|
var responseType = data[0];
|
|
|
|
|
if (responseType == MessageType.x15)
|
|
|
|
|
{
|
|
|
|
|
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.Address == address);
|
|
|
|
|
if (device == null)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"{address} hasn't save in database");
|
|
|
|
|
}
|
|
|
|
|
if (responseType == MessageType.DeviceResponse)//获取设备返回值
|
|
|
|
|
{
|
|
|
|
|
var profileId = ms.ReadInt();
|
|
|
|
|
var deviceId = ms.ReadInt();
|
|
|
|
|
var deviceType = DeviceId.List.FirstOrDefault(o => o.RawDeviceId == deviceId);
|
|
|
|
|
if (deviceType == null)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"{deviceId} hasn't config in database");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (device == null)
|
|
|
|
|
{
|
|
|
|
|
device = new FBeeDevice
|
|
|
|
|
{
|
|
|
|
|
Sn = sn,
|
|
|
|
|
Name = deviceType.Name,
|
|
|
|
|
EName = deviceType.EName,
|
|
|
|
|
Icon = deviceType.Icon,
|
|
|
|
|
CategoryNumber = deviceType.RawDeviceId,
|
|
|
|
|
CategoryName = deviceType.Category
|
|
|
|
|
};
|
|
|
|
|
deviceRepo.Add(device);
|
|
|
|
|
}
|
|
|
|
|
device.Update(data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (responseType == MessageType.DeviceNotify || responseType == MessageType.DeviceNotiryExt)//设备上报
|
|
|
|
|
{
|
|
|
|
|
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.DevicePowerResponse)
|
|
|
|
|
{
|
|
|
|
|
var powerStatus = ms.ReadByte();
|
|
|
|
|
if (device != null)
|
|
|
|
|
{
|
|
|
|
|
device.Power = powerStatus;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
deviceRepo.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
this.X15(sn, data);
|
|
|
|
|
}
|
|
|
|
|
else if (responseType == MessageType.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();
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private byte[] Command(byte[] data)
|
|
|
|
|