219 lines
9.5 KiB
C#
219 lines
9.5 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
|
|
ShellyOptions options = new ShellyOptions()
|
|
{
|
|
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 respEmDto = Task.Run(() => shellyPro3.GetEmStatus(CancellationToken.None, 0)).Result;
|
|
if (respEmDto.IsSuccess)
|
|
{
|
|
string serValSwitch = JsonConvert.SerializeObject(respEmDto.Value, Formatting.Indented);
|
|
esitoStep = respEmDto.IsSuccess ? serValSwitch : "Errore in GetEmStatus";
|
|
}
|
|
|
|
// registro ed eseguo chiamata in modalità sincrona
|
|
sw.Restart();
|
|
var respEmDataDto = Task.Run(() => shellyPro3.GetEmDataStatus(CancellationToken.None, 0)).Result;
|
|
if (respEmDataDto.IsSuccess)
|
|
{
|
|
string serVal = JsonConvert.SerializeObject(respEmDataDto.Value, Formatting.Indented);
|
|
sw.Stop();
|
|
writeResult(respEmDataDto.IsSuccess, serVal);
|
|
}
|
|
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();
|
|
if (response.IsSuccess)
|
|
{
|
|
string serVal = JsonConvert.SerializeObject(response.Value, Formatting.Indented);
|
|
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;
|
|
sw.Stop();
|
|
if (response.IsSuccess)
|
|
{
|
|
string serVal = JsonConvert.SerializeObject(response.Value, Formatting.Indented);
|
|
writeResult(response.IsSuccess, serVal);
|
|
}
|
|
}
|
|
}
|
|
}
|