Former-commit-id: 401e1d985fd6f003736372f9f508ba8bf341071a
TangShanKaiPing
wanggang 6 years ago
parent 158c9780b6
commit 080fb1e9a8

@ -45,6 +45,13 @@ namespace Infrastructure.Extensions
return BitConverter.ToString(buffer).Replace("-", "").ToLower();
}
public static string ReadHexStringDesc(this Stream bytes, int length)
{
var buffer = new byte[length];
bytes.Read(buffer);
return BitConverter.ToString(buffer.Reverse().ToArray()).Replace("-", "").ToLower();
}
public static byte[] Read(this Stream bytes, int length)
{
var buffer = new byte[length];

@ -89,7 +89,7 @@ namespace FBeeService
public void X81(string sn)
{
Console.WriteLine($"refresh {sn}");
this.Write(sn, RequestType.x81, null);
this.Write(sn, RequestType.x81);
}
/// <summary>
@ -105,7 +105,7 @@ namespace FBeeService
{
var responseType = ms.ReadByte();
var dataLength = ms.ReadByte();
var address = ms.ReadHexString(2);
var address = ms.ReadHexStringDesc(2);
var endpoint = ms.ReadByte();
using (var scope = _applicationServices.CreateScope())
{
@ -184,20 +184,10 @@ namespace FBeeService
public void X95(string sn, string ieee)
{
Console.WriteLine($"delete {ieee} from {sn}");
this.Clients.TryGetValue(sn, out TcpClientWrapper client);
using (var scope = _applicationServices.CreateScope())
{
var repo = scope.ServiceProvider.GetService<IRepository<FBeeDevice>>();
var device = repo.ReadOnlyTable().FirstOrDefault(o => o.IsOnline != 0 && o.IEEE == ieee);
var address = device.Address;
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, RequestType.x95, list);
}
var list = new List<byte>();
list.AddRange(ieee.HexToBytes());
list.Add(0x01);
this.Write(sn, RequestType.x95, ieee, list, false);
}
//0x94更改指定设备名
@ -209,24 +199,9 @@ namespace FBeeService
/// <param name="status"></param>
public void X82(string sn, string ieee, byte status)
{
using (var scope = _applicationServices.CreateScope())
{
var repo = scope.ServiceProvider.GetService<IRepository<FBeeDevice>>();
var device = repo.ReadOnlyTable().FirstOrDefault(o => o.Sn == sn && o.IEEE == ieee);
if (device != null)
{
var address = device.Address;
var list = new List<byte>();
list.Add(0x0d);
list.Add(0x02);
list.AddRange(address.HexToBytes());
list.AddRange(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });
list.Add((byte)device.Endpoint);
list.AddRange(new byte[] { 0x00, 0x00 });
list.Add(status);
this.Write(sn, RequestType.x82, list);
}
}
var list = new List<byte>();
list.Add(status);
this.Write(sn, RequestType.x82, ieee, list);
}
/// <summary>
@ -240,7 +215,7 @@ namespace FBeeService
{
var responseType = ms.ReadByte();
var dataLength = ms.ReadByte();
var address = ms.ReadHexString(2);
var address = ms.ReadHexStringDesc(2);
var endpoint = ms.ReadByte();
using (var scope = _applicationServices.CreateScope())
{
@ -267,24 +242,10 @@ namespace FBeeService
/// <param name="value">0-255</param>
public void X83(string sn, string ieee, [Range(0, 255)]byte brightness)
{
using (var scope = _applicationServices.CreateScope())
{
var repo = scope.ServiceProvider.GetService<IRepository<FBeeDevice>>();
var device = repo.ReadOnlyTable().FirstOrDefault(o => o.Sn == sn && o.IEEE == ieee);
if (device != null)
{
var address = device.Address;
var list = new List<byte>();
list.Add(0x02);
list.AddRange(address.HexToBytes());
list.AddRange(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });
list.Add((byte)device.Endpoint);
list.AddRange(new byte[] { 0x00, 0x00 });
list.Add(brightness);
list.AddRange(new byte[] { 0x00, 0x00 });
this.Write(sn, RequestType.x83, list);
}
}
var list = new List<byte>();
list.Add(brightness);
list.AddRange(new byte[] { 0x00, 0x00 });
this.Write(sn, RequestType.x83, ieee, list);
}
/// <summary>
@ -292,25 +253,11 @@ namespace FBeeService
/// </summary>
public void X84(string sn, string ieee, [Range(0, 255)]byte hue, [Range(0, 255)]byte saturation)
{
using (var scope = _applicationServices.CreateScope())
{
var repo = scope.ServiceProvider.GetService<IRepository<FBeeDevice>>();
var device = repo.ReadOnlyTable().FirstOrDefault(o => o.Sn == sn && o.IEEE == ieee);
if (device != null)
{
var address = device.Address;
var list = new List<byte>();
list.Add(0x02);
list.AddRange(address.HexToBytes());
list.AddRange(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });
list.Add((byte)device.Endpoint);
list.AddRange(new byte[] { 0x00, 0x00 });
list.Add(hue);
list.Add(saturation);
list.AddRange(new byte[] { 0x00, 0x00 });
this.Write(sn, RequestType.x84, list);
}
}
var list = new List<byte>();
list.Add(hue);
list.Add(saturation);
list.AddRange(new byte[] { 0x00, 0x00 });
this.Write(sn, RequestType.x84, ieee, list);
}
//0x84
@ -339,7 +286,7 @@ namespace FBeeService
//0x9d获取网关信息
public void X9d(string sn)
{
this.Write(sn, RequestType.x9d, null);
this.Write(sn, RequestType.x9d);
}
/// <summary>
@ -394,25 +341,10 @@ namespace FBeeService
/// </summary>
public void XA8(string sn, string ieee, [Range(2700, 6500)]ushort color)
{
using (var scope = _applicationServices.CreateScope())
{
var repo = scope.ServiceProvider.GetService<IRepository<FBeeDevice>>();
var device = repo.ReadOnlyTable().FirstOrDefault(o => o.Sn == sn && o.IEEE == ieee);
if (device != null)
{
var address = device.Address;
var list = new List<byte>();
list.Add(0x10);
list.Add(0x02);
list.AddRange(address.HexToBytes());
list.AddRange(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });
list.Add((byte)device.Endpoint);
list.AddRange(new byte[] { 0x00, 0x00 });
list.AddRange(BitConverter.GetBytes(color));
list.AddRange(new byte[] { 0x00, 0x00 });
this.Write(sn, RequestType.xa8, list);
}
}
var list = new List<byte>();
list.AddRange(BitConverter.GetBytes(color));
list.AddRange(new byte[] { 0x00, 0x00 });
this.Write(sn, RequestType.xa8, ieee, list);
}
//0xa9
@ -454,7 +386,7 @@ namespace FBeeService
{
var responseType = ms.ReadByte();
var dataLength = ms.ReadByte();
var address = ms.ReadHexString(2);
var address = ms.ReadHexStringDesc(2);
var endpoint = ms.ReadByte();
using (var scope = _applicationServices.CreateScope())
{
@ -767,18 +699,28 @@ namespace FBeeService
}
}
private void Write(string sn, byte commandType, List<byte> command)
private void Write(string sn, byte commandType, string ieee = null, List<byte> command = null, bool needEndPoint = true, byte addressType = 0x02)
{
this.Clients.TryGetValue(sn, out TcpClientWrapper client);
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);
if (command != null && command.Count > 0)
{
data.Add((byte)(command.Count() + 13));
data.Add(addressType);
using (var scope = _applicationServices.CreateScope())
{
var repo = scope.ServiceProvider.GetService<IRepository<FBeeDevice>>();
var device = repo.ReadOnlyTable().FirstOrDefault(o => o.Sn == sn && o.IEEE == ieee);
data.AddRange(device.Address.HexToBytes().Reverse());
data.AddRange(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });
data.Add((byte)device.Endpoint);
data.AddRange(new byte[] { 0x00, 0x00 });
}
data.AddRange(command);
}
var head = BitConverter.GetBytes(data.Count() + 2).ToList();
if (BitConverter.IsLittleEndian)
@ -790,6 +732,11 @@ namespace FBeeService
list.AddRange(head);
list.AddRange(data);
var bytes = list.ToArray();
this.Send(client, bytes);
}
private void Send(TcpClientWrapper client, byte[] bytes)
{
Console.WriteLine($"write:{BitConverter.ToString(bytes).Replace("-", " ")}");
try
{

@ -68,7 +68,7 @@
}
</td>
<td>
@if (new int[] { 0x0002, 0x0009, 0x0081, 0x0202 }.Contains(item.DeviceId))
@if (new int[] { 0x0002, 0x0009, 0x0081, 0x0202, 0x0051 }.Contains(item.DeviceId))
{
<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>
@ -83,11 +83,11 @@
}
@if (item.DeviceId == 0x0220)
{
<form action="/colorlight/25setcolortemperature">
<input type="hidden" name="sn" value="@item.Sn" />
<input type="hidden" name="id" value="@item.IEEE" />
<label>色温:<input name="ColorTemperature" type="range" step="1" min="2700" max="6500" class="ajax" /></label>
</form>
<form action="/colorlight/25setcolortemperature">
<input type="hidden" name="sn" value="@item.Sn" />
<input type="hidden" name="id" value="@item.IEEE" />
<label>色温:<input name="ColorTemperature" type="range" step="1" min="2700" max="6500" class="ajax" /></label>
</form>
}
</td>
<td><a class="btn btn-primary cmd" href="/api/x95?sn=@item.Sn&ieee=@item.IEEE">删除设备</a></td>

Loading…
Cancel
Save