From fe0287166da6f1d2db092d00e98fa68a3337fd6b Mon Sep 17 00:00:00 2001 From: Hinse <756681202@qq.com> Date: Fri, 30 Jul 2021 18:15:58 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E5=A4=87=E6=8E=A5=E5=8F=A3=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ResultInfos/ResultService.cs | 6 +- .../ThirdApiInfo/Dtos/ThirdApiInfoDto.cs | 108 ++++++++++- .../ThirdApiInfo/IOpenService.cs | 12 ++ .../ThirdApiInfo/OpenAppService.cs | 24 ++- Waste.Application/ThirdApiInfo/OpenService.cs | 128 ++++++++++++- Waste.Application/Waste.Application.xml | 179 +++++++++++++++++- Waste.Socket/Program.cs | 20 +- .../waste.ybhdmob.com.pubxml.user | 2 +- 8 files changed, 442 insertions(+), 37 deletions(-) diff --git a/Waste.Application/ResultInfos/ResultService.cs b/Waste.Application/ResultInfos/ResultService.cs index 0775877..4664042 100644 --- a/Waste.Application/ResultInfos/ResultService.cs +++ b/Waste.Application/ResultInfos/ResultService.cs @@ -134,7 +134,7 @@ namespace Waste.Application { //查找设备 var device = await repository.Change().Context.Queryable().FirstAsync(x => myPackage.IMEI == x.Ecode); - _loggerService.AddLogger($"接收到的数据,参数:{myPackage.ToJson()}", 2); + _loggerService.AddLogger($"接收到的数据,参数:{myPackage.ToJson()}", 3); if (device == null) { //记录日志 @@ -182,7 +182,7 @@ namespace Waste.Application myPackage.Area = Convert.ToInt32(typeHex, 16).ToString(); } //000F000002.16进制 - var areaHex = Convert.ToHexString(areaBytes); + // var areaHex = Convert.ToHexString(areaBytes); } //记录数据 @@ -223,7 +223,7 @@ namespace Waste.Application // ScanningTime = timestamp, // DStatus = 0, // deviceid = devicesecret.DevId, - // Trash = "202101", + // Trash = myPackage.Area, // Type = TrashType(myPackage.WasteType) // }); // } diff --git a/Waste.Application/ThirdApiInfo/Dtos/ThirdApiInfoDto.cs b/Waste.Application/ThirdApiInfo/Dtos/ThirdApiInfoDto.cs index c2c04c6..51993d4 100644 --- a/Waste.Application/ThirdApiInfo/Dtos/ThirdApiInfoDto.cs +++ b/Waste.Application/ThirdApiInfo/Dtos/ThirdApiInfoDto.cs @@ -7,25 +7,81 @@ using System.Threading.Tasks; namespace Waste.Application.ThirdApiInfo { /// - /// 获取设备信息请求数据 + /// 心跳包上报数据 /// - public class GetDevInfoRequestDto + public class DevHeartRequestDto { - /// - /// 重量 - /// - public decimal Weight { get; set; } = 0; - /// /// 机器码 /// - public string ECode { get; set; } + public string ECode { get; set; } = ""; + /// + /// IMEI + /// + public string IMEI { get; set; } = ""; + /// + /// ICCID + /// + public string ICCID { get; set; } = ""; + /// + /// IMSI + /// + public string IMSI { get; set; } = ""; + /// + /// 信号强度 + /// + public int GSLQ { get; set; } = 0; + /// + /// 纬度 + /// + public string Latitude { get; set; } = ""; + /// + /// 经度 + /// + public string Longitude { get; set; } = ""; + } + /// + /// 注册注册信息返回值 + /// + public class DevRegInfoResponseDto + { + /// + /// 设备状态,0-使用中,1-异常,2-检修,3-检修结束,4-启用,5-未知 + /// + public int status { get; set; } = 0; + /// + /// 波特率 + /// + public int baudrate { get; set; } = 9600; + /// + /// 串口号 + /// + public string serialno { get; set; } = "/dev/ttyS2"; + } + + /// + /// 获取设备信息请求数据,并上报数据 + /// + public class GetDevInfoRequestDto: DevHeartRequestDto + { + /// + /// 记录ID + /// + public Guid? ResultId { get; set; } = null; + /// + /// 串口数据 + /// + public string data { get; set; } = ""; } /// /// 获取设备信息响应数据 /// public class GetDevInfoResponseDto { + /// + /// 解析数据是否正常 + /// + public bool IsSuccessed { get; set; } = false; /// /// 时间戳 /// @@ -47,8 +103,42 @@ namespace Waste.Application.ThirdApiInfo /// public string SecretHash { get; set; } /// - /// 设备上报时才会有 + /// 设备ID /// public string DeviceId { get; set; } = ""; + /// + /// 上报地址 + /// + public string PostUrl { get; set; } = ""; + /// + /// 垃圾桶编号 + /// + public string trash { get; set; } = ""; + /// + /// 垃圾类型 + /// + public int type { get; set; } + /// + /// 体重 + /// + public double Weight { get; set; } + + /// + /// 设备状态,0-使用中,1-异常,2-检修,3-检修结束,4-启用,5-未知 + /// + public int status { get; set; } = 0; + + /// + /// 数据扫描时间,UNIX时间戳 + /// + public int ScanningTime { get; set; } + /// + /// 签名 + /// + public string sign { get; set; } + /// + /// 记录ID + /// + public Guid ResultId { get; set; } } } diff --git a/Waste.Application/ThirdApiInfo/IOpenService.cs b/Waste.Application/ThirdApiInfo/IOpenService.cs index 9189201..78f1f6e 100644 --- a/Waste.Application/ThirdApiInfo/IOpenService.cs +++ b/Waste.Application/ThirdApiInfo/IOpenService.cs @@ -18,5 +18,17 @@ namespace Waste.Application.ThirdApiInfo /// /// Task GetDevInfoAsync(GetDevInfoRequestDto data); + /// + /// 心跳数据上报 + /// + /// + /// + Task PostHeartAsync(DevHeartRequestDto data); + /// + /// 获取设备注册信息 + /// + /// + /// + Task RegInfoAsync(string ecode); } } diff --git a/Waste.Application/ThirdApiInfo/OpenAppService.cs b/Waste.Application/ThirdApiInfo/OpenAppService.cs index 9c88e41..747cbd9 100644 --- a/Waste.Application/ThirdApiInfo/OpenAppService.cs +++ b/Waste.Application/ThirdApiInfo/OpenAppService.cs @@ -13,6 +13,7 @@ namespace Waste.Application.ThirdApiInfo /// 开放数据 /// [ApiDescriptionSettings("DevApi")] + [NonUnify] public class OpenAppService : IDynamicApiController { private readonly IOpenService _openService; @@ -26,10 +27,31 @@ namespace Waste.Application.ThirdApiInfo /// /// [HttpPost] - [NonUnify] public async Task GetDevInfoAsync(GetDevInfoRequestDto data) { return await _openService.GetDevInfoAsync(data); } + + /// + /// 心跳数据上报 + /// + /// + /// + [HttpPost] + public async Task PostHeartAsync(DevHeartRequestDto data) + { + return await _openService.PostHeartAsync(data); + } + /// + /// 获取设备注册信息,第一次开机使用 + /// + /// + /// + [HttpGet] + [QueryParameters] + public async Task RegInfoAsync(string ecode) + { + return await _openService.RegInfoAsync(ecode); + } } } diff --git a/Waste.Application/ThirdApiInfo/OpenService.cs b/Waste.Application/ThirdApiInfo/OpenService.cs index c8b42f3..eca8578 100644 --- a/Waste.Application/ThirdApiInfo/OpenService.cs +++ b/Waste.Application/ThirdApiInfo/OpenService.cs @@ -1,5 +1,6 @@ using Furion; using Furion.DependencyInjection; +using Furion.DistributedIDGenerator; using Nirvana.Common; using SqlSugar; using System; @@ -37,12 +38,12 @@ namespace Waste.Application.ThirdApiInfo public async Task GetDevInfoAsync(GetDevInfoRequestDto data) { var device = await dbClient.Queryable().FirstAsync(x => x.Ecode == data.ECode); - if(device == null) + if (device == null) { return new ResultInfo(ResultState.FAIL, "设备未找到"); } var devicesecret = await dbClient.Queryable().FirstAsync(x => x.DeviceId == device.Id); - if(devicesecret == null || string.IsNullOrEmpty(devicesecret.Secret) + if (devicesecret == null || string.IsNullOrEmpty(devicesecret.Secret) || string.IsNullOrEmpty(devicesecret.SecretHash) || string.IsNullOrEmpty(devicesecret.DevId)) { @@ -50,15 +51,134 @@ namespace Waste.Application.ThirdApiInfo } int timestamp = _suZhouService.GetTimestamp(); int noncestr = _suZhouService.GetNonce(); - var returndata = new GetDevInfoResponseDto { + var returndata = new GetDevInfoResponseDto + { DeviceId = devicesecret.DevId, noncestr = noncestr, timestamp = timestamp, Secret = devicesecret.Secret, SecretHash = devicesecret.SecretHash, - UserId = UserId + UserId = UserId, + PostUrl = ApiUrl, + ScanningTime = GetTimestamp(DateTime.Now), + ResultId = IDGen.NextID() }; + //解析协议,IC卡数据@垃圾桶编号@厨余垃圾@7.91 + // 00000000003031 40 000F000002 40 C6E4CBFBC0ACBBF8 40 35312E300D0A + if (!string.IsNullOrEmpty(data.data) && data.data.Length> 52) + { + data.data = data.data.Replace(" ", ""); + //收到的为16进制,对数据进行解析,0-4预留,5-垃圾种类,6-垃圾桶大小,7-@,8-12垃圾桶编号,13@,14-21垃圾种类汉子,22@,23-结束重量, OD OA 回车换行 + data.data = data.data.Substring(0,data.data.Length-4); + var trashhex = data.data.Substring(16, 10); + var typehex = data.data.Substring(28, 16); + var weighthex = data.data.Substring(46, data.data.Length - 46); + returndata.trash = Convert.ToInt32(trashhex, 16).ToString(); + var type = GetChsFromHex(typehex); + var weight = GetChsFromHex(weighthex); + returndata.type = TrashType(type); + returndata.Weight = weight.ToDouble(); + returndata.IsSuccessed = true; + string[] paramlist = new string[] { + returndata.Weight.ToString(),returndata.trash,returndata.type.ToString(),returndata.ScanningTime.ToString(),returndata.status.ToString() + }; + returndata.sign = _suZhouService.GetUserApiSign(returndata.Secret, paramlist); + } return new ResultInfo(ResultState.SUCCESS, "success", returndata); } + /// + /// 16进制转汉字 + /// + /// + /// + private string GetChsFromHex(string hex) + { + if (hex == null) + return ""; + if (hex.Length % 2 != 0) + { + hex += "20";//空格 + } + // 需要将 hex 转换成 byte 数组。 + byte[] bytes = new byte[hex.Length / 2]; + for (int i = 0; i < bytes.Length; i++) + { + try + { + // 每两个字符是一个 byte。 + bytes[i] = byte.Parse(hex.Substring(i * 2, 2), + System.Globalization.NumberStyles.HexNumber); + } + catch + { + } + } + // 获得 GB2312,Chinese Simplified。 + Encoding chs = Encoding.GetEncoding("gb2312"); + return chs.GetString(bytes); + } + /// + /// 心跳数据上报 + /// + /// + /// + public async Task PostHeartAsync(DevHeartRequestDto data) + { + var device = await dbClient.Queryable().FirstAsync(x => x.Ecode == data.ECode); + if (device == null) + { + return new ResultInfo(ResultState.FAIL, "设备未找到"); + } + return new ResultInfo(ResultState.SUCCESS, "success"); + } + /// + /// 获取设备注册信息,第一次开机使用 + /// + /// + /// + public async Task RegInfoAsync(string ecode) + { + var device = await dbClient.Queryable().FirstAsync(x => x.Ecode == ecode); + //if (device == null) + //{ + // return new ResultInfo(ResultState.FAIL, "设备未找到"); + //} + var data = new DevRegInfoResponseDto + { + status = 0 + }; + return new ResultInfo(ResultState.SUCCESS, "success", data); + } + private int TrashType(string type) + { + if (type == "厨余垃圾") return 1; + else if (type == "可回收物") return 2; + else if (type == "有害垃圾") return 3; + else if (type == "其他垃圾") return 4; + else return 0; + } + private int GetTimestamp(DateTime time) + { + DateTime dateTimeStart = TimeZoneInfo.ConvertTimeToUtc(new DateTime(1970, 1, 1, 8, 0, 0)); + int timestamp = Convert.ToInt32((time - dateTimeStart).TotalSeconds); + return timestamp; + } + /// + /// 字节数组转16进制 + /// + /// + /// + private string BytesToHexStr( byte[] bt) + { + string returnStr = ""; + if (bt != null) + { + for (int i = 0; i < bt.Length; i++) + { + returnStr += bt[i].ToString("X2"); + } + } + return returnStr; + } } } diff --git a/Waste.Application/Waste.Application.xml b/Waste.Application/Waste.Application.xml index 2bb5927..4c1a732 100644 --- a/Waste.Application/Waste.Application.xml +++ b/Waste.Application/Waste.Application.xml @@ -1945,26 +1945,91 @@ - + - 获取设备信息请求数据 + 心跳包上报数据 - - - 重量 - - - + 机器码 + + + IMEI + + + + + ICCID + + + + + IMSI + + + + + 信号强度 + + + + + 纬度 + + + + + 经度 + + + + + 注册注册信息返回值 + + + + + 设备状态,0-使用中,1-异常,2-检修,3-检修结束,4-启用,5-未知 + + + + + 波特率 + + + + + 串口号 + + + + + 获取设备信息请求数据,并上报数据 + + + + + 记录ID + + + + + 串口数据 + + 获取设备信息响应数据 + + + 解析数据是否正常 + + 时间戳 @@ -1992,7 +2057,47 @@ - 设备上报时才会有 + 设备ID + + + + + 上报地址 + + + + + 垃圾桶编号 + + + + + 垃圾类型 + + + + + 体重 + + + + + 设备状态,0-使用中,1-异常,2-检修,3-检修结束,4-启用,5-未知 + + + + + 数据扫描时间,UNIX时间戳 + + + + + 签名 + + + + + 记录ID @@ -2007,6 +2112,20 @@ + + + 心跳数据上报 + + + + + + + 获取设备注册信息 + + + + 开放数据 @@ -2019,6 +2138,20 @@ + + + 心跳数据上报 + + + + + + + 获取设备注册信息,第一次开机使用 + + + + 设备对接接口 @@ -2031,6 +2164,34 @@ + + + 16进制转汉字 + + + + + + + 心跳数据上报 + + + + + + + 获取设备注册信息,第一次开机使用 + + + + + + + 字节数组转16进制 + + + + 垃圾分类列表 diff --git a/Waste.Socket/Program.cs b/Waste.Socket/Program.cs index e9dc559..8d93a86 100644 --- a/Waste.Socket/Program.cs +++ b/Waste.Socket/Program.cs @@ -36,14 +36,14 @@ namespace Waste.Socket ; - rabbitMqProxy = new RabbitMqService(new MqConfig - { - AutomaticRecoveryEnabled = true, - Host = "localhost", - HeartBeat = new TimeSpan(60), - UserName = "liuzl", - Password = "liuzl" - }); + //rabbitMqProxy = new RabbitMqService(new MqConfig + //{ + // AutomaticRecoveryEnabled = true, + // Host = "localhost", + // HeartBeat = new TimeSpan(60), + // UserName = "liuzl", + // Password = "liuzl" + //}); //创建宿主 var host = SuperSocketHostBuilder .Create() @@ -85,7 +85,7 @@ namespace Waste.Socket //} //向rabbitmq队列发布消息 var data = JsonConvert.SerializeObject(package); - rabbitMqProxy.Publish("wasteexchang", "wastequeue", "waste", data, true); + // rabbitMqProxy.Publish("wasteexchang", "wastequeue", "waste", data, true); } else { @@ -122,7 +122,7 @@ namespace Waste.Socket } public void Dispose() { - rabbitMqProxy.Dispose(); + // rabbitMqProxy.Dispose(); } } /// diff --git a/Waste.Web.Entry/Properties/PublishProfiles/waste.ybhdmob.com.pubxml.user b/Waste.Web.Entry/Properties/PublishProfiles/waste.ybhdmob.com.pubxml.user index 9d00fd6..d364909 100644 --- a/Waste.Web.Entry/Properties/PublishProfiles/waste.ybhdmob.com.pubxml.user +++ b/Waste.Web.Entry/Properties/PublishProfiles/waste.ybhdmob.com.pubxml.user @@ -5,6 +5,6 @@ https://go.microsoft.com/fwlink/?LinkID=208121. <_PublishTargetUrl>D:\webpublish\waste.ybhdmob.com - True|2021-07-30T01:32:38.2676032Z;True|2021-07-30T09:14:42.6170851+08:00;True|2021-07-29T19:06:09.1449349+08:00;True|2021-06-11T08:16:29.9542894+08:00;True|2021-06-04T14:46:02.2707457+08:00;True|2021-06-02T15:08:52.8245632+08:00;True|2021-06-02T15:05:50.3614099+08:00;True|2021-06-02T14:59:32.3690948+08:00;True|2021-06-02T14:10:25.1182836+08:00;True|2021-06-02T14:09:54.9215833+08:00;True|2021-06-01T10:41:54.9488501+08:00;True|2021-06-01T10:38:56.0283198+08:00;True|2021-05-28T13:59:02.2308877+08:00;True|2021-05-28T11:56:26.6796406+08:00;True|2021-05-28T11:28:00.4087907+08:00;True|2021-05-27T16:18:09.5993838+08:00;True|2021-05-27T16:07:31.3484951+08:00;True|2021-05-27T11:30:37.9119310+08:00;True|2021-05-27T11:28:35.5374674+08:00;True|2021-05-27T08:00:09.1625592+08:00;True|2021-05-26T20:42:17.0852150+08:00;True|2021-05-26T20:36:49.7527415+08:00;True|2021-05-25T17:57:31.8791293+08:00;True|2021-05-25T13:49:29.6488978+08:00;True|2021-05-25T13:48:24.6686105+08:00;True|2021-05-25T13:25:41.2512493+08:00;True|2021-05-24T17:55:33.3800078+08:00;True|2021-05-20T14:35:30.6957985+08:00;True|2021-05-20T13:17:22.6192995+08:00;True|2021-05-20T10:51:38.1268169+08:00;True|2021-05-19T19:50:03.7000224+08:00;True|2021-05-19T19:44:27.2518811+08:00;True|2021-05-19T19:43:26.5916681+08:00;True|2021-05-19T19:36:29.3197365+08:00;True|2021-05-19T19:30:00.3802430+08:00;True|2021-05-19T17:55:23.7939835+08:00;True|2021-05-19T11:05:17.9043392+08:00;True|2021-05-19T10:19:38.4839988+08:00;True|2021-05-19T10:17:19.7430612+08:00;True|2021-05-19T10:13:23.0031721+08:00;True|2021-05-19T10:06:03.9881599+08:00;True|2021-05-18T14:39:03.8876574+08:00;True|2021-05-18T14:23:46.9818836+08:00;True|2021-05-18T14:19:56.2382079+08:00;True|2021-05-18T11:29:53.5497590+08:00;True|2021-05-18T11:16:18.0123853+08:00;True|2021-05-17T18:59:52.4159105+08:00;True|2021-05-17T18:53:37.9438984+08:00;True|2021-05-17T18:48:14.9625161+08:00;True|2021-05-17T17:46:03.7723404+08:00;True|2021-05-17T17:14:20.2312990+08:00;True|2021-05-17T16:44:34.5837616+08:00;True|2021-05-17T16:25:20.1087804+08:00;True|2021-05-17T11:35:27.9388562+08:00; + True|2021-07-30T09:59:30.2223340Z;True|2021-07-30T17:57:35.9412910+08:00;True|2021-07-30T17:07:58.3305971+08:00;True|2021-07-30T17:04:10.9244859+08:00;True|2021-07-30T17:02:12.1943634+08:00;True|2021-07-30T16:16:22.2838331+08:00;True|2021-07-30T15:05:26.5664155+08:00;True|2021-07-30T14:57:59.1966108+08:00;True|2021-07-30T14:54:25.8172908+08:00;True|2021-07-30T14:52:20.9209995+08:00;True|2021-07-30T14:35:29.5239463+08:00;True|2021-07-30T09:32:38.2676032+08:00;True|2021-07-30T09:14:42.6170851+08:00;True|2021-07-29T19:06:09.1449349+08:00;True|2021-06-11T08:16:29.9542894+08:00;True|2021-06-04T14:46:02.2707457+08:00;True|2021-06-02T15:08:52.8245632+08:00;True|2021-06-02T15:05:50.3614099+08:00;True|2021-06-02T14:59:32.3690948+08:00;True|2021-06-02T14:10:25.1182836+08:00;True|2021-06-02T14:09:54.9215833+08:00;True|2021-06-01T10:41:54.9488501+08:00;True|2021-06-01T10:38:56.0283198+08:00;True|2021-05-28T13:59:02.2308877+08:00;True|2021-05-28T11:56:26.6796406+08:00;True|2021-05-28T11:28:00.4087907+08:00;True|2021-05-27T16:18:09.5993838+08:00;True|2021-05-27T16:07:31.3484951+08:00;True|2021-05-27T11:30:37.9119310+08:00;True|2021-05-27T11:28:35.5374674+08:00;True|2021-05-27T08:00:09.1625592+08:00;True|2021-05-26T20:42:17.0852150+08:00;True|2021-05-26T20:36:49.7527415+08:00;True|2021-05-25T17:57:31.8791293+08:00;True|2021-05-25T13:49:29.6488978+08:00;True|2021-05-25T13:48:24.6686105+08:00;True|2021-05-25T13:25:41.2512493+08:00;True|2021-05-24T17:55:33.3800078+08:00;True|2021-05-20T14:35:30.6957985+08:00;True|2021-05-20T13:17:22.6192995+08:00;True|2021-05-20T10:51:38.1268169+08:00;True|2021-05-19T19:50:03.7000224+08:00;True|2021-05-19T19:44:27.2518811+08:00;True|2021-05-19T19:43:26.5916681+08:00;True|2021-05-19T19:36:29.3197365+08:00;True|2021-05-19T19:30:00.3802430+08:00;True|2021-05-19T17:55:23.7939835+08:00;True|2021-05-19T11:05:17.9043392+08:00;True|2021-05-19T10:19:38.4839988+08:00;True|2021-05-19T10:17:19.7430612+08:00;True|2021-05-19T10:13:23.0031721+08:00;True|2021-05-19T10:06:03.9881599+08:00;True|2021-05-18T14:39:03.8876574+08:00;True|2021-05-18T14:23:46.9818836+08:00;True|2021-05-18T14:19:56.2382079+08:00;True|2021-05-18T11:29:53.5497590+08:00;True|2021-05-18T11:16:18.0123853+08:00;True|2021-05-17T18:59:52.4159105+08:00;True|2021-05-17T18:53:37.9438984+08:00;True|2021-05-17T18:48:14.9625161+08:00;True|2021-05-17T17:46:03.7723404+08:00;True|2021-05-17T17:14:20.2312990+08:00;True|2021-05-17T16:44:34.5837616+08:00;True|2021-05-17T16:25:20.1087804+08:00;True|2021-05-17T11:35:27.9388562+08:00; \ No newline at end of file