模拟测试修改
This commit is contained in:
parent
f4ec11a634
commit
a1dbd33400
|
|
@ -1,11 +1,8 @@
|
|||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分页查询当前账号下存在的地产区域数据
|
||||
/// </summary>
|
||||
|
|
@ -37,7 +36,6 @@ namespace Waste.Application
|
|||
/// <returns></returns>
|
||||
public async Task<ResultInfo> 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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询地产区域信息
|
||||
/// </summary>
|
||||
|
|
@ -73,6 +72,7 @@ namespace Waste.Application
|
|||
return new ResultInfo(ResultState.FAIL, msg);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hello接口GET测试
|
||||
/// </summary>
|
||||
|
|
@ -94,6 +94,7 @@ namespace Waste.Application
|
|||
}
|
||||
return new ResultInfo(ResultState.SUCCESS, "success", returndata);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加地产区域信息
|
||||
/// </summary>
|
||||
|
|
@ -118,6 +119,7 @@ namespace Waste.Application
|
|||
return new ResultInfo(ResultState.FAIL, msg);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向服务端推送测试用垃圾采集数据
|
||||
/// </summary>
|
||||
|
|
@ -159,6 +161,7 @@ namespace Waste.Application
|
|||
return new ResultInfo(ResultState.FAIL, msg);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hello接口Post测试
|
||||
/// </summary>
|
||||
|
|
@ -185,6 +188,7 @@ namespace Waste.Application
|
|||
}
|
||||
return new ResultInfo(ResultState.SUCCESS, "success", returndata);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取时间戳
|
||||
/// </summary>
|
||||
|
|
@ -207,6 +211,7 @@ namespace Waste.Application
|
|||
int timestamp = Convert.ToInt32((utcTime - utcStartTime).TotalSeconds);
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取随机数
|
||||
/// </summary>
|
||||
|
|
@ -217,6 +222,7 @@ namespace Waste.Application
|
|||
int nonce = random.Next(1, Int32.MaxValue);
|
||||
return nonce;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取签名
|
||||
/// </summary>
|
||||
|
|
@ -245,6 +251,7 @@ namespace Waste.Application
|
|||
string sign = Md5.md5(str, 16).ToLower();
|
||||
return sign;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// POST封装接口
|
||||
/// </summary>
|
||||
|
|
@ -285,6 +292,7 @@ namespace Waste.Application
|
|||
var testdata = await response.Content.ReadAsStringAsync();
|
||||
return response;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// GET接口封装
|
||||
/// </summary>
|
||||
|
|
@ -316,6 +324,7 @@ namespace Waste.Application
|
|||
var testdata = await response.Content.ReadAsStringAsync();
|
||||
return response;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delete接口封装
|
||||
/// </summary>
|
||||
|
|
@ -347,6 +356,7 @@ namespace Waste.Application
|
|||
var testdata = await response.Content.ReadAsStringAsync();
|
||||
return response;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除地产区域
|
||||
/// </summary>
|
||||
|
|
@ -367,6 +377,7 @@ namespace Waste.Application
|
|||
return new ResultInfo(ResultState.FAIL, msg);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除采集点
|
||||
/// </summary>
|
||||
|
|
@ -386,6 +397,7 @@ namespace Waste.Application
|
|||
return new ResultInfo(ResultState.FAIL, resdata);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加采集点
|
||||
/// </summary>
|
||||
|
|
@ -410,6 +422,7 @@ namespace Waste.Application
|
|||
return new ResultInfo(ResultState.FAIL, msg);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 上传数据
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -14,11 +14,49 @@ using System.Threading.Tasks;
|
|||
|
||||
namespace WasteConsoleTest
|
||||
{
|
||||
class Program
|
||||
/// <summary>
|
||||
/// 向服务端推送测试用垃圾采集数据
|
||||
/// </summary>
|
||||
[MessagePackObject]
|
||||
public class MessageGarbageC2SDto //客户端向服务端请求的DTO
|
||||
{
|
||||
/// <summary>
|
||||
/// 垃圾称重数据,64位浮点进度,单位为千克
|
||||
/// </summary>
|
||||
[Key("weight")]
|
||||
public double Weight { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 垃圾桶编码
|
||||
/// </summary>
|
||||
[Key("trash")]
|
||||
public string Trash { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 垃圾类型,缺省类型 : 0,厨余垃圾 : 1,可回收物 : 2,有害垃圾 : 3,其他垃圾 : 4
|
||||
/// </summary>
|
||||
[Key("type")]
|
||||
public int Type { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 数据扫描时间,UNIX时间戳
|
||||
/// </summary>
|
||||
[Key("scanningTime")]
|
||||
public long ScanningTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 设备状态,使用中 : 0:使用中,异常 : 1,检修 : 2,检修结束 : 3,启用 : 4,未知 : 5
|
||||
/// </summary>
|
||||
[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) =>
|
||||
{
|
||||
|
|
@ -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}");
|
||||
|
|
@ -137,9 +181,7 @@ namespace WasteConsoleTest
|
|||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public interface IMyService
|
||||
|
|
@ -148,11 +190,18 @@ namespace WasteConsoleTest
|
|||
/// 测试token有效期
|
||||
/// </summary>
|
||||
Task TestTokenAsync();
|
||||
|
||||
/// <summary>
|
||||
/// 传送垃圾数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Task Garbages();
|
||||
|
||||
/// <summary>
|
||||
/// Messagepack传送垃圾数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Task MessageGarbages();
|
||||
}
|
||||
|
||||
public class MyService : IMyService
|
||||
|
|
@ -163,6 +212,7 @@ namespace WasteConsoleTest
|
|||
{
|
||||
_clientFactory = clientFactory;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 传送垃圾数据
|
||||
/// </summary>
|
||||
|
|
@ -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)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// MessagePack传送垃圾数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
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未找到");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取时间戳
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取UTC时间戳
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取随机数
|
||||
/// </summary>
|
||||
|
|
@ -275,6 +390,7 @@ namespace WasteConsoleTest
|
|||
int nonce = random.Next(1, Int32.MaxValue);
|
||||
return nonce;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取签名
|
||||
/// </summary>
|
||||
|
|
@ -306,6 +422,7 @@ namespace WasteConsoleTest
|
|||
sign = sign.ToLower();
|
||||
return sign;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取token
|
||||
/// </summary>
|
||||
|
|
@ -321,6 +438,7 @@ namespace WasteConsoleTest
|
|||
return token;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向服务端推送测试用垃圾采集数据
|
||||
/// </summary>
|
||||
|
|
@ -330,24 +448,29 @@ namespace WasteConsoleTest
|
|||
/// 垃圾称重数据,64位浮点进度,单位为千克
|
||||
/// </summary>
|
||||
public double weight { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 垃圾桶编码
|
||||
/// </summary>
|
||||
public string trash { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 垃圾类型,缺省类型 : 0,厨余垃圾 : 1,可回收物 : 2,有害垃圾 : 3,其他垃圾 : 4
|
||||
/// </summary>
|
||||
|
||||
public int type { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 数据扫描时间,UNIX时间戳
|
||||
/// </summary>
|
||||
public int scanningTime { get; set; }
|
||||
public long scanningTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 设备状态,使用中 : 0:使用中,异常 : 1,检修 : 2,检修结束 : 3,启用 : 4,未知 : 5
|
||||
/// </summary>
|
||||
public int d_status { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 响应的数据
|
||||
/// </summary>
|
||||
|
|
@ -357,18 +480,22 @@ namespace WasteConsoleTest
|
|||
/// 类型,6-心跳包,1-token,3-其他
|
||||
/// </summary>
|
||||
public int type { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 请求id
|
||||
/// </summary>
|
||||
public string invocationId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 结果
|
||||
/// </summary>
|
||||
public string result { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 目标
|
||||
/// </summary>
|
||||
public string target { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 参数
|
||||
/// </summary>
|
||||
|
|
@ -379,6 +506,7 @@ namespace WasteConsoleTest
|
|||
{
|
||||
Console.WriteLine($"已连接");
|
||||
}
|
||||
|
||||
public static void SetTimeOut()
|
||||
{
|
||||
Task.Run(() =>
|
||||
|
|
@ -401,6 +529,7 @@ namespace WasteConsoleTest
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static byte[] RemoveSeparator(byte[] data)
|
||||
{
|
||||
List<byte> t = new List<byte>(data);
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
||||
// <summary>
|
||||
/// WebSocket状态
|
||||
|
|
|
|||
Loading…
Reference in New Issue