Fix gestioen ,/. per float ThermoCam

This commit is contained in:
Samuele E. Locatelli
2020-10-30 11:58:21 +01:00
parent 6b97cd1a4d
commit b39ea3815b
@@ -14,38 +14,39 @@ using Thermo.Active.Model.DTOModels.ThWarmers;
namespace Thermo.Active.Thermocamera
{
public class ThermocameraComunicator
{
const string MMF_REQ = "CMS_MMF_REQ";
const string MMF_RES = "CMS_MMF_RES";
const int NUM_CHAR_MSG = 5000;
const int REQ = 1;
const int RES = 2;
const int DIM_MMF = NUM_CHAR_MSG * 2 + 2;
#region Private Fields
MemoryMappedFile mmf;
MemoryMappedFile mmfRes;
MemoryMappedViewAccessor accessor;
MemoryMappedViewAccessor accessorResp;
private const int DIM_MMF = NUM_CHAR_MSG * 2 + 2;
private const string MMF_REQ = "CMS_MMF_REQ";
private const string MMF_RES = "CMS_MMF_RES";
private const int NUM_CHAR_MSG = 5000;
private const int REQ = 1;
private const int RES = 2;
private static ThermocameraComunicator _instance;
private MemoryMappedViewAccessor accessor;
private MemoryMappedViewAccessor accessorResp;
private MemoryMappedFile mmf;
private MemoryMappedFile mmfRes;
#endregion Private Fields
#region Protected Fields
/// <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;
/// <summary>
/// Booleana dell'uso sw ext: true = Utilizzo vero sw lettura FLIR / false = simulazione SW RAND
/// </summary>
protected bool useTCamSw = true;
public static ThermocameraComunicator getInstance()
{
if (_instance == null)
_instance = new ThermocameraComunicator();
return _instance;
}
#endregion Protected Fields
#region Private Constructors
private ThermocameraComunicator()
{
@@ -54,80 +55,95 @@ namespace Thermo.Active.Thermocamera
accessor = mmf.CreateViewAccessor();
accessorResp = mmfRes.CreateViewAccessor();
}
#endregion Private Constructors
#region Private Properties
/// <summary>
/// Richiesta di scattare fotografia
/// Simulazione valore tra 100 e 300 °C
/// </summary>
/// <param name="timeoutMS">attesa in ms, 10000 std, da conf</param>
/// <returns></returns>
public bool takePicture(int timeoutMS)
private float simVal
{
// modalità impiego vero sw esterno
if (useTCamSw)
get
{
const string tempCommand = "SetParameter_Integer";
string response;
float answ = 100 + 200 * (float)rnd.NextDouble();
return answ;
}
}
if (!writeCommand(accessor, tempCommand + ";TAKE_IMAGE;"))
return false;
if (!readCommand(accessorResp, tempCommand, timeoutMS, out response))
return false;
if (response != "True;")
return false;
}
else
#endregion Private Properties
#region Private Methods
private bool readCommand(MemoryMappedViewAccessor accessor, string command, int timeoutMS, out string resp)
{
resp = "";
byte response = 0;
int totalCycle = 0;
int sleepTime = 500;
byte[] bytesToRead = new byte[NUM_CHAR_MSG];
if (!accessor.CanRead) return false;
//Read Pre-Command
while (response != RES)
{
// attende 5 sec
Thread.Sleep(5000);
totalCycle++;
accessor.Read(0, out response);
if (response != RES)
Thread.Sleep(sleepTime);
if (totalCycle * sleepTime > timeoutMS)
return false;
}
//Read Command
accessor.ReadArray(1, bytesToRead, 0, bytesToRead.Length);
//Elaborate String
string textRecieved = Encoding.UTF8.GetString(bytesToRead);
if (textRecieved == null) return false;
string[] textSplitted = textRecieved.Replace("\0", string.Empty).Split(';');
if (textSplitted.Length < 2) return false;
if (textSplitted[0] != command) return false;
char[] bufferToClean = Enumerable.Repeat('\0', NUM_CHAR_MSG).ToArray();
accessor.WriteArray(0, bufferToClean, 0, bufferToClean.Length);
//Output
textSplitted = textSplitted.Skip(1).ToArray();
resp = String.Join(";", textSplitted);
return true;
}
public bool showWindow(int x, int y, int dimX, int dimY, int timeoutMS)
private bool writeCommand(MemoryMappedViewAccessor accessor, string command)
{
const string tempCommand = "ShowWindow";
string response;
if (!writeCommand(accessor, tempCommand + ";" + x + ";" + y + ";" + dimX + ";" + dimY + ";"))
return false;
if (!readCommand(accessorResp, tempCommand, timeoutMS, out response))
return false;
if (response != "1;")
return false;
char[] bufferToClean = Enumerable.Repeat('\0', NUM_CHAR_MSG).ToArray();
accessor.WriteArray(0, bufferToClean, 0, bufferToClean.Length);
char[] bufferToWrite = command.ToCharArray();
//Write Pre-Command
accessor.Write(0, REQ);
//Write Command
accessor.WriteArray(1, bufferToWrite, 0, bufferToWrite.Length);
return true;
}
/// <summary>
/// Lettura temp singolo punto
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="timeoutMS"></param>
/// <param name="temp"></param>
/// <returns></returns>
public bool readTemperature(int x, int y, int timeoutMS, out float temp)
#endregion Private Methods
#region Public Methods
public static ThermocameraComunicator getInstance()
{
temp = 0f;
// 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;
if (_instance == null)
_instance = new ThermocameraComunicator();
return _instance;
}
/// <summary>
/// Restituisce lettura di tutti i punti (cetroidi) richeisti
/// </summary>
@@ -184,75 +200,83 @@ namespace Thermo.Active.Thermocamera
return true;
}
private bool writeCommand(MemoryMappedViewAccessor accessor, string command)
/// <summary>
/// Lettura temp singolo punto
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="timeoutMS"></param>
/// <param name="temp"></param>
/// <returns></returns>
public bool readTemperature(int x, int y, int timeoutMS, out float temp)
{
char[] bufferToClean = Enumerable.Repeat('\0', NUM_CHAR_MSG).ToArray();
accessor.WriteArray(0, bufferToClean, 0, bufferToClean.Length);
temp = 0f;
char[] bufferToWrite = command.ToCharArray();
//Write Pre-Command
accessor.Write(0, REQ);
//Write Command
accessor.WriteArray(1, bufferToWrite, 0, bufferToWrite.Length);
return true;
}
private bool readCommand(MemoryMappedViewAccessor accessor, string command, int timeoutMS, out string resp)
{
resp = "";
byte response = 0;
int totalCycle = 0;
int sleepTime = 500;
byte[] bytesToRead = new byte[NUM_CHAR_MSG];
if (!accessor.CanRead) return false;
//Read Pre-Command
while (response != RES)
// modalità impiego vero sw esterno
if (useTCamSw)
{
totalCycle++;
accessor.Read(0, out response);
if (response != RES)
Thread.Sleep(sleepTime);
if (totalCycle * sleepTime > timeoutMS)
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;
}
//Read Command
accessor.ReadArray(1, bytesToRead, 0, bytesToRead.Length);
//Elaborate String
string textRecieved = Encoding.UTF8.GetString(bytesToRead);
if (textRecieved == null) return false;
string[] textSplitted = textRecieved.Replace("\0", string.Empty).Split(';');
if (textSplitted.Length < 2) return false;
if (textSplitted[0] != command) return false;
char[] bufferToClean = Enumerable.Repeat('\0', NUM_CHAR_MSG).ToArray();
accessor.WriteArray(0, bufferToClean, 0, bufferToClean.Length);
//Output
textSplitted = textSplitted.Skip(1).ToArray();
resp = String.Join(";", textSplitted);
else
{
// simulo!
temp = simVal;
// attende 5 sec
Thread.Sleep(5000);
}
return true;
}
/// <summary>
/// Simulazione valore tra 100 e 300 °C
/// </summary>
private float simVal
public bool showWindow(int x, int y, int dimX, int dimY, int timeoutMS)
{
get
{
float answ = 100 + 200 * (float)rnd.NextDouble();
return answ;
}
const string tempCommand = "ShowWindow";
string response;
if (!writeCommand(accessor, tempCommand + ";" + x + ";" + y + ";" + dimX + ";" + dimY + ";"))
return false;
if (!readCommand(accessorResp, tempCommand, timeoutMS, out response))
return false;
if (response != "1;")
return false;
return true;
}
/// <summary>
/// Richiesta di scattare fotografia
/// </summary>
/// <param name="timeoutMS">attesa in ms, 10000 std, da conf</param>
/// <returns></returns>
public bool takePicture(int timeoutMS)
{
// 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;
}
else
{
// attende 5 sec
Thread.Sleep(5000);
}
return true;
}
#endregion Public Methods
}
}
}