初始化
This commit is contained in:
67
BooliveMQTT_Auth/services/MyTimer.cs
Normal file
67
BooliveMQTT_Auth/services/MyTimer.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using System.Text;
|
||||
using Common;
|
||||
using NLog;
|
||||
|
||||
namespace IotManager.services
|
||||
{
|
||||
public class MyTimer : BackgroundService
|
||||
{
|
||||
public System.Timers.Timer jiankong_timer = null;
|
||||
public MyTimer()
|
||||
{
|
||||
|
||||
}
|
||||
protected override Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
jiankong_timer = new System.Timers.Timer();
|
||||
jiankong_timer.Interval = 60000;
|
||||
jiankong_timer.Elapsed += Jiankong_timer_Elapsed;
|
||||
jiankong_timer.Start();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
public static Logger logger = LogManager.GetCurrentClassLogger();
|
||||
private void Jiankong_timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
var df = Grafana.data;
|
||||
|
||||
string NewStringData = Grafana.Data;
|
||||
NewStringData = NewStringData.Replace("{1}", df.device_id)
|
||||
.Replace("{2}", df.device_status)
|
||||
.Replace("{3}", df.realtime_value)
|
||||
.Replace("{4}", df.environment_value)
|
||||
.Replace("{5}", df.mcu_temperature)
|
||||
.Replace("{6}", df.sensor_temperature)
|
||||
.Replace("{7}", df.sensor_humidity)
|
||||
.Replace("{8}", df.adc_raw_value);
|
||||
|
||||
|
||||
WanBuJianKong(NewStringData, "1.txt");
|
||||
File.WriteAllText("2.txt", NewStringData, Encoding.UTF8);
|
||||
Console.WriteLine("here");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
Console.WriteLine(ex.StackTrace);
|
||||
logger.Error(ex.Message);
|
||||
logger.Error(ex.StackTrace);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void WanBuJianKong(string Data, string SourceTxt)
|
||||
{
|
||||
System.IO.File.WriteAllText(SourceTxt, Data, Encoding.UTF8);
|
||||
// 如果目标文件存在,先删除
|
||||
if (System.IO.File.Exists(Grafana.WaiBuJianKongTargetFileName))
|
||||
{
|
||||
System.IO.File.Delete(Grafana.WaiBuJianKongTargetFileName);
|
||||
}
|
||||
|
||||
// 重命名/移动文件
|
||||
System.IO.File.Move(SourceTxt, Grafana.WaiBuJianKongTargetFileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user