From a1d55aa060450b0a16b597794686b2aa41eea259 Mon Sep 17 00:00:00 2001 From: Roy Ben Shabat Date: Sat, 30 Aug 2025 22:05:03 +0300 Subject: PPC Telemetry First Integration. --- Software/Visual_Studio/PPC/Tango.PPC.UI/App.config | 42 ++++- .../PPC/Tango.PPC.UI/Tango.PPC.UI.csproj | 177 ++++++++++++++++- .../Telemetry/DefaultTelemetryProvider.cs | 209 +++++++++++++++++++++ .../PPC/Tango.PPC.UI/ViewModelLocator.cs | 4 + .../Visual_Studio/PPC/Tango.PPC.UI/packages.config | 74 ++++++++ 5 files changed, 503 insertions(+), 3 deletions(-) create mode 100644 Software/Visual_Studio/PPC/Tango.PPC.UI/Telemetry/DefaultTelemetryProvider.cs (limited to 'Software/Visual_Studio/PPC/Tango.PPC.UI') diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/App.config b/Software/Visual_Studio/PPC/Tango.PPC.UI/App.config index 4d7554231..53a599bee 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.UI/App.config +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/App.config @@ -84,7 +84,7 @@ - + @@ -96,6 +96,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/Tango.PPC.UI.csproj b/Software/Visual_Studio/PPC/Tango.PPC.UI/Tango.PPC.UI.csproj index 31c46b296..99a7e7c75 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.UI/Tango.PPC.UI.csproj +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/Tango.PPC.UI.csproj @@ -98,6 +98,24 @@ ..\..\packages\ControlzEx.3.0.2.4\lib\net45\ControlzEx.dll + + ..\..\packages\DotNetty.Buffers.0.4.6\lib\net45\DotNetty.Buffers.dll + + + ..\..\packages\DotNetty.Codecs.0.4.6\lib\net45\DotNetty.Codecs.dll + + + ..\..\packages\DotNetty.Codecs.Mqtt.0.4.6\lib\net45\DotNetty.Codecs.Mqtt.dll + + + ..\..\packages\DotNetty.Common.0.4.6\lib\net45\DotNetty.Common.dll + + + ..\..\packages\DotNetty.Handlers.0.4.6\lib\net45\DotNetty.Handlers.dll + + + ..\..\packages\DotNetty.Transport.0.4.6\lib\net45\DotNetty.Transport.dll + ..\..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.dll @@ -110,14 +128,84 @@ ..\..\packages\Google.Protobuf.3.4.1\lib\net45\Google.Protobuf.dll + + ..\..\packages\Microsoft.Azure.Amqp.2.0.6\lib\net45\Microsoft.Azure.Amqp.dll + + + ..\..\packages\Microsoft.Azure.Devices.Client.1.6.0\lib\net45\Microsoft.Azure.Devices.Client.dll + + + ..\..\packages\Microsoft.Azure.Devices.Shared.1.3.0\lib\net45\Microsoft.Azure.Devices.Shared.dll + + + ..\..\packages\Microsoft.Azure.KeyVault.Core.1.0.0\lib\net40\Microsoft.Azure.KeyVault.Core.dll + + + ..\..\packages\Microsoft.Data.Edm.5.6.4\lib\net40\Microsoft.Data.Edm.dll + + + ..\..\packages\Microsoft.Data.OData.5.6.4\lib\net40\Microsoft.Data.OData.dll + + + ..\..\packages\Microsoft.Data.Services.Client.5.6.4\lib\net40\Microsoft.Data.Services.Client.dll + + + ..\..\packages\Microsoft.Extensions.DependencyInjection.Abstractions.1.1.0\lib\netstandard1.0\Microsoft.Extensions.DependencyInjection.Abstractions.dll + + + ..\..\packages\Microsoft.Extensions.Logging.1.1.1\lib\netstandard1.1\Microsoft.Extensions.Logging.dll + + + ..\..\packages\Microsoft.Extensions.Logging.Abstractions.1.1.1\lib\netstandard1.1\Microsoft.Extensions.Logging.Abstractions.dll + + + ..\..\packages\EnterpriseLibrary.TransientFaultHandling.6.0.1304.0\lib\portable-net45+win+wp8\Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.dll + + + ..\..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll + True + True + ..\..\packages\Microsoft.WindowsAPICodePack-Core.1.1.0.0\lib\Microsoft.WindowsAPICodePack.dll + + ..\..\packages\WindowsAzure.Storage.7.0.0\lib\net40\Microsoft.WindowsAzure.Storage.dll + + + ..\..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll + + + ..\..\packages\PCLCrypto.2.0.147\lib\net45\PCLCrypto.dll + + + ..\..\packages\PInvoke.BCrypt.0.3.2\lib\net40\PInvoke.BCrypt.dll + + + ..\..\packages\PInvoke.Kernel32.0.3.2\lib\net40\PInvoke.Kernel32.dll + + + ..\..\packages\PInvoke.NCrypt.0.3.2\lib\net40\PInvoke.NCrypt.dll + + + ..\..\packages\PInvoke.Windows.Core.0.3.2\lib\portable-net45+win+wpa81+MonoAndroid10+xamarinios10+MonoTouch10\PInvoke.Windows.Core.dll + ..\..\packages\SimpleValidator.0.6.1.0\lib\net40\SimpleValidator.dll + + ..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll + True + True + + + + ..\..\packages\System.Console.4.3.0\lib\net46\System.Console.dll + True + True + ..\..\packages\System.Data.SQLite.Core.1.0.108.0\lib\net46\System.Data.SQLite.dll @@ -131,7 +219,78 @@ ..\..\packages\System.Data.SQLite.Linq.1.0.108.0\lib\net46\System.Data.SQLite.Linq.dll True + + ..\..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll + + + ..\..\packages\System.Globalization.Calendars.4.3.0\lib\net46\System.Globalization.Calendars.dll + True + True + + + ..\..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll + True + True + + + + ..\..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll + True + True + + + ..\..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll + True + True + + + ..\..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll + True + True + + + ..\..\packages\System.Net.Http.4.3.0\lib\net46\System.Net.Http.dll + True + True + + + ..\..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll + + + ..\..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll + True + True + + + + ..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll + True + True + + + ..\..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net461\System.Security.Cryptography.Algorithms.dll + True + True + + + ..\..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll + True + True + + + ..\..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll + True + True + + + ..\..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net461\System.Security.Cryptography.X509Certificates.dll + True + True + + + ..\..\packages\System.Spatial.5.6.4\lib\net40\System.Spatial.dll + ..\..\packages\Expression.Blend.Sdk.1.0.2\lib\net45\System.Windows.Interactivity.dll @@ -141,10 +300,17 @@ - 4.0 + + ..\..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll + True + True + + + ..\..\packages\Validation.2.2.8\lib\dotnet\Validation.dll + @@ -288,6 +454,7 @@ + @@ -707,7 +874,9 @@ - + + Designer + @@ -778,6 +947,10 @@ {e1e66ed9-597d-45fa-8048-de90a6930484} Tango.SQLExaminer + + {af593663-d4e9-4a14-a3f2-fea57f30e9e6} + Tango.Telemetry + {998f8471-dc1b-41b6-9d96-354e1b4e7a32} Tango.TFS diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/Telemetry/DefaultTelemetryProvider.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/Telemetry/DefaultTelemetryProvider.cs new file mode 100644 index 000000000..04837bb38 --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/Telemetry/DefaultTelemetryProvider.cs @@ -0,0 +1,209 @@ +using System; +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.Core.DI; +using Tango.PPC.Common; +using Tango.PPC.Common.Application; +using Tango.PPC.Common.Connection; +using Tango.PPC.Common.Telemetry; +using Tango.PPC.Common.Web; +using Tango.Settings; +using Tango.Telemetry; +using Tango.Telemetry.Destinations; +using Tango.Telemetry.Helpers; +using Tango.Telemetry.Sources; + +namespace Tango.PPC.UI.Telemetry +{ + [TangoCreateWhenRegistered] + public class DefaultTelemetryProvider : ITelemetryProvider, ITelemetryCheckpointsRecoveryClient + { + private PPCWebClient _webClient; + private Machine _machine; + private IMachineProvider _machineProvider; + private PPCSettings _ppcSettings; + + public TelemetrySettings Settings { get; set; } + + public ITelemetryPublisher TelemetryPublisher { get; set; } + + public DefaultTelemetryProvider(IPPCApplicationManager applicationManager, IMachineProvider machineProvider, PPCWebClient webClient) + { + Settings = SettingsManager.Default.GetOrCreate(); + applicationManager.ApplicationReady += ApplicationManager_ApplicationReady; + _webClient = webClient; + _machineProvider = machineProvider; + } + + private void ApplicationManager_ApplicationReady(object sender, EventArgs e) + { + try + { + if (Settings.Enable) + { + Init(); + } + } + catch { } + } + + private void Init() + { + if (_machineProvider.Machine == null) return; + + _machine = _machineProvider.Machine; + + _ppcSettings = SettingsManager.Default.GetOrCreate(); + + using (ObservablesContext db = ObservablesContext.CreateDefault()) + { + var site = db.Sites.FirstOrDefault(x => x.Guid == _machine.SiteGuid); + + TelemetryPublisher = new TelemetryPublisher(new TelemetryPublisherConfiguration() + { + Environment = _ppcSettings.DeploymentSlot.ToString(), + Organization = _machine.Organization.Name, + Site = site?.Name, + SerialNumber = _machine.SerialNumber, + MachineType = _machine.Type, + EnableBackoff = Settings.EnableBackoff, + HistorySourcesRequestInterval = Settings.HistorySourcesRequestInterval, + MaxExponentialBackoff = Settings.MaxExponentialBackoff, + PendingStorageCheckInterval = Settings.PendingStorageCheckInterval, + }, this); + + (TelemetryPublisher.StorageManager as TelemetryLiteDBStorageManager).EnableCheckPointsRecovery = true; + (TelemetryPublisher.StorageManager as TelemetryLiteDBStorageManager).EnforceCheckpointsRecovery = true; + } + + InternetConnectivity.EnsureStarted(); + + Task.Factory.StartNew(async () => + { + while (!InternetConnectivity.IsInternetAvailable()) + { + Thread.Sleep(5000); + } + + if (!_webClient.IsAuthenticated) + { + await _webClient.Login(new LoginRequest() { Mode = LoginMode.Machine, SerialNumber = _machine.SerialNumber, MachineGuid = _machine.Guid }); + } + + var response = await _webClient.GetTelemetryDeviceConnection(new TelemetryDeviceRegistrationRequest()); + Settings.IoTHubConnectionString = response.ConnectionString; + Settings.Save(); + + if (Settings.EnableIoTHub) + { + TelemetryPublisher.RegisterDestination(new TelemetryAzureHubDestination(Settings.IoTHubConnectionString)); + } + + if (Settings.EnableMqtt) + { + TelemetryPublisher.RegisterDestination(new TelemetryMqttDestination($"Twine {_machine.SerialNumber}")); + } + + if (Settings.SendDiagnostics) + { + TelemetryPublisher.RegisterSource(new TelemetryDiagnosticsStreamingSource(_machineProvider.MachineOperator) + { + Config = new TelemetryDiagnosticsStreamingSourceConfig() + { + DiagnosticsSamplingInterval = Settings.DiagnosticsSamplingInterval, + } + }); + } + + if (Settings.SendEvents) + { + TelemetryPublisher.RegisterSource(new TelemetryEventsStreamingSource(_machineProvider.MachineOperator)); + } + + if (Settings.SendJobRuns) + { + TelemetryPublisher.RegisterSource(new TelemetryJobRunsStreamingSource(_machineProvider.MachineOperator)); + } + + if (Settings.SendJobRunsHistory) + { + TelemetryPublisher.RegisterSource(new TelemetryJobRunsHistorySource() { Config = new TelemetryJobRunsHistorySourceConfig() { MaxJobRunsPerRequest = 10 } }); + } + + if (Settings.SendJobStatus) + { + TelemetryPublisher.RegisterSource(new TelemetryJobStatusSource(_machineProvider.MachineOperator)); + } + + if (Settings.SendLogs) + { + TelemetryPublisher.RegisterSource(new TelemetryLogsStreamingSource(_machineProvider.MachineOperator) + { + Config = new TelemetryLogsStreamingSourceConfig() + { + Categories = Settings.LogCategories + } + }); + } + + if (Settings.SendMachineStatus) + { + TelemetryPublisher.RegisterSource(new TelemetryMachineStatusStreamingSource(_machineProvider.MachineOperator)); + } + + if (Settings.SendMachineUpdates) + { + TelemetryPublisher.RegisterSource(new TelemetryMachineUpdatesStreamingSource()); + } + + if (Settings.SendMachineUpdatesHistory) + { + TelemetryPublisher.RegisterSource(new TelemetryMachineUpdatesHistorySource()); + } + + if (Settings.SendWires) + { + TelemetryPublisher.RegisterSource(new TelemetryWireStreamingSource(_machineProvider.MachineOperator)); + } + + await TelemetryPublisher.Start(); + }); + } + + public async Task> GetCheckpointsBackup() + { + var response = await _webClient.GetTelemetryCheckPoints(new TelemetryGetCheckPointsRequest()); + return response.Checkpoints + .Select(x => new TelemetryHistorySourceCheckPoint() + { + SourceName = x.SourceName, + Time = x.Time, + TotalCount = x.TotalCount + }) + .ToList(); + } + + public async Task SaveCheckpointsBackup(List checkPoints) + { + await _webClient.SetTelemetryCheckPoints(new TelemetrySetCheckPointsRequest() + { + Checkpoints = checkPoints.Select(x => new TelemetryCheckPoint() + { + SourceName = x.SourceName, + Time = x.Time, + TotalCount = x.TotalCount + }).ToList() + }); + } + + public void Dispose() + { + + } + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModelLocator.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModelLocator.cs index 340ecb13e..56a8b4a2b 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModelLocator.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModelLocator.cs @@ -40,6 +40,7 @@ using Tango.PPC.Common.Statistics; using Tango.PPC.Common.Storage; using Tango.PPC.Common.Synchronization; using Tango.PPC.Common.SystemInfo; +using Tango.PPC.Common.Telemetry; using Tango.PPC.Common.Threading; using Tango.PPC.Common.ThreadLoading; using Tango.PPC.Common.UpdatePackages; @@ -54,6 +55,7 @@ using Tango.PPC.UI.Notifications; using Tango.PPC.UI.PPCApplication; using Tango.PPC.UI.Printing; using Tango.PPC.UI.RemoteActions; +using Tango.PPC.UI.Telemetry; using Tango.PPC.UI.Threading; using Tango.PPC.UI.ThreadLoading; using Tango.PPC.UI.ViewModels; @@ -118,6 +120,7 @@ namespace Tango.PPC.UI TangoIOC.Default.Unregister(); TangoIOC.Default.Unregister(); TangoIOC.Default.Unregister(); + TangoIOC.Default.Unregister(); if (App.StartupArgs != null && App.StartupArgs.Contains("-webDebug")) { @@ -175,6 +178,7 @@ namespace Tango.PPC.UI TangoIOC.Default.Register(); TangoIOC.Default.Register(); TangoIOC.Default.Register(); + TangoIOC.Default.Register(); TangoIOC.Default.Register(); TangoIOC.Default.Register(); diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/packages.config b/Software/Visual_Studio/PPC/Tango.PPC.UI/packages.config index 9e3e48198..59e20edb2 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.UI/packages.config +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/packages.config @@ -1,15 +1,89 @@  + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file -- cgit v1.3.1