Mock metodo lettura sw termocamera x simulare

This commit is contained in:
Samuele Locatelli
2020-10-24 14:42:00 +02:00
parent 5bf06971df
commit ca80c97455
@@ -5,6 +5,7 @@ using System.Drawing;
using System.Globalization;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
@@ -27,6 +28,15 @@ namespace Thermo.Active.Thermocamera
MemoryMappedViewAccessor accessor;
MemoryMappedViewAccessor accessorResp;
/// <summary>
/// Booleana dell'uso sw ext: true = Utilizzo vero sw lettura FLIR / false = simulazione SW RAND
/// </summary>
protected bool useTCamSw = true;
/// <summary>
/// Generatore random
/// </summary>
protected Random rnd = new Random();
private static ThermocameraComunicator _instance;
public static ThermocameraComunicator getInstance()
@@ -50,15 +60,24 @@ namespace Thermo.Active.Thermocamera
/// <returns></returns>
public bool takePicture(int timeoutMS)
{
const string tempCommand = "SetParameter_Integer";
string response;
// modalità impiego vero sw esterno
if (useTCamSw)
{
const string tempCommand = "SetParameter_Integer";
string response;
if (!writeCommand(accessor, tempCommand + ";TAKE_IMAGE;"))
return false;
if (!readCommand(accessorResp, tempCommand, timeoutMS, out response))
return false;
if (response != "True;")
return false;
if (!writeCommand(accessor, tempCommand + ";TAKE_IMAGE;"))
return false;
if (!readCommand(accessorResp, tempCommand, timeoutMS, out response))
return false;
if (response != "True;")
return false;
}
else
{
// attende 5 sec
Thread.Sleep(5000);
}
return true;
}
public bool showWindow(int x, int y, int dimX, int dimY, int timeoutMS)
@@ -84,17 +103,28 @@ namespace Thermo.Active.Thermocamera
public bool readTemperature(int x, int y, int timeoutMS, out float temp)
{
temp = 0f;
const string tempCommand = "GetTemperature";
string response;
if (!writeCommand(accessor, tempCommand + ";" + x + ";" + y + ";"))
return false;
if (!readCommand(accessorResp, tempCommand, timeoutMS, out response))
return false;
response = response.TrimEnd(';');
if (!float.TryParse(response, NumberStyles.Float, CultureInfo.InvariantCulture, out temp))
return false;
// modalità impiego vero sw esterno
if (useTCamSw)
{
const string tempCommand = "GetTemperature";
string response;
if (!writeCommand(accessor, tempCommand + ";" + x + ";" + y + ";"))
return false;
if (!readCommand(accessorResp, tempCommand, timeoutMS, out response))
return false;
response = response.TrimEnd(';');
if (!float.TryParse(response, NumberStyles.Float, CultureInfo.InvariantCulture, out temp))
return false;
}
else
{
// simulo!
temp = simVal;
// attende 5 sec
Thread.Sleep(5000);
}
return true;
}
/// <summary>
@@ -107,31 +137,45 @@ namespace Thermo.Active.Thermocamera
public bool readMultiTemperatures(IEnumerable<Point> points, int timeoutMS, out List<float> temp)
{
temp = new List<float>();
const string tempCommand = "GetTemperature";
string response;
string cmdRead = "";
foreach (Point point in points)
// modalità impiego vero sw esterno
if (useTCamSw)
{
cmdRead += point.X + ";" + point.Y + ";";
}
const string tempCommand = "GetTemperature";
string response;
if (!writeCommand(accessor, tempCommand + ";" + cmdRead))
return false;
if (!readCommand(accessorResp, tempCommand, timeoutMS, out response))
return false;
string[] respSplitted = response.Split(';');
foreach (string str in respSplitted)
{
float tmp;
if (str.Trim() != "")
string cmdRead = "";
foreach (Point point in points)
{
if (float.TryParse(str.Trim(), NumberStyles.Float, CultureInfo.InvariantCulture, out tmp))
temp.Add(tmp);
else
return false;
cmdRead += point.X + ";" + point.Y + ";";
}
if (!writeCommand(accessor, tempCommand + ";" + cmdRead))
return false;
if (!readCommand(accessorResp, tempCommand, timeoutMS, out response))
return false;
string[] respSplitted = response.Split(';');
foreach (string str in respSplitted)
{
float tmp;
if (str.Trim() != "")
{
if (float.TryParse(str.Trim(), NumberStyles.Float, CultureInfo.InvariantCulture, out tmp))
temp.Add(tmp);
else
return false;
}
}
}
else
{
// simulo!
for (int i = 0; i < points.Count(); i++)
{
temp.Add(simVal);
}
// attende 5 sec
Thread.Sleep(5000);
}
return true;
}
@@ -194,6 +238,17 @@ namespace Thermo.Active.Thermocamera
resp = String.Join(";", textSplitted);
return true;
}
/// <summary>
/// Simulazione valore tra 100 e 300 °C
/// </summary>
private float simVal
{
get
{
float answ = 100 + 200 * (float)rnd.NextDouble();
return answ;
}
}
}
}