我做了一个函数,它从数据库(CouchDB)中获取新到达的数据,然后使用这些数据进行分析。一切都很好,但现在我正在努力如何在没有更多数据的情况下保持函数的活动。
我现在得到的是:
public async Task<FsResponseModel.Root> AnalyzeImage()
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes($"user:pw" + $"")));
HttpResponseMessage httpResponse = await client.GetAsync("CouchDBUrl/_changes?feed=continuous&filter=_view&view=list/images&include_docs=true&attachments=true");
httpResponse.EnsureSuccessStatusCode();
string json = await httpResponse.Content.ReadAsStringAsync();
//Somewhere to keep the parsed results
var results = new List<ViewResponseModel>();
Root root = null;
var serializer = new JsonSerializer();
using (var stringReader = new StringReader(json))
using (var jsonReader = new JsonTextReader(stringReader))
{
//Make sure the JsonReader knows we have multiple documents
jsonReader.SupportMultipleContent = true;
while (jsonReader.Read())
{
//Read in the next document
var nextObject = JObject.ReadFrom(jsonReader);
//Determine if we are on the last item or not
if (nextObject["last_seq"] != null)
{
root = nextObject.ToObject<Root>();
}
else
{
results.Add(nextObject.ToObject<ViewResponseModel>());
}
}
}
if (results.Count >= 0)
{
while (results.Count >= 0)
{
string base64Image = results[0].doc._attachments.image.data;
string refNr = results[0].doc.creator;
string token = "myToken";
var httpWebRequest = (HttpWebRequest)WebRequest.Create("MyApi");
httpWebRequest.PreAuthenticate = true;
httpWebRequest.Headers.Add("Authorization", "Bearer " + token);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Accept = "application/json";
httpWebRequest.Method = "POST";
var obj = new FsRequestModel() { };
obj.Image = base64Image;
obj.Application = "MyApp";
obj.RefNr = "hzd." + refNr + results[0].doc._id;
string imageJson = JsonConvert.SerializeObject(obj);
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(imageJson);
}
var httpResponseApi = (HttpWebResponse)httpWebRequest.GetResponse();
var result = "";
using (var streamReader = new StreamReader(httpResponseApi.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
FsResponseModel.Root response = JsonConvert.DeserializeObject<FsResponseModel.Root>(result);
return response;
}
}
else
{
//w8 for new data
return null;
}
return null;
}
我需要一个解决方案来告诉代码它必须等待新数据,如果新数据可用,则重新开始该过程。
编辑:
我现在正在尝试处理传入的提要。有什么想法如何消费这个?
public async Task ObserveDbAndTrigger()
{
var url = "http://localhost:5984/mydb/_changes?feed=continuous&filter=_view&view=MyView&include_docs=true&attachments=true&heartbeat=1000";
using (var client = new HttpClient())
{
client.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes($"user:pw" + $"")));
string line = "";
var request = new HttpRequestMessage(HttpMethod.Get, url);
using (var response = await client.SendAsync(
request,
HttpCompletionOption.ResponseHeadersRead))
{
using (var body = await response.Content.ReadAsStreamAsync())
using (var reader = new StreamReader(body))
while (!reader.EndOfStream)
line = reader.ReadLine();
if (line != "")
{
//get data and work with it
}
}
}
}
我有流,但我不知道如何处理传入的线路。任何帮助将是伟大的。
编辑2:
流不会工作,我已经测试过了,它不承认新的行...
我的下一个想法是用eventsource来代替continuous feed。
对此有何建议?
1条答案
按热度按时间gdx19jrr1#
我把它改成这样,现在我可以处理数据了。