107 lines
3.5 KiB
C#
107 lines
3.5 KiB
C#
using System.Globalization;
|
|
using System.Net.Http.Headers;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace Tagger.Service;
|
|
|
|
public class HAResponse
|
|
{
|
|
public string state { get; set; }
|
|
}
|
|
|
|
public class ParsingManager
|
|
{
|
|
Configuration Conf;
|
|
HttpClient Client;
|
|
string Pattern = @"\{\{([a-zA-Z0-9._]+)\:([a-zA-Z0-9._]+)(?:,([a-zA-Z0-9._]+))*\}\}";
|
|
|
|
readonly string SimpleDisplay = "display";
|
|
readonly string MinMaxDisplay = "minmax_display";
|
|
|
|
public ParsingManager(IConfiguration configuration)
|
|
{
|
|
Conf = configuration.Get<Configuration>() ?? new();
|
|
Client = new();
|
|
Client.BaseAddress = new(Conf.HomeAssistantUrl);
|
|
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Conf.HomeAssistantToken);
|
|
}
|
|
|
|
public async Task<string> Parse(string input)
|
|
{
|
|
return Regex.Replace(input, Pattern, (match) =>
|
|
{
|
|
if (match.Groups.Count <= 1)
|
|
return ($"(Bad func at {match.Index}, not any func)");
|
|
|
|
var allArgs = match.Groups.Values.Skip(1).ToArray();
|
|
|
|
string func = allArgs[0].ToString().ToLower();
|
|
List<string> args = new();
|
|
foreach (var i in allArgs.Skip(1))
|
|
foreach (var j in i.Captures)
|
|
if(j is not null)
|
|
args.Add(j.ToString());
|
|
|
|
if(func == SimpleDisplay)
|
|
return ParseDisplay(args.ToArray()).Result;
|
|
else if(func == MinMaxDisplay)
|
|
return ParseMinMaxDisplay(args.ToArray()).Result;
|
|
else
|
|
return match.Value;
|
|
});
|
|
}
|
|
|
|
public async Task<string> ParseDisplay(string[] args)
|
|
{
|
|
if(args.Length < 1)
|
|
return ($"(Bad {SimpleDisplay} call, need at least 1 argument)");
|
|
|
|
var r = await Get(args[0]).ConfigureAwait(false);
|
|
if (r is null)
|
|
return $"(Bad {SimpleDisplay} call, Not any result from call)";
|
|
|
|
return $"<span class=\"numeric-value\">{r}</span>";
|
|
}
|
|
|
|
public async Task<string> ParseMinMaxDisplay(string[] args)
|
|
{
|
|
if (args.Length < 3)
|
|
return ($"(Bad {SimpleDisplay} call, need at least 3 argument)");
|
|
|
|
var val = await Get(args[0]).ConfigureAwait(false);
|
|
if (val is null)
|
|
return $"(Bad {SimpleDisplay} call, Not any result from call for main (1st) argument)";
|
|
|
|
var min = await Get(args[1]).ConfigureAwait(false);
|
|
if (min is null)
|
|
return $"(Bad {SimpleDisplay} call, Not any result from call for min (2nd) argument)";
|
|
|
|
var max = await Get(args[2]).ConfigureAwait(false);
|
|
if (max is null)
|
|
return $"(Bad {SimpleDisplay} call, Not any result from call for max (3th) argument)";
|
|
|
|
bool isOk = val > min && val < max;
|
|
string emoji = isOk ? "✔️" : "❌";
|
|
string tag = isOk ? "numeric-value" : "numeric-value-error";
|
|
return $"<span class=\"{tag}\">{val}</span> {emoji} [<span class=\"numeric-value-range\">{min}</span>, <span class=\"numeric-value-range\">{max}</span>]";
|
|
}
|
|
|
|
public async Task<float?> Get(string item, CancellationToken tk = default)
|
|
{
|
|
try
|
|
{
|
|
var resp = await Client.GetFromJsonAsync<HAResponse>(item, tk).ConfigureAwait(false);
|
|
if (resp is null)
|
|
return null;
|
|
|
|
if (float.TryParse(resp.state, CultureInfo.InvariantCulture, out float value))
|
|
return value;
|
|
return null;
|
|
}
|
|
catch(Exception e)
|
|
{
|
|
return null;
|
|
};
|
|
}
|
|
}
|