diff --git a/LanSheng/LanShengAPI/appsettings.json b/LanSheng/LanShengAPI/appsettings.json index 5983db0..1e360d4 100644 --- a/LanSheng/LanShengAPI/appsettings.json +++ b/LanSheng/LanShengAPI/appsettings.json @@ -14,5 +14,6 @@ "ThreadPool": 8, "Port": 9001 }, - "Redis": "redis:6379" + "Redis": "redis:6379", + "urls": "http://localhost:5000" } diff --git a/LanSheng/LanShengModel/DeviceData.cs b/LanSheng/LanShengModel/DeviceData.cs index 63e52d0..485ce08 100644 --- a/LanSheng/LanShengModel/DeviceData.cs +++ b/LanSheng/LanShengModel/DeviceData.cs @@ -190,6 +190,18 @@ namespace LanShengModel [SugarColumn(IsNullable = true)] public int? Data13 { get; set; } + /// + /// 进料门接触器 (0:断开 1:吸合) + /// + [SugarColumn(IsNullable = true)] + public int? Data13_B7 { get; set; } + + /// + /// 出料门接触器 (0:断开 1:吸合) + /// + [SugarColumn(IsNullable = true)] + public int? Data13_B6 { get; set; } + /// /// 一体机操作模式(1:智能模式2:笼顶操作3防坠测试) /// @@ -448,6 +460,12 @@ namespace LanShengModel [SugarColumn(Length = 50, IsNullable = true)] public string? ICCID { get; set; } + /// + /// 外召盒GPS编号 + /// + [SugarColumn(Length = 50, IsNullable = true)] + public string? WZGPS { get; set; } + /// /// 东经 /// @@ -1088,9 +1106,38 @@ namespace LanShengModel [SugarColumn(IsIgnore = true)] public int? Data8_B7_1104 { get; set; } + /// + /// 左笼配电箱急停按钮(0断开,1闭合) + /// + [SugarColumn(IsNullable = true)] + public int? Data2_B7_2103 { get; set; } + + /// + /// 右笼配电箱急停按钮(0断开,1闭合) + /// + [SugarColumn(IsNullable = true)] + public int? Data2_B6_2103 { get; set; } + #region 业务字段 [SugarColumn(IsIgnore = true)] public string? MsgType { get; set; } + + [SugarColumn(IsIgnore = true)] + public DeviceData? WZ { get; set; } #endregion + + public string[] GetNotNullColumns() + { + var Result = new List(); + var Type = typeof(DeviceData); + foreach (var Propety in Type.GetProperties()) + { + if(Propety.GetValue(this) != null) + { + Result.Add(Propety.Name); + } + } + return Result.ToArray(); + } } } diff --git a/LanSheng/LanShengService/DeviceService.cs b/LanSheng/LanShengService/DeviceService.cs index f95473e..d490e39 100644 --- a/LanSheng/LanShengService/DeviceService.cs +++ b/LanSheng/LanShengService/DeviceService.cs @@ -36,10 +36,6 @@ namespace LanShengService Db.CodeFirst.SplitTables().InitTables(typeof(DeviceError)); - Columns1001 = DeviceData1001.GetColumns(); - - Columns1103 = DeviceData1103.GetColumns(); - Logger.LogDebug("加载数据结构"); CacheService = services.GetRequiredService(); @@ -69,21 +65,18 @@ namespace LanShengService private readonly int ValidClearDataLogDo = 1000 * 60 * 60 * 24 * 3; - private readonly string[] Columns1001; - - private readonly string[] Columns1103; - public async Task InsertData(DeviceData deviceData) { Logger.LogDebug($"设备数据插入队列{JsonSerializer.Serialize(deviceData)}"); - await SetError(deviceData); + if (deviceData.MsgType!.StartsWith("1")) + { + await SetError(deviceData); - await SetRunCount(deviceData); + await SetRunCount(deviceData); + } await CacheService.Set($"DeviceData:{deviceData.Id}.{Guid.NewGuid().ToString()}", deviceData, 5); - - await CacheService.Set($"CacheData:{deviceData.Id}", deviceData, 5); } public async Task TimerInsertDataDo() @@ -128,7 +121,7 @@ namespace LanShengService await temp.AsInsertable.ExecuteCommandAsync(); await temp.AsUpdateable - .UpdateColumns(Columns1001) + .UpdateColumns(Values1001[0].GetNotNullColumns()) .ExecuteCommandAsync(); } @@ -140,20 +133,34 @@ namespace LanShengService await temp.AsInsertable.ExecuteCommandAsync(); - var TempType = typeof(DeviceData); + await temp.AsUpdateable + .UpdateColumns(Values1103[0].GetNotNullColumns()) + .ExecuteCommandAsync(); + } - var TempPropertys = TempType.GetProperties(); + var Values2001 = DeviceValues.Where(x => x != null && (x.MsgType == "2001" || x.MsgType == "8104")).ToList(); - var TempColumns1103 = new List(); + if (Values2001.Count > 0) + { + var temp = Db.Storageable(Values2001).ToStorage(); - foreach (var TempProperty in TempPropertys) { - if (TempProperty.GetValue(Values1103[0]) != null) { - TempColumns1103.Add(TempProperty.Name); - } - } + await temp.AsInsertable.ExecuteCommandAsync(); + + await temp.AsUpdateable + .UpdateColumns(Values2001[0].GetNotNullColumns()) + .ExecuteCommandAsync(); + } + + var Values2103 = DeviceValues.Where(x => x != null && x.MsgType == "2103").ToList(); + + if (Values2103.Count > 0) + { + var temp = Db.Storageable(Values2103).ToStorage(); + + await temp.AsInsertable.ExecuteCommandAsync(); await temp.AsUpdateable - .UpdateColumns(TempColumns1103.ToArray()) + .UpdateColumns(Values2103[0].GetNotNullColumns()) .ExecuteCommandAsync(); } } @@ -267,44 +274,17 @@ namespace LanShengService public async Task> GetDeviceData(string[] ids) { - var Result = new List(); - - var CacheIds = new List(); - ids.ToList().ForEach((x) => + var Result = await Db.Queryable().In("Id", ids) + .IgnoreColumns(x => new { - CacheIds.Add($"CacheData:{x}"); - }); - var CacheResult = await CacheService.BatchGet(CacheIds); - - Result.AddRange(CacheResult.Where(x => x.Value != null).Select(x => x.Value)); - - var NotCacheIds = CacheResult.Where(x => x.Value == null).Select(x => x.Key.Remove(0, 10)).ToList(); - - if (NotCacheIds.Count() > 0) - { - var DbResult = await Db.Queryable().In("Id", NotCacheIds) - .IgnoreColumns(x => new - { - x.Data25_B4, - x.Data25_B0, - x.Data26_B4, - x.Data26_B0 - }) - .ToListAsync(); - - Result.AddRange(DbResult.ToList()); - - var DbCache = new Dictionary(); - - foreach (var Item in DbResult) - { - DbCache.Add($"CacheData:{Item.Id}", Item); - } - - await CacheService.BatchSet(DbCache, 60); - } - Result = JsonSerializer.Deserialize>(JsonSerializer.Serialize(Result))!; - Result.ForEach(x => + x.Data25_B4, + x.Data25_B0, + x.Data26_B4, + x.Data26_B0 + }) + .ToListAsync(); + + Result.ForEach(async x => { x.Data6 = x.Data6 / 10; decimal Latitude = 0; @@ -313,6 +293,10 @@ namespace LanShengService decimal Longitude = 0; Decimal.TryParse(x.Longitude, out Longitude); x.Longitude = (Longitude / 1000000).ToString(); + + if (!string.IsNullOrEmpty(x.WZGPS)) { + x.WZ = await Db.Queryable().FirstAsync(y => y.Id == x.WZGPS); + } }); return Result; @@ -1298,7 +1282,8 @@ namespace LanShengService throw new BadRequestException("单元无效"); } #endregion - if ((report.CompanyIds != null && report.CompanyIds.Count() > 0) || (report.ProjectIds != null && report.ProjectIds.Count() > 0) || (report.DeviceIds != null && report.DeviceIds.Count() > 0)) { + if ((report.CompanyIds != null && report.CompanyIds.Count() > 0) || (report.ProjectIds != null && report.ProjectIds.Count() > 0) || (report.DeviceIds != null && report.DeviceIds.Count() > 0)) + { var TempCompanyIds = report.CompanyIds; var TempProjectIds = report.ProjectIds; var TempDeviceIds = report.DeviceIds; @@ -1313,17 +1298,19 @@ namespace LanShengService .ToList(); var TempGpsIds = new List(); - TempGpss.ForEach(v => { + TempGpss.ForEach(v => + { try { var tempGps = JsonSerializer.Deserialize>(v!); TempGpsIds.AddRange(tempGps!.Select(x => x.Id!)); } - catch { - + catch + { + } }); - report.GpsIds = report.GpsIds == null? TempGpsIds!: report.GpsIds.Concat(TempGpsIds!); + report.GpsIds = report.GpsIds == null ? TempGpsIds! : report.GpsIds.Concat(TempGpsIds!); } var TempErrors = await GetDayError(report); var TempErrorGroup = TempErrors.Select(x => x.ErrCode).Distinct(); @@ -1359,7 +1346,8 @@ namespace LanShengService TempItems.Add(item); } } - else if (report.Unit.ToLower() == "month") { + else if (report.Unit.ToLower() == "month") + { var TempErrorDate = TempErrors.Select(x => x.Id!.Substring(0, 7)).Distinct(); foreach (var group in TempErrorGroup) { @@ -1437,7 +1425,8 @@ namespace LanShengService return Task.FromResult(result); } - public async Task> GetErrorList(string search) { + public async Task> GetErrorList(string search) + { var _Query = Db.Queryable() .WhereIF(!string.IsNullOrWhiteSpace(search), x => x.ErrCode!.Contains(search) || x.ErrMsg!.Contains(search)) .Distinct() diff --git a/LanSheng/LanShengService/Tcp/ChannelHandler.cs b/LanSheng/LanShengService/Tcp/ChannelHandler.cs index 7226bcd..c6a3f47 100644 --- a/LanSheng/LanShengService/Tcp/ChannelHandler.cs +++ b/LanSheng/LanShengService/Tcp/ChannelHandler.cs @@ -43,15 +43,13 @@ namespace LanShengService.Tcp DTU_Id = packet!.DTU_ID!; Logger.LogDebug($"接收到数据:{JsonSerializer.Serialize>(packet!)}"); await TcpService.AddChannel(packet!.DTU_ID!, context); - await DeviceService.InsertDataLog(packet!); - - if (packet!.Content != null) - { - await DeviceService.InsertData(packet!.Content); - } - if (packet.Msg_ID == "1001" || packet.Msg_ID == "1103") { + if (packet!.Content != null) + { + await DeviceService.InsertData(packet!.Content); + } + await DeviceService.InsertDataLog(packet!); var Reply = new TcpDataLog { Frame_START = packet.Frame_START, @@ -63,7 +61,7 @@ namespace LanShengService.Tcp { Reply.Msg_ID = "9001"; } - if (packet.Msg_ID == "1103") + else if (packet.Msg_ID == "1103") { Reply.Msg_ID = "9103"; } @@ -76,11 +74,45 @@ namespace LanShengService.Tcp await context.CloseAsync(); } } + else if (packet.Msg_ID == "2001" || packet.Msg_ID == "2103") + { + if (packet!.Content != null) + { + await DeviceService.InsertData(packet!.Content); + } + var Reply = new TcpDataLog + { + Frame_START = packet.Frame_START, + Frame_number = packet.Frame_number, + DTU_ID = packet.DTU_ID, + Frame_END = packet.Frame_END + }; + if (packet.Msg_ID == "2001") + { + Reply.Msg_ID = "8001"; + } + else if (packet.Msg_ID == "2103") + { + Reply.Msg_ID = "8103"; + } + try + { + await context.WriteAndFlushAsync(Reply); + } + catch + { + await context.CloseAsync(); + } + } if (packet.Msg_ID == "9104") { await TcpService.RemoveMessageItem(packet); } - if (packet.Msg_ID == "9107") { + if (packet.Msg_ID == "9107") + { + await TcpService.RemoveMessageItem(packet); + } + if (packet.Msg_ID == "8104") { await TcpService.RemoveMessageItem(packet); } TryCount = 0; diff --git a/LanSheng/LanShengService/Tcp/DecoderHandler.cs b/LanSheng/LanShengService/Tcp/DecoderHandler.cs index 9236e7c..60e3dff 100644 --- a/LanSheng/LanShengService/Tcp/DecoderHandler.cs +++ b/LanSheng/LanShengService/Tcp/DecoderHandler.cs @@ -69,14 +69,14 @@ namespace LanShengService.Tcp { case "1001": case "9104": - request.Content.AgreementVersion = Content.ReadInt(1); - request.Content.DataTick = Content.ReadInt(2); - request.Content.ICCID = Content.ReadString(20, Encoding.ASCII); - Content.SkipBytes(4); - request.Content.Longitude = Content.ReadString(9, Encoding.ASCII); - request.Content.Latitude = Content.ReadString(8, Encoding.ASCII); - request.Content.ConnectStrLength = Content.ReadInt(1); - request.Content.ConnectString = Content.ReadString((int)request.Content.ConnectStrLength, Encoding.ASCII); + if (request.Content.Id.StartsWith("QC") || request.Content.Id.StartsWith("ZK")) + { + Decode1001ZK(request.Content, Content); + } + else + { + Decode1001RY(request.Content, Content); + } break; case "1103": if (request.Content.Id.StartsWith("QC") || request.Content.Id.StartsWith("ZK")) @@ -88,6 +88,12 @@ namespace LanShengService.Tcp Decode1103RY(request.Content, Content); } break; + case "2001": + Decode2001ZK(request.Content, Content); + break; + case "2103": + Decode2103ZK(request.Content, Content); + break; default: break; } @@ -187,7 +193,8 @@ namespace LanShengService.Tcp device.Data27_B0 = Data27[7]; } - private void Decode1103ZK(DeviceData device, IByteBuffer content) { + private void Decode1103ZK(DeviceData device, IByteBuffer content) + { device.Version = content.ReadInt(1); var Data2 = content.ReadBitArray(); device.Data2_B7 = Data2[0]; @@ -215,7 +222,9 @@ namespace LanShengService.Tcp device.Data10 = content.ReadInt(1); device.Data11 = content.ReadInt(1); device.Data12 = content.ReadInt(1); - content.SkipBytes(1); + var Data13 = content.ReadBitArray(); + device.Data13_B7 = Data13[0]; + device.Data13_B6 = Data13[1]; var Data14 = content.ReadBitArray(); device.Data14_B6 = Convert.ToInt32($"{Data14[1]}{Data14[0]}", 2); device.Data14_B5 = Data14[2]; @@ -354,5 +363,51 @@ namespace LanShengService.Tcp device.Data72_B5 = Data72[2]; device.Data72_B4 = Data72[3]; } + + private void Decode1001RY(DeviceData device, IByteBuffer content) + { + device.AgreementVersion = content.ReadInt(1); + device.DataTick = content.ReadInt(2); + device.ICCID = content.ReadString(20, Encoding.ASCII); + content.SkipBytes(4); + device.Longitude = content.ReadString(9, Encoding.ASCII); + device.Latitude = content.ReadString(8, Encoding.ASCII); + device.ConnectStrLength = content.ReadInt(1); + device.ConnectString = content.ReadString((int)device.ConnectStrLength, Encoding.ASCII); + } + + private void Decode1001ZK(DeviceData device, IByteBuffer content) + { + device.AgreementVersion = content.ReadInt(1); + device.DataTick = content.ReadInt(2); + device.ICCID = content.ReadString(20, Encoding.ASCII); + device.WZGPS = content.ReadString(17, Encoding.ASCII); + content.SkipBytes(4); + device.Longitude = content.ReadString(9, Encoding.ASCII); + device.Latitude = content.ReadString(8, Encoding.ASCII); + device.ConnectStrLength = content.ReadInt(1); + device.ConnectString = content.ReadString((int)device.ConnectStrLength, Encoding.ASCII); + } + + private void Decode2001ZK(DeviceData device,IByteBuffer content) + { + device.AgreementVersion = content.ReadInt(1); + device.DataTick = content.ReadInt(2); + device.ICCID = content.ReadString(20, Encoding.ASCII); + device.WZGPS = content.ReadString(17, Encoding.ASCII); + content.SkipBytes(4); + device.Longitude = content.ReadString(9, Encoding.ASCII); + device.Latitude = content.ReadString(8, Encoding.ASCII); + device.ConnectStrLength = content.ReadInt(1); + device.ConnectString = content.ReadString((int)device.ConnectStrLength, Encoding.ASCII); + } + + private void Decode2103ZK(DeviceData device,IByteBuffer content) + { + device.AgreementVersion = content.ReadInt(1); + var Data2 = content.ReadBitArray(); + device.Data2_B7_2103 = Data2[0]; + device.Data2_B6_2103 = Data2[1]; + } } } diff --git a/LanSheng/LanShengService/Tcp/EncoderHandler.cs b/LanSheng/LanShengService/Tcp/EncoderHandler.cs index 612406e..5090530 100644 --- a/LanSheng/LanShengService/Tcp/EncoderHandler.cs +++ b/LanSheng/LanShengService/Tcp/EncoderHandler.cs @@ -28,26 +28,16 @@ namespace LanShengService.Tcp { Encode1104ZK(Temp, message.Content!); } - else { - Encode1104RY(Temp,message.Content!); + else + { + Encode1104RY(Temp, message.Content!); } break; case "1107": - Temp.AddRange(((int)message.Content!.UVersion!).Int2Byte().BytePadLeft(2)); - var FtpUrlByte = Encoding.ASCII.GetBytes(message.Content.FtpUrl!); - message.Content.FtpLength = message.Content.FtpUrl!.Length; - Temp.AddRange(((int)message.Content.FtpLength!).Int2Byte().BytePadLeft(1)); - Temp.AddRange(FtpUrlByte); - var FtpUserByte = Encoding.ASCII.GetBytes(message.Content.UserPwd!); - message.Content.UserLength = message.Content.UserPwd!.Length; - Temp.AddRange(((int)message.Content.UserLength!).Int2Byte().BytePadLeft(1)); - Temp.AddRange(FtpUserByte); - var BinUrlByte = Encoding.ASCII.GetBytes(message.Content.BinUrl!); - message.Content.BinLength = message.Content.BinUrl!.Length; - Temp.AddRange(((int)message.Content.BinLength!).Int2Byte().BytePadLeft(1)); - Temp.AddRange(BinUrlByte); - Temp.AddRange(((int)message.Content!.BinSize!).Int2Byte().BytePadLeft(3)); - Temp.AddRange(((int)message.Content!.UpdateSpan!).Int2Byte().BytePadLeft(1)); + Encode1107ZK(Temp, message.Content!); + break; + case "2104": + Encode2104ZK(Temp, message.Content!); break; default: break; @@ -60,7 +50,8 @@ namespace LanShengService.Tcp output.WriteBytes(Temp.ToArray()); } - private void Encode1104RY(List bytes,DeviceData device) { + private void Encode1104RY(List bytes, DeviceData device) + { bytes.AddRange(((int)device.Version!).Int2Byte().BytePadLeft(1)); bytes.AddRange(((int)device.DataTick!).Int2Byte().BytePadLeft(2)); int[] Data1 = new int[] { @@ -242,5 +233,35 @@ namespace LanShengService.Tcp bytes.AddRange(((int)device.ConnectStrLength!).Int2Byte().BytePadLeft(1)); bytes.AddRange(ConnectByte); } + + private void Encode1107ZK(List bytes, DeviceData device) + { + bytes.AddRange(((int)device.UVersion!).Int2Byte().BytePadLeft(2)); + var FtpUrlByte = Encoding.ASCII.GetBytes(device.FtpUrl!); + device.FtpLength = device.FtpUrl!.Length; + bytes.AddRange(((int)device.FtpLength!).Int2Byte().BytePadLeft(1)); + bytes.AddRange(FtpUrlByte); + var FtpUserByte = Encoding.ASCII.GetBytes(device.UserPwd!); + device.UserLength = device.UserPwd!.Length; + bytes.AddRange(((int)device.UserLength!).Int2Byte().BytePadLeft(1)); + bytes.AddRange(FtpUserByte); + var BinUrlByte = Encoding.ASCII.GetBytes(device.BinUrl!); + device.BinLength = device.BinUrl!.Length; + bytes.AddRange(((int)device.BinLength!).Int2Byte().BytePadLeft(1)); + bytes.AddRange(BinUrlByte); + bytes.AddRange(((int)device.BinSize!).Int2Byte().BytePadLeft(3)); + bytes.AddRange(((int)device.UpdateSpan!).Int2Byte().BytePadLeft(1)); + } + + private void Encode2104ZK(List bytes, DeviceData device) + { + bytes.AddRange(((int)device.Version!).Int2Byte().BytePadLeft(1)); + bytes.AddRange(((int)device.DataTick!).Int2Byte().BytePadLeft(2)); + bytes.AddRange(0.Int2Byte().BytePadLeft(4)); + var ConnectByte = Encoding.ASCII.GetBytes(device.ConnectString!); + device.ConnectStrLength = device.ConnectString!.Length; + bytes.AddRange(((int)device.ConnectStrLength!).Int2Byte().BytePadLeft(1)); + bytes.AddRange(ConnectByte); + } } } diff --git a/LanSheng/LanShengService/TcpService.cs b/LanSheng/LanShengService/TcpService.cs index d19bd85..4aeaaae 100644 --- a/LanSheng/LanShengService/TcpService.cs +++ b/LanSheng/LanShengService/TcpService.cs @@ -209,7 +209,7 @@ namespace LanShengService Frame_START = "FAAA", Frame_number = Frame_number, DTU_ID = deviceData.Id, - Msg_ID = deviceData.UVersion == null ? "1104" : "1107", + Msg_ID = string.IsNullOrEmpty(deviceData.MsgType) ? "1104" : deviceData.MsgType, Frame_END = "BBFB", Content = deviceData }; @@ -246,16 +246,18 @@ namespace LanShengService { deviceData.UpdateTag = 0; } - if (deviceData.Data8_B7_1104 == null) { + if (deviceData.Data8_B7_1104 == null) + { deviceData.Data8_B7_1104 = 0; } - if(deviceData.BinSize == null) + if (deviceData.BinSize == null) { deviceData.BinSize = 0; } foreach (PropertyInfo propertyInfo in TempPropertys) { - if (propertyInfo.GetValue(deviceData) == null) { + if (propertyInfo.GetValue(deviceData) == null) + { propertyInfo.SetValue(deviceData, propertyInfo.GetValue(oldData)); } }