diff --git a/Libs/CMS_CORE_Library.dll b/Libs/CMS_CORE_Library.dll index 672d6e2b..a40b9395 100644 Binary files a/Libs/CMS_CORE_Library.dll and b/Libs/CMS_CORE_Library.dll differ diff --git a/Step.Model/DTOModels/DTONcGenericDataModel.cs b/Step.Model/DTOModels/DTONcGenericDataModel.cs index 65abad3b..e967fc2e 100644 --- a/Step.Model/DTOModels/DTONcGenericDataModel.cs +++ b/Step.Model/DTOModels/DTONcGenericDataModel.cs @@ -9,7 +9,7 @@ namespace Step.Model.DTOModels { public class DTONcGenericDataModel { - public CultureInfo Language { get; set; } + public string Language { get; set; } public string MachineModel { get; set; } public string SerialNumber { get; set; } public string NcSoftwareVersion { get; set; } diff --git a/Step.Model/DTOModels/DTOProcessStatus.cs b/Step.Model/DTOModels/DTOProcessStatus.cs new file mode 100644 index 00000000..66af3523 --- /dev/null +++ b/Step.Model/DTOModels/DTOProcessStatus.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Step.Model.DTOModels +{ + public class DTOProcessStatus + { + public ushort processId; + public string PartProgramName; + public int Mode; + public int Status; + } +} diff --git a/Step.Model/Step.Model.csproj b/Step.Model/Step.Model.csproj index 9a588d5d..763a1e2d 100644 --- a/Step.Model/Step.Model.csproj +++ b/Step.Model/Step.Model.csproj @@ -80,6 +80,7 @@ DTOLanguageModel.cs.d.ts + diff --git a/Step.NC/NcHandler.cs b/Step.NC/NcHandler.cs index d8f4ee6f..0392734e 100644 --- a/Step.NC/NcHandler.cs +++ b/Step.NC/NcHandler.cs @@ -71,7 +71,7 @@ namespace Step.NC if (cmsError.errorCode != 0) return cmsError; - genericData.Language = lang; + genericData.Language = lang.EnglishName; string tmpInfo = ""; // Get serial number @@ -251,6 +251,26 @@ namespace Step.NC return cmsError; } + public CmsError GetProcessesPartProgramData(out List processesPPData) + { + processesPPData = new List(); + + // Get NC max process number + ushort maxProcNumber = 0; + CmsError cmsError = numericalControl.NC_RProcessesNum(ref maxProcNumber); + if (cmsError.errorCode != 0) + return cmsError; + // For each process get info + for(ushort i = 1; i <= maxProcNumber; i++) + { + ProcessPartProgramData tmpProcPP = new ProcessPartProgramData(); + cmsError = numericalControl.PROC_RPartProgramData(i, ref tmpProcPP); + processesPPData.Add(tmpProcPP); + } + + return cmsError; + } + public void Dispose() { numericalControl.NC_Disconnect(); diff --git a/Step.Tasks/ThreadsFunctions.cs b/Step.Tasks/ThreadsFunctions.cs index 320d2c73..267b679e 100644 --- a/Step.Tasks/ThreadsFunctions.cs +++ b/Step.Tasks/ThreadsFunctions.cs @@ -17,6 +17,8 @@ public static class ThreadsFunctions private static long ReadNcGenericInfoTimer = 0, ReadNcGenericInfoTimes = 0; private static long ReadAxesTimer = 0, ReadAxesTimes = 0; private static long ReadPowerOnTimer = 0, ReadPowerOnTimes = 0; + private static long ReadProcPPTimer = 0, ReadProcPPTimes = 0; + private static Thread ConnThread; #region Nc Threads @@ -191,6 +193,47 @@ public static class ThreadsFunctions } } + // Read processes part program status + public static void ReadProcessesPPStatus() + { + NcHandler ncHandler = new NcHandler(); + Stopwatch sw = new Stopwatch(); + + try + { + // Try connection + CmsError libraryError = ncHandler.Connect(); + if (libraryError.errorCode != 0) + ManageLibraryError(libraryError); + + while (true) + { + sw.Restart(); + + if (ncHandler.numericalControl.NC_IsConnected()) + { + // Get Data from NC + libraryError = ncHandler.GetProcessesPartProgramData(out List processesPPData); + if (libraryError.errorCode != 0) + ManageLibraryError(libraryError); + else + // Send through signalR + MessageServices.Current.Publish(SEND_PROCESSES_PP_STATUS, null, processesPPData); + } + else + TryNcConnection(); + sw.Stop(); + //Send to the UI the time + ReadProcPPTimer += sw.ElapsedMilliseconds; + ReadProcPPTimes++; + } + } + catch (ThreadAbortException) + { + ncHandler.Dispose(); + } + } + #endregion #region SupportFunctions @@ -311,6 +354,13 @@ public static class ThreadsFunctions ReadPowerOnTimes = 0; } + if (ThreadsHandler.RunningThreadStatus.ContainsKey("ReadProcessesPPStatus") && ReadProcPPTimes != 0) + { + ThreadsHandler.RunningThreadStatus["ReadProcessesPPStatus"] = (ReadProcPPTimer / ReadProcPPTimes) + " mS"; + ReadProcPPTimer = 0; + ReadProcPPTimes = 0; + } + MessageServices.Current.Publish(SEND_THREADS_STATUS, null, ThreadsHandler.RunningThreadStatus); Thread.Sleep(2000); diff --git a/Step.Tasks/ThreadsHandler.cs b/Step.Tasks/ThreadsHandler.cs index ac303ed6..2db714ce 100644 --- a/Step.Tasks/ThreadsHandler.cs +++ b/Step.Tasks/ThreadsHandler.cs @@ -25,7 +25,8 @@ namespace Step.Core ThreadsFunctions.ReadNcGenericInfo, ThreadsFunctions.ReadAxes, ThreadsFunctions.ReadPowerOnData, - ThreadsFunctions.StatThread + ThreadsFunctions.StatThread, + ThreadsFunctions.ReadProcessesPPStatus, }; private volatile static List RunningThreadsList = new List(); internal volatile static Dictionary RunningThreadStatus = new Dictionary(); diff --git a/Step.UI/ServerControlWindow.cs b/Step.UI/ServerControlWindow.cs index 9a4b0b7f..c5d6c5fa 100644 --- a/Step.UI/ServerControlWindow.cs +++ b/Step.UI/ServerControlWindow.cs @@ -252,11 +252,10 @@ namespace Step.UI TXTCMSMach.Text = data.CmsMachineIdNumber; TXTSftVers.Text = data.NcSoftwareVersion; TXTNCProc.Text = data.ProcessNumber.ToString(); - TXTLang.Text = data.Language.EnglishName; + TXTLang.Text = data.Language; TXTTime.Text = data.DateTime.ToShortDateString() + " " + data.DateTime.ToShortTimeString(); }); })); - } } } diff --git a/Step.Utils/Constants.cs b/Step.Utils/Constants.cs index 98cd49a0..d1aeadd7 100644 --- a/Step.Utils/Constants.cs +++ b/Step.Utils/Constants.cs @@ -73,5 +73,6 @@ namespace Step.Utils public const string SEND_POWER_ON_DATA = "SEND_POWER_ON_DATA"; public const string SEND_GENERIC_DATA = "SEND_GENERIC_DATA"; public const string SEND_AXES = "SEND_AXES"; + public const string SEND_PROCESSES_PP_STATUS = "SEND_PROCESSES_STATUS"; } } diff --git a/Step/Attributes/SignalRAuthorizeAttribute.cs b/Step/Attributes/SignalRAuthorizeAttribute.cs index a9d344cc..af050436 100644 --- a/Step/Attributes/SignalRAuthorizeAttribute.cs +++ b/Step/Attributes/SignalRAuthorizeAttribute.cs @@ -8,68 +8,95 @@ using Step.Database.Controllers; using Step.Model.DatabaseModels; using static Step.Utils.Constants; using static Step.Config.ServerConfig; +using Microsoft.AspNet.SignalR.Hubs; namespace Step.Attributes { - class SignalRAuthorizeAttribute : AuthorizeAttribute + public class SignalRAuthorizeAttribute : AuthorizeAttribute { public string FunctionAccess; public ACTIONS Action; + public override bool AuthorizeHubConnection(HubDescriptor hubDescriptor, IRequest request) + { + string token = request.Headers["Authorization"]; + if (string.IsNullOrWhiteSpace(token)) + return false; + + return base.AuthorizeHubConnection(hubDescriptor, request); + } + protected override bool UserAuthorized(IPrincipal user) { if (!base.UserAuthorized(user)) return false; - - // Get user level stored in the bearer token + // Get Claims from context ClaimsIdentity identity = user.Identity as ClaimsIdentity; - var userRoleLevel = identity.Claims.Where(c => c.Type == ROLE_LEVEL_KEY).SingleOrDefault(); + // Get user id stored in the bearer token + var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).SingleOrDefault(); // Get machine unique id stored in the bearer token - var machineUniqueId = identity.Claims.Where(c => c.Type == MACHINE_ID_KEY).SingleOrDefault(); + var machineId = identity.Claims.Where(c => c.Type == MACHINE_ID_KEY).SingleOrDefault(); + + //User data not found -> not authorized + if (userId == null || machineId == null) + return false; // User data not found -> not authorized - if (userRoleLevel == null || machineUniqueId == null) + if (userId == null || machineId == null) return false; - // check authorization - if (!CheckAuthorization(machineUniqueId.ToString(), Convert.ToInt32(userRoleLevel.Value), FunctionAccess)) - return false; + //// check authorization + //if (!CheckAuthorization(Convert.ToInt32(machineId), Convert.ToInt32(userId.Value), FunctionAccess)) + // return false; return true; } - private bool CheckAuthorization(string machineUniqueId, int userLevel, string functionName) + private bool CheckAuthorization(int machineId, int userId, string functionName, string token) { - using (FunctionsAccessController acController = new FunctionsAccessController()) + // Check if the machine is the same where the user logged in + if (machineId != MachineConfig.MachineId) + return false; + + using (SessionsController sessionsController = new SessionsController()) { - // Check if the machine is the same where the user logged in - if (machineUniqueId != MachineConfig.UniqueId) + // Find user session on this machine + SessionModel session = sessionsController.FindSessionByToken(token); + if (session == null) return false; - // Read from db category levels - FunctionAccessModel functionAccess = acController.FindEnabledFunctionByName(functionName); - if (functionAccess != null && ServerConfigController.CheckAreaStatus(functionAccess.Area)) + MachineUserModel machineUser = new MachineUserModel(); + using (MachinesUsersController machineUsersController = new MachinesUsersController()) { - if (Action == ACTIONS.READ) - { // Check read permissions - if (functionAccess.ReadLevelMin > userLevel) - return false; // Not authorized + // Find machineUser data and joined to user data, role data, machine data + machineUser = machineUsersController.FindById(session.MachineUserId); + } + + using (FunctionsAccessController acController = new FunctionsAccessController()) + { + // Read from db function levels + FunctionAccessModel functionAccess = acController.FindEnabledFunctionByName(functionName); + if (functionAccess != null && ServerConfigController.CheckAreaStatus(functionAccess.Area)) + { + if (Action == ACTIONS.READ) + { // Check read permissions + if (functionAccess.ReadLevelMin > machineUser.Role.Level) + return false; // Not authorized + } + else + { // Check write permissions + if (functionAccess.WriteLevelMin > machineUser.Role.Level) + return false; // Not authorized + } } else - { // Check write permissions - if (functionAccess.WriteLevelMin > userLevel) - return false; // Not authorized - } - } - else - { - return false; + return false; + + // Authorized + return true; } } - - // Authorized - return true; } } } diff --git a/Step/Controllers/SignalR/NcHub.cs b/Step/Controllers/SignalR/NcHub.cs index 2808b7fd..435ef4c9 100644 --- a/Step/Controllers/SignalR/NcHub.cs +++ b/Step/Controllers/SignalR/NcHub.cs @@ -4,9 +4,13 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.AspNet.SignalR; +using Step.Attributes; +using static Step.Utils.Constants; namespace Step.Controllers.SignalR { + [SignalRAuthorize(FunctionAccess = "functionAccess", Action = ACTIONS.READ)] + public class NcHub : Hub { public static List ConnectedClients = new List(); diff --git a/Step/Listeners/ListenersHandler.cs b/Step/Listeners/ListenersHandler.cs index cf7e6e4c..b37d2f59 100644 --- a/Step/Listeners/ListenersHandler.cs +++ b/Step/Listeners/ListenersHandler.cs @@ -47,6 +47,11 @@ namespace Step.Listeners { SignalRListener.SendNcNetworkStatus(a); })); + + infos.Add(MessageServices.Current.Subscribe(SEND_PROCESSES_PP_STATUS, async (a, b) => + { + SignalRListener.SendProcessesPPStatus(a); + })); } public static void Stop() diff --git a/Step/Listeners/SignalR/SignalRListener.cs b/Step/Listeners/SignalR/SignalRListener.cs index c8c9c23a..7d620a4d 100644 --- a/Step/Listeners/SignalR/SignalRListener.cs +++ b/Step/Listeners/SignalR/SignalRListener.cs @@ -44,8 +44,14 @@ namespace Step.Listeners.SignalR if ((bool)ncStatus == false) { var context = GlobalHost.ConnectionManager.GetHubContext(); - context.Clients.Group("ncData").ncNetworkStatus(new { connected = false}); + context.Clients.Group("ncData").ncNetworkStatus(new { connected = false }); } } + + public static void SendProcessesPPStatus(object processesStatus) + { + var context = GlobalHost.ConnectionManager.GetHubContext(); + context.Clients.Group("ncData").processesPPStatus(processesStatus); + } } }