Skip to content

Commit

Permalink
Separate layers
Browse files Browse the repository at this point in the history
  • Loading branch information
Agustín Ibars committed Jul 14, 2016
1 parent 24e2208 commit 5e77cbe
Show file tree
Hide file tree
Showing 30 changed files with 498 additions and 2,732 deletions.
127 changes: 63 additions & 64 deletions PLCLogger.UI/Database.cs → PLCLogger.Data/Database.cs
Original file line number Diff line number Diff line change
@@ -1,88 +1,87 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using PLCLogger.Entities;
using PLCLogger.Logic;
using PLCLogger.Messages;

namespace PLCLogger
namespace PLCLogger.Data
{
public class Database : IDisposable
{

private readonly PLCInterface plcInterface;
protected static Configuration cfg;
protected static ISessionFactory sessionFactory = null;
protected static ITransaction transaction = null;
public static bool error;

public Log MessageLog;
public enum Modos
{
Guardar, //escribe en la db las variables leidas del PLC
LeerEscrituras //obtiene desde la db las variables que deben ser escritas en el PLC
}

public string quotes(string str)
{
return ("'" + str + "'");
}

public Database()
public Database(PLCInterface plcInterface)
{
this.plcInterface = plcInterface;

this.ConfigurarNHibernate();
MessageLog = new Log("Database");

}

}

public void Dispose()
public void Dispose()
{

}


//------------------------------------------------------------------------------------------------------------------
bool EscribirDatosVariables(PLC_Interface plc)
bool EscribirDatosVariables(PLCInterface plc)
{
bool retval = true;
plc.Variables_Escritura = new List<Variable>();
try
{
using(ISession session = sessionFactory.OpenSession())
using (ITransaction tx = session.BeginTransaction())
using (ISession session = sessionFactory.OpenSession())
using (ITransaction tx = session.BeginTransaction())
{
string hql = "from Variable v where v.escribir=:escribir and v.instante_escritura>=:t_max_escritura";
IQuery query = session.CreateQuery(hql);
query.SetParameter("escribir", true);
//define un tiempo máximo dentro del cual la escritura debe ser efectuada
query.SetParameter("t_max_escritura", DateTime.Now.AddSeconds(-10));
var var_a_escribir = (List<Variable>)query.List<Variable>();
foreach (var v in var_a_escribir)
{
string hql = "from Variable v where v.escribir=:escribir and v.instante_escritura>=:t_max_escritura";
IQuery query = session.CreateQuery(hql);
query.SetParameter("escribir", true);
//define un tiempo máximo dentro del cual la escritura debe ser efectuada
query.SetParameter("t_max_escritura", DateTime.Now.AddSeconds(-10));
var var_a_escribir = (List<Variable>)query.List<Variable>();
foreach (var v in var_a_escribir)
{
v.Address = Config.convertAdrress(v.Direccion, 2);
if (v.Type == "bit") v.Subaddress = Config.convertAdrress(v.Direccion, 3);
plc.Variables_Escritura.Add(v);
v.Address = Config.convertAdrress(v.Direccion, 2);
if (v.Type == "bit") v.Subaddress = Config.convertAdrress(v.Direccion, 3);
plc.Variables_Escritura.Add(v);

}
}

query = session.CreateQuery("from Variable v where v.escribir=:escribir");
query.SetParameter("escribir", true);
List<Variable> var_no_escritas = (List<Variable>)query.List<Variable>();
query = session.CreateQuery("from Variable v where v.escribir=:escribir");
query.SetParameter("escribir", true);
List<Variable> var_no_escritas = (List<Variable>)query.List<Variable>();

foreach (Variable v in var_no_escritas)
{
hql = "update Variable set ValorEscritura=:Valor, instante_escritura=:inst, escribir=:escribir where Name=:Name ";
query = session.CreateQuery(hql);
query.SetParameter("valor", null);
query.SetParameter("escribir", false);
query.SetParameter("name", v.Name);
query.SetParameter("inst", null);
query.ExecuteUpdate();
}
tx.Commit();
foreach (Variable v in var_no_escritas)
{
hql = "update Variable set ValorEscritura=:Valor, instante_escritura=:inst, escribir=:escribir where Name=:Name ";
query = session.CreateQuery(hql);
query.SetParameter("valor", null);
query.SetParameter("escribir", false);
query.SetParameter("name", v.Name);
query.SetParameter("inst", null);
query.ExecuteUpdate();
}
tx.Commit();
}


}


catch (Exception ex)
{
MessageLog.Add(ex.Message);
Expand All @@ -91,22 +90,22 @@ bool EscribirDatosVariables(PLC_Interface plc)
return retval;
}


//------------------------------------------------------------------------------------------------------------------
bool GuardarDatosVariables(PLC_Interface plc)
bool GuardarDatosVariables(PLCInterface plc)
{

var retval = true;
List<Variable> variables_db = null;
using(var session = sessionFactory.OpenSession())
using (var session = sessionFactory.OpenSession())
using (var tx = session.BeginTransaction())
{
string hql = "from Variable";
IQuery query = session.CreateQuery(hql).SetMaxResults(32000);
variables_db = (List<Variable>)query.List<Variable>();
tx.Commit();
}

// Lee las variables para comprobar si hay cambios
// traer las variables de la db para ver si han cambiado

Expand Down Expand Up @@ -164,7 +163,7 @@ bool GuardarDatosVariables(PLC_Interface plc)
}
tx.Commit();
}

}

catch (Exception ex)
Expand All @@ -176,22 +175,22 @@ bool GuardarDatosVariables(PLC_Interface plc)
return (retval);
}

public bool Sync(PLC_Interface plc, Modos modo)
public bool Sync(PLCInterface plc, Modos modo)
{

var retval = false;
switch (modo)
{
case Modos.Guardar:
retval = GuardarDatosVariables(plc);
break;
case Modos.LeerEscrituras:
retval = EscribirDatosVariables(plc);
break;
default:
MessageLog.Add(this.ToString() + ": No se reconoce modo=" + modo.ToString());
break;
}
switch (modo)
{
case Modos.Guardar:
retval = GuardarDatosVariables(plc);
break;
case Modos.LeerEscrituras:
retval = EscribirDatosVariables(plc);
break;
default:
MessageLog.Add(": No se reconoce modo=" + modo);
break;
}


return (retval);
Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions PLCLogger.Data/Modos.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace PLCLogger.Data
{
public enum Modos
{
Guardar, //escribe en la db las variables leidas del PLC
LeerEscrituras //obtiene desde la db las variables que deben ser escritas en el PLC
}
}
96 changes: 96 additions & 0 deletions PLCLogger.Data/PLCLogger.Data.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" 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>{D46A1FCD-060A-4503-A7A8-E70BDA6512B6}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>PLCLogger.Data</RootNamespace>
<AssemblyName>PLCLogger.Data</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</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="Iesi.Collections, Version=4.0.0.0, Culture=neutral, PublicKeyToken=aa95f207798dfdb4, processorArchitecture=MSIL">
<HintPath>..\packages\Iesi.Collections.4.0.0.4000\lib\net40\Iesi.Collections.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Modbus">
<HintPath>..\lib\Modbus.dll</HintPath>
</Reference>
<Reference Include="ModbusTCP">
<HintPath>..\lib\ModbusTCP.dll</HintPath>
</Reference>
<Reference Include="NHibernate, Version=4.0.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4, processorArchitecture=MSIL">
<HintPath>..\packages\NHibernate.4.0.4.4000\lib\net40\NHibernate.dll</HintPath>
<Private>True</Private>
</Reference>
<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" />
</ItemGroup>
<ItemGroup>
<Compile Include="Database.cs" />
<Compile Include="Modos.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PLCLogger.Entities\PLCLogger.Entities.csproj">
<Project>{ff3de516-e0c8-4c58-8b30-ce973715fe95}</Project>
<Name>PLCLogger.Entities</Name>
</ProjectReference>
<ProjectReference Include="..\PLCLogger.Logic\PLCLogger.Logic.csproj">
<Project>{ead40118-7956-47c9-81de-2bb548b0e6e5}</Project>
<Name>PLCLogger.Logic</Name>
</ProjectReference>
<ProjectReference Include="..\PLCLogger.Messages\PLCLogger.Messages.csproj">
<Project>{0fe95aa9-9e26-4fe9-8e76-6d81ae66e013}</Project>
<Name>PLCLogger.Messages</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Mapping\Variable.hbm.xml">
<SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Mapping\VariableLog.hbm.xml">
<SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
36 changes: 36 additions & 0 deletions PLCLogger.Data/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -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("PLCLogger.Data")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("PLCLogger.Data")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[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("d46a1fcd-060a-4503-a7a8-e70bda6512b6")]

// 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")]
5 changes: 5 additions & 0 deletions PLCLogger.Data/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Iesi.Collections" version="4.0.0.4000" targetFramework="net452" />
<package id="NHibernate" version="4.0.4.4000" targetFramework="net452" />
</packages>
Loading

0 comments on commit 5e77cbe

Please sign in to comment.