From 95484c7ee7c5b605932e210cebd81cbe0caaa3a7 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Tue, 19 May 2020 16:23:10 +0200 Subject: [PATCH] Update metodi preliminari signal-r --- Thermo.Active.Core/ThreadsFunctions.cs | 56 +++++++++++++++-- Thermo.Active.Core/ThreadsHandler.cs | 3 +- Thermo.Active.Model/Constants.cs | 1 + .../DTOModels/DTOGaugeDataModel.cs | 33 ++++++++++ .../DTOModels/Recipe/DTORecipeParam.cs | 60 +++++++++++++++++++ .../Thermo.Active.Model.csproj | 1 + Thermo.Active.NC/NcAdapter.cs | 27 ++++----- Thermo.Active.Utils/supportFunctions.cs | 28 +++++---- Thermo.Active/Listeners/ListenersHandler.cs | 4 ++ .../Listeners/SignalR/SignalRListener.cs | 50 +++++++++++++--- .../Listeners/SignalRStaticObjects.cs | 2 + 11 files changed, 226 insertions(+), 39 deletions(-) create mode 100644 Thermo.Active.Model/DTOModels/DTOGaugeDataModel.cs diff --git a/Thermo.Active.Core/ThreadsFunctions.cs b/Thermo.Active.Core/ThreadsFunctions.cs index c98c3cd1..1329d902 100644 --- a/Thermo.Active.Core/ThreadsFunctions.cs +++ b/Thermo.Active.Core/ThreadsFunctions.cs @@ -377,6 +377,7 @@ public static class ThreadsFunctions } } +#if false public static void ReadHeadsData() { NcAdapter ncAdapter = new NcAdapter(); @@ -419,7 +420,8 @@ public static class ThreadsFunctions { ncAdapter.Dispose(); } - } + } +#endif public static void ReadAxesNamesData() { @@ -509,6 +511,7 @@ public static class ThreadsFunctions } } +#if false public static void ReadActiveProgramData() { NcFileAdapter ncAdapter = new NcFileAdapter(); @@ -552,8 +555,10 @@ public static class ThreadsFunctions { ncAdapter.Dispose(); } - } + } +#endif +#if false public static void ReadPartProgramQueueData() { NcFileAdapter ncAdapter = new NcFileAdapter(); @@ -599,7 +604,8 @@ public static class ThreadsFunctions { ncAdapter.Dispose(); } - } + } +#endif public static void ReadScadaData() { @@ -645,7 +651,49 @@ public static class ThreadsFunctions ncAdapter.Dispose(); } } + public static void ReadGaugeData() + { + NcAdapter ncAdapter = new NcAdapter(); + Stopwatch sw = new Stopwatch(); + try + { + // Try connection + CmsError libraryError = ncAdapter.Connect(); + if (libraryError.errorCode != 0) + ManageLibraryError(libraryError); + + while (true) + { + sw.Restart(); + + // Check if client is connected + if (ncAdapter.numericalControl.NC_IsConnected()) + { + + // Get new data from PLC + libraryError = ncAdapter.ReadGaugeData(out ThermoModels.GaugeModel gaugeData); + if (libraryError.IsError()) + ManageLibraryError(libraryError); + + MessageServices.Current.Publish(SEND_THERMO_GAUGE_DATA, null, gaugeData); + } + else + RestoreConnection(); + + sw.Stop(); + + //Update thread timer + UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds); + // Wait + Thread.Sleep(CalcSleepTime(500, (int)sw.ElapsedMilliseconds)); + } + } + catch (ThreadAbortException) + { + ncAdapter.Dispose(); + } + } public static void ReadRecipeData() { NcAdapter ncAdapter = new NcAdapter(); @@ -687,7 +735,7 @@ public static class ThreadsFunctions // Update thread timer UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds); // Wait - Thread.Sleep(CalcSleepTime(500, (int)sw.ElapsedMilliseconds)); + Thread.Sleep(CalcSleepTime(250, (int)sw.ElapsedMilliseconds)); } } catch (ThreadAbortException) diff --git a/Thermo.Active.Core/ThreadsHandler.cs b/Thermo.Active.Core/ThreadsHandler.cs index c1d27e90..49bc82d0 100644 --- a/Thermo.Active.Core/ThreadsHandler.cs +++ b/Thermo.Active.Core/ThreadsHandler.cs @@ -24,8 +24,9 @@ namespace Thermo.Active.Core //ThreadsFunctions.ReadActiveProgramData, //ThreadsFunctions.ReadPartProgramQueueData, ThreadsFunctions.ReadScadaData, - ThreadsFunctions.ReadM154Data, + ThreadsFunctions.ReadM154Data, // levare? // ThreadsFunctions.ReadNcSoftKeysData, + ThreadsFunctions.ReadGaugeData, ThreadsFunctions.ReadRecipeData, ThreadsFunctions.ReadWarmersData, ThreadsFunctions.ReadModulesData diff --git a/Thermo.Active.Model/Constants.cs b/Thermo.Active.Model/Constants.cs index dbb42bbf..b3e58e33 100644 --- a/Thermo.Active.Model/Constants.cs +++ b/Thermo.Active.Model/Constants.cs @@ -269,6 +269,7 @@ namespace Thermo.Active.Model public const string SEND_THERMO_RECIPE_OVERWIEW = "SEND_THERMO_RECIPE_OVERWIEW"; public const string SEND_THERMO_MODULE_DATA = "SEND_THERMO_MODULE_DATA"; public const string SEND_THERMO_WARMERS_DATA = "SEND_THERMO_WARMERS_DATA"; + public const string SEND_THERMO_GAUGE_DATA = "SEND_THERMO_GAUGE_DATA"; public const string BROADCAST_DATA = "BROADCAST_DATA"; diff --git a/Thermo.Active.Model/DTOModels/DTOGaugeDataModel.cs b/Thermo.Active.Model/DTOModels/DTOGaugeDataModel.cs new file mode 100644 index 00000000..ce04a94e --- /dev/null +++ b/Thermo.Active.Model/DTOModels/DTOGaugeDataModel.cs @@ -0,0 +1,33 @@ +using CMS_CORE_Library.Models; +using System.Linq; +using static CMS_CORE_Library.Models.DataStructures; +using static CMS_CORE_Library.Models.ThermoModels; + +namespace Thermo.Active.Model.DTOModels +{ + public class DTOGaugeDataModel : GaugeModel + { + public override bool Equals(object obj) + { + if (!(obj is DTOGaugeDataModel item)) + return false; + + if (TimeAdv != item.TimeAdv) + return false; + if (Power != item.Power) + return false; + if (Vacuum != item.Vacuum) + return false; + if (Air != item.Air) + return false; + + return true; + } + + public override int GetHashCode() + { + return base.GetHashCode(); + } + } + +} \ No newline at end of file diff --git a/Thermo.Active.Model/DTOModels/Recipe/DTORecipeParam.cs b/Thermo.Active.Model/DTOModels/Recipe/DTORecipeParam.cs index 4ea7a14c..c308032d 100644 --- a/Thermo.Active.Model/DTOModels/Recipe/DTORecipeParam.cs +++ b/Thermo.Active.Model/DTOModels/Recipe/DTORecipeParam.cs @@ -13,17 +13,77 @@ namespace Thermo.Active.Model.DTOModels.Recipe public RPStatus Status { get; set; } public string UnitMeasure { get; set; } public double ValueAct { get; set; } + + public override bool Equals(object obj) + { + if (!(obj is DTORecipeParam item)) + return false; + + if (Id!= item.Id) + return false; + if (!Range.Equals(item.Range)) + return false; + if (!Status.Equals(item.Status)) + return false; + if (UnitMeasure != item.UnitMeasure) + return false; + if (ValueAct != item.ValueAct) + return false; + + return true; + } + + public override int GetHashCode() + { + return base.GetHashCode(); + } } public struct RPRange { public double Min { get; set; } public double Max { get; set; } + public override bool Equals(object obj) + { + if (!(obj is RPRange item)) + return false; + + if (Min != item.Min) + return false; + if (Max != item.Max) + return false; + + return true; + } + + public override int GetHashCode() + { + return base.GetHashCode(); + } } public struct RPStatus { public bool Visible { get; set; } public bool Enabled { get; set; } public bool HasError { get; set; } + public override bool Equals(object obj) + { + if (!(obj is RPStatus item)) + return false; + + if (Visible != item.Visible) + return false; + if (Enabled != item.Enabled) + return false; + if (HasError != item.HasError) + return false; + + return true; + } + + public override int GetHashCode() + { + return base.GetHashCode(); + } } } diff --git a/Thermo.Active.Model/Thermo.Active.Model.csproj b/Thermo.Active.Model/Thermo.Active.Model.csproj index 3dd39dcf..b4394495 100644 --- a/Thermo.Active.Model/Thermo.Active.Model.csproj +++ b/Thermo.Active.Model/Thermo.Active.Model.csproj @@ -95,6 +95,7 @@ + diff --git a/Thermo.Active.NC/NcAdapter.cs b/Thermo.Active.NC/NcAdapter.cs index 2bf6264b..3558caf1 100644 --- a/Thermo.Active.NC/NcAdapter.cs +++ b/Thermo.Active.NC/NcAdapter.cs @@ -33,7 +33,7 @@ namespace Thermo.Active.NC /// public bool simData = true; - public Nc numericalControl; + public NcThermo numericalControl; public NcAdapter() => // Choose NC @@ -59,23 +59,11 @@ namespace Thermo.Active.NC return null; } - public Nc SetNumericalControl() + public NcThermo SetNumericalControl() { // Return new Numerical control instance choosed from the configuration switch (NcConfig.NcVendor) { - case NC_VENDOR.DEMO: - return new Nc_Demo(NcConfig.NcIpAddress, NcConfig.NcPort); - - case NC_VENDOR.FANUC: - return new Nc_Fanuc(NcConfig.NcIpAddress, NcConfig.NcPort, 2000); - - case NC_VENDOR.SIEMENS: - return new Nc_Siemens(2000); - - case NC_VENDOR.OSAI: - return new Nc_Osai(NcConfig.NcIpAddress, NcConfig.NcPort, 2000); - case NC_VENDOR.S7NET: return new Nc_S7Net(NcConfig.NcIpAddress, NcConfig.NcPort, 2000); } @@ -1071,6 +1059,15 @@ namespace Thermo.Active.NC return NO_ERROR; } + + public CmsError ReadGaugeData(out ThermoModels.GaugeModel gaugeData) + { + gaugeData = new ThermoModels.GaugeModel(); + CmsError cmsError = numericalControl.PLC_RGaugeData(ref gaugeData); + + return cmsError; + } + public CmsError ReadRecipeData(bool refreshOnlyRT, out Dictionary currentRecipe) { // init obj @@ -1696,7 +1693,7 @@ namespace Thermo.Active.NC public CmsError SetActiveScreen(short screen) { // Set to true power on data by id - return numericalControl.NC_SetScreenVisible((Nc.SCREEN_PAGE)screen); + return numericalControl.NC_SetScreenVisible((NcThermo.SCREEN_PAGE)screen); } public CmsError WriteM155Data(int process, double responseValue) diff --git a/Thermo.Active.Utils/supportFunctions.cs b/Thermo.Active.Utils/supportFunctions.cs index d0cbdcde..269cd3f8 100644 --- a/Thermo.Active.Utils/supportFunctions.cs +++ b/Thermo.Active.Utils/supportFunctions.cs @@ -20,7 +20,7 @@ namespace Thermo.Active.Utils { private static readonly string CMSCONNECT_TOKEN = "59f1qik5PYfiJLiXZ4xZ05pjzx5E9FscQWtj4lmfLKXaF1OAxkvu7ziBzXFBuuVQ"; - public static SOFTKEY_TYPE GetSoftKeyType(string type) + public static SOFTKEY_TYPE GetSoftKeyType(string type) { switch (type) { @@ -42,8 +42,9 @@ namespace Thermo.Active.Utils public static TACT_PARAM_TYPE GetTActCategory(string strCategory) { - TACT_PARAM_TYPE answ = TACT_PARAM_TYPE.ND; - try { + TACT_PARAM_TYPE answ = TACT_PARAM_TYPE.ND; + try + { answ = (TACT_PARAM_TYPE)Enum.Parse(typeof(TACT_PARAM_TYPE), strCategory); } catch { } @@ -82,7 +83,7 @@ namespace Thermo.Active.Utils case "xTen": return 26; case "xHundred": return 27; case "xThousand": return 28; - case "overstroke": return 30; + case "overstroke": return 30; case "feedByPass": return 31; default: return -1; } @@ -163,7 +164,7 @@ namespace Thermo.Active.Utils List listOfPossibleIds = Enumerable.Range(1, objIds.Max() + 1).Select(x => (short)x).ToList(); - IEnumerable res = listOfPossibleIds.Except(objIds).ToList(); + IEnumerable res = listOfPossibleIds.Except(objIds).ToList(); if (res.Count() > 0) return res.First(); @@ -172,7 +173,7 @@ namespace Thermo.Active.Utils } public static string GetImageBase64String(string directoryPath, string imageName) - { + { string fileName = Path.GetFileNameWithoutExtension(imageName); foreach (string ext in VALID_IMAGE_EXTENSIONS) { @@ -209,7 +210,7 @@ namespace Thermo.Active.Utils return null; EmptyFolder(jobFolderPath); - + using (ZipArchive zipExtractor = ZipFile.OpenRead(filePath)) { // Setup main job fields @@ -317,7 +318,7 @@ namespace Thermo.Active.Utils string filePath = JOB_TMP_DIRECTORY + "\\" + processId + "\\"; if (!Directory.Exists(filePath)) return null; - + // Setup main Fields DTOJobModel jobData = new DTOJobModel() { @@ -382,7 +383,7 @@ namespace Thermo.Active.Utils return SCADA_MEM_TYPE.WORD; case "INT": - return SCADA_MEM_TYPE.INT; + return SCADA_MEM_TYPE.INT; case "REAL": return SCADA_MEM_TYPE.REAL; @@ -417,7 +418,7 @@ namespace Thermo.Active.Utils public static string GetSoftwareVersionAndBuildDate() { - Version v = Assembly.GetEntryAssembly()?.GetName().Version; + Version v = Assembly.GetEntryAssembly()?.GetName().Version; if (v != null) { // Get first 2 number of the version @@ -434,7 +435,7 @@ namespace Thermo.Active.Utils return ""; } - + public static Boolean DecodeCMSConnectGatewayLogin(string encryptedString, out string login, out string password) { @@ -506,7 +507,10 @@ namespace Thermo.Active.Utils { intMachineVal = 0; containsLetters = Regex.IsMatch(machineNumber, @".*?[a-zA-Z].*?"); - + if (string.IsNullOrEmpty(machineNumber)) + { + machineNumber = "0000"; + } if (containsLetters) // Convert ASCII string to a single INT intMachineVal = SupportFunctions.ConvertAsciiStringToInt(machineNumber); diff --git a/Thermo.Active/Listeners/ListenersHandler.cs b/Thermo.Active/Listeners/ListenersHandler.cs index 5f39f252..1571ea8b 100644 --- a/Thermo.Active/Listeners/ListenersHandler.cs +++ b/Thermo.Active/Listeners/ListenersHandler.cs @@ -109,6 +109,10 @@ namespace Thermo.Active.Listeners { SignalRListener.SendThermoWarmersData(a); })); + infos.Add(MessageServices.Current.Subscribe(SEND_THERMO_GAUGE_DATA, (a, b) => + { + SignalRListener.SendThermoGaugeData(a); + })); // Broadcast diff --git a/Thermo.Active/Listeners/SignalR/SignalRListener.cs b/Thermo.Active/Listeners/SignalR/SignalRListener.cs index 0f0b918e..946d3ea4 100644 --- a/Thermo.Active/Listeners/SignalR/SignalRListener.cs +++ b/Thermo.Active/Listeners/SignalR/SignalRListener.cs @@ -15,6 +15,9 @@ using static Thermo.Active.Listeners.SignalRStaticObjects; using static Thermo.Active.Model.Constants; using static Thermo.Active.Config.ServerConfig; using Thermo.Active.Model.DTOModels.Recipe; +using CMS_CORE_Library.Models; +using static CMS_CORE_Library.Models.ThermoModels; +using System.Threading.Tasks; namespace Thermo.Active.Listeners.SignalR { @@ -247,21 +250,41 @@ namespace Thermo.Active.Listeners.SignalR var context = GlobalHost.ConnectionManager.GetHubContext(); context.Clients.Group("ncData").scadaData(scadaVal); - } + } } public static void SendThermoRecipeFullData(object recipeFull) { Dictionary currRecipeFull = recipeFull as Dictionary; - // FIXME TODO: usare un foreach comparer? - if (!LastRecipeFullData.SequenceEqual(currRecipeFull)) + Dictionary diffData = new Dictionary(); + + // calcola ed invia SOLO le differenze... + Parallel.ForEach(currRecipeFull, item => { + if (!LastRecipeFullData.ContainsKey(item.Key)) + { + diffData.Add(item.Key, item.Value); + } + else + { + // comparazione + if (!item.Value.Equals(LastRecipeFullData[item.Key])) + { + diffData.Add(item.Key, item.Value); + } + } + }); + + if (diffData.Count > 0) + { + // salvo update LastRecipeFullData = currRecipeFull; var context = GlobalHost.ConnectionManager.GetHubContext(); - context.Clients.Group("ncData").recipeFullData(currRecipeFull); - } + context.Clients.Group("ncData").recipeFullData(diffData); + //context.Clients.Group("ncData").recipeFullData(currRecipeFull); + } } public static void SendThermoRecipeOverviewData(object recipeOver) @@ -287,7 +310,7 @@ namespace Thermo.Active.Listeners.SignalR LastRecipeFullData = currModules; var context = GlobalHost.ConnectionManager.GetHubContext(); - context.Clients.Group("ncData").recipeModulesData(currModules); + context.Clients.Group("ncData").modulesData(currModules); } } @@ -301,7 +324,20 @@ namespace Thermo.Active.Listeners.SignalR LastRecipeFullData = currWarmers; var context = GlobalHost.ConnectionManager.GetHubContext(); - context.Clients.Group("ncData").recipeWarmersData(currWarmers); + context.Clients.Group("ncData").warmersData(currWarmers); + } + } + public static void SendThermoGaugeData(object gaugeData) + { + GaugeModel currGauge = gaugeData as GaugeModel; + + // FIXME TODO: usare un foreach comparer? + if (!LastGaugeData.Equals(currGauge)) + { + LastGaugeData = currGauge; + + var context = GlobalHost.ConnectionManager.GetHubContext(); + context.Clients.Group("ncData").gaugeData(currGauge); } } diff --git a/Thermo.Active/Listeners/SignalRStaticObjects.cs b/Thermo.Active/Listeners/SignalRStaticObjects.cs index 34ac9733..4f98fc8d 100644 --- a/Thermo.Active/Listeners/SignalRStaticObjects.cs +++ b/Thermo.Active/Listeners/SignalRStaticObjects.cs @@ -3,6 +3,7 @@ using Thermo.Active.Model.DTOModels.AlarmModels; using Thermo.Active.Model.DTOModels.Scada; using System.Collections.Generic; using Thermo.Active.Model.DTOModels.Recipe; +using CMS_CORE_Library.Models; namespace Thermo.Active.Listeners { @@ -31,6 +32,7 @@ namespace Thermo.Active.Listeners public static Dictionary LastRecipeOverData = new Dictionary(); public static List LastModulesData = new List(); public static List LastWarmersData = new List(); + public static ThermoModels.GaugeModel LastGaugeData = new ThermoModels.GaugeModel(); public static bool LastIsNcConnected = false;