예제 #1
0
        private static void ProgressUpdated(object sender, EventArgs <ProgressUpdate> e)
        {
            ProgressUpdate update = e.Argument;

            Downloader downloader = sender as Downloader;

            if ((object)downloader != null)
            {
                update.DeviceName      = downloader.Name;
                update.FilesDownloaded = downloader.FilesDownloaded;
            }

            ModbusPoller modbusPoller = sender as ModbusPoller;

            if ((object)modbusPoller != null)
            {
                update.DeviceName      = modbusPoller.Name;
                update.ValuesProcessed = modbusPoller.MeasurementsReceived;
            }

            if (!string.IsNullOrEmpty(update.ProgressMessage))
            {
                update.ProgressMessage += $"\r\n\r\n[{DateTime.UtcNow:yyyy-MM-dd HH:mm:ss.fff}]";
            }

            GlobalHost.ConnectionManager.GetHubContext <DataHub>().Clients.All.deviceProgressUpdate(e.Argument);
        }
예제 #2
0
        private static void ProgressUpdated(object sender, EventArgs <string, List <ProgressUpdate> > e)
        {
            Downloader downloader = sender as Downloader;
            string     deviceName = null;

            if ((object)downloader != null)
            {
                deviceName = downloader.Name;
            }

            ModbusPoller modbusPoller = sender as ModbusPoller;

            if ((object)modbusPoller != null)
            {
                deviceName = modbusPoller.Name;
            }

            if ((object)deviceName == null)
            {
                return;
            }

            string clientID = e.Argument1;

            List <object> updates = e.Argument2
                                    .Select(update => update.AsExpandoObject())
                                    .ToList();

            if ((object)clientID != null)
            {
                GlobalHost.ConnectionManager.GetHubContext <DataHub>().Clients.Client(clientID).deviceProgressUpdate(deviceName, updates);
            }
            else
            {
                GlobalHost.ConnectionManager.GetHubContext <DataHub>().Clients.All.deviceProgressUpdate(deviceName, updates);
            }
        }
예제 #3
0
 public DeviceProxy(ModbusPoller parent)
 {
     m_parent = parent;
 }
예제 #4
0
        // Static Methods

        private static void OnProgressUpdated(ModbusPoller instance, ProgressUpdate update)
        {
            ProgressUpdated?.Invoke(instance, new EventArgs <ProgressUpdate>(update));
        }
예제 #5
0
        public void Configuration(IAppBuilder app)
        {
            // Modify the JSON serializer to serialize dates as UTC - otherwise, timezone will not be appended
            // to date strings and browsers will select whatever timezone suits them
            JsonSerializerSettings settings = JsonUtility.CreateDefaultSerializerSettings();

            settings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
            JsonSerializer serializer = JsonSerializer.Create(settings);

            GlobalHost.DependencyResolver.Register(typeof(JsonSerializer), () => serializer);
            AppModel model = Program.Host.Model;

            // Load security hub into application domain before establishing SignalR hub configuration, initializing default status and exception handlers
            try
            {
                using (new SecurityHub(
                           (message, updateType) => Program.Host.LogWebHostStatusMessage(message, updateType),
                           ex => Program.Host.LogException(ex)
                           )) { }
            }
            catch (Exception ex)
            {
                Program.Host.LogException(new SecurityException($"Failed to load Security Hub, validate database connection string in configuration file: {ex.Message}", ex));
            }

            // Load shared hub into application domain, initializing default status and exception handlers
            try
            {
                using (new SharedHub(
                           (message, updateType) => Program.Host.LogWebHostStatusMessage(message, updateType),
                           ex => Program.Host.LogException(ex)
                           )) { }
            }
            catch (Exception ex)
            {
                Program.Host.LogException(new SecurityException($"Failed to load Shared Hub: {ex.Message}", ex));
            }

            // Load Modbus assembly
            try
            {
                // Make embedded resources of Modbus poller available to web server
                using (ModbusPoller poller = new ModbusPoller())
                    WebExtensions.AddEmbeddedResourceAssembly(poller.GetType().Assembly);

                ModbusPoller.RestoreConfigurations(FilePath.GetAbsolutePath("ModbusConfigs"));
            }
            catch (Exception ex)
            {
                Program.Host.LogException(new InvalidOperationException($"Failed to load Modbus assembly: {ex.Message}", ex));
            }

            // Configure Windows Authentication for self-hosted web service
            HubConfiguration  hubConfig  = new HubConfiguration();
            HttpConfiguration httpConfig = new HttpConfiguration();

            // Setup resolver for web page controller instances
            httpConfig.DependencyResolver = WebPageController.GetDependencyResolver(WebServer.Default, Program.Host.DefaultWebPage, model, typeof(AppModel));

            // Make sure any hosted exceptions get propagated to service error handling
            httpConfig.Services.Replace(typeof(IExceptionHandler), new HostedExceptionHandler());

            // Enabled detailed client errors
            hubConfig.EnableDetailedErrors = true;

            // Enable GSF session management
            httpConfig.EnableSessions(AuthenticationOptions);

            // Enable GSF role-based security authentication
            app.UseAuthentication(AuthenticationOptions);

            // Enable cross-domain scripting default policy - controllers can manually
            // apply "EnableCors" attribute to class or an action to override default
            // policy configured here
            try
            {
                if (!string.IsNullOrWhiteSpace(model.Global.DefaultCorsOrigins))
                {
                    httpConfig.EnableCors(new EnableCorsAttribute(model.Global.DefaultCorsOrigins, model.Global.DefaultCorsHeaders, model.Global.DefaultCorsMethods)
                    {
                        SupportsCredentials = model.Global.DefaultCorsSupportsCredentials
                    });
                }
            }
            catch (Exception ex)
            {
                Program.Host.LogException(new InvalidOperationException($"Failed to establish default CORS policy: {ex.Message}", ex));
            }

            // Load ServiceHub SignalR class
            app.MapSignalR(hubConfig);

            // Map custom API controllers
            try
            {
                httpConfig.Routes.MapHttpRoute(
                    name: "CustomAPIs",
                    routeTemplate: "api/{controller}/{action}/{id}",
                    defaults: new { action = "Index", id = RouteParameter.Optional }
                    );
            }
            catch (Exception ex)
            {
                Program.Host.LogException(new InvalidOperationException($"Failed to initialize custom API controllers: {ex.Message}", ex));
            }

            // Set configuration to use reflection to setup routes
            httpConfig.MapHttpAttributeRoutes();

            // Load the WebPageController class and assign its routes
            app.UseWebApi(httpConfig);

            // Check for configuration issues before first request
            httpConfig.EnsureInitialized();
        }