Former-commit-id: 5af4232e76428b8c09f7c8e14d06dab44424b4d8
TangShanKaiPing
wanggang 6 years ago
parent a713ba59b5
commit bd1289876a

@ -0,0 +1,11 @@
namespace Application.Domain.Entities
{
public class RequestType
{
public const int xfe = 0xfe;
public const int x81 = 0x81;
public const int x95 = 0x95;
public const int x9d = 0x9d;
public const int x82 = 0x82;
}
}

@ -1,6 +1,6 @@
namespace Application.Domain.Entities
{
public class MessageType
public class ResponseType
{
public const int x01 = 0x01;
public const int x07 = 0x07;

@ -67,6 +67,23 @@ namespace FBeeService.Controllers
return ApiResponse.AsyncSuccess();
}
[SwaggerOperation("查询网关信息")]
[HttpGet]
[Route("/api/x9d")]
public ApiResponse X9d(string sn)
{
try
{
this._deviceService.X9d(sn);
}
catch (Exception ex)
{
ex.PrintStack();
return ApiResponse.Error(ex.Message);
}
return ApiResponse.AsyncSuccess();
}
[SwaggerOperation("查询所有设备")]
[HttpGet]
[Route("/api/x81")]

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

@ -11,6 +11,7 @@
<th>名称</th>
<th>分类</th>
<th>分类编号</th>
<th>电源</th>
<th>开关</th>
<th>删除</th>
</tr>
@ -23,6 +24,7 @@
<td>@item.Name</td>
<td>@item.CategoryName</td>
<td>@item.CategoryNumber</td>
<td>@item.Power</td>
<td>
<a class="btn btn-primary cmd" href="/api/x82?sn=@item.Sn&ieee=@item.IEEE&status=1">开</a>
<a class="btn btn-primary cmd" href="/api/x82?sn=@item.Sn&ieee=@item.IEEE&status=0">关</a>

@ -4,7 +4,6 @@
}
<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">
@ -33,7 +32,7 @@
<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/x9d?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