353 lines
9.9 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
class Program
{
static SignLanguageReportApiClient api;
2026-01-18 12:52:43 +03:00
static List<string> defaultClientIds = new List<string> { "13351", "13355", "13356" };
static string defaultServer = "MORO";
static void Main(string[] args)
{
Console.Title = "SL REPORT API TEST HARNESS";
2026-01-18 12:52:43 +03:00
if (args.Length == 0)
{
Console.WriteLine("Usage:");
Console.WriteLine(" registerUser <username> <password>");
Console.WriteLine(" registerClient <cid> <srv>");
Console.WriteLine(" mapUser <userId> <cid> <srv>");
Console.WriteLine(" capture <cid> <srv>");
Console.WriteLine(" reportAll <cid> <srv>");
Console.WriteLine(" reportFiltered <cid> <srv>");
Console.WriteLine(" listClients");
Console.WriteLine(" all <username> <password>");
return;
}
try
{
api = new SignLanguageReportApiClient("https://localhost:44367/");
LogHeader("SL REPORT TEST STARTED");
2026-01-18 12:52:43 +03:00
var command = args[0].ToLower();
switch (command)
{
case "registerdefaultclients":
RegisterDefaultClients();
break;
case "capturedefaultclients":
CaptureDefaultClients();
break;
case "registeruser":
if (args.Length < 3)
{
Console.WriteLine("registerUser requires <username> <password>");
return;
}
RegisterUser(args[1], args[2]);
break;
case "registerclient":
string cid = args.Length > 1 ? args[1] : null;
string srv = args.Length > 2 ? args[2] : defaultServer;
if (string.IsNullOrEmpty(cid))
{
Console.WriteLine("registerClient requires <cid> [<srv>]");
return;
}
RegisterClient(cid, srv);
break;
case "mapuser":
if (args.Length < 4)
{
Console.WriteLine("mapUser requires <userId> <cid> <srv>");
return;
}
MapUser(Guid.Parse(args[1]), args[2], args[3]);
break;
case "capture":
if (args.Length < 3)
{
Console.WriteLine("capture requires <cid> <srv>");
return;
}
Capture(args[1], args[2]);
break;
case "reportall":
if (args.Length < 3)
{
Console.WriteLine("reportAll requires <cid> <srv>");
return;
}
ReportAllHistory(args[1], args[2]);
break;
case "reportfiltered":
if (args.Length < 3)
{
Console.WriteLine("reportFiltered requires <cid> <srv>");
return;
}
ReportFilteredHistory(args[1], args[2]);
break;
case "listclients":
ListAllClients();
break;
case "all":
if (args.Length < 3)
{
Console.WriteLine("all requires <username> <password>");
return;
}
var userId = RegisterUser(args[1], args[2]);
RegisterDefaultClients();
MapUserForAllClients(userId);
CaptureAllClients();
ReportAllHistoryForAllClients();
ReportFilteredHistoryForAllClients();
ListAllClients();
break;
2026-01-18 12:52:43 +03:00
default:
Console.WriteLine($"Unknown command: {command}");
break;
}
LogHeader("SL REPORT TEST COMPLETED");
}
catch (Exception ex)
{
LogFatal("UNHANDLED FATAL ERROR", ex);
}
Console.WriteLine("\nPress ENTER to exit...");
Console.ReadLine();
}
2026-01-18 12:52:43 +03:00
#region Actions
static Guid RegisterUser(string username, string password)
{
return SafeCall(
2026-01-18 12:52:43 +03:00
() => api.RegisterUser(username, password),
$"RegisterUser {username}"
);
}
2026-01-18 12:52:43 +03:00
static void RegisterClient(string cid, string srv)
{
SafeCall(
2026-01-18 12:52:43 +03:00
() => api.RegisterClient(cid, srv),
$"RegisterClient {cid}"
);
}
2026-01-18 12:52:43 +03:00
static void RegisterDefaultClients()
{
foreach (var cid in defaultClientIds)
{
RegisterClient(cid, defaultServer);
}
}
static void MapUser(Guid userId, string cid, string srv)
{
SafeCall(
2026-01-18 12:52:43 +03:00
() => api.MapUser(userId, cid, srv),
$"MapUser {cid}"
);
}
2026-01-18 12:52:43 +03:00
static void MapUserForAllClients(Guid userId)
{
2026-01-18 12:52:43 +03:00
foreach (var cid in defaultClientIds)
{
MapUser(userId, cid, defaultServer);
}
}
2026-01-18 12:52:43 +03:00
static void Capture(string cid, string srv)
{
SafeCall(() => api.Capture(cid, srv), $"Capture {cid} #1");
}
static void CaptureAllClients()
{
foreach (var cid in defaultClientIds)
{
Capture(cid, defaultServer);
}
}
static void ReportAllHistory(string cid, string srv)
{
var history = SafeCall(
2026-01-18 12:52:43 +03:00
() => api.ReportAllClientHistory(cid, srv),
$"ReportAllClientHistory {cid}"
);
if (history == null) return;
2026-01-18 12:52:43 +03:00
LogInfo($"History records for {cid}: {history.Count}");
foreach (var h in history)
LogInfo($"{h.captDate:yyyy-MM-dd HH:mm:ss} , Sessions: {h.NoOfSessions}");
}
2026-01-18 12:52:43 +03:00
static void ReportFilteredHistory(string cid, string srv)
{
var from = DateTime.Today.AddDays(-7);
var to = DateTime.Today;
var history = SafeCall(
2026-01-18 12:52:43 +03:00
() => api.ReportClientHistory(cid, srv, from, to),
$"ReportClientHistory {cid}"
);
2026-01-18 12:52:43 +03:00
if (history == null) return;
LogInfo($"Filtered records for {cid}: {history.Count}");
foreach (var h in history)
LogInfo($"{h.captDate:yyyy-MM-dd HH:mm:ss} , Sessions: {h.NoOfSessions}");
2026-01-18 12:52:43 +03:00
}
2026-01-18 12:52:43 +03:00
static void ReportAllHistoryForAllClients()
{
foreach (var cid in defaultClientIds)
{
ReportAllHistory(cid, defaultServer);
}
}
2026-01-18 12:52:43 +03:00
static void ReportFilteredHistoryForAllClients()
{
foreach (var cid in defaultClientIds)
{
ReportFilteredHistory(cid, defaultServer);
}
}
static void ListAllClients()
{
var clients = SafeCall(
() => api.ListAllClients(),
"ListAllClients"
);
2026-01-18 12:52:43 +03:00
if (clients == null) return;
LogInfo($"Total clients: {clients.Count}");
foreach (var c in clients)
LogInfo($"cid: {c.cid}, srv: {c.srv}, addingDate: {c.addingDate}, lastCaptDate: {c.lastCaptDate}, NoOfCapt: {c.NoOfCapt}");
}
2026-01-18 12:52:43 +03:00
static void CaptureDefaultClients()
{
foreach (var cid in defaultClientIds)
{
SafeCall(() => api.Capture(cid, defaultServer), $"Capture {cid} #1");
}
}
#endregion
#region Helpers
static T SafeCall<T>(Func<T> action, string stepName)
{
var sw = Stopwatch.StartNew();
LogStep(stepName);
try
{
var result = action();
sw.Stop();
LogSuccess($"{stepName} completed in {sw.ElapsedMilliseconds} ms");
return result;
}
catch (WebException wex)
{
sw.Stop();
LogError(stepName, "NETWORK / HTTP ERROR", wex);
}
catch (Exception ex)
{
sw.Stop();
LogError(stepName, "APPLICATION ERROR", ex);
}
return default;
}
static void LogHeader(string msg)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("\n======================================");
Console.WriteLine(msg);
Console.WriteLine("======================================\n");
Console.ResetColor();
}
static void LogStep(string msg)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"> {msg}");
Console.ResetColor();
}
static void LogSuccess(string msg)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"✔ {msg}\n");
Console.ResetColor();
}
static void LogInfo(string msg)
{
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine($" {msg}");
Console.ResetColor();
}
static void LogError(string step, string type, Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"✖ {step} FAILED [{type}]");
Console.WriteLine($" Message: {ex.Message}");
if (ex.InnerException != null)
Console.WriteLine($" Inner: {ex.InnerException.Message}");
Console.ResetColor();
Console.WriteLine();
}
static void LogFatal(string msg, Exception ex)
{
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine("!!! FATAL ERROR !!!");
Console.WriteLine(msg);
if (ex != null)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
Console.ResetColor();
}
2026-01-18 12:52:43 +03:00
#endregion
}