Added user state (nested menus) and sub-menu to control different potential problems and system status
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ZaoBot.OpenHab
|
||||
{
|
||||
class OpenHabClient
|
||||
{
|
||||
private string url;
|
||||
HttpClient http = new HttpClient();
|
||||
|
||||
public OpenHabClient(string url)
|
||||
{
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public async Task<ItemState> GetItemState(string item)
|
||||
{
|
||||
var result = await http.GetAsync(url + "/rest/items/" + item);
|
||||
var content = await result.Content.ReadAsStringAsync();
|
||||
return ParseState(content);
|
||||
}
|
||||
|
||||
private ItemState ParseState(string content)
|
||||
{
|
||||
var doc = JsonDocument.Parse(content);
|
||||
|
||||
var pattern = "";
|
||||
JsonElement stateDescription;
|
||||
if (doc.RootElement.TryGetProperty("stateDescription", out stateDescription))
|
||||
{
|
||||
pattern = stateDescription.GetProperty("pattern").GetString();
|
||||
}
|
||||
var result = new ItemState()
|
||||
{
|
||||
state = doc.RootElement.GetProperty("state").GetString(),
|
||||
pattern = pattern,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<string> PostItemState(string item, string state)
|
||||
{
|
||||
return await Post(url + "/rest/items/" + item, state);
|
||||
}
|
||||
|
||||
private async Task<string> Post(string url, string v)
|
||||
{
|
||||
var content = new ByteArrayContent(Encoding.UTF8.GetBytes(v));
|
||||
content.Headers.Add("Content-Type", "text/plain");
|
||||
var result = await http.PostAsync(url, content);
|
||||
if (result.IsSuccessStatusCode) return "OK";
|
||||
else return "Fail. " + result.ReasonPhrase;
|
||||
}
|
||||
|
||||
public async Task<List<Item>> GetItems()
|
||||
{
|
||||
var result = await http.GetAsync(url + "/rest/items/?fields=name,type,label");
|
||||
var content = await result.Content.ReadAsStringAsync();
|
||||
var doc = JsonDocument.Parse(content);
|
||||
var items = new List<Item>();
|
||||
foreach (var element in doc.RootElement.EnumerateArray())
|
||||
{
|
||||
var label = "";
|
||||
JsonElement l;
|
||||
if (element.TryGetProperty("label", out l))
|
||||
{
|
||||
label = l.GetString();
|
||||
}
|
||||
items.Add(new Item()
|
||||
{
|
||||
name = element.GetProperty("name").GetString(),
|
||||
type = element.GetProperty("type").GetString(),
|
||||
label = label,
|
||||
});
|
||||
}
|
||||
return items;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user