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.
88 lines
2.6 KiB
88 lines
2.6 KiB
using Newtonsoft.Json; |
|
using System; |
|
using System.Net.Http; |
|
using System.Text; |
|
using System.Threading.Tasks; |
|
|
|
namespace SparkClient.Model.Services |
|
{ |
|
public class SOCClientService |
|
{ |
|
private readonly string _baseUrl; |
|
private readonly string _authToken; |
|
|
|
public SOCClientService(string baseUrl, string authToken) |
|
{ |
|
_baseUrl = baseUrl; |
|
_authToken = authToken; |
|
} |
|
|
|
// 通用GET请求方法 |
|
private async Task<HttpResponseMessage> SendGetRequestAsync(string url) |
|
{ |
|
using (var client = new HttpClient()) |
|
{ |
|
client.DefaultRequestHeaders.Add("Authorization", "Basic " + _authToken); |
|
return await client.GetAsync(url); |
|
} |
|
} |
|
|
|
// 1. 启动图片收集任务 |
|
public async Task<string> CollectImagesAsync(int lightLevel) |
|
{ |
|
string url = $"{_baseUrl}/collect_images?light_level={lightLevel}"; |
|
var response = await SendGetRequestAsync(url); |
|
|
|
if (response.IsSuccessStatusCode) |
|
{ |
|
var jsonResponse = await response.Content.ReadAsStringAsync(); |
|
var result = JsonConvert.DeserializeObject<ResponseStatus>(jsonResponse); |
|
return result.Status; |
|
} |
|
else |
|
{ |
|
return "Error: " + response.StatusCode; |
|
} |
|
} |
|
|
|
// 2. 获取图片 |
|
public async Task<byte[]> RetrieveImageAsync(int index) |
|
{ |
|
string url = $"{_baseUrl}/retrieve_image/{index}"; |
|
var response = await SendGetRequestAsync(url); |
|
|
|
if (response.IsSuccessStatusCode) |
|
{ |
|
return await response.Content.ReadAsByteArrayAsync(); |
|
} |
|
else |
|
{ |
|
throw new Exception("Error retrieving image: " + response.StatusCode); |
|
} |
|
} |
|
|
|
// 3. 获取采集状态 |
|
public async Task<string> CollectStatusAsync() |
|
{ |
|
string url = $"{_baseUrl}/collect_status"; |
|
var response = await SendGetRequestAsync(url); |
|
|
|
if (response.IsSuccessStatusCode) |
|
{ |
|
var jsonResponse = await response.Content.ReadAsStringAsync(); |
|
var result = JsonConvert.DeserializeObject<ResponseStatus>(jsonResponse); |
|
return result.Status; |
|
} |
|
else |
|
{ |
|
return "Error: " + response.StatusCode; |
|
} |
|
} |
|
} |
|
|
|
public class ResponseStatus |
|
{ |
|
public string Status { get; set; } |
|
public string Message { get; set; } |
|
} |
|
}
|
|
|