Former-commit-id: 704be4f60256d8c940b6e6235caac3bbb3372e4b
TangShanKaiPing
wanggang 6 years ago
parent 2ee6f70caf
commit 9700e86f6b

@ -33,5 +33,8 @@ namespace Application.Domain.Entities
[Display(Name = "设备")] [Display(Name = "设备")]
public Device Device { get; set; } public Device Device { get; set; }
[Display(Name = "隐藏")]
public bool Hidden { get; set; }
} }
} }

@ -2,6 +2,7 @@ using Infrastructure.Domain;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace Application.Domain.Entities namespace Application.Domain.Entities
{ {
@ -58,5 +59,26 @@ namespace Application.Domain.Entities
[Display(Name = "数据")] [Display(Name = "数据")]
public List<Data> Data { get; set; } = new List<Data>(); public List<Data> Data { get; set; } = new List<Data>();
public Data GetData(string key)
{
return this.Data.FirstOrDefault(o => o.Key == key);
}
public string GetDataValue(string key)
{
return this.GetData(key)?.Value;
}
public void AddorUpdateData(string key, object value, DeviceDataType type, string name, string unit = null, string description = null, bool hidden = false)
{
var data = this.Data.FirstOrDefault(o => o.Key == key);
if (data == null)
{
data = new Data { Key = key, Type = type, Name = name, Unit = unit, Description = description, Hidden = hidden };
this.Data.Add(data);
}
data.Value = Convert.ToString(value);
}
} }
} }

@ -313,10 +313,10 @@ namespace FBeeService
var payload = new List<byte>(); var payload = new List<byte>();
using (var scope = _applicationServices.CreateScope()) using (var scope = _applicationServices.CreateScope())
{ {
var repo = scope.ServiceProvider.GetService<IRepository<Device>>(); var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>();
var device = repo.ReadOnlyTable().Include(o => o.Data).FirstOrDefault(o => o.GatewayNumber == sn && o.Number == ieee); var device = this.GetDevice(deviceRepo, sn, ieee);
var address = device.Data.FirstOrDefault(o => o.Key == "Address").Value.HexToBytes().Reverse(); var address = device.GetDataValue(Keys.Address).HexToBytes().Reverse();
var endPoint = Convert.ToInt32(device.Data.FirstOrDefault(o => o.Key == "EndPoint").Value); var endPoint = Convert.ToInt32(device.GetDataValue(Keys.EndPoint));
if (format == 0) if (format == 0)
{ {
payload.Add((byte)(command.Count() + 1 + 2));//数据总长 payload.Add((byte)(command.Count() + 1 + 2));//数据总长
@ -357,21 +357,6 @@ namespace FBeeService
this.Send(client, bytes); this.Send(client, bytes);
} }
private void AddLengthToData(string sn, byte[] data)
{
this.Clients.TryGetValue(sn, out TcpClientWrapper client);
var length = BitConverter.GetBytes((ushort)(data.Count() + 2));
if (BitConverter.IsLittleEndian)
{
length.Reverse();
}
var list = new List<byte>();
list.AddRange(length);
list.AddRange(data);
var bytes = list.ToArray();
this.Send(client, bytes);
}
private void Send(TcpClientWrapper client, byte[] bytes) private void Send(TcpClientWrapper client, byte[] bytes)
{ {
Console.WriteLine($"write:{BitConverter.ToString(bytes).Replace("-", " ")}"); Console.WriteLine($"write:{BitConverter.ToString(bytes).Replace("-", " ")}");
@ -454,7 +439,7 @@ namespace FBeeService
deviceCategoryRepo.SaveChanges(); deviceCategoryRepo.SaveChanges();
} }
var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>(); var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>();
var device = deviceRepo.Table().Include(o => o.Data).FirstOrDefault(o => o.GatewayNumber == sn && o.Number == ieee); var device = this.GetDevice(deviceRepo, sn, ieee);
if (device == null) if (device == null)
{ {
create = true; create = true;
@ -500,21 +485,20 @@ namespace FBeeService
device.Name = "3路开关"; device.Name = "3路开关";
} }
} }
this.AddorUpdateData(device, "DeviceId", deviceId, DeviceDataType.Int, "DeviceId"); device.AddorUpdateData(Keys.DeviceId, deviceId, DeviceDataType.Int, Keys.DeviceId, hidden: true);
this.AddorUpdateData(device, "Address", address, DeviceDataType.String, "地址"); device.AddorUpdateData(Keys.Address, address, DeviceDataType.String, Keys.Address, hidden: true);
this.AddorUpdateData(device, "EndPoint", endpoint, DeviceDataType.Int, "EndPoint"); device.AddorUpdateData(Keys.EndPoint, endpoint, DeviceDataType.Int, Keys.EndPoint, hidden: true);
if (new int[] { 0x0002, 0x0009, 0x0081, 0x0202, 0x0220, 0x0051 }.Contains(deviceId)) if (new int[] { 0x0002, 0x0009, 0x0081, 0x0202, 0x0220, 0x0051 }.Contains(deviceId))
{ {
this.AddorUpdateData(device, "State", switchState, DeviceDataType.Int, "状态"); device.AddorUpdateData(Keys.State, switchState, DeviceDataType.Int, "状态");
} }
var battery = ms.ReadByte(); var battery = ms.ReadByte();
this.AddorUpdateData(device, "Battery", battery, DeviceDataType.Int, "Battery"); device.AddorUpdateData(Keys.Battery, battery, DeviceDataType.Int, Keys.Battery);
var epCount = ms.ReadByte(); var epCount = ms.ReadByte();
this.AddorUpdateData(device, "EndPointCount", battery, DeviceDataType.Int, "EndPoint数量"); device.AddorUpdateData(Keys.EndPointCount, battery, DeviceDataType.Int, Keys.EndPointCount);
var data2 = ms.ReadHexString(4); var historyData = ms.ReadHexString(4);
this.AddorUpdateData(device, "Data", data2, DeviceDataType.String, "Data"); device.AddorUpdateData(Keys.Data, historyData, DeviceDataType.String, Keys.Data, hidden: true);
this.AddorUpdateData(device, "RawData", BitConverter.ToString(data), DeviceDataType.String, "RawData"); device.AddorUpdateData(Keys.ZoneType, zoneType, DeviceDataType.String, Keys.ZoneType, hidden: true);
device.Description = $"{ieee}/{address}/{deviceId}/{zoneType}";
deviceRepo.SaveChanges(); deviceRepo.SaveChanges();
if (create) if (create)
{ {
@ -536,7 +520,7 @@ namespace FBeeService
private void UpdateStatus(Device device) private void UpdateStatus(Device device)
{ {
var deviceId = Convert.ToInt32(device.Data.FirstOrDefault(o => o.Key == "DeviceId").Value); var deviceId = Convert.ToInt32(device.GetDataValue(Keys.DeviceId));
var sn = device.GatewayNumber; var sn = device.GatewayNumber;
var ieee = device.Number; var ieee = device.Number;
if (new int[] { 0x0002, 0x0009, 0x0081, 0x0202, 0x0220, 0x0051 }.Contains(deviceId)) if (new int[] { 0x0002, 0x0009, 0x0081, 0x0202, 0x0220, 0x0051 }.Contains(deviceId))
@ -606,10 +590,10 @@ namespace FBeeService
using (var scope = _applicationServices.CreateScope()) using (var scope = _applicationServices.CreateScope())
{ {
var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>(); var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>();
var device = deviceRepo.Table().Include(o => o.Data).FirstOrDefault(o => o.GatewayNumber == sn && o.Data.Any(d => d.Key == "Address" && d.Value == address)); var device = this.GetDeviceByAddress(deviceRepo, sn, address);
if (device != null) if (device != null)
{ {
this.AddorUpdateData(device, "State", ms.ReadInt(), DeviceDataType.Int, "状态"); device.AddorUpdateData("State", ms.ReadInt(), DeviceDataType.Int, "状态");
deviceRepo.SaveChanges(); deviceRepo.SaveChanges();
} }
else else
@ -652,10 +636,10 @@ namespace FBeeService
using (var scope = _applicationServices.CreateScope()) using (var scope = _applicationServices.CreateScope())
{ {
var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>(); var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>();
var device = deviceRepo.Table().Include(o => o.Data).FirstOrDefault(o => o.GatewayNumber == sn && o.Data.Any(d => d.Key == "Address" && d.Value == address)); var device = this.GetDeviceByAddress(deviceRepo, sn, address);
if (device != null) if (device != null)
{ {
this.AddorUpdateData(device, "Brightness", ms.ReadByte(), DeviceDataType.Int, "亮度"); device.AddorUpdateData(Keys.Brightness, ms.ReadByte(), DeviceDataType.Int, "亮度");
deviceRepo.SaveChanges(); deviceRepo.SaveChanges();
} }
else else
@ -696,10 +680,10 @@ namespace FBeeService
using (var scope = _applicationServices.CreateScope()) using (var scope = _applicationServices.CreateScope())
{ {
var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>(); var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>();
var device = deviceRepo.Table().Include(o => o.Data).FirstOrDefault(o => o.GatewayNumber == sn && o.Data.Any(d => d.Key == "Address" && d.Value == address)); var device = this.GetDeviceByAddress(deviceRepo, sn, address);
if (device != null) if (device != null)
{ {
this.AddorUpdateData(device, "Hue", ms.ReadByte(), DeviceDataType.Int, "色调"); device.AddorUpdateData(Keys.Hue, ms.ReadByte(), DeviceDataType.Int, "色调");
deviceRepo.SaveChanges(); deviceRepo.SaveChanges();
} }
else else
@ -726,10 +710,10 @@ namespace FBeeService
using (var scope = _applicationServices.CreateScope()) using (var scope = _applicationServices.CreateScope())
{ {
var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>(); var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>();
var device = deviceRepo.Table().Include(o => o.Data).FirstOrDefault(o => o.GatewayNumber == sn && o.Data.Any(d => d.Key == "Address" && d.Value == address)); var device = this.GetDeviceByAddress(deviceRepo, sn, address);
if (device != null) if (device != null)
{ {
this.AddorUpdateData(device, "Saturation", ms.ReadByte(), DeviceDataType.Int, "饱和度"); device.AddorUpdateData(Keys.Saturation, ms.ReadByte(), DeviceDataType.Int, "饱和度");
deviceRepo.SaveChanges(); deviceRepo.SaveChanges();
} }
else else
@ -835,10 +819,9 @@ namespace FBeeService
{ {
gateway.UserName = ms.ReadASIIString(20); gateway.UserName = ms.ReadASIIString(20);
gateway.Password = ms.ReadASIIString(20); gateway.Password = ms.ReadASIIString(20);
gateway.Description = version;
this.AddorUpdateData(gateway, "Version", version, DeviceDataType.String, "版本"); gateway.AddorUpdateData(Keys.Version, version, DeviceDataType.String, "版本");
this.AddorUpdateData(gateway, "DeviceCount", ms.ReadByte(), DeviceDataType.Int, "设备数量"); gateway.AddorUpdateData(Keys.DeviceCount, ms.ReadByte(), DeviceDataType.Int, "设备数量");
ms.ReadByte(); ms.ReadByte();
ms.ReadByte(); ms.ReadByte();
ms.ReadByte(); ms.ReadByte();
@ -851,12 +834,12 @@ namespace FBeeService
} }
} }
private void AddorUpdateData(Device gateway, string key, object value, DeviceDataType type, string name, string unit = null) private void AddorUpdateData(Device gateway, string key, object value, DeviceDataType type, string name, string unit = null, bool hidden = false)
{ {
var data = gateway.Data.FirstOrDefault(o => o.Key == key); var data = gateway.Data.FirstOrDefault(o => o.Key == key);
if (data == null) if (data == null)
{ {
data = new Data { Key = key, Type = type, Name = name }; data = new Data { Key = key, Type = type, Name = name, Hidden = hidden };
gateway.Data.Add(data); gateway.Data.Add(data);
} }
data.Value = Convert.ToString(value); data.Value = Convert.ToString(value);
@ -901,10 +884,10 @@ namespace FBeeService
using (var scope = _applicationServices.CreateScope()) using (var scope = _applicationServices.CreateScope())
{ {
var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>(); var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>();
var device = deviceRepo.Table().Include(o => o.Data).FirstOrDefault(o => o.GatewayNumber == sn && o.Data.Any(d => d.Key == "Address" && d.Value == address)); var device = this.GetDeviceByAddress(deviceRepo, sn, address);
if (device != null) if (device != null)
{ {
this.AddorUpdateData(device, "ColorTemperature", ms.ReadInt(), DeviceDataType.Int, "色温"); deviceRepo.SaveChanges(); device.AddorUpdateData(Keys.ColorTemperature, ms.ReadInt(), DeviceDataType.Int, "色温"); deviceRepo.SaveChanges();
} }
else else
{ {
@ -973,7 +956,7 @@ namespace FBeeService
{ {
var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>(); var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>();
var device = deviceRepo.Table().Include(o => o.Data).FirstOrDefault(o => o.GatewayNumber == sn && o.Number == ieee); var device = deviceRepo.Table().Include(o => o.Data).FirstOrDefault(o => o.GatewayNumber == sn && o.Number == ieee);
var version = device.Data.FirstOrDefault(o => o.Key == "Version").Value; var version = device.GetDataValue(Keys.Version);
list.AddRange(version.HexToBytes()); list.AddRange(version.HexToBytes());
} }
list.AddRange(new byte[] { 0x82, 0x00 }); list.AddRange(new byte[] { 0x82, 0x00 });
@ -1012,7 +995,7 @@ namespace FBeeService
{ {
var repo = scope.ServiceProvider.GetService<IRepository<Device>>(); var repo = scope.ServiceProvider.GetService<IRepository<Device>>();
var device = repo.ReadOnlyTable().Include(o => o.Data).FirstOrDefault(o => o.GatewayNumber == sn && o.Number == ieee); var device = repo.ReadOnlyTable().Include(o => o.Data).FirstOrDefault(o => o.GatewayNumber == sn && o.Number == ieee);
var version = device.Data.FirstOrDefault(o => o.Key == "Version").Value; var version = device.GetDataValue(Keys.Version);
list.AddRange(version.HexToBytes()); list.AddRange(version.HexToBytes());
} }
list.AddRange(new byte[] { 0x83, 0x00 }); list.AddRange(new byte[] { 0x83, 0x00 });
@ -1076,8 +1059,8 @@ namespace FBeeService
using (var scope = _applicationServices.CreateScope()) using (var scope = _applicationServices.CreateScope())
{ {
var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>(); var deviceRepo = scope.ServiceProvider.GetService<IRepository<Device>>();
var device = deviceRepo.Table().Include(o => o.Data).FirstOrDefault(o => o.GatewayNumber == sn && o.Data.Any(d => d.Key == "Address" && d.Value == address)); var device = this.GetDeviceByAddress(deviceRepo, sn, address);
var deviceId = Convert.ToInt32(device.Data.FirstOrDefault(o => o.Key == "DeviceId").Value); var deviceId = Convert.ToInt32(device.GetDataValue(Keys.DeviceId));
var clusterId = (ClusterId)ms.ReadInt(); var clusterId = (ClusterId)ms.ReadInt();
if (device != null) if (device != null)
{ {
@ -1141,30 +1124,30 @@ namespace FBeeService
} }
else if (clusterId == ClusterId.light) else if (clusterId == ClusterId.light)
{ {
this.AddorUpdateData(device, "Light", BitConverter.ToInt16(props[0x0000]), DeviceDataType.Int, "光照"); device.AddorUpdateData(Keys.Light, BitConverter.ToInt16(props[0x0000]), DeviceDataType.Int, "光照");
} }
else if (clusterId == ClusterId.alarm) else if (clusterId == ClusterId.alarm)
{ {
this.AddorUpdateData(device, "Warning", BitConverter.ToInt16(props[0x0080]), DeviceDataType.Int, "警报"); device.AddorUpdateData(Keys.Warning, BitConverter.ToInt16(props[0x0080]), DeviceDataType.Int, "警报");
this.AddorUpdateData(device, "UnderVoltage", new BitArray(props[0x0080])[3], DeviceDataType.Int, "低电量"); device.AddorUpdateData(Keys.UnderVoltage, new BitArray(props[0x0080])[3], DeviceDataType.Int, "低电量");
} }
else if (clusterId == ClusterId.pm25) else if (clusterId == ClusterId.pm25)
{ {
this.AddorUpdateData(device, "PM25", BitConverter.ToInt16(props[0x0000]), DeviceDataType.Int, "PM2.5"); device.AddorUpdateData(Keys.PM25, BitConverter.ToInt16(props[0x0000]), DeviceDataType.Int, "PM2.5");
this.AddorUpdateData(device, "PM100", BitConverter.ToInt16(props[0x0002]), DeviceDataType.Int, "PM10"); device.AddorUpdateData(Keys.PM100, BitConverter.ToInt16(props[0x0002]), DeviceDataType.Int, "PM10");
this.AddorUpdateData(device, "PM10", BitConverter.ToInt16(props[0x0001]), DeviceDataType.Int, "PM1"); device.AddorUpdateData(Keys.PM10, BitConverter.ToInt16(props[0x0001]), DeviceDataType.Int, "PM1");
} }
else if (clusterId == ClusterId.temperature) else if (clusterId == ClusterId.temperature)
{ {
this.AddorUpdateData(device, "Temperature", BitConverter.ToInt16(props[0x0000]) / 100f, DeviceDataType.Float, "温度"); device.AddorUpdateData(Keys.Temperature, BitConverter.ToInt16(props[0x0000]) / 100f, DeviceDataType.Float, "温度", "℃");
} }
else if (clusterId == ClusterId.humidity) else if (clusterId == ClusterId.humidity)
{ {
this.AddorUpdateData(device, "Humidity", BitConverter.ToInt16(props[0x0000]) / 100f, DeviceDataType.Float, "湿度"); device.AddorUpdateData(Keys.Humidity, BitConverter.ToInt16(props[0x0000]) / 100f, DeviceDataType.Float, "湿度", "RH%");
} }
else if (clusterId == ClusterId.voltage) else if (clusterId == ClusterId.voltage)
{ {
this.AddorUpdateData(device, "Voltage", props[0x21][0] / 2f, DeviceDataType.Float, "电量"); device.AddorUpdateData(Keys.Voltage, props[0x21][0] / 2f, DeviceDataType.Float, "电量");
} }
else if (clusterId == ClusterId.socket) else if (clusterId == ClusterId.socket)
{ {
@ -1175,7 +1158,7 @@ namespace FBeeService
tempBytes.Add(0x00); tempBytes.Add(0x00);
tempBytes.Add(0x00); tempBytes.Add(0x00);
var electricity = BitConverter.ToInt64(tempBytes.ToArray()) / 1000f; var electricity = BitConverter.ToInt64(tempBytes.ToArray()) / 1000f;
this.AddorUpdateData(device, "Electricity", electricity, DeviceDataType.Long, "电量", "kw/h"); device.AddorUpdateData(Keys.Electricity, electricity, DeviceDataType.Long, "电量", "kw/h");
} }
} }
if (deviceId == 0x0163) if (deviceId == 0x0163)
@ -1185,7 +1168,7 @@ namespace FBeeService
var irdata = props[0x400a]; var irdata = props[0x400a];
if (irdata.Length == 10) if (irdata.Length == 10)
{ {
this.AddorUpdateData(device, "Version", BitConverter.ToString((props[0x400a].Skip(3).Take(6).ToArray())).Replace("-", "").ToLower(), DeviceDataType.String, "红外版本"); device.AddorUpdateData(Keys.Version, BitConverter.ToString((props[0x400a].Skip(3).Take(6).ToArray())).Replace("-", "").ToLower(), DeviceDataType.String, "红外版本");
} }
else else
{ {
@ -1196,7 +1179,7 @@ namespace FBeeService
if (irDeviceType == 0x01) if (irDeviceType == 0x01)
{ {
var keyCode = BitConverter.ToInt16(irdata.Skip(12).Take(2).ToArray()); var keyCode = BitConverter.ToInt16(irdata.Skip(12).Take(2).ToArray());
this.AddorUpdateData(device, "KeyPress", keyCode, DeviceDataType.Int, "按键"); device.AddorUpdateData(Keys.KeyPress, keyCode, DeviceDataType.Int, "按键");
} }
} }
} }
@ -1257,5 +1240,19 @@ namespace FBeeService
} }
#endregion notify server #endregion notify server
private Device GetDevice(IRepository<Device> deviceRepo, string sn, string ieee)
{
return deviceRepo.Table()
.Include(o => o.Data)
.FirstOrDefault(o => o.GatewayNumber == sn && o.Number == ieee);
}
private Device GetDeviceByAddress(IRepository<Device> deviceRepo, string sn, string address)
{
return deviceRepo.Table()
.Include(o => o.Data)
.FirstOrDefault(o => o.GatewayNumber == sn && o.Data.Any(d => d.Key == Keys.Address && d.Value == address));
}
} }
} }

@ -0,0 +1,31 @@
namespace FBeeService
{
public static class Keys
{
public const string DeviceId = "DeviceId";
public const string Address = "Address";
public const string EndPoint = "EndPoint";
public const string Version = "Version";
public const string Electricity = "Electricity";
public const string KeyPress = "KeyPress";
public const string State = "State";
public const string Battery = "Battery";
public const string EndPointCount = "EndPointCount";
public const string Data = "Data";
public const string ZoneType = "ZoneType";
public const string Brightness = "Brightness";
public const string Hue = "Hue";
public const string Saturation = "Saturation";
public const string ColorTemperature = "ColorTemperature";
public const string Light = "Light";
public const string Warning = "Warning";
public const string UnderVoltage = "UnderVoltage";
public const string PM25 = "PM25";
public const string PM100 = "PM100";
public const string PM10 = "PM10";
public const string Temperature = "Temperature";
public const string Humidity = "Humidity";
public const string Voltage = "Voltage";
public const string DeviceCount = "DeviceCount";
}
}
Loading…
Cancel
Save