示例#1
0
        private static IHubProxy InitializeHubConnection(IReadOnlyConfiguration configuration, Action <string, int?, FunctionTypeEnum, bool, string, Action <string> > handler, out DisposalQueue toDispose, Action <string> logger)
        {
            toDispose = new DisposalQueue();

            var hubConnection = new HubConnection(configuration["Settings:WebAddress"],
                                                  new Dictionary <string, string>
            {
                {
                    configuration["Settings:SignalREngineFlagName"], true.ToString().ToLowerInvariant()
                }
            });

            hubConnection.StateChanged += change => logger($"InitializeHubConnection: StateChanged: '{change.OldState}' -> '{change.NewState}'!");

            hubConnection.JsonSerializer.Converters.Add(new NullConverter()); // handle NULLs

            var hubProxy = hubConnection.CreateHubProxy(configuration["Settings:SignalRHubName"]);

            toDispose.Enqueue(hubProxy.On <string, int?, FunctionTypeEnum, string>("sendMessage",
                                                                                   (clientID, moduleID, functionType, text)
                                                                                   => handler(clientID, moduleID, functionType, false, text, logger)));

            toDispose.Enqueue(hubProxy.On <string, int?, FunctionTypeEnum>("sendQuery",
                                                                           (clientID, moduleID, functionType)
                                                                           => handler(clientID, moduleID, functionType, true, null, logger)));

            try
            {
                hubConnection.Start().Wait();
            }
            catch (Exception e)
            {
                var message = $"InitializeHubConnection: ERROR:{Environment.NewLine}Exception '{typeof(Exception).Name}' occurred while initializing hub connection: {e.Message}.";
                if (e.InnerException != null)
                {
                    message += Environment.NewLine
                               + $"Inner exception '{e.InnerException.GetType().Name}': {e.InnerException.Message}.";
                }

                logger(message);

                toDispose = null;
                return(null);
            }

            logger($"InitializeHubConnection: Connection to hub started with ID '{hubConnection.ConnectionId}'!");

            toDispose.Enqueue(hubConnection);

            return(hubProxy);
        }
示例#2
0
        private static IHubProxy InitializeHubConnection(IReadOnlyConfiguration configuration, IpiSensorNetConfiguration moduleConfiguration, MessageHandler handler, IReadOnlyMap <string, int> moduleAddresses, IReadOnlyMap <FunctionTypeEnum, int> functionTypes, int?serialProcessID, DisposalQueue toDispose, Action <string> logger)
        {
            var hubConnection = new HubConnection(configuration["Settings:WebAddress"],
                                                  new Dictionary <string, string>
            {
                {
                    configuration["Settings:SignalREngineFlagName"], true.ToString().ToLowerInvariant()
                }
            });

            hubConnection.StateChanged += change => logger($"InitializeHubConnection: StateChanged: '{change.OldState}' -> '{change.NewState}'!");

            hubConnection.JsonSerializer.Converters.Add(new NullConverter()); // handle NULLs

            var hubProxy = hubConnection.CreateHubProxy(configuration["Settings:SignalRHubName"]);

            toDispose += hubProxy.On <string, int?, FunctionTypeEnum, string>("sendMessage",
                                                                              (clientID, moduleID, functionType, text)
                                                                              => handler(clientID, moduleID, functionType, false, text, moduleAddresses, moduleConfiguration, functionTypes, serialProcessID, hubProxy, logger));

            toDispose += hubProxy.On <string, int?, FunctionTypeEnum>("sendQuery",
                                                                      (clientID, moduleID, functionType)
                                                                      => handler(clientID, moduleID, functionType, true, null, moduleAddresses, moduleConfiguration, functionTypes, serialProcessID, hubProxy, logger));

            try
            {
                hubConnection.Start().Wait();
            }
            catch (Exception e)
            {
                logger($"InitializeHubConnection: ERROR: Exception occurred while initializing hub connection: {e.Message}.");

                return(null);
            }

            logger($"InitializeHubConnection: Connection to hub started with ID '{hubConnection.ConnectionId}'!");

            toDispose += hubConnection;

            return(hubProxy);
        }
示例#3
0
        private static int?FindSerialProcessID(IReadOnlyConfiguration configuration, Action <string> logger)
        {
            if (Constants.IsWindows)
            {
                throw new Exception("Linux only!");
            }

            var nameFragment = configuration["Settings:EngineProcessNameFragment"];

            var pid = Processess.FindByFragment(nameFragment, StringComparison.InvariantCulture, "sudo");

            if (!pid.HasValue)
            {
                logger($"FindSerialProcessID: ERROR: Engine process with name fragment '{nameFragment}' not found!");
                Environment.Exit(-1);
            }

            logger($"FindSerialProcessID: Found process #{pid.Value} with name fragment '{nameFragment}!");

            return(pid.Value);
        }