diff --git a/Problems/Item.cs b/Problems/Item.cs new file mode 100644 index 0000000..49bd6ff --- /dev/null +++ b/Problems/Item.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ZaoBot.Problems +{ + public enum Status + { + UNKNOWN, + OK, + PROBLEM + } + + class Item + { + public string label; + public string name; + public Status status; + } +} diff --git a/Problems/StatusManager.cs b/Problems/StatusManager.cs new file mode 100644 index 0000000..67853a8 --- /dev/null +++ b/Problems/StatusManager.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Timers; + +namespace ZaoBot.Problems +{ + class StatusManager + { + const int DEFAULT_INTERVAL = 2000; + + private OpenHab.OpenHabClient openhab; + + private Timer timer; + private Dictionary statusItems = new Dictionary(); + + public IEnumerable StatusItems => statusItems.Values; + + public StatusManager(OpenHab.OpenHabClient openhab) + { + this.openhab = openhab; + UpdateStatus(); + timer = new Timer(DEFAULT_INTERVAL); + timer.Elapsed += OnTimer; + timer.Enabled = true; + } + + private void OnTimer(object sender, ElapsedEventArgs e) + { + UpdateStatus(); + } + + private async void UpdateStatus() + { + try + { + var items = await openhab.GetItems(); + foreach (var item in items) + { + if (!item.name.StartsWith("Problem_")) continue; + Item statusItem = null; + if (statusItems.ContainsKey(item.name)) statusItem = statusItems[item.name]; + else + { + 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; + } + + } + catch (Exception ex) + { + Console.WriteLine("Exception on getting status: " + ex.ToString()); + } + } + } +} diff --git a/Program.cs b/Program.cs index 73fcea6..2677536 100644 --- a/Program.cs +++ b/Program.cs @@ -8,6 +8,7 @@ using System.Threading; using Telegram.Bot; using ZaoBot.Config; using ZaoBot.OpenHab; +using ZaoBot.Problems; namespace ZaoBot { @@ -26,6 +27,7 @@ namespace ZaoBot services.AddSingleton(CreateConfig); services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); services.AddScoped(); services.AddScoped(p => p); diff --git a/StatusMenuUserState.cs b/StatusMenuUserState.cs index fa65782..76df23c 100644 --- a/StatusMenuUserState.cs +++ b/StatusMenuUserState.cs @@ -10,6 +10,7 @@ using Telegram.Bot.Args; using Telegram.Bot.Types.ReplyMarkups; using ZaoBot.Config; using ZaoBot.OpenHab; +using ZaoBot.Problems; namespace ZaoBot { @@ -22,13 +23,18 @@ namespace ZaoBot HttpClient http = new HttpClient(); OpenHabClient openhab; IServiceProvider services; + StatusManager statusManager; - public StatusMenuUserState(IServiceProvider services, ITelegramBotClient botClient, IConfig config, OpenHabClient openhab) + public StatusMenuUserState(IServiceProvider services, ITelegramBotClient botClient, + IConfig config, + OpenHabClient openhab, + StatusManager statusManager) { this.botClient = botClient; this.config = config.Data; this.openhab = openhab; this.services = services; + this.statusManager = statusManager; } public async Task ProcessMessage(IUserState previous, Telegram.Bot.Types.Update e) @@ -37,16 +43,9 @@ namespace ZaoBot if (previous != this) { response = "Возможные проблемы:\n"; - var items = await openhab.GetItems(); - foreach (var item in items) + foreach (var item in statusManager.StatusItems) { - if (!item.name.StartsWith("Problem_")) continue; - response += item.label + ": "; - var state = await openhab.GetItemState(item.name); - if (state.state == "OFF") response += "OK"; - else if (state.state == "ON") response += "PROBLEM!"; - else response += "UNKNOWN"; - response += "\n"; + response += item.label + ": " + item.status + "\n"; } } else if (e.Message.Text == CMD_BACK)