Aggiunta progetto test REst eseguibile in VsCode x testing di base

This commit is contained in:
2024-11-11 19:59:57 +01:00
parent b74465fff7
commit e727f695ae
16 changed files with 211 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
{
"dotnet.defaultSolution": "RestApp.sln"
}
+22
View File
@@ -0,0 +1,22 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RestApp", "RestApp\RestApp.csproj", "{E3094EFE-15CB-40A4-8163-194AE19A7969}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E3094EFE-15CB-40A4-8163-194AE19A7969}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E3094EFE-15CB-40A4-8163-194AE19A7969}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E3094EFE-15CB-40A4-8163-194AE19A7969}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E3094EFE-15CB-40A4-8163-194AE19A7969}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
+99
View File
@@ -0,0 +1,99 @@
// See https://aka.ms/new-console-template for more information
using System.Diagnostics;
using System.Dynamic;
using Newtonsoft.Json;
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine(sep);
Console.WriteLine("Rest Client test application");
Console.WriteLine(sep);
Console.WriteLine();
Console.WriteLine("Call connection");
// setup oggetti
int tOut = 60;
string apiUrl = "http://192.168.80.61:8733/DRIVER_MES/rest/";
rCall = new RestCaller(apiUrl, tOut);
// chiamate!
CallAndLog("checkconnection");
string token = CallAndLog("gettoken/mecmaticames/mecmatica@mes").Replace("\"", "");
string gLamp = CallAndLog($"getsingleladderio/{token}/Y/8E");
string yLamp = CallAndLog($"getsingleladderio/{token}/Y/21");
string rLamp = CallAndLog($"getsingleladderio/{token}/Y/22");
string qtyTot = CallAndLog($"gettotalquantity/{token}");
string qtyReq = CallAndLog($"getneededquantity/{token}");
string qtyWrk = CallAndLog($"getworkedquantity/{token}");
string cTime = CallAndLog($"getcycletime/{token}");
string pName = CallAndLog($"getmainprogram/{token}");
string cAlarm = CallAndLog($"getalarm/{token}");
var sAlarm = JsonConvert.DeserializeObject<WarnInfo>(cAlarm);
cAlarm = JsonConvert.SerializeObject(sAlarm, Formatting.Indented);
string allAlarm = CallAndLog($"getallalarmlog/{token}");
var alarmList = JsonConvert.DeserializeObject<List<AlarmInfo>>(allAlarm);
allAlarm = JsonConvert.SerializeObject(alarmList, Formatting.Indented);
string err = CallAndLog($"getsingleladderio/ABC/Y/22");
// ora scrivo i files dei test x debug successivo
var appPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
var fPath = Directory.GetParent(appPath).FullName;
string basePath = Path.Combine(fPath, "Test");
if (!Directory.Exists(basePath))
{
Directory.CreateDirectory(basePath);
}
File.WriteAllText(Path.Combine(basePath, "token.txt"), token);
File.WriteAllText(Path.Combine(basePath, "gLamp.txt"), gLamp);
File.WriteAllText(Path.Combine(basePath, "yLamp.txt"), yLamp);
File.WriteAllText(Path.Combine(basePath, "rLamp.txt"), rLamp);
File.WriteAllText(Path.Combine(basePath, "qtyTot.txt"), qtyTot);
File.WriteAllText(Path.Combine(basePath, "qtyReq.txt"), qtyReq);
File.WriteAllText(Path.Combine(basePath, "qtyWrk.txt"), qtyWrk);
File.WriteAllText(Path.Combine(basePath, "cTime.txt"), cTime);
File.WriteAllText(Path.Combine(basePath, "pName.txt"), pName);
File.WriteAllText(Path.Combine(basePath, "cAlarm.txt"), cAlarm);
File.WriteAllText(Path.Combine(basePath, "allAlarm.txt"), allAlarm);
}
private static RestCaller rCall { get; set; } = new RestCaller("http://localhost", 60);
private static string sep = "------------------------";
private static Stopwatch sw = new Stopwatch();
private static string CallAndLog(string fullUri)
{
sw.Restart();
Console.WriteLine(sep);
Console.WriteLine($"Calling: {fullUri}");
// chiamo
string answ = rCall.ExecuteCallGet(fullUri);
sw.Stop();
// loggo
Console.WriteLine(answ);
Console.WriteLine($"Elapsed: {sw.ElapsedMilliseconds}");
Console.WriteLine(sep);
Console.WriteLine();
return answ;
}
}
internal class AlarmInfo
{
public int id { get; set; } = 0;
public string? ErrorMessage { get; set; } = null;
public DateTime date { get; set; } = DateTime.Today.AddYears(-10);
public string message { get; set; } = "";
public string type { get; set; } = "";
}
internal class WarnInfo
{
public bool IsAlarm { get; set; } = false;
public bool IsCaution { get; set; } = false;
public string? ErrorMessage { get; set; } = null;
public string message { get; set; } = "";
}
+15
View File
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="RestSharp" Version="112.1.0" />
</ItemGroup>
</Project>
+62
View File
@@ -0,0 +1,62 @@
using RestSharp;
public class RestCaller
{
/// <summary>
/// Classe gestione chiamate REST
/// </summary>
/// <param name="ApiUrl"></param>
/// <param name="TimeOutSec"></param>
public RestCaller(string ApiUrl, int TimeOutSec)
{
tOut = TimeOutSec;
apiUrl = ApiUrl;
restOptStd = new RestClientOptions
{
Timeout = TimeSpan.FromSeconds(tOut),
BaseUrl = new Uri(apiUrl)
};
}
/// <summary>
/// Esecuzione chiamata Rest tipo GET
/// </summary>
/// <param name="resource"></param>
/// <returns></returns>
public string ExecuteCallGet(string resource)
{
string answ = "";
if (!string.IsNullOrEmpty(resource))
{
// client chiamate rest
using (var client = new RestClient(restOptStd))
{
var currReq = new RestRequest(resource, Method.Get);
// currReq.AddHeader("Content-type", "application/xml");
currReq.AddHeader("Content-type", "application/json");
// effettuo vera chiamata
var currResp = client.Get(currReq);
if (currResp.StatusCode == System.Net.HttpStatusCode.OK && currResp.Content != null)
{
answ = $"{currResp.Content}";
}
}
}
return answ;
}
protected string apiUrl = "";
protected int tOut = 60;
/// <summary>
/// Conf client RestSharp standard:
/// - timeout 1 min
/// </summary>
private RestClientOptions restOptStd = new RestClientOptions { Timeout = TimeSpan.FromSeconds(60) };
}
File diff suppressed because one or more lines are too long
+1
View File
@@ -0,0 +1 @@
{"ErrorMessage":null,"IsAlarm":false,"IsCaution":true,"message":"CAUTION ON MACHINE"}
+1
View File
@@ -0,0 +1 @@
56
+1
View File
@@ -0,0 +1 @@
{"Address":"8E","Description":"","ErrorMessage":null,"IOType":"Y","VarValue":"0"}
+1
View File
@@ -0,0 +1 @@
500
+1
View File
@@ -0,0 +1 @@
500
+1
View File
@@ -0,0 +1 @@
0
+1
View File
@@ -0,0 +1 @@
{"Address":"22","Description":"","ErrorMessage":null,"IOType":"Y","VarValue":"0"}
+1
View File
@@ -0,0 +1 @@
6d65636d61746963616d6573c2a76d65636d6174696361406d6573c2a731
+1
View File
@@ -0,0 +1 @@
{"Address":"21","Description":"","ErrorMessage":null,"IOType":"Y","VarValue":"0"}