2026-01-18 12:59:45 +03:00

353 lines
9.9 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
class Program
{
static SignLanguageReportApiClient api;
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";
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://mindrockets.org/control/");
LogHeader("SL REPORT TEST STARTED");
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;
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();
}
#region Actions
static Guid RegisterUser(string username, string password)
{
return SafeCall(
() => api.RegisterUser(username, password),
$"RegisterUser {username}"
);
}
static void RegisterClient(string cid, string srv)
{
SafeCall(
() => api.RegisterClient(cid, srv),
$"RegisterClient {cid}"
);
}
static void RegisterDefaultClients()
{
foreach (var cid in defaultClientIds)
{
RegisterClient(cid, defaultServer);
}
}
static void MapUser(Guid userId, string cid, string srv)
{
SafeCall(
() => api.MapUser(userId, cid, srv),
$"MapUser {cid}"
);
}
static void MapUserForAllClients(Guid userId)
{
foreach (var cid in defaultClientIds)
{
MapUser(userId, cid, defaultServer);
}
}
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(
() => api.ReportAllClientHistory(cid, srv),
$"ReportAllClientHistory {cid}"
);
if (history == null) return;
LogInfo($"History records for {cid}: {history.Count}");
foreach (var h in history)
LogInfo($"{h.captDate:yyyy-MM-dd HH:mm:ss} , Sessions: {h.NoOfSessions}");
}
static void ReportFilteredHistory(string cid, string srv)
{
var from = DateTime.Today.AddDays(-7);
var to = DateTime.Today;
var history = SafeCall(
() => api.ReportClientHistory(cid, srv, from, to),
$"ReportClientHistory {cid}"
);
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}");
}
static void ReportAllHistoryForAllClients()
{
foreach (var cid in defaultClientIds)
{
ReportAllHistory(cid, defaultServer);
}
}
static void ReportFilteredHistoryForAllClients()
{
foreach (var cid in defaultClientIds)
{
ReportFilteredHistory(cid, defaultServer);
}
}
static void ListAllClients()
{
var clients = SafeCall(
() => api.ListAllClients(),
"ListAllClients"
);
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}");
}
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();
}
#endregion
}