Ejemplo n.º 1
0
        protected override void OnStart(string[] args)
        {
            _logger.LogInfo("Service starting... ");

            if (_webHost != null)
            {
                _webHost.Close( );
            }

            _batchSenderThread.Start( );

            _webHost = new WebServiceHost(typeof(Microsoft.ConnectTheDots.Gateway.GatewayService));
            Gateway.GatewayService service = new Microsoft.ConnectTheDots.Gateway.GatewayService(
                _gatewayQueue,
                _batchSenderThread,
                m => DataTransforms.QueuedItemFromSensorDataContract(
                    DataTransforms.AddTimeCreated(DataTransforms.SensorDataContractFromString(m, _logger)), _logger)
                );
            _webHost.Description.Behaviors.Add(new ServiceBehavior(() => service));

            service.Logger         = _logger;
            service.OnDataInQueue += OnData;

            _webHost.Open( );

            _dataIntakeLoader.StartAll(service.Enqueue);

            _logger.LogInfo("...started");
        }
Ejemplo n.º 2
0
        //--//

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

            _completed = new AutoResetEvent(false);

            _logger = logger;

            _rand = new Random( );
            _totalMessagesSent   = 0;
            _totalMessagesToSend = 0;
            _gatewayQueue        = new GatewayQueue <QueuedItem>( );

#if MOCK_SENDER
            _sender = new MockSender <SensorDataContract>(this);
#else
            IotHubConfig iotHubConfig = Loader.GetIotHubConfig( );

            _sender = new MessageSender <SensorDataContract>(iotHubConfig.IotHubConnectionString, _logger);
#endif

            _batchSenderThread = new BatchSenderThread <QueuedItem, SensorDataContract>(
                _gatewayQueue,
                _sender,
                dataTransform: null,
                serializedData: m => (m == null) ? null : m.JsonData,
                logger: _logger
                );

            string gatewayIPAddressString = string.Empty;
            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)));
            }
        }
Ejemplo n.º 3
0
        private GatewayService PrepareGatewayService( )
        {
            _batchSenderThread.Start( );

            GatewayService service = new GatewayService(_gatewayQueue, _batchSenderThread,
                                                        m => DataTransforms.QueuedItemFromSensorDataContract(
                                                            DataTransforms.AddTimeCreated(DataTransforms.SensorDataContractFromString(m, _logger)), _logger));

            service.Logger         = _logger;
            service.OnDataInQueue += DataInQueue;

            return(service);
        }
Ejemplo n.º 4
0
        //--//

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

            _completed = new AutoResetEvent(false);

            _logger = logger;

            _rand = new Random();
            _totalMessagesSent   = 0;
            _totalMessagesToSend = 0;
            _gatewayQueue        = new GatewayQueue <QueuedItem>();

#if MOCK_SENDER
            _sender = new IotHubSender <SensorDataContract>("HostName=technobee-infrastructure-testbed-01-iot-hub.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=0JgLhqkHopPQVQhvm07R20T/6JZ9JTzt5T8rSRkLvBg=", _logger); // new RabbitMQSender<SensorDataContract>(_logger/*, "address"*/); // MockSender

            //"HostName=technobee-infrastructure-testbed-01-iot-hub.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=0JgLhqkHopPQVQhvm07R20T/6JZ9JTzt5T8rSRkLvBg="
#else
            string IotHubConnectionString = "";

            _sender = new MessageSender <SensorDataContract>(IotHubConnectionString, _logger);
#endif

            _batchSenderThread = new BatchSenderThread <QueuedItem, SensorDataContract>(
                _gatewayQueue,
                _sender,
                dataTransform: null,
                serializedData: m => (m == null) ? null : m.JsonData,
                logger: _logger
                );

            string gatewayIPAddressString = string.Empty;

            Func <string, SensorDataContract> transform = (m => DataTransforms.SensorDataContractFromString(m, _logger));


            _gatewayTransform = (m => DataTransforms.QueuedItemFromSensorDataContract(transform(m)));
        }
Ejemplo n.º 5
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);

                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 WindowsService: " + ex.Message);
            }
        }
Ejemplo n.º 6
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>( );
                IotHubConfig iotHubConfig = Loader.GetIotHubConfig();

                if (iotHubConfig == null)
                {
                    _logger.LogError("IoT Hub connection configuration is missing");
                    return;
                }
                _MessageSender = new MessageSender <SensorDataContract>(iotHubConfig.IotHubConnectionString, _logger);

                _batchSenderThread = new BatchSenderThread <QueuedItem, SensorDataContract>(
                    _gatewayQueue,
                    _MessageSender,
                    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);
            }
        }