diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-03-14 19:02:38 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-03-14 19:02:38 +0200 |
| commit | d87881183d74fa692b598f170dd820adeaf54fca (patch) | |
| tree | d41721b5c5f9c7e8335bb5e65d7a16a20b4d901d /Software/Visual_Studio/MachineStudio | |
| parent | 748d18fe3bfa60a586de151c3bb0d1fd608b1e13 (diff) | |
| download | Tango-d87881183d74fa692b598f170dd820adeaf54fca.tar.gz Tango-d87881183d74fa692b598f170dd820adeaf54fca.zip | |
Working on event handling !
Diffstat (limited to 'Software/Visual_Studio/MachineStudio')
5 files changed, 209 insertions, 0 deletions
diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/DefaultEventLogger.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/DefaultEventLogger.cs new file mode 100644 index 000000000..c22ce335c --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/DefaultEventLogger.cs @@ -0,0 +1,144 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Tango.BL; +using Tango.BL.Entities; +using Tango.BL.Enumerations; +using Tango.Logging; +using Tango.MachineStudio.Common.Authentication; +using Tango.MachineStudio.Common.StudioApplication; +using Tango.PMR.Diagnostics; + +namespace Tango.MachineStudio.Common.EventLogging +{ + public class DefaultEventLogger : IEventLogger + { + private ObservablesContext _db; + private Thread _logThread; + private ConcurrentQueue<MachinesEvent> _events; + private IStudioApplicationManager _application; + private IAuthenticationProvider _authentication; + private Dictionary<EventTypes, String> _eventTypesGuids; + + /// <summary> + /// Initializes a new instance of the <see cref="DefaultEventLogger"/> class. + /// </summary> + /// <param name="applicationManager">The application manager.</param> + /// <param name="authenticationProvider">The authentication provider.</param> + public DefaultEventLogger(IStudioApplicationManager applicationManager, IAuthenticationProvider authenticationProvider) + { + _events = new ConcurrentQueue<MachinesEvent>(); + + _db = ObservablesContext.CreateDefault(); + _db.Configuration.LazyLoadingEnabled = false; + + _eventTypesGuids = new Dictionary<EventTypes, string>(); + + _db.ActionTypes.ToList(); + _db.EventTypesCategories.ToList(); + _db.EventTypesGroups.ToList(); + _db.EventTypes.ToList(); + + foreach (var type in _db.EventTypes) + { + _eventTypesGuids.Add((EventTypes)type.Code, type.Guid); + } + + _application = applicationManager; + _authentication = authenticationProvider; + _logThread = new Thread(LogThreadMethod); + _logThread.IsBackground = true; + _logThread.Start(); + } + + /// <summary> + /// Logs the specified machine event. + /// </summary> + /// <param name="machineEvent">The machine event.</param> + public void Log(MachinesEvent machineEvent) + { + _events.Enqueue(machineEvent); + } + + /// <summary> + /// Logs the specified event type. + /// </summary> + /// <param name="eventType">Type of the event.</param> + /// <param name="message">The message.</param> + public void Log(EventTypes eventType, string message) + { + MachinesEvent machineEvent = new MachinesEvent(); + machineEvent.MachineGuid = _application.ConnectedMachine.Guid; + machineEvent.DateTime = DateTime.UtcNow; + machineEvent.Description = message; + machineEvent.EventTypeGuid = _eventTypesGuids[eventType]; + machineEvent.UserGuid = _authentication.CurrentUser.Guid; + + Log(machineEvent); + } + + /// <summary> + /// Logs the specified hardware event. + /// </summary> + /// <param name="hardwareEvent">The hardware event.</param> + public void Log(Event hardwareEvent) + { + Log((EventTypes)hardwareEvent.Type, hardwareEvent.Message); + } + + /// <summary> + /// Logs the specified exception using the <see cref="EventTypes.ApplicationException"/>. + /// </summary> + /// <param name="exception">The exception.</param> + public void Log(Exception exception) + { + Log(EventTypes.ApplicationException, exception.Message); + } + + /// <summary> + /// Logs the specified message using the <see cref="EventTypes.ApplicationInformation"/>. + /// </summary> + /// <param name="message">The message.</param> + public void Log(String message) + { + Log(EventTypes.ApplicationInformation, message); + } + + private void LogThreadMethod() + { + try + { + while (true) + { + bool _saveChanges = false; + + while (_events.Count > 0) + { + MachinesEvent ev = null; + + if (_events.TryDequeue(out ev)) + { + _db.MachinesEvents.Add(ev); + _saveChanges = true; + } + } + + if (_saveChanges) + { + _db.SaveChanges(); + } + + Thread.Sleep(5000); + } + } + catch (Exception ex) + { + LogManager.Default.Log(ex, "Error saving machine event to database."); + } + } + } +} diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/IEventLogger.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/IEventLogger.cs new file mode 100644 index 000000000..645bd4a95 --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/IEventLogger.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.BL.Entities; +using Tango.BL.Enumerations; +using Tango.PMR.Diagnostics; + +namespace Tango.MachineStudio.Common.EventLogging +{ + public interface IEventLogger + { + /// <summary> + /// Logs the specified machine event. + /// </summary> + /// <param name="machineEvent">The machine event.</param> + void Log(MachinesEvent machineEvent); + + /// <summary> + /// Logs the specified event type. + /// </summary> + /// <param name="eventType">Type of the event.</param> + /// <param name="message">The message.</param> + void Log(EventTypes eventType, String message); + + /// <summary> + /// Logs the specified hardware event. + /// </summary> + /// <param name="hardwareEvent">The hardware event.</param> + void Log(Event hardwareEvent); + + /// <summary> + /// Logs the specified exception using the <see cref="EventTypes.ApplicationException"/>. + /// </summary> + /// <param name="exception">The exception.</param> + void Log(Exception exception); + + /// <summary> + /// Logs the specified message using the <see cref="EventTypes.ApplicationInformation"/>. + /// </summary> + /// <param name="message">The message.</param> + void Log(String message); + } +} diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Tango.MachineStudio.Common.csproj b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Tango.MachineStudio.Common.csproj index 6dfc72c7f..529419bd7 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Tango.MachineStudio.Common.csproj +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Tango.MachineStudio.Common.csproj @@ -31,6 +31,12 @@ <WarningLevel>4</WarningLevel> </PropertyGroup> <ItemGroup> + <Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL"> + <HintPath>..\..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.dll</HintPath> + </Reference> + <Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL"> + <HintPath>..\..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.SqlServer.dll</HintPath> + </Reference> <Reference Include="GalaSoft.MvvmLight, Version=5.3.0.19026, Culture=neutral, PublicKeyToken=e7570ab207bcb616, processorArchitecture=MSIL"> <HintPath>..\..\packages\MvvmLightLibs.5.3.0.0\lib\net45\GalaSoft.MvvmLight.dll</HintPath> </Reference> @@ -53,6 +59,7 @@ <HintPath>..\..\packages\CommonServiceLocator.1.3\lib\portable-net4+sl5+netcore45+wpa81+wp8\Microsoft.Practices.ServiceLocation.dll</HintPath> </Reference> <Reference Include="System" /> + <Reference Include="System.ComponentModel.DataAnnotations" /> <Reference Include="System.Data" /> <Reference Include="System.Runtime.Serialization" /> <Reference Include="System.ServiceModel" /> @@ -90,6 +97,8 @@ <Compile Include="Converters\SecondsToGraphPointsConverter.cs" /> <Compile Include="Diagnostics\DefaultDiagnosticsFrameProvider.cs" /> <Compile Include="Diagnostics\IDiagnosticsFrameProvider.cs" /> + <Compile Include="EventLogging\DefaultEventLogger.cs" /> + <Compile Include="EventLogging\IEventLogger.cs" /> <Compile Include="ExtensionMethods\CommonDialogExtensions.cs" /> <Compile Include="Helpers\GraphsHelper.cs" /> <Compile Include="Notifications\BarItem.cs" /> diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/app.config b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/app.config index cacd4cd77..4a6cb0526 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/app.config +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/app.config @@ -1,5 +1,9 @@ <?xml version="1.0" encoding="utf-8"?> <configuration> + <configSections> + <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> + <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> + </configSections> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> @@ -8,4 +12,10 @@ </dependentAssembly> </assemblyBinding> </runtime> + <entityFramework> + <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> + <providers> + <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> + </providers> + </entityFramework> </configuration>
\ No newline at end of file diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/packages.config b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/packages.config index 4fd672b32..cf0df03c8 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/packages.config +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/packages.config @@ -1,6 +1,7 @@ <?xml version="1.0" encoding="utf-8"?> <packages> <package id="CommonServiceLocator" version="1.3" targetFramework="net46" /> + <package id="EntityFramework" version="6.0.0" targetFramework="net46" /> <package id="MahApps.Metro" version="1.5.0" targetFramework="net46" /> <package id="MaterialDesignColors" version="1.1.2" targetFramework="net46" /> <package id="MaterialDesignThemes" version="2.3.1.953" targetFramework="net46" /> |
