Заготовки под подтверждение статуса проблемы и рассылку нотификации при её появлении
This commit is contained in:
@@ -16,5 +16,6 @@ namespace ZaoBot.Problems
|
|||||||
public string label;
|
public string label;
|
||||||
public string name;
|
public string name;
|
||||||
public Status status;
|
public Status status;
|
||||||
|
public bool notified = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+41
-13
@@ -1,7 +1,10 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
using System.Timers;
|
using System.Timers;
|
||||||
|
using Telegram.Bot;
|
||||||
|
using ZaoBot.Config;
|
||||||
|
|
||||||
namespace ZaoBot.Problems
|
namespace ZaoBot.Problems
|
||||||
{
|
{
|
||||||
@@ -10,15 +13,19 @@ namespace ZaoBot.Problems
|
|||||||
const int DEFAULT_INTERVAL = 2000;
|
const int DEFAULT_INTERVAL = 2000;
|
||||||
|
|
||||||
private OpenHab.OpenHabClient openhab;
|
private OpenHab.OpenHabClient openhab;
|
||||||
|
private ITelegramBotClient bot;
|
||||||
|
private JsonDocument config;
|
||||||
|
|
||||||
private Timer timer;
|
private Timer timer;
|
||||||
private Dictionary<string, Item> statusItems = new Dictionary<string, Item>();
|
private Dictionary<string, Item> statusItems = new Dictionary<string, Item>();
|
||||||
|
|
||||||
public IEnumerable<Item> StatusItems => statusItems.Values;
|
public IEnumerable<Item> StatusItems => statusItems.Values;
|
||||||
|
|
||||||
public StatusManager(OpenHab.OpenHabClient openhab)
|
public StatusManager(OpenHab.OpenHabClient openhab, ITelegramBotClient bot, IConfig config)
|
||||||
{
|
{
|
||||||
|
this.bot = bot;
|
||||||
this.openhab = openhab;
|
this.openhab = openhab;
|
||||||
|
this.config = config.Data;
|
||||||
UpdateStatus();
|
UpdateStatus();
|
||||||
timer = new Timer(DEFAULT_INTERVAL);
|
timer = new Timer(DEFAULT_INTERVAL);
|
||||||
timer.Elapsed += OnTimer;
|
timer.Elapsed += OnTimer;
|
||||||
@@ -39,28 +46,49 @@ namespace ZaoBot.Problems
|
|||||||
{
|
{
|
||||||
if (!item.name.StartsWith("Problem_")) continue;
|
if (!item.name.StartsWith("Problem_")) continue;
|
||||||
Item statusItem = null;
|
Item statusItem = null;
|
||||||
if (statusItems.ContainsKey(item.name)) statusItem = statusItems[item.name];
|
lock (statusItems)
|
||||||
else
|
|
||||||
{
|
{
|
||||||
statusItem = new Item()
|
if (statusItems.ContainsKey(item.name)) statusItem = statusItems[item.name];
|
||||||
|
else
|
||||||
{
|
{
|
||||||
name = item.name,
|
statusItem = new Item()
|
||||||
label = item.label,
|
{
|
||||||
status = Status.UNKNOWN
|
name = item.name,
|
||||||
};
|
label = item.label,
|
||||||
statusItems[item.name] = statusItem;
|
status = Status.UNKNOWN
|
||||||
|
};
|
||||||
|
statusItems[item.name] = statusItem;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
var state = await openhab.GetItemState(item.name);
|
var state = await openhab.GetItemState(item.name);
|
||||||
if (state.state == "OFF") statusItem.status = Status.OK;
|
lock (statusItems)
|
||||||
else if (state.state == "ON") statusItem.status = Status.PROBLEM;
|
{
|
||||||
else statusItem.status = Status.UNKNOWN;
|
if (state.state == "OFF") statusItem.status = Status.OK;
|
||||||
|
else if (state.state == "ON") statusItem.status = Status.PROBLEM;
|
||||||
|
else statusItem.status = Status.UNKNOWN;
|
||||||
|
if (statusItem.status == Status.PROBLEM && statusItem.notified == false)
|
||||||
|
{
|
||||||
|
Notify(statusItem);
|
||||||
|
statusItem.notified = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Exception on getting status: " + ex.ToString());
|
Console.WriteLine("Exception on getting status: " + ex.ToString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async void Notify(Item item)
|
||||||
|
{
|
||||||
|
foreach (var element in config.RootElement.GetProperty("notify_user_ids").EnumerateArray())
|
||||||
|
{
|
||||||
|
await bot.SendTextMessageAsync(
|
||||||
|
chatId: element.GetString(),
|
||||||
|
text: "Problem appeared: " + item.label
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ namespace ZaoBot
|
|||||||
var receiver = provider.GetRequiredService<NotificationReceiver>();
|
var receiver = provider.GetRequiredService<NotificationReceiver>();
|
||||||
var notificationThread = new Thread(new ThreadStart(receiver.Run));
|
var notificationThread = new Thread(new ThreadStart(receiver.Run));
|
||||||
notificationThread.Start();
|
notificationThread.Start();
|
||||||
|
var statusManager = provider.GetRequiredService<StatusManager>();
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|||||||
Reference in New Issue
Block a user