Files
mapo-iob-libs/EgwProxy.Shelly.Test/Program.cs
T
Samuele Locatelli a486724c2e Refrest test project
2025-08-06 18:38:42 +02:00

209 lines
8.8 KiB
C#

using System;
using System.Diagnostics;
using System.Configuration;
using System.IO;
using System.Threading.Tasks;
using Newtonsoft.Json;
using EgwProxy.Shelly.Clients;
using System.Net.Http;
using EgwProxy.Shelly.Options;
using System.Threading;
using Newtonsoft.Json.Linq;
namespace EgwProxy.Shelly.Test
{
internal class Program
{
/// <summary>
/// Helper separatore dash
/// </summary>
private const string separator = "------------------------";
private static Stopwatch sw = new Stopwatch();
/// <summary>
/// legge conf in formato stringa
/// </summary>
/// <param power="key"></param>
/// <returns></returns>
protected static string ReadSetting(string key)
{
string answ = "";
try
{
answ = $"{ConfigurationManager.AppSettings[key]}" ?? "";
}
catch (Exception exc)
{
Console.Write("Eccezione in ReadSettings");
Console.Write(exc.Message);
}
return answ;
}
////APC
//private string shellyAddr = "10.74.81.71";
//// caffè
//private string shellyAddr = "10.74.81.72";
//private string model = "Pm1";
////3EM Trifase
//private string shellyAddr = "10.74.81.73";
//private string model = "Pro3Em";
/// <summary>
/// Programma principale
/// </summary>
/// <param power="args"></param>
private static void Main(string[] args)
{
Console.WriteLine(separator);
Console.WriteLine("Test Shelly Client");
Console.WriteLine(separator);
Console.WriteLine();
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string BaseDirectory = Path.GetDirectoryName(exePath);
string testFile = Path.Combine(BaseDirectory, "conf", ReadSetting("testFile"));
if (!string.IsNullOrEmpty(testFile))
{
Console.WriteLine(separator);
Console.WriteLine($"Mode json ({testFile})");
Console.WriteLine(separator);
Console.WriteLine();
if (File.Exists(testFile))
{
var rawData = File.ReadAllText(testFile);
if (!string.IsNullOrEmpty(rawData))
{
TestSetup testConf = new TestSetup();
try
{
testConf = JsonConvert.DeserializeObject<TestSetup>(rawData);
}
catch
{ }
// setup devAddr
Shelly1PmOptions options = new Shelly1PmOptions()
{
DefaultTimeout = TimeSpan.FromSeconds(testConf.tOutSec),
ServerUri = new Uri($"http://{testConf.devAddr}/rpc")
};
Shelly1PmClient shelly1PM = new Shelly1PmClient(new HttpClient(), options);
ShellyPro3EmClient shellyPro3 = new ShellyPro3EmClient(new HttpClient(), options);
switch (testConf.model)
{
case shellyModel.nd:
break;
case shellyModel.gen1:
// test chiamata completa
serverTest(shelly1PM);
break;
case shellyModel.gen2:
// test chiamata completa
serverTest(shellyPro3);
break;
default:
break;
}
bool doRepeat = true;
while (doRepeat)
{
// eseguo per ogni step
foreach (var item in testConf.steps)
{
// ripeto per il num di volte richieste...
for (int i = 0; i < item.numRep; i++)
{
Console.WriteLine(separator);
Console.WriteLine($"Rep: {i + 1}");
Console.WriteLine(separator);
string esitoStep = "";
sw.Restart();
switch (item.action)
{
case stepType.getFullStatus:
var respFull = Task.Run(() => shelly1PM.GetStatus(CancellationToken.None)).Result;
if (respFull.IsSuccess)
{
string serValFull = JsonConvert.SerializeObject(respFull.Value, Formatting.Indented);
esitoStep = respFull.IsSuccess ? serValFull : "Errore in GetStatus";
}
break;
case stepType.getSwitchStatus:
var respSwitch = Task.Run(() => shellyPro3.GetEmStatus(CancellationToken.None)).Result;
if (respSwitch.IsSuccess)
{
string serValSwitch = JsonConvert.SerializeObject(respSwitch.Value, Formatting.Indented);
esitoStep = respSwitch.IsSuccess ? serValSwitch : "Errore in GetEmStatus";
}
break;
default:
esitoStep = "Action not managed: skipping";
break;
}
sw.Stop();
Console.WriteLine(esitoStep);
Console.WriteLine(separator);
Console.WriteLine($"Elapsed: {sw.Elapsed.TotalMilliseconds:N3}ms");
Console.WriteLine(separator);
Console.WriteLine();
Thread.Sleep(item.waitTime);
}
Console.WriteLine($"------ Done Step {item.id} ------");
}
Console.WriteLine("Do you want to repeat from the beginnning? esc to close");
ConsoleKeyInfo answ = Console.ReadKey();
doRepeat = answ.Key != ConsoleKey.Escape;
}
}
}
}
}
private static void serverTest(Shelly1PmClient shellyClient)
{
Console.WriteLine(separator);
// registro ed eseguo chiamata in modalità sincrona
sw.Restart();
var response = Task.Run(() => shellyClient.GetStatus(CancellationToken.None)).Result;
if (response.IsSuccess)
{
string serVal = JsonConvert.SerializeObject(response.Value, Formatting.Indented);
sw.Stop();
writeResult(response.IsSuccess, serVal);
}
}
private static void writeResult(bool isSuccess, string serVal)
{
Console.WriteLine($"CallSuccess: {isSuccess}");
if (!string.IsNullOrEmpty(serVal))
{
Console.WriteLine(separator);
Console.WriteLine(serVal);
Console.WriteLine(separator);
Console.WriteLine($"Elapsed: {sw.Elapsed.TotalMilliseconds:N3}ms");
Console.WriteLine(separator);
Console.WriteLine();
}
}
private static void serverTest(ShellyPro3EmClient shellyClient)
{
Console.WriteLine(separator);
// registro ed eseguo chiamata in modalità sincrona
sw.Restart();
var response = Task.Run(() => shellyClient.GetStatus(CancellationToken.None)).Result;
if (response.IsSuccess)
{
string serVal = JsonConvert.SerializeObject(response.Value, Formatting.Indented);
sw.Stop();
writeResult(response.IsSuccess, serVal);
}
}
}
}