diff --git a/Libs/CMS_CORE_Library.dll b/Libs/CMS_CORE_Library.dll index 411d8570..4f7ecbde 100644 Binary files a/Libs/CMS_CORE_Library.dll and b/Libs/CMS_CORE_Library.dll differ diff --git a/Step.Core/ThreadsFunctions.cs b/Step.Core/ThreadsFunctions.cs index df221279..d2269c3f 100644 --- a/Step.Core/ThreadsFunctions.cs +++ b/Step.Core/ThreadsFunctions.cs @@ -24,10 +24,52 @@ public static class ThreadsFunctions private static long ReadAxesNamesTimer = 0, ReadAxesNamesTimes = 0; private static long ReadHeadsTimer = 0, ReadHeadsTimes = 0; private static long ReadToolDataTimer = 0, ReadToolDataTimes = 0; + private static long WatchdogTimer = 0, WatchdogTimes = 0; + private static Thread ConnThread; - #region Nc functions threads + #region Functions + + public static void ManageWatchdog() + { + NcHandler ncHandler = new NcHandler(); + Stopwatch sw = new Stopwatch(); + + try + { + CmsError libraryError = ncHandler.Connect(); + if (libraryError.errorCode != 0) + ManageLibraryError(libraryError); + + while (true) + { + sw.Restart(); + + // Check if client is connected + if (ncHandler.numericalControl.NC_IsConnected()) + { + // Manage watchdog + libraryError = ncHandler.ManageWatchdog(); + } + else + TryNcConnection(); + + sw.Stop(); + + //Update thread timer + WatchdogTimer += sw.ElapsedMilliseconds; + WatchdogTimes++; + + // Wait + Thread.Sleep(CalcSleepTime(500, (int)sw.ElapsedMilliseconds)); + } + } + catch (ThreadAbortException) + { + ncHandler.Dispose(); + } + } public static void ReadAlarms() { @@ -115,7 +157,6 @@ public static class ThreadsFunctions } } - // Read processes part program status public static void ReadProcessesPPStatus() { NcHandler ncHandler = new NcHandler(); @@ -157,7 +198,6 @@ public static class ThreadsFunctions MessageServices.Current.Publish(SEND_NC_SOFTKEYS_DATA, null, ncSoftKeys); // Send m155 through signalR MessageServices.Current.Publish(SEND_M155_DATA, null, m155Data); - } } } @@ -459,7 +499,7 @@ public static class ThreadsFunctions try { // Try connection - CmsError libraryError = ncHandler.Connect(); + CmsError libraryError = ncHandler.Connect(); if (libraryError.errorCode != 0) ManageLibraryError(libraryError); @@ -675,7 +715,7 @@ public static class ThreadsFunctions } } - #endregion Nc functions threads + #endregion Functions #region SupportFunctions diff --git a/Step.Database/Controllers/AlarmsController.cs b/Step.Database/Controllers/AlarmsController.cs index 29b0f86a..2e47b214 100644 --- a/Step.Database/Controllers/AlarmsController.cs +++ b/Step.Database/Controllers/AlarmsController.cs @@ -8,6 +8,7 @@ using System.IO; using System.Linq; using static Step.Model.Constants; using static Step.Config.ServerConfig; +using System.Diagnostics; namespace Step.Database.Controllers { @@ -47,21 +48,19 @@ namespace Step.Database.Controllers .OrderBy(x => x.AlarmOccurrenceId) .Include("Users") .Where(x => - x.TimeStamp >= startDate && x.TimeStamp <= endDate - && types.Contains(x.Type) - && x.Users.Any(y => userIds.Any(z => z == y.UserId)) + x.TimeStamp >= startDate && x.TimeStamp <= endDate // Filter by date + && types.Contains(x.Type) // Type + && x.Users.Any(y => userIds.Any(z => z == y.UserId)) // Check user && - ((x.Source == ALARM_SOURCE.NC && ncAlarmDescIds.Contains(x.AlarmDescriptionId.Value)) - || (x.Source == ALARM_SOURCE.PLC && plcAlarmDescIds.Contains(x.PlcMessageId.Value))) - ); - - var occurrences = occurrencesQuery - .Skip(page * pageSize) - .Take(pageSize) - .Include("AlarmDescription") - .ToList(); + ((x.Source == ALARM_SOURCE.NC && ncAlarmDescIds.Contains(x.AlarmDescriptionId.Value)) // Check if message is contained in NC messages + || (x.Source == ALARM_SOURCE.PLC && plcAlarmDescIds.Contains(x.PlcMessageId.Value))) // Check if message is contained in PLC messages + ) + .Skip(page * pageSize) // Paginate + .Take(pageSize) + .Include("AlarmDescription") // Include foreign key + .ToList(); - return occurrences + return occurrencesQuery .Select(x => (DTOAlarmHistoricModel)x) .ToList(); } diff --git a/Step.Model/DatabaseModels/NcShankModel.cs b/Step.Model/DatabaseModels/NcShankModel.cs index 49d9ef96..f1cb5dc2 100644 --- a/Step.Model/DatabaseModels/NcShankModel.cs +++ b/Step.Model/DatabaseModels/NcShankModel.cs @@ -34,12 +34,12 @@ namespace Step.Model.DatabaseModels { return new NcShankModel() { - ShankId = obj.ShankId, + ShankId = (ushort)obj.ShankId, Balluf = obj.Balluf == null ? (ushort)0 : (ushort)obj.Balluf.Value, MagazineId = obj.MagazineId == null ? (byte)0 : obj.MagazineId.Value, PositionId = obj.PositionId == null ? (byte)0 : obj.PositionId.Value, MagazinePositionType = obj.MagazinePositionType, - }; + }; } } } \ No newline at end of file diff --git a/Step.NC/NcHandler.cs b/Step.NC/NcHandler.cs index 604cdeab..b350b261 100644 --- a/Step.NC/NcHandler.cs +++ b/Step.NC/NcHandler.cs @@ -30,8 +30,8 @@ namespace Step.NC public Nc numericalControl; public NcHandler() => - // Choose NC - numericalControl = SetNumericalControl(); + // Choose NC + numericalControl = SetNumericalControl(); public void Dispose() { @@ -87,7 +87,7 @@ namespace Step.NC CmsError cmsError = NO_ERROR; if (File.Exists(path)) - cmsError = LocalPartProgramFileInfo(path, out fileInfo); + cmsError = LocalPartProgramFileInfo(path, out fileInfo); else cmsError = numericalControl.FILES_RGetFileInfo(path, ref fileInfo); @@ -120,10 +120,10 @@ namespace Step.NC return FILE_NOT_FOUND_ERROR; string base64Img = model.Metadata.Generics.Images.Count() > 0 ? model.Metadata.Generics.Images[0].Base64 : ""; - + fileInfo = new DTOActiveImageAndNameDataModel() { - Image = base64Img, + Image = base64Img, Name = Path.GetFileName(path) // TODO: Get From NC }; @@ -139,13 +139,13 @@ namespace Step.NC fileInfo = new DTOActiveImageAndNameDataModel() { - Image = SupportFunctions.FindImageBase64String(IMAGES_PATH, name), + Image = SupportFunctions.FindImageBase64String(IMAGES_PATH, name), Name = Path.GetFileName(name) // TODO: Get From NC }; } return NO_ERROR; - } + } public CmsError LocalPartProgramFileInfo(string path, out InfoFile fileInfo) { @@ -164,7 +164,7 @@ namespace Step.NC // Populate fileInfo content fileInfo.Content = new List(); - while ((line = fileRead.ReadLine()) != null && count < 10) + while ((line = fileRead.ReadLine()) != null && count < 10) { fileInfo.Content.Add(line); count++; @@ -250,7 +250,7 @@ namespace Step.NC }; return cmsError; - } + } public CmsError UploadPartProgram(string localPath, string fileName, out string newFilePath) { @@ -430,34 +430,6 @@ namespace Step.NC return NO_ERROR; } - //public CmsError SwapQueueItems(int processId, int item1Index, int item2Index) - //{ - // item1Index = item1Index - 1; - // item2Index = item2Index - 1; - - // // Check if there is a queue - // if (!PartProgramQueue.ContainsKey(processId)) - // return INCORRECT_PARAMETERS_ERROR; - // // Check if items exists - // if (PartProgramQueue[processId].ElementAtOrDefault(item1Index) == null || PartProgramQueue[processId].ElementAtOrDefault(item2Index) == null) - // return INCORRECT_PARAMETERS_ERROR; - - // // Create a item - // DTOQueueModel tmp = PartProgramQueue[processId][item1Index]; - - // // Swap item 1 with 2 - // PartProgramQueue[processId][item1Index] = PartProgramQueue[processId][item2Index]; - // // Swap 2 with tmp - // PartProgramQueue[processId][item2Index] = tmp; - - // // Swap ids - // PartProgramQueue[processId][item2Index].Id = item2Index + 1; - // // Swap 2 with tmp - // PartProgramQueue[processId][item1Index].Id = item1Index + 1; - - // return NO_ERROR; - //} - public CmsError MoveQueueItems(int processId, int itemId, int newIndex, out List queue) { itemId = itemId - 1; @@ -578,6 +550,11 @@ namespace Step.NC #region Read Data + public CmsError ManageWatchdog() + { + return numericalControl.PLC_RWManageWatchdog(); + } + public CmsError GetNcGenericData(out DTONcGenericDataModel genericData) { genericData = new DTONcGenericDataModel(MachineConfig.Model); @@ -1327,7 +1304,7 @@ namespace Step.NC public CmsError ReadAxisData(out List axesNames) { axesNames = new List(); - List plcAxes = new List(); + List plcAxes = new List(); // Read selected process ushort selectedProcess = 0; CmsError cmsError = numericalControl.PROC_RSelectedProcess(ref selectedProcess); @@ -1343,7 +1320,7 @@ namespace Step.NC { Id = x.Id, Name = x.Name, - IsSelectable = x.IsSelectable + IsSelectable = x.IsSelectable }).ToList(); } @@ -1442,7 +1419,7 @@ namespace Step.NC families = new List(); return numericalControl.TOOLS_RFamilyData(ref families); } - + public CmsError GetMagazinesPositionsData(out List magazinesPositions) { magazinesPositions = new List(); @@ -1474,10 +1451,10 @@ namespace Step.NC data = ncData.Select(x => new DTOM155InputModel() { - Buttons = x.Buttons, - IsNeeded = x.IsNeeded, + Buttons = x.Buttons, + IsNeeded = x.IsNeeded, Message = x.Message, - Process = x.Process, + Process = x.Process, Type = x.Type.ToString() }).ToList(); diff --git a/Step/Controllers/WebApi/FavoriteUserSoftKeyController.cs b/Step/Controllers/WebApi/FavoriteUserSoftKeyController.cs index 2d56b122..0b5f81e1 100644 --- a/Step/Controllers/WebApi/FavoriteUserSoftKeyController.cs +++ b/Step/Controllers/WebApi/FavoriteUserSoftKeyController.cs @@ -16,7 +16,7 @@ namespace Step.Controllers.WebApi public class FavoriteUserSoftkeyController : ApiController { [Route("favorite"), HttpGet] - [WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.MAINTENANCE, Action = ACTIONS.WRITE)] + [WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.USER_SOFTKEY, Action = ACTIONS.WRITE)] public IHttpActionResult GetFavoriteSoftkeys() { var identity = User.Identity as ClaimsIdentity; @@ -32,7 +32,7 @@ namespace Step.Controllers.WebApi } [Route("favorite"), HttpPut] - [WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.MAINTENANCE, Action = ACTIONS.WRITE)] + [WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.USER_SOFTKEY, Action = ACTIONS.WRITE)] public IHttpActionResult PutFavoriteSoftkeys(List favoriteSoftkeyIds) { var identity = User.Identity as ClaimsIdentity; diff --git a/Step/program.cs b/Step/program.cs index 752edef4..9765ae6c 100644 --- a/Step/program.cs +++ b/Step/program.cs @@ -38,7 +38,7 @@ namespace Step ServerControlWindow.Start(); // Create unhandled exception handler - // AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(ExceptionHandler); + AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(ExceptionHandler); // Read config ServerConfigController.ReadStartupConfig(); @@ -117,7 +117,12 @@ namespace Step private static void ExceptionHandler(object sender, UnhandledExceptionEventArgs args) { using (NcHandler ncHandler = new NcHandler()) - ncHandler.Disconnect(); + { + if (ncHandler.numericalControl.NC_IsConnected()) + { + ncHandler.Disconnect(); + } + } LogException((Exception)args.ExceptionObject, ERROR_LEVEL.FATAL); }