Обновление статуса переехало в отдельный класс, в реакции на сообщение только известный набор статусов возвращаем
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user