Ejemplo n.º 1
0
        //--//

        public WindowsService(ILogger logger)
        {
            if (logger == null)
            {
                throw new ArgumentException("Cannot run service without logging");
            }

            _logger = logger;

            if (logger is TunableLogger)
            {
                TunableLogger.LoggingLevel loggingLevel = TunableLogger.LevelFromString(ConfigurationManager.AppSettings.Get("LoggingLevel"));

                (( TunableLogger )logger).Level = (loggingLevel != TunableLogger.LoggingLevel.Undefined) ? loggingLevel : TunableLogger.LoggingLevel.Errors;
            }

            try
            {
                _THREADING.TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;

                // Name the Windows Service
                ServiceName = Constants.WindowsServiceName;

                _gatewayQueue = new GatewayQueue <QueuedItem>( );
                AMQPConfig amqpConfig = Loader.GetAMQPConfig( );

                if (amqpConfig == null)
                {
                    _logger.LogError("AMQP configuration is missing");
                    return;
                }
                _AMPQSender = new AMQPSender <SensorDataContract>(
                    amqpConfig.AMQPSAddress,
                    amqpConfig.EventHubName,
                    amqpConfig.EventHubMessageSubject,
                    amqpConfig.EventHubDeviceId,
                    amqpConfig.EventHubDeviceDisplayName,
                    _logger
                    );
                _batchSenderThread = new BatchSenderThread <QueuedItem, SensorDataContract>(
                    _gatewayQueue,
                    _AMPQSender,
                    null,                                //m => DataTransforms.AddTimeCreated(DataTransforms.SensorDataContractFromQueuedItem(m, _Logger)),
                    new Func <QueuedItem, string>(m => m.JsonData),
                    _logger);

                _dataIntakeLoader = new DeviceAdapterLoader(Loader.GetSources( ), Loader.GetEndpoints( ), _logger);
            }
            catch (Exception ex)
            {
                _logger.LogError("Exception creating WindowsService: " + ex.Message);
            }
        }
        private static GatewayService CreateGateway(AMQPConfig amqpConfig)
        {
            try
            {
                var _gatewayQueue = new GatewayQueue<QueuedItem>();

                var _AMPQSender = new AMQPSender<string>(
                                                    amqpConfig.AMQPSAddress,
                                                    amqpConfig.EventHubName,
                                                    amqpConfig.EventHubMessageSubject,
                                                    amqpConfig.EventHubDeviceId,
                                                    amqpConfig.EventHubDeviceDisplayName,
                                                    _Logger
                                                    );

                var _batchSenderThread = new BatchSenderThread<QueuedItem, string>(
                                                    _gatewayQueue,
                                                    _AMPQSender,
                                                    null,
                                                    m => m.JsonData,
                                                    _Logger);

                _batchSenderThread.Start();

                GatewayService service = new GatewayService(
                    _gatewayQueue,
                    _batchSenderThread
                )
                {
                    Logger = _Logger
                };

                service.OnDataInQueue += (data) => _batchSenderThread.Process();
                _Logger.Flush();

                return service;
            }
            catch (Exception ex)
            {
                _Logger.LogError("Exception on creating Gateway: " + ex.Message);
            }

            return null;
        }
Ejemplo n.º 3
0
        internal static AMQPConfig GetAMQPConfig( )
        {
            AMQPServiceConfigSection section = ConfigurationManager.GetSection( "AMQPServiceConfig" ) as AMQPServiceConfigSection;
            AMQPConfig configData = null;

            if( section != null )
            {
                configData = new AMQPConfig
                {
                    AMQPSAddress = section.AMQPSAddress,
                    EventHubName = section.EventHubName,
                    EventHubMessageSubject = section.EventHubMessageSubject,
                    EventHubDeviceId = section.EventHubDeviceId,
                    EventHubDeviceDisplayName = section.EventHubDeviceDisplayName
                };
            }

            return configData;
        }
Ejemplo n.º 4
0
        //--//

        private static void InitGateway( ILogger logger )
        {
            if( logger == null )
            {
                throw new ArgumentException( "Cannot run service without logging" );
            }

            _logger = logger;

            if( logger is TunableLogger )
            {
                TunableLogger.LoggingLevel loggingLevel = TunableLogger.LevelFromString( ConfigurationManager.AppSettings.Get( "LoggingLevel" ) );

                ( ( TunableLogger )logger ).Level = ( loggingLevel != TunableLogger.LoggingLevel.Undefined ) ? loggingLevel : TunableLogger.LoggingLevel.Errors;
            }

            try
            {
                System.Threading.Tasks.TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;

                // Name the Windows Service
                
                _gatewayQueue = new GatewayQueue<QueuedItem>( );
                AMQPConfig amqpConfig = Loader.GetAMQPConfig( );

                if( amqpConfig == null )
                {
                    _logger.LogError( "AMQP configuration is missing" );
                    return;
                }
                _AMPQSender = new AMQPSender<SensorDataContract>(
                                                    amqpConfig.AMQPSAddress,
                                                    amqpConfig.EventHubName,
                                                    amqpConfig.EventHubMessageSubject,
                                                    amqpConfig.EventHubDeviceId,
                                                    amqpConfig.EventHubDeviceDisplayName,
                                                    _logger
                                                    );
                
                _batchSenderThread = new BatchSenderThread<QueuedItem, SensorDataContract>(
                                                    _gatewayQueue,
                                                    _AMPQSender,
                                                    null,//m => DataTransforms.AddTimeCreated(DataTransforms.SensorDataContractFromQueuedItem(m, _Logger)),
                                                    new Func<QueuedItem, string>( m => m.JsonData ),
                                                    _logger );

                _dataIntakeLoader = new DeviceAdapterLoader( Loader.GetSources( ), Loader.GetEndpoints( ), _logger );

                TaskWrapper.Run( ( ) => IPAddressHelper.GetIPAddressString( ref _gatewayIPAddressString ) );

                DataTransformsConfig dataTransformsConfig = Loader.GetDataTransformsConfig( );
                if( dataTransformsConfig.AttachIP || dataTransformsConfig.AttachTime )
                {
                    Func<string, SensorDataContract> transform = ( m => DataTransforms.SensorDataContractFromString( m, _logger ) );

                    if( dataTransformsConfig.AttachTime )
                    {
                        var transformPrev = transform;
                        transform = ( m => DataTransforms.AddTimeCreated( transformPrev( m ) ) );
                    }

                    if( dataTransformsConfig.AttachTime )
                    {
                        var transformPrev = transform;
                        transform = ( m => DataTransforms.AddIPToLocation( transformPrev( m ), _gatewayIPAddressString ) );
                    }

                    _gatewayTransform = ( m => DataTransforms.QueuedItemFromSensorDataContract( transform( m ) ) );
                }
            }
            catch( Exception ex )
            {
                _logger.LogError( "Exception creating Gateway: " + ex.Message );
            }
        }
        private static AMQPConfig PrepareAMQPConfig(string connectionString, string hubName,
            string messageSubject, string messageDeviceId, string messageDeviceDisplayName)
        {
            try
            {
                NamespaceManager nsmgr = NamespaceManager.CreateFromConnectionString(connectionString);
                EventHubDescription desc = nsmgr.GetEventHub(hubName);

                foreach (var rule in desc.Authorization)
                {
                    var accessAuthorizationRule = rule as SharedAccessAuthorizationRule;
                    if (accessAuthorizationRule == null) continue;
                    if (!accessAuthorizationRule.Rights.Contains(AccessRights.Send)) continue;

                    string amqpAddress = string.Format("amqps://{0}:{1}@{2}",
                        accessAuthorizationRule.KeyName,
                        Uri.EscapeDataString(accessAuthorizationRule.PrimaryKey), nsmgr.Address.Host);

                    AMQPConfig amqpConfig = new AMQPConfig
                    {
                        AMQPSAddress = amqpAddress,
                        EventHubName = hubName,
                        EventHubDeviceDisplayName =
                            string.IsNullOrEmpty(messageSubject) ? "SensorGatewayService" : messageSubject,
                        EventHubDeviceId =
                            string.IsNullOrEmpty(messageDeviceId)
                                ? "a94cd58f-4698-4d6a-b9b5-4e3e0f794618"
                                : messageDeviceId,
                        EventHubMessageSubject =
                            string.IsNullOrEmpty(messageDeviceDisplayName) ? "gtsv" : messageDeviceDisplayName
                    };
                    return amqpConfig;
                }
            }
            catch (Exception)
            {
            }
            return null;
        }