You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
327 lines
13 KiB
327 lines
13 KiB
using System.Threading.Tasks;
|
|
using System.Threading;
|
|
using AIK.Common;
|
|
using AIK.Common.Interface;
|
|
using AIK.Common.SysCommon;
|
|
using AIK.Common.WebHelper;
|
|
using AIK.Models;
|
|
using AIK.Models.HttpPolicy;
|
|
using AIK.Service.IService;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Security.AccessControl;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Media.Imaging;
|
|
|
|
namespace AIK.Service.Service
|
|
{
|
|
public class CarfactoryService : ICarfactoryService
|
|
{
|
|
//private static string weburlTest = "https://keytest.aik518.com";
|
|
//public static string fileurl = "https://keytest.aik518.com/prod-api/work_file/file/file_download?objectName=";
|
|
//当前平台语言
|
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
private readonly IConfigurationService _configurationService;
|
|
private readonly ILogger<CarfactoryService> _logger;
|
|
private readonly IRetryPolicyService _retryPolicyService;
|
|
private readonly RetryPolicyConfig _retryConfig;
|
|
private readonly IHttpClientService _httpClientService;
|
|
private readonly ISettingsService _settingsService;
|
|
|
|
// 通过构造函数注入
|
|
public CarfactoryService(IHttpClientFactory httpClientFactory, ILogger<CarfactoryService> logger, IConfigurationService configurationService, IRetryPolicyService retryPolicyService, IHttpClientService httpClientService, ISettingsService settingsService)
|
|
{
|
|
_httpClientFactory = httpClientFactory;
|
|
_configurationService = configurationService;
|
|
_logger = logger;
|
|
_retryPolicyService = retryPolicyService;
|
|
_httpClientService = httpClientService;
|
|
_retryConfig = new RetryPolicyConfig
|
|
{
|
|
MaxRetryCount = 3,
|
|
RetryDelay = TimeSpan.FromSeconds(1),
|
|
DefaultTimeout = TimeSpan.FromSeconds(15)
|
|
};
|
|
_settingsService = settingsService;
|
|
|
|
}
|
|
#region 接口实现
|
|
|
|
|
|
private static string GetVhetype(string vhetype)
|
|
{
|
|
//后台没有对应接口暂时赋值car
|
|
if (vhetype == "yssc" || vhetype == "sjxf" || vhetype == "bcq")
|
|
{
|
|
return "ysjs";
|
|
}
|
|
if (vhetype == "car")
|
|
{
|
|
return string.Empty;
|
|
}
|
|
return string.Empty;
|
|
}
|
|
|
|
#region 异步方法(带重试)
|
|
/// <summary>
|
|
/// 获取车辆品牌数据
|
|
/// (车car,车库:barn 电动车:dd 摩托车:mt 后装:hz i2车库门:i2_barn i2电动车:i2_dd obd数据:obd ,原车钥匙解锁:ysjs)
|
|
/// </summary>
|
|
/// <param name="vhetype"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<CarfactoryClass>> GetCarfactoryClassListAsync(string vhetype)
|
|
{
|
|
return await _retryPolicyService.ExecuteWithRetryAsync(async () =>
|
|
{
|
|
try
|
|
{
|
|
vhetype = GetVhetype(vhetype);
|
|
string url = $"{_configurationService.ApiSettings.BaseUrl}{WebApiAddress.Vehicle_keyBrand_List}";
|
|
|
|
var requestData = new
|
|
{
|
|
brandType = vhetype,
|
|
language = _settingsService.Load().Language
|
|
};
|
|
|
|
string sendData = JsonConvert.SerializeObject(requestData);
|
|
|
|
// 使用带超时的HTTP请求
|
|
var response = await _httpClientService.PostJsonWithTimeoutAsync(
|
|
url, sendData,
|
|
TimeSpan.FromSeconds(15));
|
|
|
|
var jobj = JObject.Parse(response);
|
|
var status = jobj["code"]?.ToString();
|
|
|
|
if (status == "1")
|
|
{
|
|
var jsonstr = jobj["data"]?.ToString();
|
|
return JsonConvert.DeserializeObject<List<CarfactoryClass>>(jsonstr) ?? new List<CarfactoryClass>();
|
|
}
|
|
else
|
|
{
|
|
_logger.LogWarning("获取车辆品牌数据失败,状态码: {Status}, 响应: {Response}", status, jobj.ToString());
|
|
return new List<CarfactoryClass>();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "获取车辆品牌数据失败: {Vhetype}", vhetype);
|
|
throw; // 重新抛出,让重试机制处理
|
|
}
|
|
}, nameof(GetCarfactoryClassListAsync));
|
|
}
|
|
|
|
public async Task<List<VehModelClass>> GetVehKeyClassListAsync(int id)
|
|
{
|
|
return await _retryPolicyService.ExecuteWithRetryAsync(async () =>
|
|
{
|
|
try
|
|
{
|
|
List<VehModelClass> cflist = new List<VehModelClass>();
|
|
string url = $"{_configurationService.ApiSettings.BaseUrl}{WebApiAddress.Vehicle_keyBrand_Detail}/{id}?language={_settingsService.Load().Language}";
|
|
string response = await _httpClientService.GetStringWithTimeoutAsync(url, TimeSpan.FromSeconds(15));
|
|
var jobj = JObject.Parse(response);
|
|
var status = jobj["code"]?.ToString();
|
|
if (status == "1")
|
|
{
|
|
var dataObject = JObject.Parse(jobj["data"]?.ToString() ?? "{}");
|
|
var typeList = dataObject["typeList"]?.ToString();
|
|
return JsonConvert.DeserializeObject<List<VehModelClass>>(typeList) ?? new List<VehModelClass>();
|
|
}
|
|
else
|
|
{
|
|
_logger.LogWarning("获取车辆钥匙数据失败,状态码: {Status}, 响应: {Response}", status, jobj.ToString());
|
|
return new List<VehModelClass>();
|
|
}
|
|
}
|
|
catch (HttpRequestException ex)
|
|
{
|
|
throw new HttpRequestException("无网络连接,请检测网络连接情况");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError("执行方法: {MethodName},{Message}", nameof(GetVehKeyClassListAsync), ex.Message);
|
|
//未知异常默认返回空
|
|
return new List<VehModelClass>();
|
|
}
|
|
}, nameof(GetVehKeyClassListAsync));
|
|
}
|
|
#endregion
|
|
|
|
#region 同步方法
|
|
/// <summary>
|
|
/// 获取车辆品牌数据
|
|
/// (车car,车库:barn 电动车:dd 摩托车:mt 后装:hz i2车库门:i2_barn i2电动车:i2_dd obd数据:obd ,原车钥匙解锁:ysjs)
|
|
/// </summary>
|
|
/// <param name="vhetype"></param>
|
|
/// <returns></returns>
|
|
public List<CarfactoryClass> GetCarfactoryClassList(string vhetype)
|
|
{
|
|
try
|
|
{
|
|
vhetype = GetVhetype(vhetype);
|
|
List<CarfactoryClass> cflist = new List<CarfactoryClass>();
|
|
string url = string.Format("{0}/prod-api/work_key/vehicle_key/brand_list", _configurationService.ApiSettings.BaseUrl);
|
|
JObject patientinfo = new JObject();
|
|
patientinfo["brandType"] = vhetype;
|
|
patientinfo["language"] = _settingsService.Load().Language;
|
|
|
|
string sendData = JsonConvert.SerializeObject(patientinfo);
|
|
Newtonsoft.Json.Linq.JObject jobj = jobj = Newtonsoft.Json.Linq.JObject.Parse(WebHelper.PostJson(url, sendData));
|
|
var status = jobj["code"].ToString();
|
|
if (status == "1")
|
|
{
|
|
var jsonstr = jobj["data"].ToString();
|
|
cflist = JsonConvert.DeserializeObject<List<CarfactoryClass>>(jsonstr);
|
|
}
|
|
else
|
|
{
|
|
_logger.LogInformation($"第三方状态码:{status},数据: {jobj.ToString()}");
|
|
}
|
|
return cflist;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError("执行方法: {MethodName},{Message}", nameof(GetVehKeyImg), ex.Message);
|
|
return new List<CarfactoryClass>();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 获取车辆钥匙数据
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public List<VehModelClass> GetVehKeyClassList(int id)
|
|
{
|
|
try
|
|
{
|
|
List<VehModelClass> cflist = new List<VehModelClass>();
|
|
string url = string.Format("{0}/prod-api/work_key/vehicle_key/brand_detail/{1}?language={2}", _configurationService.ApiSettings.BaseUrl, id, _settingsService.Load().Language);
|
|
Newtonsoft.Json.Linq.JObject jobj = Newtonsoft.Json.Linq.JObject.Parse(WebHelper.HttpWebRequest(url, null, 10 * 1000, false));
|
|
var status = jobj["code"].ToString();
|
|
if (status == "1")
|
|
{
|
|
var jsonstr = Newtonsoft.Json.Linq.JObject.Parse(jobj["data"].ToString());
|
|
cflist = JsonConvert.DeserializeObject<List<VehModelClass>>(jsonstr["typeList"].ToString());
|
|
}
|
|
else
|
|
{
|
|
_logger.LogInformation($"第三方状态码:{status},数据: {jobj.ToString()}");
|
|
}
|
|
return cflist;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError("执行方法: {MethodName},{Message}", nameof(GetVehKeyClassList), ex.Message);
|
|
return new List<VehModelClass>();
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 获取K3TOOL和K3Genie固件版本数据 copying(k3mini),i2(k3tool),k3_jingling
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Obsolete]
|
|
public List<VersionClass> GetVersionClass(string type)
|
|
{
|
|
try
|
|
{
|
|
List<VersionClass> cflist = new List<VersionClass>();
|
|
string url = string.Format("{0}/prod-api/work_key/app/more_new?type={1}", _configurationService.ApiSettings.BaseUrl, type);
|
|
Newtonsoft.Json.Linq.JObject jobj = Newtonsoft.Json.Linq.JObject.Parse(WebHelper.HttpWebRequest(url, null, 10 * 1000, false));
|
|
var status = jobj["code"].ToString();
|
|
if (status == "1")
|
|
{
|
|
cflist = JsonConvert.DeserializeObject<List<VersionClass>>(jobj["data"].ToString());
|
|
}
|
|
return cflist;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError("执行方法: {MethodName},{Message}", nameof(GetVersionClass), ex.Message);
|
|
return new List<VersionClass>();
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 获取车钥匙图片和Bin文件
|
|
|
|
/// <summary>
|
|
/// 获取车钥匙图片(WPF)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Obsolete]
|
|
public async Task<BitmapImage> GetVehKeyImg(string url)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(url))
|
|
return null;
|
|
|
|
if (GlobalImageCache.TryGetValue(url, out var cachedImage))
|
|
return cachedImage;
|
|
|
|
try
|
|
{
|
|
using var httpClient = _httpClientFactory.CreateClient();
|
|
using var response = await httpClient.GetAsync($"{_configurationService.ApiSettings.FileUrl}{url}");
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
using var stream = await response.Content.ReadAsStreamAsync();
|
|
var image = ImageUtils.StreamToBitmapImage(stream);
|
|
|
|
if (image is not null)
|
|
GlobalImageCache.GetOrAdd(url, _ => image);
|
|
|
|
return image;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError("执行方法: {MethodName},{Message}", nameof(GetVehKeyImg), ex.Message);
|
|
return null;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 获取车钥匙升级文件
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Obsolete]
|
|
public async Task<byte[]> GetVehKeyBin(string url)
|
|
{
|
|
try
|
|
{
|
|
using (var client = new HttpClient())
|
|
{
|
|
using (var content = new MultipartFormDataContent())
|
|
{
|
|
HttpResponseMessage response = await client.GetAsync(_configurationService.ApiSettings.FileUrl + url);
|
|
response.EnsureSuccessStatusCode();
|
|
return await response.Content.ReadAsByteArrayAsync();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError("执行方法: {MethodName},{Message}", nameof(GetVehKeyBin), ex.Message);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
}
|
|
}
|
|
|