Обновление статуса переехало в отдельный класс, в реакции на сообщение только известный набор статусов возвращаем
This commit is contained in:
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<string, Item> statusItems = new Dictionary<string, Item>();
|
||||||
|
|
||||||
|
public IEnumerable<Item> 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,7 @@ using System.Threading;
|
|||||||
using Telegram.Bot;
|
using Telegram.Bot;
|
||||||
using ZaoBot.Config;
|
using ZaoBot.Config;
|
||||||
using ZaoBot.OpenHab;
|
using ZaoBot.OpenHab;
|
||||||
|
using ZaoBot.Problems;
|
||||||
|
|
||||||
namespace ZaoBot
|
namespace ZaoBot
|
||||||
{
|
{
|
||||||
@@ -26,6 +27,7 @@ namespace ZaoBot
|
|||||||
services.AddSingleton<IConfig, Config.Config>(CreateConfig);
|
services.AddSingleton<IConfig, Config.Config>(CreateConfig);
|
||||||
services.AddSingleton<OpenHabClient, OpenHabClient>();
|
services.AddSingleton<OpenHabClient, OpenHabClient>();
|
||||||
services.AddSingleton<NotificationReceiver, NotificationReceiver>();
|
services.AddSingleton<NotificationReceiver, NotificationReceiver>();
|
||||||
|
services.AddSingleton<StatusManager, StatusManager>();
|
||||||
|
|
||||||
services.AddScoped<BotService, BotService>();
|
services.AddScoped<BotService, BotService>();
|
||||||
services.AddScoped<IServiceProvider, IServiceProvider>(p => p);
|
services.AddScoped<IServiceProvider, IServiceProvider>(p => p);
|
||||||
|
|||||||
+9
-10
@@ -10,6 +10,7 @@ using Telegram.Bot.Args;
|
|||||||
using Telegram.Bot.Types.ReplyMarkups;
|
using Telegram.Bot.Types.ReplyMarkups;
|
||||||
using ZaoBot.Config;
|
using ZaoBot.Config;
|
||||||
using ZaoBot.OpenHab;
|
using ZaoBot.OpenHab;
|
||||||
|
using ZaoBot.Problems;
|
||||||
|
|
||||||
namespace ZaoBot
|
namespace ZaoBot
|
||||||
{
|
{
|
||||||
@@ -22,13 +23,18 @@ namespace ZaoBot
|
|||||||
HttpClient http = new HttpClient();
|
HttpClient http = new HttpClient();
|
||||||
OpenHabClient openhab;
|
OpenHabClient openhab;
|
||||||
IServiceProvider services;
|
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.botClient = botClient;
|
||||||
this.config = config.Data;
|
this.config = config.Data;
|
||||||
this.openhab = openhab;
|
this.openhab = openhab;
|
||||||
this.services = services;
|
this.services = services;
|
||||||
|
this.statusManager = statusManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IUserState> ProcessMessage(IUserState previous, Telegram.Bot.Types.Update e)
|
public async Task<IUserState> ProcessMessage(IUserState previous, Telegram.Bot.Types.Update e)
|
||||||
@@ -37,16 +43,9 @@ namespace ZaoBot
|
|||||||
if (previous != this)
|
if (previous != this)
|
||||||
{
|
{
|
||||||
response = "Возможные проблемы:\n";
|
response = "Возможные проблемы:\n";
|
||||||
var items = await openhab.GetItems();
|
foreach (var item in statusManager.StatusItems)
|
||||||
foreach (var item in items)
|
|
||||||
{
|
{
|
||||||
if (!item.name.StartsWith("Problem_")) continue;
|
response += item.label + ": " + item.status + "\n";
|
||||||
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";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (e.Message.Text == CMD_BACK)
|
else if (e.Message.Text == CMD_BACK)
|
||||||
|
|||||||
Reference in New Issue
Block a user