First commit of thermocamera

This commit is contained in:
Samuele E. Locatelli
2020-10-22 17:02:39 +02:00
parent cbe5e7d303
commit 44702e769a
7 changed files with 347 additions and 0 deletions
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("THermo.Active.Thermocamera")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("THermo.Active.Thermocamera")]
[assembly: AssemblyCopyright("Copyright © 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("8d8ec91a-3a15-4a1d-951b-a35e7068debd")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{8D8EC91A-3A15-4A1D-951B-A35E7068DEBD}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>THermo.Active.Thermocamera</RootNamespace>
<AssemblyName>THermo.Active.Thermocamera</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="ThermocameraComunicator.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
@@ -0,0 +1,181 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
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;
MemoryMappedFile mmf;
MemoryMappedFile mmfRes;
MemoryMappedViewAccessor accessor;
MemoryMappedViewAccessor accessorResp;
private static ThermocameraComunicator _instance;
public static ThermocameraComunicator getInstance()
{
if (_instance == null)
_instance = new ThermocameraComunicator();
return _instance;
}
private ThermocameraComunicator()
{
mmf = MemoryMappedFile.CreateOrOpen(MMF_REQ, DIM_MMF);
mmfRes = MemoryMappedFile.CreateOrOpen(MMF_RES, DIM_MMF);
accessor = mmf.CreateViewAccessor();
accessorResp = mmfRes.CreateViewAccessor();
}
public bool takePicture(int timeoutMS)
{
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;
return true;
}
public bool showWindow(int x, int y, int dimX, int dimY, int timeoutMS)
{
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;
}
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;
return true;
}
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)
{
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;
}
}
return true;
}
private bool writeCommand(MemoryMappedViewAccessor accessor, string command)
{
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;
}
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)
{
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;
}
}
}
@@ -76,6 +76,22 @@
<key>LoaderXsize</key>
<value>1400</value>
</entry>
<entry>
<key>ThermoCameraXpos</key>
<value>68</value>
</entry>
<entry>
<key>ThermoCameraYpos</key>
<value>728</value>
</entry>
<entry>
<key>ThermoCameraXdim</key>
<value>828</value>
</entry>
<entry>
<key>ThermoCameraYdim</key>
<value>1004</value>
</entry>
</additionalParameters>
<unitOfMeasures>
<unitOfMeasure id="0" value="" />
+15
View File
@@ -41,6 +41,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Thermo.Active.CmsConnectGat
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CMS_CORE_Library", "..\cms_core_library\CMS_CORE_Library\CMS_CORE_Library.csproj", "{4ABF8EEF-2B23-483E-ACDC-53214FE28681}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Thermo.Active.Thermocamera", "THermo.Active.Thermocamera\Thermo.Active.Thermocamera.csproj", "{8D8EC91A-3A15-4A1D-951B-A35E7068DEBD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -219,6 +221,18 @@ Global
{4ABF8EEF-2B23-483E-ACDC-53214FE28681}.Release|x64.Build.0 = Release|x64
{4ABF8EEF-2B23-483E-ACDC-53214FE28681}.Release|x86.ActiveCfg = Release|x86
{4ABF8EEF-2B23-483E-ACDC-53214FE28681}.Release|x86.Build.0 = Release|x86
{8D8EC91A-3A15-4A1D-951B-A35E7068DEBD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8D8EC91A-3A15-4A1D-951B-A35E7068DEBD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8D8EC91A-3A15-4A1D-951B-A35E7068DEBD}.Debug|x64.ActiveCfg = Debug|Any CPU
{8D8EC91A-3A15-4A1D-951B-A35E7068DEBD}.Debug|x64.Build.0 = Debug|Any CPU
{8D8EC91A-3A15-4A1D-951B-A35E7068DEBD}.Debug|x86.ActiveCfg = Debug|Any CPU
{8D8EC91A-3A15-4A1D-951B-A35E7068DEBD}.Debug|x86.Build.0 = Debug|Any CPU
{8D8EC91A-3A15-4A1D-951B-A35E7068DEBD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8D8EC91A-3A15-4A1D-951B-A35E7068DEBD}.Release|Any CPU.Build.0 = Release|Any CPU
{8D8EC91A-3A15-4A1D-951B-A35E7068DEBD}.Release|x64.ActiveCfg = Release|Any CPU
{8D8EC91A-3A15-4A1D-951B-A35E7068DEBD}.Release|x64.Build.0 = Release|Any CPU
{8D8EC91A-3A15-4A1D-951B-A35E7068DEBD}.Release|x86.ActiveCfg = Release|Any CPU
{8D8EC91A-3A15-4A1D-951B-A35E7068DEBD}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -237,6 +251,7 @@ Global
{B2366B08-96BD-4F6B-B748-B45089B87A14} = {0769EE3C-4259-4C72-97B4-0CCAEBFA7724}
{F9F17F23-660E-488C-A7FA-6A5B35D64313} = {2F873243-A483-40B6-A0F7-65FC3541A269}
{49B04D99-0ECD-4900-86D3-7098D61314D7} = {0769EE3C-4259-4C72-97B4-0CCAEBFA7724}
{8D8EC91A-3A15-4A1D-951B-A35E7068DEBD} = {0769EE3C-4259-4C72-97B4-0CCAEBFA7724}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {51D459FB-B45B-4A47-984E-46C35F933A82}
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using Thermo.Active.Database.Controllers;
using Thermo.Active.Listeners;
using Thermo.Active.Model.DatabaseModels;
using Thermo.Active.Model.DTOModels.AlarmModels;
using Thermo.Active.Provider;
using Thermo.Active.Thermocamera;
using static Thermo.Active.Config.ServerConfig;
using static Thermo.Active.Model.Constants;
namespace Thermo.Active.Controllers.WebApi
{
[RoutePrefix("api/thermocamera")]
public class ThermocameraController : ApiController
{
[Route("show"), HttpPost]
public IHttpActionResult GetDataPaginated()
{
String ThermoCameraXpos = AdditionalParametersConfig["ThermoCameraXpos"];
String ThermoCameraYpos = AdditionalParametersConfig["ThermoCameraYpos"];
String ThermoCameraXdim = AdditionalParametersConfig["ThermoCameraXdim"];
String ThermoCameraYdim = AdditionalParametersConfig["ThermoCameraYdim"];
if(ThermoCameraXpos != null && ThermoCameraYpos != null && ThermoCameraXdim != null && ThermoCameraYdim != null)
{
if (ThermocameraComunicator.getInstance().showWindow(Int32.Parse(ThermoCameraXpos), Int32.Parse(ThermoCameraYpos), Int32.Parse(ThermoCameraXdim), Int32.Parse(ThermoCameraYdim), 3000))
return Ok();
else
return BadRequest();
}
return BadRequest();
}
}
}
+5
View File
@@ -227,6 +227,7 @@
<Compile Include="Controllers\WebApi\ModulesController.cs" />
<Compile Include="Controllers\WebApi\ProdController.cs" />
<Compile Include="Controllers\WebApi\StarredUserSoftKeyController.cs" />
<Compile Include="Controllers\WebApi\ThermocameraController.cs" />
<Compile Include="Controllers\WebApi\WarmersController.cs" />
<Compile Include="Controllers\WebApi\RecipeController.cs" />
<Compile Include="Controllers\WebApi\AuthorizationController.cs" />
@@ -16195,6 +16196,10 @@
<Project>{b2366b08-96bd-4f6b-b748-b45089b87a14}</Project>
<Name>Thermo.Active.NC</Name>
</ProjectReference>
<ProjectReference Include="..\THermo.Active.Thermocamera\Thermo.Active.Thermocamera.csproj">
<Project>{8d8ec91a-3a15-4a1d-951b-a35e7068debd}</Project>
<Name>Thermo.Active.Thermocamera</Name>
</ProjectReference>
<ProjectReference Include="..\Thermo.Active.UI\Thermo.Active.UI.csproj">
<Project>{20fc0937-e7ca-4693-95f9-7a948efd173b}</Project>
<Name>Thermo.Active.UI</Name>