在C#控制台应用程序中,每隔10秒从api更新恒温器结果

在C#控制台应用程序中,每隔10秒从api更新恒温器结果,c#,json,api,console-application,C#,Json,Api,Console Application,我需要每10秒从thingspeak.io API更新恒温器温度的帮助。我从thingspeak频道获取JSON数据,然后将其转换并显示在控制台中 这是我目前的代码 string url = "http://api.thingspeak.com/channels/135/feed.json"; WebClient webClient = new WebClient(); var data = webClient.DownloadString(url); dynamic feed = JsonC

我需要每10秒从thingspeak.io API更新恒温器温度的帮助。我从thingspeak频道获取JSON数据,然后将其转换并显示在控制台中

这是我目前的代码

string url = "http://api.thingspeak.com/channels/135/feed.json";

WebClient webClient = new WebClient();
var data = webClient.DownloadString(url);
dynamic feed = JsonConvert.DeserializeObject<dynamic>(data);
List<dynamic> feeds = feed.feeds.ToObject<List<dynamic>>();
string field1 = feeds.Last().field1;
float temperature = float.Parse(field1, CultureInfo.InvariantCulture);

Console.WriteLine("----------CURRENT CHANNEL----------");
Console.WriteLine("\n");
Console.WriteLine("Channel name: " + feed.channel.name);
Console.WriteLine("Temperature: " + temperature.ToString() + " °C");
Console.WriteLine("\n");

int trenutna_temp = Convert.ToInt32(temperature);

Console.WriteLine("----------DEVICES----------");

if (trenutna_temp < 10)
{
    Console.WriteLine("turn on heating);
}
else if (trenutna_temp > 10 && trenutna_temp < 20)
{
    Console.WriteLine("turn off");
}
else if (trenutna_temp > 20)
{
    Console.WriteLine("Turn on cooling");
}
Console.ReadLine();
stringurl=”http://api.thingspeak.com/channels/135/feed.json";
WebClient WebClient=新的WebClient();
var data=webClient.DownloadString(url);
动态提要=JsonConvert.DeserializeObject(数据);
List feed=feed.feed.ToObject();
字符串field1=feeds.Last().field1;
浮点温度=float.Parse(field1,CultureInfo.InvariantCulture);
Console.WriteLine(“------------当前频道-----------------”;
Console.WriteLine(“\n”);
Console.WriteLine(“频道名称:”+feed.Channel.name);
Console.WriteLine(“温度:+Temperature.ToString()+”°C”);
Console.WriteLine(“\n”);
内部温度=转换为32(温度);
Console.WriteLine(“--------------设备------------”;
如果(trenutna_温度<10)
{
控制台。WriteLine(“打开加热装置”);
}
如果出现其他情况(trenutna_温度>10和&trenutna_温度<20)
{
控制台写入线(“关闭”);
}
如果出现其他情况(trenutna_温度>20)
{
控制台写入线(“打开冷却”);
}
Console.ReadLine();

现在,我想每10秒更新一次此数据。如果你们中的任何人能为我指出正确的方向或帮助我修复代码,我将不胜感激。

一个选项是使用System.Threading.Timer:

public static void Main() 
{  
   System.Threading.Timer t = new System.Threading.Timer(UpdateThermostat, 5, 0, 2000); //10 times 1000 miliseconds
   Console.ReadLine();
   t.Dispose(); // dispose the timer
}


private static void UpdateThermostat(Object state) 
{ 
   // your code to get your thermostat
   //option one to print on the same line:
   //move the cursor to the beginning of the line before printing:
    Console.SetCursorPosition(0, Console.CursorTop);
    Console.Write(DateTime.Now);

   //option two to print on the same line:
   //printing "\r" moves cursor back to the beginning of the line so it's a trick:
    Console.Write("\r{0}",DateTime.Now);
}

hmm.似乎不起作用,或者我做错了什么:您需要某种方法来延迟主线程,这样应用程序就不会停止执行。例如Console.ReadLine();运行上面的代码,它应该会演示。就是这样!这很有效。尽管它触发它,但它会不断重复并在已显示状态下显示它(如:).有没有可能刷新已经显示的状态?不能说我能做到:-)但是非常欢迎!