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)
{
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));
}
}
@ -64,7 +64,7 @@ public abstract class Api : IDisposable
while (_workOnQueue)
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);
Thread.Sleep(action.Item4 + CommandDelay);
}

View File

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

View File

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

View File

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