From a1dbd33400c670150282da3e10e7412af2960741 Mon Sep 17 00:00:00 2001 From: liuzl Date: Sat, 27 Aug 2022 17:57:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A8=A1=E6=8B=9F=E6=B5=8B=E8=AF=95=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PostInfo/SuZhou/SuZhouService.cs | 31 ++- WasteConsoleTest/WasteConsoleTest/Program.cs | 177 +++++++++++++++--- .../WasteConsoleTest/WSocketClientHelp.cs | 6 +- 3 files changed, 178 insertions(+), 36 deletions(-) diff --git a/Waste.Application/PostInfo/SuZhou/SuZhouService.cs b/Waste.Application/PostInfo/SuZhou/SuZhouService.cs index d12889e..95e4cb7 100644 --- a/Waste.Application/PostInfo/SuZhou/SuZhouService.cs +++ b/Waste.Application/PostInfo/SuZhou/SuZhouService.cs @@ -1,11 +1,8 @@ - using Furion; +using Furion; using Furion.DependencyInjection; -using Furion.RemoteRequest.Extensions; using MessagePack; using Nirvana.Common; using System; -using System.Collections.Generic; -using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; @@ -23,11 +20,13 @@ namespace Waste.Application private static string ApiSecretHash = App.Configuration["SZDevPlatSetting:ApiSecretHash"]; private readonly IHttpClientFactory _clientFactory; private readonly ILoggerService _loggerService; + public SuZhouService(IHttpClientFactory clientFactory, ILoggerService loggerService) { _clientFactory = clientFactory; _loggerService = loggerService; } + /// /// 分页查询当前账号下存在的地产区域数据 /// @@ -37,7 +36,6 @@ namespace Waste.Application /// public async Task GetEstatesAsync(int page = 1, int size = 10, string search = "") { - var url = $"{ApiUrl}/api/estates?page={page}&size={size}&search={search}"; var response = await GetDataAsync(url, 1, ""); var resdata = await response.Content.ReadAsByteArrayAsync(); @@ -52,6 +50,7 @@ namespace Waste.Application return new ResultInfo(ResultState.FAIL, msg); } } + /// /// 查询地产区域信息 /// @@ -73,6 +72,7 @@ namespace Waste.Application return new ResultInfo(ResultState.FAIL, msg); } } + /// /// Hello接口GET测试 /// @@ -94,6 +94,7 @@ namespace Waste.Application } return new ResultInfo(ResultState.SUCCESS, "success", returndata); } + /// /// 添加地产区域信息 /// @@ -118,6 +119,7 @@ namespace Waste.Application return new ResultInfo(ResultState.FAIL, msg); } } + /// /// 向服务端推送测试用垃圾采集数据 /// @@ -159,6 +161,7 @@ namespace Waste.Application return new ResultInfo(ResultState.FAIL, msg); } } + /// /// Hello接口Post测试 /// @@ -185,11 +188,12 @@ namespace Waste.Application } return new ResultInfo(ResultState.SUCCESS, "success", returndata); } + /// /// 获取时间戳 /// /// - public int GetTimestamp() + public int GetTimestamp() { DateTime dateTimeStart = TimeZoneInfo.ConvertTimeToUtc(new DateTime(1970, 1, 1, 8, 0, 0)); int timestamp = Convert.ToInt32((DateTime.Now - dateTimeStart).TotalSeconds); @@ -207,23 +211,25 @@ namespace Waste.Application int timestamp = Convert.ToInt32((utcTime - utcStartTime).TotalSeconds); return timestamp; } + /// /// 获取随机数 /// /// - public int GetNonce() + public int GetNonce() { var random = new Random(); int nonce = random.Next(1, Int32.MaxValue); return nonce; } + /// /// 获取签名 /// /// /// /// - public string GetUserApiSign(string secret, params string[] dataparams) + public string GetUserApiSign(string secret, params string[] dataparams) { StringBuilder sb = new StringBuilder(); if (dataparams != null && dataparams.Length > 0) @@ -245,6 +251,7 @@ namespace Waste.Application string sign = Md5.md5(str, 16).ToLower(); return sign; } + /// /// POST封装接口 /// @@ -285,6 +292,7 @@ namespace Waste.Application var testdata = await response.Content.ReadAsStringAsync(); return response; } + /// /// GET接口封装 /// @@ -316,6 +324,7 @@ namespace Waste.Application var testdata = await response.Content.ReadAsStringAsync(); return response; } + /// /// Delete接口封装 /// @@ -347,6 +356,7 @@ namespace Waste.Application var testdata = await response.Content.ReadAsStringAsync(); return response; } + /// /// 删除地产区域 /// @@ -367,6 +377,7 @@ namespace Waste.Application return new ResultInfo(ResultState.FAIL, msg); } } + /// /// 删除采集点 /// @@ -386,6 +397,7 @@ namespace Waste.Application return new ResultInfo(ResultState.FAIL, resdata); } } + /// /// 添加采集点 /// @@ -410,6 +422,7 @@ namespace Waste.Application return new ResultInfo(ResultState.FAIL, msg); } } + /// /// 上传数据 /// @@ -462,4 +475,4 @@ namespace Waste.Application } } } -} +} \ No newline at end of file diff --git a/WasteConsoleTest/WasteConsoleTest/Program.cs b/WasteConsoleTest/WasteConsoleTest/Program.cs index d85244f..66dea2c 100644 --- a/WasteConsoleTest/WasteConsoleTest/Program.cs +++ b/WasteConsoleTest/WasteConsoleTest/Program.cs @@ -14,11 +14,49 @@ using System.Threading.Tasks; namespace WasteConsoleTest { - class Program + /// + /// 向服务端推送测试用垃圾采集数据 + /// + [MessagePackObject] + public class MessageGarbageC2SDto //客户端向服务端请求的DTO + { + /// + /// 垃圾称重数据,64位浮点进度,单位为千克 + /// + [Key("weight")] + public double Weight { get; set; } + + /// + /// 垃圾桶编码 + /// + [Key("trash")] + public string Trash { get; set; } + + /// + /// 垃圾类型,缺省类型 : 0,厨余垃圾 : 1,可回收物 : 2,有害垃圾 : 3,其他垃圾 : 4 + /// + [Key("type")] + public int Type { get; set; } + + /// + /// 数据扫描时间,UNIX时间戳 + /// + [Key("scanningTime")] + public long ScanningTime { get; set; } + + /// + /// 设备状态,使用中 : 0:使用中,异常 : 1,检修 : 2,检修结束 : 3,启用 : 4,未知 : 5 + /// + [Key("d_status")] + public int DStatus { get; set; } + } + + internal class Program { private static WSocketClientHelp wSocketClient = null; public static string SocketURL = "wss://api.device.suzhou.ljflytjl.cn/device_rpc"; - static async Task Main(string[] args) + + private static async Task Main(string[] args) { var builder = new HostBuilder().ConfigureServices((hostContext, services) => { @@ -38,7 +76,7 @@ namespace WasteConsoleTest wSocketClient.OnError += WSocketClient_OnError; wSocketClient.Open(); var myService = host.Services.GetRequiredService(); - // SetTimeOut(); + // SetTimeOut(); while (true) { string cmd = Console.ReadLine(); @@ -86,8 +124,14 @@ namespace WasteConsoleTest { await myService.Garbages(); } + //messagepack测试上报 + else if (cmd.Equals("postmessagepack", StringComparison.OrdinalIgnoreCase)) + { + await myService.MessageGarbages(); + } } } + private static void WSocketClient_OnError(object sender, Exception ex) { Console.WriteLine($"发生异常:{ex.Message}"); @@ -126,7 +170,7 @@ namespace WasteConsoleTest } } //如果收到的消息为type=6心跳包,需要响应Pong - if(jsondata !=null && jsondata.type == 6) + if (jsondata != null && jsondata.type == 6) { var senddata = @"{ ""type"": 6 @@ -137,9 +181,7 @@ namespace WasteConsoleTest } catch (Exception) { - } - } public interface IMyService @@ -148,11 +190,18 @@ namespace WasteConsoleTest /// 测试token有效期 /// Task TestTokenAsync(); + /// /// 传送垃圾数据 /// /// Task Garbages(); + + /// + /// Messagepack传送垃圾数据 + /// + /// + Task MessageGarbages(); } public class MyService : IMyService @@ -163,6 +212,7 @@ namespace WasteConsoleTest { _clientFactory = clientFactory; } + /// /// 传送垃圾数据 /// @@ -174,19 +224,14 @@ namespace WasteConsoleTest string token = gettoken(); if (!string.IsNullOrEmpty(token)) { - int timestamp = GetTimestamp(); - DateTime testtime = DateTime.Parse("2021-08-22 20:00:00"); - DateTime utcTime = TimeZoneInfo.ConvertTimeToUtc(testtime); - DateTime utcStartTime = new DateTime(1970, 1, 1, 0, 0, 0, 0); - int scantime= timestamp = Convert.ToInt32((utcTime - utcStartTime).TotalSeconds); - + var scantime = GetTimestamp(); var garbageC2SDto = new GarbageC2SDto { - weight = 50.25, - trash = "251658245", + weight = 42.3, + trash = "64424512085", scanningTime = scantime, d_status = 0, - type = 1 + type = 4 }; int nonce = GetNonce(); string[] paramlist = new string[] { @@ -194,14 +239,14 @@ namespace WasteConsoleTest }; string sign = GetUserApiSign(WSocketClientHelp.Secret, paramlist); var request = new HttpRequestMessage(HttpMethod.Post, -"https://api.data.suzhou.ljflytjl.cn/api/Garbages"); +"https://api.data.suzhou.ljflytjl.cn/api/Garbages?api-version=1.0"); request.Headers.Add("Authorization", $"Bearer {token}"); request.Headers.Add("secret", WSocketClientHelp.Secret); request.Headers.Add("nonce", nonce.ToString()); - request.Headers.Add("time", timestamp.ToString()); + request.Headers.Add("time", scantime.ToString()); request.Headers.Add("sign", sign); var message = JsonConvert.SerializeObject(garbageC2SDto); - request.Content = new StringContent(message, Encoding.UTF8, "application/json"); + request.Content = new StringContent(message, Encoding.Default, "application/json"); var client = _clientFactory.CreateClient(); var response = await client.SendAsync(request); var result = await response.Content.ReadAsStringAsync(); @@ -221,8 +266,65 @@ namespace WasteConsoleTest } catch (Exception) { + } + } - + /// + /// MessagePack传送垃圾数据 + /// + /// + public async Task MessageGarbages() + { + try + { + string token = gettoken(); + if (!string.IsNullOrEmpty(token)) + { + var timestamp = GetTimestamp(); + var garbageC2SDto = new MessageGarbageC2SDto + { + Weight = 50.2, + Trash = "64424509988", + ScanningTime = timestamp, + DStatus = 0, + Type = 4 + }; + int nonce = GetNonce(); + string[] paramlist = new string[] { + garbageC2SDto.Weight.ToString(),garbageC2SDto.Trash,garbageC2SDto.Type.ToString(),garbageC2SDto.ScanningTime.ToString(),garbageC2SDto.DStatus.ToString() + }; + string sign = GetUserApiSign(WSocketClientHelp.Secret, paramlist); + var request = new HttpRequestMessage(HttpMethod.Post, +"https://api.data.suzhou.ljflytjl.cn/api/Garbages"); + request.Headers.Add("Authorization", $"Bearer {token}"); + request.Headers.Add("secret", WSocketClientHelp.Secret); + request.Headers.Add("nonce", nonce.ToString()); + request.Headers.Add("time", timestamp.ToString()); + request.Headers.Add("sign", sign); + + var bytes = MessagePackSerializer.Serialize(garbageC2SDto); + request.Content = new ByteArrayContent(bytes); + request.Headers.Add("Accept", "application/x-msgpack"); + request.Content.Headers.Add("Content-Type", "application/x-msgpack"); + var client = _clientFactory.CreateClient(); + var response = await client.SendAsync(request); + var result = await response.Content.ReadAsStringAsync(); + if (response.IsSuccessStatusCode) + { + Console.WriteLine($"上报成功:{result}"); + } + else + { + Console.WriteLine($"上报失败:{response.StatusCode},{result}"); + } + } + else + { + Console.Write("token未找到"); + } + } + catch (Exception) + { } } @@ -254,17 +356,30 @@ namespace WasteConsoleTest Console.Write("token未找到"); } } + /// /// 获取时间戳 /// /// - private int GetTimestamp() + private long GetTimestamp() { DateTime utcTime = TimeZoneInfo.ConvertTimeToUtc(DateTime.Now); DateTime utcStartTime = new DateTime(1970, 1, 1, 0, 0, 0, 0); - int timestamp = Convert.ToInt32((utcTime-utcStartTime).TotalSeconds); + var timestamp = Convert.ToInt32((utcTime - utcStartTime).TotalSeconds); return timestamp; } + + /// + /// 获取UTC时间戳 + /// + /// + public static int CurrentUTCTimeStamp() + { + var ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); + var times = Convert.ToInt32(ts.TotalSeconds); + return times; + } + /// /// 获取随机数 /// @@ -275,6 +390,7 @@ namespace WasteConsoleTest int nonce = random.Next(1, Int32.MaxValue); return nonce; } + /// /// 获取签名 /// @@ -306,6 +422,7 @@ namespace WasteConsoleTest sign = sign.ToLower(); return sign; } + /// /// 获取token /// @@ -321,6 +438,7 @@ namespace WasteConsoleTest return token; } } + /// /// 向服务端推送测试用垃圾采集数据 /// @@ -330,24 +448,29 @@ namespace WasteConsoleTest /// 垃圾称重数据,64位浮点进度,单位为千克 /// public double weight { get; set; } + /// /// 垃圾桶编码 /// public string trash { get; set; } + /// /// 垃圾类型,缺省类型 : 0,厨余垃圾 : 1,可回收物 : 2,有害垃圾 : 3,其他垃圾 : 4 /// public int type { get; set; } + /// /// 数据扫描时间,UNIX时间戳 /// - public int scanningTime { get; set; } + public long scanningTime { get; set; } + /// /// 设备状态,使用中 : 0:使用中,异常 : 1,检修 : 2,检修结束 : 3,启用 : 4,未知 : 5 /// public int d_status { get; set; } } + /// /// 响应的数据 /// @@ -357,18 +480,22 @@ namespace WasteConsoleTest /// 类型,6-心跳包,1-token,3-其他 /// public int type { get; set; } + /// /// 请求id /// public string invocationId { get; set; } + /// /// 结果 /// public string result { get; set; } + /// /// 目标 /// public string target { get; set; } + /// /// 参数 /// @@ -379,6 +506,7 @@ namespace WasteConsoleTest { Console.WriteLine($"已连接"); } + public static void SetTimeOut() { Task.Run(() => @@ -386,7 +514,7 @@ namespace WasteConsoleTest DateTime startime = DateTime.Now; while (true) { - if(wSocketClient.State == WebSocketState.Open) + if (wSocketClient.State == WebSocketState.Open) { if ((DateTime.Now - startime).TotalSeconds >= 15) { @@ -401,6 +529,7 @@ namespace WasteConsoleTest } }); } + private static byte[] RemoveSeparator(byte[] data) { List t = new List(data); @@ -408,4 +537,4 @@ namespace WasteConsoleTest return t.ToArray(); } } -} +} \ No newline at end of file diff --git a/WasteConsoleTest/WasteConsoleTest/WSocketClientHelp.cs b/WasteConsoleTest/WasteConsoleTest/WSocketClientHelp.cs index 1a1817b..19b4acc 100644 --- a/WasteConsoleTest/WasteConsoleTest/WSocketClientHelp.cs +++ b/WasteConsoleTest/WasteConsoleTest/WSocketClientHelp.cs @@ -13,9 +13,9 @@ namespace WasteConsoleTest private ClientWebSocket ws = null; private Uri uri = null; private bool isUserClose = false;//是否最后由用户手动关闭 - public static string Secret = "IICmWj5giYTWoCYd"; - public static string SecretHash = "6176c58a6569b3cf"; - public static string deviceid = "08d96cf1-d6b4-4e73-8356-02816912b3ba"; + public static string Secret = "VOVGXa8Rw7hwp4p5"; + public static string SecretHash = "wSQnkR1Eec4U5FPRA5aGDwpmH3+P6YtEK3yHYQ79r8w="; + public static string deviceid = "08da3552-46ee-41b5-88ee-ea3f0047b57f"; // /// WebSocket状态