diff --git a/Waste.Application/ResultInfos/Dtos/ResultDto.cs b/Waste.Application/ResultInfos/Dtos/ResultDto.cs index bbe8089..a260cc3 100644 --- a/Waste.Application/ResultInfos/Dtos/ResultDto.cs +++ b/Waste.Application/ResultInfos/Dtos/ResultDto.cs @@ -8,7 +8,7 @@ using Waste.Domain; namespace Waste.Application { public class ResultList : W_Result - { + { /// /// 商户名称 /// @@ -191,4 +191,34 @@ namespace Waste.Application /// public bool IsWeight { get; set; } = false; } + /// + /// wifi模块上传的数据包体 + /// + public class WifiPackage + { + /// + /// 设备机器码 + /// + public string sn { get; set; } + /// + /// 桶大小 + /// + public string size { get; set; } = ""; + /// + /// 垃圾桶编号 + /// + public string trashcode { get; set; } = ""; + /// + /// 垃圾类别 + /// + public string WasteType { get; set; } = ""; + /// + /// 重量,KG + /// + public string Weight { get; set; } = "0"; + /// + /// 是否为心跳包 + /// + public bool IsHeart { get; set; } = false; + } } diff --git a/Waste.Application/ResultInfos/IResultService.cs b/Waste.Application/ResultInfos/IResultService.cs index e28e157..0a2a862 100644 --- a/Waste.Application/ResultInfos/IResultService.cs +++ b/Waste.Application/ResultInfos/IResultService.cs @@ -30,5 +30,11 @@ namespace Waste.Application /// /// Task InsertResultBy4GAsync(nMyPackage myPackage); + /// + /// wifi模块测量结果增加 + /// + /// + /// + Task InsertResultByWifiAsync(WifiPackage data); } } diff --git a/Waste.Application/ResultInfos/ResultService.cs b/Waste.Application/ResultInfos/ResultService.cs index 549c636..2e4bc30 100644 --- a/Waste.Application/ResultInfos/ResultService.cs +++ b/Waste.Application/ResultInfos/ResultService.cs @@ -93,7 +93,7 @@ namespace Waste.Application NetWeight = x.NetWeight, Registration = x.Registration, CreateTime = x.CreateTime, - PostStatus= -1 + PostStatus = -1 }) .Mapper((it, cache) => { @@ -116,13 +116,13 @@ namespace Waste.Application it.DeviceEcode = dev.Ecode; it.DeviceAddress = dev.Address; } - var allres = cache.Get(list => - { - var ids = list.Select(e => e.Id).ToList(); - return dbClient.Queryable().Where(e => ids.Contains(e.ResultId)).ToList(); - }); + var allres = cache.Get(list => + { + var ids = list.Select(e => e.Id).ToList(); + return dbClient.Queryable().Where(e => ids.Contains(e.ResultId)).ToList(); + }); var res = allres.FirstOrDefault(e => e.ResultId == it.Id); - if(res != null) + if (res != null) { it.PostStatus = res.Status; } @@ -136,6 +136,53 @@ namespace Waste.Application limit = param.limit }; } + /// + /// wifi模块测量结果增加 + /// + /// + /// + public async Task InsertResultByWifiAsync(WifiPackage data) + { + //查找设备 + var device = await dbClient.Queryable().FirstAsync(x => data.sn == x.Ecode); + // _loggerService.AddLogger($"接收到的数据,参数:{myPackage.ToJson()}", 3); + if (device == null) + { + //记录日志 + _loggerService.AddLogger($"设备未找到,参数:{data.ToJson()}", 3); + } + var devicedata = await dbClient.Queryable().FirstAsync(x => x.DeviceId == device.Id); + var resultid = IDGen.NextID(); + DateTime time = DateTime.Now; + //检查设备是否为今天第一次上报 + bool isfrist = false; + if (device.LastHeartTime.HasValue && device.LastHeartTime.Value.Date != DateTime.Now.Date) + { + isfrist = true; + } + //记录数据 + await dbClient.Ado.UseStoredProcedure().ExecuteCommandAsync("Proc_InsertResult", new + { + deviceid = device.Id, + businessid = device.Businessid, + resultid = resultid, + imei = devicedata.IMSI, + iccid = devicedata.ICCID, + imsi = devicedata.IMSI, + time = time, + latitude = "", + longitude = "", + sign = "", + city = "", + area = data.trashcode, + wastetype = data.WasteType, + weigth = data.Weight, + isheart = data.IsHeart, + tare = device.Tare, + isfrist = isfrist + }); + + } /// /// 新的4G模块测量结果增加 @@ -207,7 +254,7 @@ namespace Waste.Application { //查找设备 var device = await dbClient.Queryable().FirstAsync(x => myPackage.IMEI == x.Ecode); - // _loggerService.AddLogger($"接收到的数据,参数:{myPackage.ToJson()}", 3); + // _loggerService.AddLogger($"接收到的数据,参数:{myPackage.ToJson()}", 3); if (device == null) { //记录日志 @@ -255,7 +302,7 @@ namespace Waste.Application myPackage.Area = Convert.ToInt32(typeHex, 16).ToString(); } //000F000002.16进制 - // var areaHex = Convert.ToHexString(areaBytes); + // var areaHex = Convert.ToHexString(areaBytes); } //记录数据 diff --git a/Waste.Application/ThirdApiInfo/Dtos/ThirdApiInfoDto.cs b/Waste.Application/ThirdApiInfo/Dtos/ThirdApiInfoDto.cs index a4e3daa..f91acf0 100644 --- a/Waste.Application/ThirdApiInfo/Dtos/ThirdApiInfoDto.cs +++ b/Waste.Application/ThirdApiInfo/Dtos/ThirdApiInfoDto.cs @@ -207,4 +207,52 @@ namespace Waste.Application.ThirdApiInfo /// public string ExceptionInfo { get; set; } } + /// + /// wifi模块发送的数据 + /// + public class WifiRequestC2SDto + { + /// + /// 参数,包含sn和测量数据 + /// + public string @params { get; set; } + } + /// + /// wifi模块发送的数据解析 + /// + public class WifiRequestS2SDto + { + /// + /// 设备机器码 + /// + public string sn { get; set; } + + /// + /// 包头,固定为A9 + /// + public string Header { get; set; } = ""; + + /// + /// 分隔符,固定为00 + /// + public string splitstr { get; set; } = ""; + /// + /// 解密出的16进制数据 + /// + public string data { get; set; } = ""; + + /// + /// 数据是否验证通过 + /// + public bool ischecked { get; set; } = true; + /// + /// 是否为心跳包 + /// + public bool IsHeart { get; set; } = false; + /// + /// 结果集 + /// + public byte[] databyte { get; set; } + + } } diff --git a/Waste.Application/ThirdApiInfo/IOpenService.cs b/Waste.Application/ThirdApiInfo/IOpenService.cs index bfd4341..6d4f652 100644 --- a/Waste.Application/ThirdApiInfo/IOpenService.cs +++ b/Waste.Application/ThirdApiInfo/IOpenService.cs @@ -44,5 +44,11 @@ namespace Waste.Application.ThirdApiInfo /// /// Task UpdateVersionAsync(DeviceVerS2SDto data); + /// + /// 通过ailink wifi模式发送的数据 + /// + /// + /// + Task WifiPostAsync(WifiRequestC2SDto data); } } diff --git a/Waste.Application/ThirdApiInfo/OpenAppService.cs b/Waste.Application/ThirdApiInfo/OpenAppService.cs index e7b35e9..7416f86 100644 --- a/Waste.Application/ThirdApiInfo/OpenAppService.cs +++ b/Waste.Application/ThirdApiInfo/OpenAppService.cs @@ -299,5 +299,15 @@ namespace Waste.Application.ThirdApiInfo } }; } + + /// + /// 通过ailink wifi模式发送的数据 + /// + /// + /// + public async Task WifiAsync(WifiRequestC2SDto data) + { + return await _openService.WifiPostAsync(data); + } } } diff --git a/Waste.Application/ThirdApiInfo/OpenService.cs b/Waste.Application/ThirdApiInfo/OpenService.cs index cac7ef9..46ebaa9 100644 --- a/Waste.Application/ThirdApiInfo/OpenService.cs +++ b/Waste.Application/ThirdApiInfo/OpenService.cs @@ -2,6 +2,7 @@ using Furion; using Furion.DependencyInjection; using Furion.DistributedIDGenerator; +using Microsoft.Extensions.Caching.Distributed; using Nirvana.Common; using SqlSugar; using System; @@ -30,13 +31,18 @@ namespace Waste.Application.ThirdApiInfo private readonly ISuZhouService _suZhouService; private readonly ILoggerService _loggerService; private readonly ICapPublisher _capBus; - public OpenService(ISqlSugarRepository sqlSugarRepository, ISuZhouService suZhouService, ILoggerService loggerService, ICapPublisher capPublisher) + private readonly IDistributedCache _cahce; + private readonly IResultService _resultService; + + public OpenService(ISqlSugarRepository sqlSugarRepository, ISuZhouService suZhouService, ILoggerService loggerService, ICapPublisher capPublisher, IDistributedCache distributedCache, IResultService resultService) { repository = sqlSugarRepository; dbClient = repository.Context; _suZhouService = suZhouService; _loggerService = loggerService; _capBus = capPublisher; + _cahce = distributedCache; + _resultService = resultService; } /// /// 更新上报状态 @@ -376,5 +382,159 @@ namespace Waste.Application.ThirdApiInfo { await _capBus.PublishAsync("device.service.updatever", data); } + /// + /// 通过ailink wifi模式发送的数据 + /// + /// + /// + public async Task WifiPostAsync(WifiRequestC2SDto data) + { + //记录发送过来的数据 + _loggerService.AddLogger($"wifi发送原始数据,data={data.ToJson()}", 3); + var successdata = Encoding.UTF8.GetBytes("A901019A"); + var returndata = new + { + status = "1", + data = Convert.ToBase64String(successdata) + }; + //数据解析 + var result = AnalyProto(data); + if (!result.ischecked) + { + return returndata; + } + if (result.databyte.Length == 0) + { + return returndata; + } + //小于14则表示结束 + if (result.databyte.Length < 14) + { + var databytes = await _cahce.GetAsync(result.sn); + if (databytes != null && databytes.Length > 0) + { + byte[] newval = new byte[databytes.Length + result.databyte.Length]; + databytes.CopyTo(newval, 0); + result.databyte.CopyTo(newval, databytes.Length); + var datastr = Encoding.GetEncoding("gb2312").GetString(newval); + datastr.Replace(@"\r\n", ""); + var arr = datastr.Split('@'); + string wastetype = arr[2];//垃圾类型 + string weight = arr[3];//重量,kg + string trashno = string.Empty; //垃圾桶编号 + string size = string.Empty;//桶大小 + var sizebytes = Encoding.Default.GetBytes(arr[0]); + size = Convert.ToHexString(sizebytes); + var trashbytes = Encoding.Default.GetBytes(arr[1]); + //长度为5个字节,后三位为垃圾桶编号(16进制) + if (trashbytes.Length == 5) + { + var typeBytes = new byte[3]; + for (var i = 0; i < 3; i++) + { + typeBytes[i] = trashbytes[i + 2]; + } + var typeHex = Convert.ToHexString(typeBytes); + trashno = Convert.ToInt32(typeHex, 16).ToString(); + } + await _resultService.InsertResultByWifiAsync(new WifiPackage + { + sn = result.sn, + WasteType = wastetype, + Weight = weight, + trashcode = trashno, + size = size, + IsHeart = result.IsHeart + }); + } + } + else + { + var val = await _cahce.GetAsync(result.sn); + var time = new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(60)); + if (val != null && val.Length > 0) + { + byte[] newval = new byte[val.Length + result.databyte.Length]; + val.CopyTo(newval, 0); + result.databyte.CopyTo(newval, val.Length); + await _cahce.SetAsync(result.sn, newval, time); + } + else + { + await _cahce.SetAsync(result.sn, result.databyte, time); + } + } + return returndata; + } + /// + /// wifi数据解析 + /// + /// + /// + private WifiRequestS2SDto AnalyProto(WifiRequestC2SDto data) + { + WifiRequestS2SDto dto = new WifiRequestS2SDto(); + //检查是否为有效的base64数据 + if (!AESEncrypt.IsBase64(data.@params)) + { + dto.data = data.@params; + return dto; + } + //数据为base64加密 + byte[] resultByte = Convert.FromBase64String(data.@params); + dto.data = BytesToHexStr(resultByte); + //574D07884A1882FCC2FE346BA1D80E 00 A903000D0A1A9A + _loggerService.AddLogger($"wifi结果推送,解析出的数据:{dto.data}", 3); + int len = resultByte.Count();//数据总长度 + //长度格式是否正确,固定长度为36位 + if (len < 23) + { + return dto; + } + //前15个字节为sn。中间以 00 分隔。后面为测量结果数据 A9 03 00 0D 0A 1A 9A + int i = 0; + byte[] snbyte = new byte[15]; + for (i = 0; i < 15; i++) + { + snbyte[i] = resultByte[i]; + } + dto.sn = BytesToHexStr(snbyte).Replace(" ", ""); + dto.splitstr = ByteToHexStr(resultByte[i]); + i++; + dto.Header = ByteToHexStr(resultByte[i]);//固定为A9 + if (dto.Header != "A9") //固定为A9 + { + return dto; + } + string heartstr = ByteToHexStr(resultByte[i + 2]);//是否为测量数据 + if (heartstr != "00") + { + dto.IsHeart = true; + } + dto.ischecked = true; + string datalenstr = ByteToHexStr(resultByte[i + 1]); //数据体长度 + int datalen = HextToDec(datalenstr).ToInt(); + i += 3; + dto.databyte = new byte[datalen - 1]; + for (int j = 0; j < datalen; j++) + { + if (i < len - 2) + { + dto.databyte[j] = resultByte[i]; + } + i++; + } + return dto; + } + + /// + /// 字节转16进制 + /// + /// + /// + private string ByteToHexStr(byte bt) + { + return ((int)bt).ToString("X2"); + } } } diff --git a/Waste.Application/Waste.Application.xml b/Waste.Application/Waste.Application.xml index ad56070..25069e0 100644 --- a/Waste.Application/Waste.Application.xml +++ b/Waste.Application/Waste.Application.xml @@ -1752,6 +1752,36 @@ 是否为有效测量 + + + wifi模块上传的数据包体 + + + + + 设备机器码 + + + + + 桶大小 + + + + + 垃圾桶编号 + + + + + 垃圾类别 + + + + + 重量,KG + + 投放记录 @@ -1778,6 +1808,13 @@ + + + wifi模块测量结果增加 + + + + 投放记录 @@ -1809,6 +1846,13 @@ + + + wifi模块测量结果增加 + + + + 新的4G模块测量结果增加 @@ -2577,6 +2621,56 @@ 错误信息 + + + wifi模块发送的数据 + + + + + 参数,包含sn和测量数据 + + + + + wifi模块发送的数据解析 + + + + + 设备机器码 + + + + + 包头,固定为A9 + + + + + 分隔符,固定为00 + + + + + 解密出的16进制数据 + + + + + 数据是否验证通过 + + + + + 是否为心跳包 + + + + + 结果集 + + 设备对接接口 @@ -2617,6 +2711,13 @@ + + + 通过ailink wifi模式发送的数据 + + + + 开放数据 @@ -2678,6 +2779,13 @@ + + + 通过ailink wifi模式发送的数据 + + + + 设备对接接口 @@ -2739,6 +2847,27 @@ + + + 通过ailink wifi模式发送的数据 + + + + + + + wifi数据解析 + + + + + + + 字节转16进制 + + + + 垃圾分类列表 diff --git a/Waste.Core/Waste.Core.csproj b/Waste.Core/Waste.Core.csproj index cb6d47c..8633834 100644 --- a/Waste.Core/Waste.Core.csproj +++ b/Waste.Core/Waste.Core.csproj @@ -23,6 +23,7 @@ + diff --git a/Waste.Web.Core/Startup.cs b/Waste.Web.Core/Startup.cs index 6a41e01..e4f0404 100644 --- a/Waste.Web.Core/Startup.cs +++ b/Waste.Web.Core/Startup.cs @@ -13,6 +13,7 @@ using Serilog; using System; using System.Collections.Generic; using System.IO; +using System.Text; using System.Threading.Tasks; using Waste.Core; @@ -70,12 +71,22 @@ namespace Waste.Web.Core services.AddSingleton(); MyHttpContext.serviceCollection = services; #endregion + //添加redis缓存 + services.AddStackExchangeRedisCache(options => + { + // 连接字符串,这里也可以读取配置文件 + options.Configuration = "localhost"; + // 键名前缀 + options.InstanceName = "jt_"; + }); //添加即时通讯 // services.AddSignalR(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { + //启用 GB2312(按需) + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); app.UseSession(); if (env.IsDevelopment()) {