From 070bc3fbf0e8b9b3377fe443ebd12062e1ecbdff Mon Sep 17 00:00:00 2001 From: Denis Denisov Date: Sat, 20 Jan 2024 19:22:00 +0300 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D0=BE=D1=82=D0=BE=D0=B2?= =?UTF-8?q?=D0=BA=D0=B8=20=D0=BF=D0=BE=D0=B4=20=D0=BF=D0=BE=D0=B4=D1=82?= =?UTF-8?q?=D0=B2=D0=B5=D1=80=D0=B6=D0=B4=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=81?= =?UTF-8?q?=D1=82=D0=B0=D1=82=D1=83=D1=81=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B1?= =?UTF-8?q?=D0=BB=D0=B5=D0=BC=D1=8B=20=D0=B8=20=D1=80=D0=B0=D1=81=D1=81?= =?UTF-8?q?=D1=8B=D0=BB=D0=BA=D1=83=20=D0=BD=D0=BE=D1=82=D0=B8=D1=84=D0=B8?= =?UTF-8?q?=D0=BA=D0=B0=D1=86=D0=B8=D0=B8=20=D0=BF=D1=80=D0=B8=20=D0=B5?= =?UTF-8?q?=D1=91=20=D0=BF=D0=BE=D1=8F=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Problems/Item.cs | 1 + Problems/StatusManager.cs | 54 +++++++++++++++++++++++++++++---------- Program.cs | 1 + 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/Problems/Item.cs b/Problems/Item.cs index 49bd6ff..5a018a9 100644 --- a/Problems/Item.cs +++ b/Problems/Item.cs @@ -16,5 +16,6 @@ namespace ZaoBot.Problems public string label; public string name; public Status status; + public bool notified = false; } } diff --git a/Problems/StatusManager.cs b/Problems/StatusManager.cs index 67853a8..05f9f8f 100644 --- a/Problems/StatusManager.cs +++ b/Problems/StatusManager.cs @@ -1,7 +1,10 @@ using System; using System.Collections.Generic; using System.Text; +using System.Text.Json; using System.Timers; +using Telegram.Bot; +using ZaoBot.Config; namespace ZaoBot.Problems { @@ -10,15 +13,19 @@ namespace ZaoBot.Problems const int DEFAULT_INTERVAL = 2000; private OpenHab.OpenHabClient openhab; + private ITelegramBotClient bot; + private JsonDocument config; private Timer timer; private Dictionary statusItems = new Dictionary(); public IEnumerable StatusItems => statusItems.Values; - public StatusManager(OpenHab.OpenHabClient openhab) + public StatusManager(OpenHab.OpenHabClient openhab, ITelegramBotClient bot, IConfig config) { + this.bot = bot; this.openhab = openhab; + this.config = config.Data; UpdateStatus(); timer = new Timer(DEFAULT_INTERVAL); timer.Elapsed += OnTimer; @@ -39,28 +46,49 @@ namespace ZaoBot.Problems { if (!item.name.StartsWith("Problem_")) continue; Item statusItem = null; - if (statusItems.ContainsKey(item.name)) statusItem = statusItems[item.name]; - else + lock (statusItems) { - statusItem = new Item() + if (statusItems.ContainsKey(item.name)) statusItem = statusItems[item.name]; + else { - name = item.name, - label = item.label, - status = Status.UNKNOWN - }; - statusItems[item.name] = statusItem; + statusItem = new Item() + { + name = item.name, + label = item.label, + status = Status.UNKNOWN + }; + statusItems[item.name] = statusItem; + } } var state = await openhab.GetItemState(item.name); - if (state.state == "OFF") statusItem.status = Status.OK; - else if (state.state == "ON") statusItem.status = Status.PROBLEM; - else statusItem.status = Status.UNKNOWN; + lock (statusItems) + { + 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) { 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 + ); + } + } } } diff --git a/Program.cs b/Program.cs index 2677536..a1690a4 100644 --- a/Program.cs +++ b/Program.cs @@ -41,6 +41,7 @@ namespace ZaoBot var receiver = provider.GetRequiredService(); var notificationThread = new Thread(new ThreadStart(receiver.Run)); notificationThread.Start(); + var statusManager = provider.GetRequiredService(); while (true) { try