【发布时间】:2020-08-20 23:49:56
【问题描述】:
我有以下函数允许我通过这个函数发出 REST 请求:
static Observation GetData(string baseUrl, string provider, string key, string sensor)
{
var client = new RestClient(String.Format("{0}/data/{1}/{2}", baseUrl, provider, sensor));
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("IDENTITY_KEY", key);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
var observationsModel = JsonSerializer.Deserialize<SentiloResponse>(response.Content, options);
Console.WriteLine("This is observationsModel:");
Console.WriteLine(observationsModel);
Console.WriteLine("This is the outcome of GetData:");
Console.WriteLine(observationsModel.Observations.FirstOrDefault());
return observationsModel.Observations.FirstOrDefault();
}
现在,从这里开始
在下一部分我调用 GetData,我想选择值和时间戳变量来执行其他任务,但我无法(我的尝试在声明 myTest 和 myTest_2 的行中):
static void Main(string[] args)
{
configuration = JsonSerializer.Deserialize<SentiloConfig>(File.ReadAllText("configuration.json"), options);
Console.WriteLine("Configuration Loaded");
foreach(var componentMapping in configuration.ComponentMappings)
{
var inputComponent = componentMapping.InputComponent;
Console.WriteLine("Getting data");
var sensorsContent = inputComponent.Sensors.AsParallel().Select(s =>
new SensorContent{
Sensor = GetSensorString(s.SensorName),
Observations = new List<Observation>() {
GetData(
inputComponent.ServerUrl,
inputComponent.Provider,
inputComponent.Token,
GetSensorString(s.SensorName)
)
}
}
).Where(s => s.Observations.First() != null).ToList();
var myTest = sensorsContent.Select(s => s.Observations.First().Value).ToList();
var myTest_2 = sensorsContent.Select(s => s.Observations.First().Value.ToList());
Console.WriteLine(myTest.ToString());
Console.WriteLine(myTest_2);
Console.WriteLine("data retrieved");
if (!sensorsContent.Any())
{
Console.WriteLine("No sensor data for component {0} with sensors: {1}, check configuration", inputComponent.ComponentName, string.Join(",", inputComponent.Sensors.Select(s => s.SensorName)));
continue;
}
var sensorRequest = new SentiloPutRequest { Sensors = sensorsContent };
}
System.Threading.Thread.Sleep(configuration.SendTime*1000);
}
但 myTest 和 myTest_2 的结果如下:
System.Collections.Generic.List
1[System.String] System.Linq.Enumerable+SelectListIterator2[SentiloSpoofer.SensorContent,System.Collections.Generic.List`1[System.Char]]
我在想也许最好在 Lambda 之外调用 GetData 并绕过这些问题,但如果我想检索这些数据,有什么好的方法呢?
【问题讨论】: