Former-commit-id: 9faebbd0ea03bdc92a1f37c16c83c304c1eedead
TangShanKaiPing
wanggang 6 years ago
parent e81861d6ad
commit a713ba59b5

@ -24,6 +24,13 @@ namespace Infrastructure.Extensions
return BitConverter.ToInt16(buffer);
}
public static string ReadIntString(this Stream bytes, int length)
{
var buffer = new byte[length];
bytes.Read(buffer);
return string.Concat(buffer.Select(o => o.ToString()));
}
public static string ReadASIIString(this Stream bytes, int length)
{
var buffer = new byte[length];

@ -34,30 +34,5 @@ namespace Application.Domain.Entities
public int CategoryNumber { get; set; }
public string CategoryName { get; set; }
public string EName { get; set; }
public void Update(byte[] data)
{
using (var ms = new MemoryStream(data))
{
this.DataType = ms.ReadByte();
this.DataLength = ms.ReadByte();
this.Address = ms.ReadHexString(2);
this.Endpoint = ms.ReadByte();
this.ProfileId = ms.ReadInt();
this.DeviceId = ms.ReadInt();
this.Status = ms.ReadByte();
this.NameLength = ms.ReadByte();
this.CName = ms.ReadASIIString(this.NameLength);
this.Online = ms.ReadByte();
this.IEEE = ms.ReadHexString(8);
this.SNLength = ms.ReadByte();
this.RawSN = ms.ReadHexString(this.SNLength);
this.ZoneType = ms.ReadByte();
this.Power = ms.ReadByte();
this.Data = ms.ReadHexString(4);
this.Safe = ms.ReadHexString(2);
this.RawValue = BitConverter.ToString(data);
}
}
}
}

@ -6,7 +6,22 @@ namespace Application.Domain.Entities
{
public string Sn { get; set; }
public string Ip { get; set; }
//
public string Version { get; set; }
public string UserName { get; internal set; }
public string Password { get; internal set; }
public int DeviceCount { get; internal set; }
public int GroupCount { get; internal set; }
public int TimerCount { get; internal set; }
public int SceneCount { get; internal set; }
public int TaskCount { get; internal set; }
public string CompileVersion { get; internal set; }
//
public string NodeName { get; set; }
public bool Enable { get; set; }
}
}

@ -2,9 +2,10 @@
{
public class MessageType
{
public const int DeviceResponse = 0x01;
public const int DevicePowerResponse = 0x07;
public const int DeviceNotify = 0x70;
public const int DeviceNotiryExt = 0x72;
public const int x01 = 0x01;
public const int x07 = 0x07;
public const int x70 = 0x70;
public const int x72 = 0x72;
public const int x15 = 0x15;
}
}

@ -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)

@ -2,6 +2,10 @@
@{
ViewBag.HideBread = true;
}
<div class="box">
<a class="btn btn-primary cmd" href="/api/refresh">查询所有网关</a>
<a class="btn btn-primary cmd" href="/api/refresh">查询所有网关</a>
</div>
<div class="box">
<table class="table table-bordered">
<tr>
@ -9,7 +13,12 @@
<th>Ip地址</th>
<th>节点</th>
<th>推送</th>
<th><a class="btn btn-primary cmd" href="/api/refresh">查询所有网关</a></th>
<th>版本</th>
<th>用户名</th>
<th>密码</th>
<th>设备</th>
<th>编译版本</th>
<th>查询网关设备</th>
<th>查看</th>
</tr>
@foreach (var item in Model)
@ -19,6 +28,12 @@
<td>@item.Ip</td>
<td>@item.NodeName</td>
<td>@Html.DisplayFor(o => item.Enable)</td>
<td>@item.Version</td>
<td>@item.UserName</td>
<td>@item.Password</td>
<td>@item.DeviceCount</td>
<td>@item.CompileVersion</td>
<td><a class="btn btn-primary cmd" href="/api/x81?sn=@item.Sn">查询网关信息</a></td>
<td><a class="btn btn-primary cmd" href="/api/x81?sn=@item.Sn">查询网关设备</a></td>
<td><a href="/Home/Gateway?sn=@item.Sn">查看设备</a></td>
</tr>

Loading…
Cancel
Save