ToString fancy

This commit is contained in:
glax 2024-02-12 03:06:28 +01:00
parent 2c7da0352b
commit eebf15804a
4 changed files with 35 additions and 24 deletions

View File

@ -42,7 +42,7 @@ public abstract class Api : IDisposable
} }
foreach (Shocker shocker in shockers) foreach (Shocker shocker in shockers)
{ {
this.Logger?.Log(LogLevel.Debug, $"Enqueueing {action} {intensity} {duration}"); this.Logger?.Log(LogLevel.Debug, $"Enqueueing {action} Intensity: {intensity} Duration: {duration}\nShocker:\n{shocker}");
_queue.Enqueue(new(action, shocker, intensity, duration)); _queue.Enqueue(new(action, shocker, intensity, duration));
} }
} }
@ -64,7 +64,7 @@ public abstract class Api : IDisposable
while (_workOnQueue) while (_workOnQueue)
if (_queue.Count > 0 && _queue.Dequeue() is { } action) if (_queue.Count > 0 && _queue.Dequeue() is { } action)
{ {
this.Logger?.Log(LogLevel.Information, $"{action.Item1} {action.Item2} {action.Item3} {action.Item4}"); this.Logger?.Log(LogLevel.Information, $"Executing: {Enum.GetName(action.Item1)} Intensity: {action.Item3} Duration: {action.Item4}\nShocker:\n{action.Item2.ToString()}");
ControlInternal(action.Item1, action.Item2, action.Item3, action.Item4); ControlInternal(action.Item1, action.Item2, action.Item3, action.Item4);
Thread.Sleep(action.Item4 + CommandDelay); Thread.Sleep(action.Item4 + CommandDelay);
} }

View File

@ -26,18 +26,19 @@ public static class ApiHttpClient
new StringContent(jsonContent, Encoding.UTF8, new MediaTypeHeaderValue("application/json")); new StringContent(jsonContent, Encoding.UTF8, new MediaTypeHeaderValue("application/json"));
foreach ((string, string) customHeader in customHeaders) foreach ((string, string) customHeader in customHeaders)
request.Headers.Add(customHeader.Item1, customHeader.Item2); request.Headers.Add(customHeader.Item1, customHeader.Item2);
logger?.Log(LogLevel.Debug, $"Request: \n" + logger?.Log(LogLevel.Debug, string.Join("\n\t",
$"-URI: {request.RequestUri}\n" + "Request:",
$"-Headers: \n\t{string.Join("\n\t", request.Headers.Select(h => $"{h.Key} {string.Join(", ", h.Value)}"))}\n" + $"\u251c\u2500\u2500 URI: {request.RequestUri}",
$"-Content: {request.Content?.ReadAsStringAsync().Result}"); $"\u251c\u2500\u2510 Headers: {string.Concat(request.Headers.Select(h => $"\n\t\u2502 {(request.Headers.Last().Key.Equals(h.Key) ? "\u2514" : "\u251c")} {h.Key}: {string.Join(", ", h.Value)}"))}",
$"\u2514\u2500\u2500 Content: {request.Content?.ReadAsStringAsync().Result}"));
HttpClient httpClient = new(); HttpClient httpClient = new();
HttpResponseMessage response = httpClient.Send(request); HttpResponseMessage response = httpClient.Send(request);
logger?.Log(!response.IsSuccessStatusCode ? LogLevel.Error : LogLevel.Debug, logger?.Log(!response.IsSuccessStatusCode ? LogLevel.Error : LogLevel.Debug, string.Join("\n\t",
$"Response: \n" + "Request:",
$"-URI: {request.RequestUri}\n" + $"\u251c\u2500\u2500 URI: {request.RequestUri}",
$"-Headers: \n\t{string.Join("\n\t", response.Headers.Select(h => $"{h.Key} {string.Join(", ", h.Value)}"))}\n" + $"\u251c\u2500\u2510 Headers: {string.Concat(response.Headers.Select(h => $"\n\t\u2502 {(response.Headers.Last().Key.Equals(h.Key) ? "\u2514" : "\u251c")} {h.Key}: {string.Join(", ", h.Value)}"))}",
$"-Content: {response.Content?.ReadAsStringAsync().Result}"); $"\u2514\u2500\u2500 Content: {response.Content?.ReadAsStringAsync().Result}"));
httpClient.Dispose(); httpClient.Dispose();
return response; return response;
} }

View File

@ -32,14 +32,15 @@ public class OpenShockShocker : Shocker
public override string ToString() public override string ToString()
{ {
return $"{string.Join("\n\t", const int tabWidth = -12;
$"{GetType().Name}", return $"\t{string.Join("\n\t",
$"Name: {Name}", $"\u251c {"Type",tabWidth}: {GetType().Name}",
$"ID: {ID}", $"\u251c {"Name",tabWidth}: {Name}",
$"RF-ID: {RfId}", $"\u251c {"ID",tabWidth}: {ID}",
$"Model: {Enum.GetName(Model)}", $"\u251c {"RF-ID",tabWidth}: {RfId}",
$"Created On: {CreatedOn}", $"\u251c {"Model",tabWidth}: {Enum.GetName(Model)}",
$"Paused: {IsPaused}")}" + $"\u251c {"Created On",tabWidth}: {CreatedOn}",
$"\u2514 {"Paused",tabWidth}: {IsPaused}")}" +
$"\n\r"; $"\n\r";
} }

View File

@ -7,6 +7,13 @@ public class PiShockShocker : Shocker
{ {
public readonly string Code; public readonly string Code;
public PiShockShocker(Api api, string code) : base(api)
{
if (api is not PiShockApi)
throw new Exception($"API-Type {api.GetType().FullName} is not usable with Shocker {this.GetType().FullName}");
Code = code;
}
public override bool Equals(object? obj) public override bool Equals(object? obj)
{ {
return obj is PiShockShocker pss && Equals(pss); return obj is PiShockShocker pss && Equals(pss);
@ -21,11 +28,13 @@ public class PiShockShocker : Shocker
{ {
return Code.GetHashCode(); return Code.GetHashCode();
} }
public PiShockShocker(Api api, string code) : base(api) public override string ToString()
{ {
if (api is not PiShockApi) const int tabWidth = -5;
throw new Exception($"API-Type {api.GetType().FullName} is not usable with Shocker {this.GetType().FullName}"); return $"{string.Join("\n\t",
Code = code; $"\u251c {"Type",tabWidth}: {GetType().Name}",
$"\u2514 {"Code",tabWidth}: {Code}")}" +
$"\n\r";
} }
} }