-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
63 lines (51 loc) · 1.84 KB
/
Program.cs
File metadata and controls
63 lines (51 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
namespace WxStar_EKG;
public class Program
{
static async Task Main(string[] args)
{
Config config = Config.Load();
await MqttPublisher.Connect();
int failover = 0;
Console.WriteLine("WeatherStar EKG Daemon");
Console.WriteLine("May your i2 not fall 2 hours behind <3");
Console.WriteLine("======================================");
while (true)
{
try
{
DateTime currentNetTime = NtpClient.GetNetworkTime();
string currentNetTimeStr = currentNetTime.ToString("MM/dd/yyyy hh:mm:ss.fff tt");
await MqttPublisher.PublishCommand($"heartbeat(Time={currentNetTimeStr})", "i2m/heartbeat");
await Task.Delay(1000 * 60 * 5); // 5-minute intervals
}
catch (Exception e)
{
Console.WriteLine("Failed to post heartbeat, trying again in 5 minutes..");
Console.WriteLine("Either NTP is blocked or we're being rate limited.");
failover++;
// Try to refresh the connection to the MQTT broker
if (failover > 5)
{
await AttemptMqttReconnect();
continue;
}
await Task.Delay(1000 * 60 * 5);
}
}
}
static async Task AttemptMqttReconnect()
{
try
{
Console.WriteLine("Attempting to reconnect to MQTT broker..");
await MqttPublisher.Disconnect();
await MqttPublisher.Connect();
}
catch (Exception e)
{
Console.WriteLine("Failed to reconnect to MQTT broker. Daemon cannot continue.");
Console.WriteLine(e.Message);
throw;
}
}
}