|
|
|
|
@ -11,6 +11,7 @@ using HandyControl.Tools.Extension; |
|
|
|
|
using log4net; |
|
|
|
|
using SparkClient.Model.Common; |
|
|
|
|
using SparkClient.Model.Entity.ApiEntity; |
|
|
|
|
using SparkClient.Model.Extension; |
|
|
|
|
using SparkClient.Model.Helper; |
|
|
|
|
|
|
|
|
|
namespace SparkClient.Model.Services |
|
|
|
|
@ -62,7 +63,7 @@ namespace SparkClient.Model.Services |
|
|
|
|
/// </summary> |
|
|
|
|
/// <param name="url">请求的完整URL</param> |
|
|
|
|
/// <returns>HTTP响应</returns> |
|
|
|
|
private async Task<HttpResponseMessage> SendGetRequestAsync(string url) |
|
|
|
|
private async Task<HttpSendResult> SendGetRequestAsync(string url) |
|
|
|
|
{ |
|
|
|
|
using (var client = new HttpClient()) |
|
|
|
|
{ |
|
|
|
|
@ -80,20 +81,23 @@ namespace SparkClient.Model.Services |
|
|
|
|
if (StatusCodes.PreviousTaskIncomplete.Equals(tempEntity.Status)) |
|
|
|
|
{ |
|
|
|
|
Logger.Info($"S009 请求重试"); |
|
|
|
|
await Task.Delay(50); |
|
|
|
|
await Task.Delay(500); |
|
|
|
|
return await SendGetRequestAsync(url); |
|
|
|
|
} |
|
|
|
|
result.Content = new StringContent(responseBody); |
|
|
|
|
return result; |
|
|
|
|
|
|
|
|
|
return new HttpSendResult() |
|
|
|
|
{ |
|
|
|
|
StatusCode = statusCode, |
|
|
|
|
Content = tempEntity, |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
private async Task<HttpResponseMessage> SendGetRequestImageAsync(string url) |
|
|
|
|
{ |
|
|
|
|
using (var client = new HttpClient()) |
|
|
|
|
{ |
|
|
|
|
Logger.Info($"Request sent to URL: {url}"); |
|
|
|
|
Logger.Info($"Request Image Download URL: {url}"); |
|
|
|
|
client.DefaultRequestHeaders.Add("Authorization", "Basic " + _authToken); |
|
|
|
|
|
|
|
|
|
return await client.GetAsync(url); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
@ -139,13 +143,12 @@ namespace SparkClient.Model.Services |
|
|
|
|
|
|
|
|
|
var response = await SendGetRequestAsync(url); |
|
|
|
|
|
|
|
|
|
if (!response.IsSuccessStatusCode) |
|
|
|
|
if (response.StatusCode != 200) |
|
|
|
|
{ |
|
|
|
|
return new SocResultEntity { Status = StatusCodes.DeviceNotFound, Images = new List<string>() }; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var jsonResponse = await response.Content.ReadAsStringAsync(); |
|
|
|
|
var result = JsonConvert.DeserializeObject<ResponseStatus>(jsonResponse); |
|
|
|
|
var result = response.Content; |
|
|
|
|
|
|
|
|
|
if (result == null) |
|
|
|
|
{ |
|
|
|
|
@ -171,13 +174,71 @@ namespace SparkClient.Model.Services |
|
|
|
|
GenImage = false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
/// <summary> |
|
|
|
|
/// 启动图片收集任务。 |
|
|
|
|
/// </summary> |
|
|
|
|
/// <returns>任务状态</returns> |
|
|
|
|
public Task<HttpSendResult> CollectImagesAsyncNotAwait() |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
GenImage = true; |
|
|
|
|
// 光照度和半圆 |
|
|
|
|
int lightLevel = 0; |
|
|
|
|
string halfCircle = string.Empty; |
|
|
|
|
// 查询光照度和半圆配置 |
|
|
|
|
string sql = $"SELECT KEY, VALUE FROM CUTTER_CONFIG WHERE KEY IN ('light_level', 'half_circle')"; |
|
|
|
|
DataTable table = DataBaseHelper.ExecuteQuery(sql); |
|
|
|
|
|
|
|
|
|
if (table == null || table.Rows.Count == 0) |
|
|
|
|
{ |
|
|
|
|
throw new Exception("No data found for the specified keys."); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
foreach (DataRow row in table.Rows) |
|
|
|
|
{ |
|
|
|
|
string key = row["Key"].ToString() ?? string.Empty; |
|
|
|
|
string value = row["Value"].ToString() ?? string.Empty; |
|
|
|
|
|
|
|
|
|
if (key == "light_level" && int.TryParse(value, out int parsedLightLevel)) |
|
|
|
|
{ |
|
|
|
|
lightLevel = parsedLightLevel; // 光照度 |
|
|
|
|
} |
|
|
|
|
else if (key == "half_circle") |
|
|
|
|
{ |
|
|
|
|
halfCircle = value; // 半圆 |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
string url = $"{_baseUrl}/collect_images?light_level={lightLevel}&half_circle={halfCircle}"; |
|
|
|
|
|
|
|
|
|
var result = SendGetRequestAsync(url); |
|
|
|
|
|
|
|
|
|
return result; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
// 记录日志或进行其他处理 |
|
|
|
|
Console.WriteLine($"Error in DoSoc: {ex.Message}"); |
|
|
|
|
Logger.Warn($"Error in DoSoc: {ex.Message}"); |
|
|
|
|
// 或者使用日志框架记录日志 |
|
|
|
|
// logger.LogError(ex, "Error in DoSoc method."); |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
finally |
|
|
|
|
{ |
|
|
|
|
GenImage = false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// 获取指定索引的图片。 |
|
|
|
|
/// </summary> |
|
|
|
|
/// <param name="savePath">保存图片路径</param> |
|
|
|
|
/// <returns>图片的字节数组</returns> |
|
|
|
|
public async Task<List<string>> RetrieveImageAsync(string? savePath) |
|
|
|
|
public async Task<List<string>> RetrieveImageAsync(string? savePath, CancellationToken token = default) |
|
|
|
|
{ |
|
|
|
|
List<string> imageNames = new List<string>(); |
|
|
|
|
// 读取图片接口 |
|
|
|
|
@ -189,7 +250,7 @@ namespace SparkClient.Model.Services |
|
|
|
|
{ |
|
|
|
|
var response = await SendGetRequestImageAsync(url); |
|
|
|
|
int status = (int)response.StatusCode; |
|
|
|
|
|
|
|
|
|
token.ThrowIfCancellationRequested(); |
|
|
|
|
switch (status) |
|
|
|
|
{ |
|
|
|
|
case 200: |
|
|
|
|
@ -208,7 +269,7 @@ namespace SparkClient.Model.Services |
|
|
|
|
break; |
|
|
|
|
case 425: |
|
|
|
|
// 请求被锁定,等待一段时间后重试 |
|
|
|
|
await Task.Delay(1000); |
|
|
|
|
await Task.Delay(500); |
|
|
|
|
break; |
|
|
|
|
case 410: |
|
|
|
|
// 资源已被永久删除,跳过当前索引 |
|
|
|
|
@ -252,11 +313,11 @@ namespace SparkClient.Model.Services |
|
|
|
|
{ |
|
|
|
|
var response = await SendGetRequestAsync(url); |
|
|
|
|
Logger.Debug($" CollectStatusAsync url :{ url} "); |
|
|
|
|
if (response.IsSuccessStatusCode) |
|
|
|
|
if (response.StatusCode == 200) |
|
|
|
|
{ |
|
|
|
|
var jsonResponse = await response.Content.ReadAsStringAsync(); |
|
|
|
|
var result = JsonConvert.DeserializeObject<ResponseStatus>(jsonResponse); |
|
|
|
|
Logger.Debug($" CollectStatusAsync result :{ result} "); |
|
|
|
|
|
|
|
|
|
ResponseStatus result = response.Content; |
|
|
|
|
Logger.Debug($" CollectStatusAsync result :{ result.ToSafeAbundantString()} "); |
|
|
|
|
return result.Status; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -292,18 +353,49 @@ namespace SparkClient.Model.Services |
|
|
|
|
Directory.Delete(savePath, true); |
|
|
|
|
} |
|
|
|
|
Directory.CreateDirectory(savePath); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 启动任务接口 |
|
|
|
|
SocResultEntity entity = await CollectImagesAsync(); |
|
|
|
|
// SocResultEntity entity = await CollectImagesAsync(); |
|
|
|
|
// 成功 |
|
|
|
|
Logger.Debug($"entity :{entity.Status} {entity.ToString()} "); |
|
|
|
|
if (entity.Status != StatusCodes.Success) |
|
|
|
|
// Logger.Debug($"entity :{entity.Status} {entity.ToString()} "); |
|
|
|
|
// if (entity.Status != StatusCodes.Success) |
|
|
|
|
// { |
|
|
|
|
// // 启动任务失败 |
|
|
|
|
// return new SocResultEntity { Status = entity.Status, Images = new List<string>() }; |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
// 读取图片接口 |
|
|
|
|
// List<string> imageNames = await RetrieveImageAsync(savePath); |
|
|
|
|
var cts = new CancellationTokenSource(); |
|
|
|
|
Task<HttpSendResult> startImage = CollectImagesAsyncNotAwait(); |
|
|
|
|
Task<List<string>> downloadImage = RetrieveImageAsync(savePath, cts.Token); |
|
|
|
|
await startImage; |
|
|
|
|
var entity = startImage.Result; |
|
|
|
|
if (entity.StatusCode != 200) |
|
|
|
|
{ |
|
|
|
|
//downloadImage怎么取消并终止 |
|
|
|
|
cts.Cancel(); |
|
|
|
|
return new SocResultEntity { Status = StatusCodes.DeviceNotFound, Images = new List<string>() }; |
|
|
|
|
} |
|
|
|
|
if (entity.Content == null) |
|
|
|
|
{ |
|
|
|
|
// 启动任务失败 |
|
|
|
|
return new SocResultEntity { Status = entity.Status, Images = new List<string>() }; |
|
|
|
|
//downloadImage怎么取消并终止 |
|
|
|
|
cts.Cancel(); |
|
|
|
|
return new SocResultEntity { Status = StatusCodes.DeviceNotFound, Images = new List<string>() }; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
List<string> imageNames = new List<string>(); |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
await downloadImage; |
|
|
|
|
imageNames = downloadImage.Result; |
|
|
|
|
} |
|
|
|
|
catch (Exception e) |
|
|
|
|
{ |
|
|
|
|
Logger.Error($"Error in Await downloadImage: {e.Message}"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 读取图片接口 |
|
|
|
|
List<string> imageNames = await RetrieveImageAsync(savePath); |
|
|
|
|
// 采集状态接口 |
|
|
|
|
string acquisitionStatus = await CollectStatusAsync(); |
|
|
|
|
// 成功 |
|
|
|
|
@ -319,7 +411,7 @@ namespace SparkClient.Model.Services |
|
|
|
|
// 图片文件读取失败 |
|
|
|
|
return new SocResultEntity { Status = StatusCodes.ImageFileReadFailure, Images = new List<string>() }; |
|
|
|
|
} |
|
|
|
|
return new SocResultEntity { Status = acquisitionStatus, Images = imageNames, DeviceId = entity.DeviceId}; |
|
|
|
|
return new SocResultEntity { Status = acquisitionStatus, Images = imageNames, DeviceId = entity.Content.device_id}; |
|
|
|
|
} |
|
|
|
|
catch (Exception e) |
|
|
|
|
{ |
|
|
|
|
@ -363,11 +455,10 @@ namespace SparkClient.Model.Services |
|
|
|
|
{ |
|
|
|
|
var response = await SendGetRequestAsync(url); |
|
|
|
|
Logger.Debug(url); |
|
|
|
|
if (response.IsSuccessStatusCode) |
|
|
|
|
if (response.StatusCode == 200) |
|
|
|
|
{ |
|
|
|
|
var jsonResponse = await response.Content.ReadAsStringAsync(); |
|
|
|
|
var result = JsonConvert.DeserializeObject<ResponseStatus>(jsonResponse); |
|
|
|
|
Logger.Debug(result); |
|
|
|
|
var result = response.Content; |
|
|
|
|
Logger.Debug($"Rotate Angle : {result.ToSafeAbundantString()} "); |
|
|
|
|
return result.Status; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -397,11 +488,10 @@ namespace SparkClient.Model.Services |
|
|
|
|
{ |
|
|
|
|
var response = await SendGetRequestAsync(url); |
|
|
|
|
|
|
|
|
|
if (response.IsSuccessStatusCode) |
|
|
|
|
if (response.StatusCode == 200) |
|
|
|
|
{ |
|
|
|
|
var jsonResponse = await response.Content.ReadAsStringAsync(); |
|
|
|
|
var result = JsonConvert.DeserializeObject<ResponseStatus>(jsonResponse); |
|
|
|
|
Logger.Debug(result.Status); |
|
|
|
|
var result = response.Content; |
|
|
|
|
Logger.Debug($"Set Pump : {result.ToSafeAbundantString()} "); |
|
|
|
|
return result.Status; |
|
|
|
|
} |
|
|
|
|
Logger.Info($"气泵开关请求发起完毕"); |
|
|
|
|
|