using Microsoft.AspNet.SignalR; using Step.NC; using System.Collections.Generic; using System.Threading.Tasks; using TeamDev.SDK.MVVM; using static CMS_CORE_Library.DataStructures; using static Step.Config.ServerConfig; using static Step.Model.Constants; namespace Step.Controllers.SignalR { // [SignalRAuthorize(FunctionAccess = "functionAccess", Action = ACTIONS.READ)] public class NcHub : Hub { public static List ConnectedClients = new List(); public static bool NewConnectedClients = false; public override Task OnConnected() { Groups.Add(Context.ConnectionId, "ncData"); NewConnectedClients = true; ConnectedClients.Add(Context.ConnectionId); var result = base.OnConnected(); // Resends data to all the clients MessageServices.Current.Publish(BROADCAST_DATA); return result; } public override Task OnReconnected() { Groups.Add(Context.ConnectionId, "ncData"); NewConnectedClients = true; ConnectedClients.Add(Context.ConnectionId); var result = base.OnReconnected(); // Resends data to all the clients MessageServices.Current.Publish(BROADCAST_DATA); return result; } public override Task OnDisconnected(bool stopCalled) { Groups.Remove(Context.ConnectionId, "ncData"); ConnectedClients.Remove(Context.ConnectionId); return base.OnDisconnected(stopCalled); } public void PutPowerOnData(uint id) { using (NcHandler ncHandler = new NcHandler()) { ncHandler.Connect(); ncHandler.PutPowerOnData(id); } } // [SignalRAuthorize(FunctionAccess = "functionAccess", Action = ACTIONS.READ)] public void RefreshAllAlarms() { using (NcHandler ncHandler = new NcHandler()) { CmsError libraryError = ncHandler.Connect(); // Call the library functions libraryError = ncHandler.RefreshAllAlarms(); } MessageServices.Current.Publish(SHOW_MSG_UI, null, "Refresh All Alarms."); } public void RefreshAlarm(uint id) { using (NcHandler ncHandler = new NcHandler()) { CmsError libraryError = ncHandler.Connect(); // Call the library functions libraryError = ncHandler.RefreshAlarm(id); if (libraryError.IsError()) throw new HubException(libraryError.message); } MessageServices.Current.Publish(SHOW_MSG_UI, null, "Refresh Single Alarm. ID: " + id); } public void RecoveryProcedure(uint id) { using (NcHandler ncHandler = new NcHandler()) { CmsError libraryError = ncHandler.Connect(); // Call the library functions libraryError = ncHandler.RestoreAlarm(id); if (libraryError.IsError()) throw new HubException(libraryError.message); } MessageServices.Current.Publish(SHOW_MSG_UI, null, "Recovery Alarm. ID: " + id); } public void NcSoftKeyClick(int id) { using (NcHandler ncHandler = new NcHandler()) { ncHandler.Connect(); // Write the click into memory CmsError libraryError = ncHandler.PutNcSoftKeyClick((uint)id); if (libraryError.IsError()) throw new HubException(libraryError.message); } } public void UserSoftKeyClick(int id) { using (NcHandler ncHandler = new NcHandler()) { ncHandler.Connect(); foreach (var softKey in SoftKeysConfig) { // Check softKey type if (softKey.Type == SOFTKEY_TYPE.GROUP) { // Find in the subkeys foreach (var subkey in softKey.SubKeys) { if (subkey.Id == id) { // Write click button ncHandler.PutUserSoftKeyClick((uint)subkey.PlcId); break; } } } else { if (softKey.Id == id) { // Write click button ncHandler.PutUserSoftKeyClick((uint)softKey.PlcId); break; } } } } } public void SelectProcess(ushort procNumber) { using (NcHandler ncHandler = new NcHandler()) { // Connect the NC CmsError libraryError = ncHandler.Connect(); // Put value into memory libraryError = ncHandler.PutSelectProcess(procNumber); if (libraryError.IsError()) throw new HubException(libraryError.message); } } public void SelectAxis(byte axisId) { using (NcHandler ncHandler = new NcHandler()) { // Connect the Nc CmsError libraryError = ncHandler.Connect(); // Put value into memory libraryError = ncHandler.PutSelectAxis(axisId); if (libraryError.IsError()) throw new HubException(libraryError.message); } } public void AddOverride(uint id) { using (NcHandler ncHandler = new NcHandler()) { // Connect the Nc CmsError libraryError = ncHandler.Connect(); // Put value into memory libraryError = ncHandler.PutOverride(id, "plus"); if (libraryError.IsError()) throw new HubException(libraryError.message); } } public void SubOverride(uint id) { using (NcHandler ncHandler = new NcHandler()) { // Connect the Nc CmsError libraryError = ncHandler.Connect(); // Put value into memory libraryError = ncHandler.PutOverride(id, "minus"); if (libraryError.IsError()) throw new HubException(libraryError.message); } } } }