Beispiel #1
0
 public SimulatedDevice(string deviceId, RunnerConfiguration config, ISender sender)
 {
     this.DeviceID = deviceId;
     this.config   = config;
     this.sender   = sender;
     this.interval = config.GetMessageIntervalForDevice(deviceId);
 }
Beispiel #2
0
 public SimulationWorker(IDeviceSimulatorFactory deviceSimulatorFactory,
                         IHostApplicationLifetime applicationLifetime,
                         IConfiguration configuration,
                         ILogger <SimulationWorker> logger)
 {
     this.deviceSimulatorFactory = deviceSimulatorFactory;
     this.applicationLifetime    = applicationLifetime;
     this.stopping = new CancellationTokenSource();
     this.config   = RunnerConfiguration.Load(configuration, logger);
     this.config.EnsureIsValid();
 }
        private ISender GetSender(string deviceId, RunnerConfiguration config)
        {
            if (!string.IsNullOrEmpty(config.IotHubConnectionString))
            {
                return(GetIotHubSender(deviceId, config));
            }

            if (!string.IsNullOrEmpty(config.EventHubConnectionString))
            {
                return(this.CreateEventHubSender(deviceId, config));
            }

            throw new ArgumentException("No connnection string specified");
        }
        public SimulatedDevice Create(string deviceId, RunnerConfiguration config)
        {
            var deviceClient = DeviceClient.CreateFromConnectionString(
                config.IotHubConnectionString,
                deviceId,
                new ITransportSettings[]
            {
                new AmqpTransportSettings(TransportType.Amqp_Tcp_Only)
                {
                    AmqpConnectionPoolSettings = new AmqpConnectionPoolSettings()
                    {
                        Pooling = true,
                    }
                }
            });

            return(new SimulatedDevice(deviceId, config, deviceClient));
        }
        private static ISender GetIotHubSender(string deviceId, RunnerConfiguration config)
        {
            // create one deviceClient for each device
            var deviceClient = DeviceClient.CreateFromConnectionString(
                config.IotHubConnectionString,
                deviceId,
                new ITransportSettings[]
            {
                new AmqpTransportSettings(Microsoft.Azure.Devices.Client.TransportType.Amqp_Tcp_Only)
                {
                    AmqpConnectionPoolSettings = new AmqpConnectionPoolSettings()
                    {
                        Pooling = true,
                    }
                }
            });

            return(new IotHubSender(deviceClient, deviceId, config));
        }
Beispiel #6
0
 public SenderBase(string deviceId, RunnerConfiguration config)
 {
     this.deviceId = deviceId;
     this.Config   = config;
 }
Beispiel #7
0
        internal static RunnerConfiguration Load(IConfiguration configuration, ILogger logger)
        {
            var config = new RunnerConfiguration();

            config.IotHubConnectionString = configuration.GetValue <string>(nameof(IotHubConnectionString));
            config.DevicePrefix           = configuration.GetValue(nameof(DevicePrefix), config.DevicePrefix);
            config.DeviceIndex            = configuration.GetValue(nameof(DeviceIndex), config.DeviceIndex);
            config.DeviceCount            = configuration.GetValue(nameof(DeviceCount), config.DeviceCount);
            config.MessageCount           = configuration.GetValue(nameof(MessageCount), config.MessageCount);
            config.Interval = configuration.GetValue(nameof(Interval), config.Interval);

            var rawFixTelemetry = configuration.GetValue <string>(nameof(FixPayload));

            if (rawFixTelemetry != null)
            {
                config.FixPayload = Convert.FromBase64String(rawFixTelemetry);
                logger.LogWarning("Using fix payload telemetry");
            }
            else if (int.TryParse(configuration.GetValue <string>("FixPayloadSize"), out var fixPayloadSize) && fixPayloadSize > 0)
            {
                config.FixPayload = new byte[fixPayloadSize];
                logger.LogWarning("Using fix payload telemetry with size {size}", fixPayloadSize);
            }
            else
            {
                var rawTelemetryTemplate = configuration.GetValue <string>(nameof(Template));
                if (!string.IsNullOrWhiteSpace(rawTelemetryTemplate))
                {
                    config.Template = new TelemetryTemplate(rawTelemetryTemplate);
                }
                else
                {
                    logger.LogWarning("Using default telemetry template");
                    config.Template = new TelemetryTemplate();
                }
            }

            var rawDeviceList = configuration.GetValue <string>(nameof(DeviceList));

            if (!string.IsNullOrWhiteSpace(rawDeviceList))
            {
                config.DeviceList = rawDeviceList.Split(new[] { ';', ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (config.DeviceList.Count > 0)
                {
                    config.DeviceCount  = config.DeviceList.Count;
                    config.DevicePrefix = string.Empty;
                }
            }

            var rawHeaderTemplate = configuration.GetValue <string>(nameof(Header));

            if (!string.IsNullOrWhiteSpace(rawHeaderTemplate))
            {
                config.Header = new TelemetryTemplate(rawHeaderTemplate);
            }

            var rawValues = configuration.GetValue <string>(nameof(Variables));

            if (!string.IsNullOrWhiteSpace(rawValues))
            {
                try
                {
                    var values = JsonConvert.DeserializeObject <TelemetryVariable[]>(rawValues);
                    config.Variables = new TelemetryValues(values);
                }
                catch (JsonReaderException ex)
                {
                    throw new Exception($"Failed to parse variables from: {rawValues}", ex);
                }
            }
            else
            {
                logger.LogWarning("No custom telemetry variables found");
                config.Variables = new TelemetryValues(new TelemetryVariable[] {
                    new TelemetryVariable
                    {
                        Min  = 1,
                        Name = "Counter",
                        Step = 1,
                    }
                });
            }

            return(config);
        }
Beispiel #8
0
 public SimulatedDevice(string deviceId, RunnerConfiguration config, ISender sender)
 {
     this.DeviceID = deviceId;
     this.config   = config;
     this.sender   = sender;
 }
Beispiel #9
0
 public SimulatedDevice(string deviceId, RunnerConfiguration config, DeviceClient deviceClient)
 {
     this.deviceId     = deviceId;
     this.config       = config;
     this.deviceClient = deviceClient;
 }
 public KafkaSender(IProducer <Null, byte[]> producer, string deviceId, RunnerConfiguration config, string topic)
     : base(deviceId, config)
 {
     this.producer = producer;
     this.topic    = topic;
 }
Beispiel #11
0
        private static List <PayloadBase> LoadPayloads(IConfiguration configuration, RunnerConfiguration config, ILogger logger)
        {
            var payloads = new List <PayloadBase>();

            var isDefaultTemplateContent             = false;
            TelemetryTemplate defaultPayloadTemplate = null;
            var rawTelemetryTemplate = configuration.GetValue <string>(Constants.TemplateConfigName);

            if (!string.IsNullOrWhiteSpace(rawTelemetryTemplate))
            {
                defaultPayloadTemplate = new TelemetryTemplate(rawTelemetryTemplate);
            }
            else
            {
                defaultPayloadTemplate   = new TelemetryTemplate();
                isDefaultTemplateContent = true;
            }

            var rawDynamicPayload = configuration.GetValue <string>(Constants.PayloadDistributionConfigName);

            if (!string.IsNullOrEmpty(rawDynamicPayload))
            {
                var matches = templateParser.Matches(rawDynamicPayload);
                foreach (Match m in matches)
                {
                    if (m.Groups.TryGetValue("type", out var templateType) && m.Groups.TryGetValue("pv", out var paramValuesRaw))
                    {
                        var templateTypeLowercase = templateType.Value.ToLowerInvariant();

                        var paramValues = paramValuesRaw.Value.Split(",", StringSplitOptions.RemoveEmptyEntries);
                        if (paramValues.Length == 0)
                        {
                            logger.LogWarning("Expecting parameters in payload definition, found nothing template type '{value}'", templateTypeLowercase);
                            continue;
                        }

                        if (!int.TryParse(paramValues[0].Replace("%", string.Empty), out var distribution))
                        {
                            logger.LogWarning("Could not parse payload distribution from '{value}'", paramValues[0]);
                            continue;
                        }

                        switch (templateTypeLowercase)
                        {
                        case "fixsize":
                            if (paramValues.Length > 0 && int.TryParse(paramValues[1], out var fixSize) && fixSize >= 0)
                            {
                                payloads.Add(new FixPayload(distribution, new byte[fixSize]));
                            }

                            break;

                        case "fix":
                            if (paramValues.Length > 0 && !string.IsNullOrWhiteSpace(paramValues[1]))
                            {
                                var base64Text = paramValues[1].Trim();
                                try
                                {
                                    var bytes = Convert.FromBase64String(base64Text);
                                    payloads.Add(new FixPayload(distribution, bytes));
                                }
                                catch (Exception)
                                {
                                    logger.LogWarning("Could not parse base64 payload '{value}'", base64Text);
                                }
                            }

                            break;

                        case "template":
                            if (paramValues.Length == 1)
                            {
                                payloads.Add(new TemplatedPayload(distribution, defaultPayloadTemplate, config.Variables));
                            }
                            else
                            {
                                var templateName = paramValues[1].Trim();
                                if (templateName.Equals("default", StringComparison.OrdinalIgnoreCase))
                                {
                                    payloads.Add(new TemplatedPayload(distribution, defaultPayloadTemplate, config.Variables));
                                }
                                else
                                {
                                    var rawTemplateValue = configuration.GetValue <string>(templateName);
                                    if (string.IsNullOrWhiteSpace(rawTemplateValue))
                                    {
                                        logger.LogWarning("Could not find template with name '{name}'", templateName);
                                    }
                                    else
                                    {
                                        payloads.Add(new TemplatedPayload(distribution, new TelemetryTemplate(rawTemplateValue), config.Variables));
                                    }
                                }
                            }

                            break;

                        default:
                            logger.LogWarning("Unknown payload type '{type}'", templateTypeLowercase);
                            break;
                        }
                    }
                }
            }

            if (payloads.Count == 0)
            {
                payloads.Add(new TemplatedPayload(100, defaultPayloadTemplate, config.Variables));
                if (isDefaultTemplateContent)
                {
                    logger.LogWarning("Using default telemetry template");
                }
            }

            if (payloads.Select(x => x.Distribution).Sum() != 100)
            {
                logger.LogWarning("Payload percentage distribution is not equal 100");
            }

            return(payloads);
        }
 private ISender CreateKafkaSender(string deviceId, RunnerConfiguration config)
 {
     // Reuse the same KafkaProducer for all devices
     this.kafkaProducer ??= new ProducerBuilder <Null, byte[]>(config.KafkaConnectionProperties.ToList()).Build();
     return(new KafkaSender(this.kafkaProducer, deviceId, config, config.KafkaTopic));
 }
 private ISender CreateEventHubSender(string deviceId, RunnerConfiguration config)
 {
     // Reuse the same eventHubClient for all devices
     this.eventHubProducerClient = this.eventHubProducerClient ?? new EventHubProducerClient(config.EventHubConnectionString);
     return(new EventHubSender(this.eventHubProducerClient, deviceId, config));
 }
        public SimulatedDevice Create(string deviceId, RunnerConfiguration config)
        {
            var sender = this.GetSender(deviceId, config);

            return(new SimulatedDevice(deviceId, config, sender));
        }
 public IotHubSender(DeviceClient deviceClient, string deviceId, RunnerConfiguration config)
     : base(deviceId, config)
 {
     this.deviceClient = deviceClient;
 }
Beispiel #16
0
 public EventHubSender(EventHubClient eventHubClient, string deviceId, RunnerConfiguration config)
     : base(deviceId, config)
 {
     this.eventHubClient = eventHubClient;
 }
Beispiel #17
0
 public EventHubSender(EventHubProducerClient eventHubProducer, string deviceId, RunnerConfiguration config)
     : base(deviceId, config)
 {
     this.eventHubProducer = eventHubProducer;
 }
Beispiel #18
0
        public static RunnerConfiguration Load(IConfiguration configuration, ILogger logger)
        {
            var config = new RunnerConfiguration();

            config.IotHubConnectionString   = configuration.GetValue <string>(nameof(IotHubConnectionString));
            config.EventHubConnectionString = configuration.GetValue <string>(nameof(EventHubConnectionString));
            config.DevicePrefix             = configuration.GetValue(nameof(DevicePrefix), config.DevicePrefix);
            config.DeviceIndex  = configuration.GetValue(nameof(DeviceIndex), config.DeviceIndex);
            config.DeviceCount  = configuration.GetValue(nameof(DeviceCount), config.DeviceCount);
            config.MessageCount = configuration.GetValue(nameof(MessageCount), config.MessageCount);
            config.Interval     = configuration.GetValue(nameof(Interval), config.Interval);

            var rawValues = configuration.GetValue <string>(nameof(Variables));

            if (!string.IsNullOrWhiteSpace(rawValues))
            {
                try
                {
                    var values = JsonConvert.DeserializeObject <TelemetryVariable[]>(rawValues);
                    config.Variables = new TelemetryValues(values);
                }
                catch (JsonReaderException ex)
                {
                    throw new Exception($"Failed to parse variables from: {rawValues}", ex);
                }
            }
            else
            {
                logger.LogWarning("No custom telemetry variables found");
                config.Variables = new TelemetryValues(new TelemetryVariable[]
                {
                    new TelemetryVariable
                    {
                        Min  = 1,
                        Name = "Counter",
                        Step = 1,
                    }
                });
            }

            config.PayloadGenerator = new PayloadGenerator(LoadPayloads(configuration, config, logger), new DefaultRandomizer());

            var rawDeviceList = configuration.GetValue <string>(nameof(DeviceList));

            if (!string.IsNullOrWhiteSpace(rawDeviceList))
            {
                config.DeviceList = rawDeviceList.Split(new[] { ';', ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (config.DeviceList.Count > 0)
                {
                    config.DeviceCount  = config.DeviceList.Count;
                    config.DevicePrefix = string.Empty;
                }
            }

            var rawHeaderTemplate = configuration.GetValue <string>(nameof(Header));

            if (!string.IsNullOrWhiteSpace(rawHeaderTemplate))
            {
                config.Header = new TelemetryTemplate(rawHeaderTemplate);
            }

            return(config);
        }