Files
mapo-iob-libs/EgwProxy.Shelly.Test/Program.cs
T
Samuele Locatelli ca956afd7e - update SHelly libs
- update EgwConf (valutare se da postare iin MapoSDK finito il lavoro ora è direttamente in IOB-WIN)
2025-02-14 07:22:20 +01:00

174 lines
7.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
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;
}
//// caffè
//private string shellyAddr = "10.74.81.72";
//APC
private string shellyAddr = "10.74.81.71";
/// <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 = System.IO.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 shelly = new Shelly1PmClient(new HttpClient(), options);
// test chiamata completa
serverTest(shelly);
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(() => shelly.GetStatus(CancellationToken.None)).Result;
if (respFull.IsSuccess)
{
esitoStep = JsonConvert.SerializeObject(respFull.Value, Formatting.Indented);
}
else
{
esitoStep = "Errore in GetStatus";
}
break;
case stepType.getSwitchStatus:
var respSwitch = Task.Run(() => shelly.GetSwitchStatus(CancellationToken.None, 0)).Result;
if (respSwitch.IsSuccess)
{
esitoStep = JsonConvert.SerializeObject(respSwitch.Value, Formatting.Indented);
}
else
{
esitoStep = "Errore in GetSwitchStatus";
}
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;
sw.Stop();
Console.WriteLine($"CallSuccess: {response.IsSuccess}");
if (response.Value != null)
{
Console.WriteLine(separator);
Console.WriteLine(JsonConvert.SerializeObject(response.Value, Formatting.Indented));
Console.WriteLine(separator);
Console.WriteLine($"Elapsed: {sw.Elapsed.TotalMilliseconds:N3}ms");
Console.WriteLine(separator);
Console.WriteLine();
}
}
}
}