public static async Task SendDeviceToCloudMessageAsync(CancellationToken cancelToken)
        {
            var diagnosticProvider = new ProbabilityDiagnosticProvider(SamplingRateSource.Client, 50);
            var deviceClient       = DeviceClientWrapper.CreateFromConnectionString(deviceConnectionString, diagnosticProvider);

            const int avgWindSpeed = 10; // m/s
            var       rand         = new Random();

            while (true)
            {
                if (cancelToken.IsCancellationRequested)
                {
                    break;
                }

                var currentWindSpeed = avgWindSpeed + rand.NextDouble() * 4 - 2;

                var telemetryDataPoint = new
                {
                    windSpeed = currentWindSpeed
                };

                var messageString = JsonConvert.SerializeObject(telemetryDataPoint);
                var message       = new Message(Encoding.ASCII.GetBytes(messageString));
                await deviceClient.SendEventAsync(message);

                Console.WriteLine("{0} > Sending message: {1} | Count:{2}", DateTime.Now, messageString, diagnosticProvider.MessageNumber);
                await Task.Delay(500, cancelToken);
            }
        }
        public void SetDefaultDiagnosticProviderWhenUserDoesNotProvideOne()
        {
            var deviceClient       = DeviceClientWrapper.CreateFromConnectionString(fakeConnectionString);
            var diagnosticProvider = deviceClient.GetDiagnosticProvider();

            Assert.AreEqual(diagnosticProvider.GetSamplingRateSource(), SamplingRateSource.None);
            Assert.AreEqual(diagnosticProvider.SamplingRatePercentage, 0);
        }
        public void UserWillReceiveCustomTwinUpdate()
        {
            var fakeDiagnosticProvider = Substitute.For <IDiagnosticProvider>();

            var deviceClient = DeviceClientWrapper.CreateFromConnectionString(fakeConnectionString, fakeDiagnosticProvider);

            var userCallback = Substitute.For <DesiredPropertyUpdateCallback>();

            deviceClient.SetDesiredPropertyUpdateCallback(userCallback, new object());
            var twin = new Twin();

            twin.Properties.Desired["custom_settings"] = "xxxx";
            deviceClient.callbackWrapper(twin.Properties.Desired, new object());
            userCallback.Received(1).Invoke(Arg.Any <TwinCollection>(), Arg.Any <Object>());
        }
        public void TransportSettingsDoNotContainMqttProtocal()
        {
            var transportSetting = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only);

            ITransportSettings[] transportSettings = { transportSetting };

            Assert.ThrowsException <ProtocalNotSupportException>(() =>
            {
                DeviceClientWrapper.CreateFromConnectionString(fakeConnectionString, transportSettings);
            });

            var transportSetting2 = new MqttTransportSettings(TransportType.Mqtt_Tcp_Only);

            ITransportSettings[] transportSettings2 = { transportSetting2 };

            DeviceClientWrapper.CreateFromConnectionString(fakeConnectionString, transportSettings2);
        }
        public void UserWillNotReceiveDiagnosticTwinUpdate()
        {
            var fakeDiagnosticProvider = Substitute.For <IDiagnosticProvider>();

            var deviceClient = DeviceClientWrapper.CreateFromConnectionString(fakeConnectionString, fakeDiagnosticProvider);

            DesiredPropertyUpdateCallback userCallback = (desiredProperties, context) =>
            {
                return(Task.Run(() =>
                {
                    if (desiredProperties.Contains("diag_enabled") || desiredProperties.Contains("diag_sample_rate"))
                    {
                        Assert.Fail();
                    }
                }));
            };

            deviceClient.SetDesiredPropertyUpdateCallback(userCallback, new object());
            var twin = new Twin();

            twin.Properties.Desired["diag_enable"]      = "true";
            twin.Properties.Desired["diag_sample_rate"] = "10";
            deviceClient.callbackWrapper(new TwinCollection(), new object());
        }
Exemple #6
0
        public static async Task SendDeviceToCloudMessageAsync(CancellationToken cancelToken)
        {
            Console.ReadLine();
            var diagnosticProvider = new ProbabilityDiagnosticProvider(SamplingRateSource.Server);
            var deviceClient       = DeviceClientWrapper.CreateFromConnectionString(_deviceConnectionString, diagnosticProvider);

            const int avgWindSpeed             = 10;
            var       rand                     = new Random();
            var       count                    = 0;
            var       beforeStartSendTimeStamp = DateTime.Now;

            while (true)
            {
                count++;

                if (cancelToken.IsCancellationRequested)
                {
                    break;
                }

                var currentWindSpeed = avgWindSpeed + rand.NextDouble() * 4 - 2;

                var telemetryDataPoint = new
                {
                    windSpeed = currentWindSpeed
                };

                var messageString = JsonConvert.SerializeObject(telemetryDataPoint);
                var message       = new Message(Encoding.ASCII.GetBytes(messageString));

                try
                {
                    var timeout = 1000000;

                    Console.WriteLine("{0} > Start to send D2C message: {1} | Count:{2}", DateTime.Now, messageString, count);

                    var task = deviceClient.SendEventAsync(message);

                    if (await Task.WhenAny(task, Task.Delay(timeout)) == task)
                    {
                        Console.WriteLine("{0} > Sending D2C message success: {1} | Count:{2}", DateTime.Now, messageString, count);
                    }
                    else
                    {
                        Console.WriteLine("Send message timeout");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error occur when send D2C message:" + ex);
                }

                if (count == _maxMessageCount)
                {
                    break;
                }
                await Task.Delay(500, cancelToken);
            }
            var afterSendTimeStamp = DateTime.Now;
            var totalTimeConsume   = afterSendTimeStamp - beforeStartSendTimeStamp;

            Console.WriteLine($"StartTime: {beforeStartSendTimeStamp} | EndTime: {afterSendTimeStamp} | TotalTimeConsume: {totalTimeConsume.TotalMilliseconds}");
            Console.WriteLine("All task complete!");
        }