public void ValidConnectingString(string connectionString)
        {
            var deviceFactory = DeviceClientFactory.CreateDeviceClientAsync(connectionString);

            Assert.NotNull(deviceFactory);
            Assert.NotEqual(ConnectionStringType.Invalid, DeviceClientFactory.Instance.connectionStringType);
        }
        public async Task RunAsync(string connectionString, ILogger logger, CancellationToken quitSignal)
        {
            this.logger = logger;

            deviceClient = await DeviceClientFactory.CreateDeviceClientAsync(connectionString, logger, modelId);

            await deviceClient.SetDesiredPropertyUpdateCallbackAsync(DesiredPropertyUpdateCallback, this, quitSignal);

            await deviceClient.SetMethodDefaultHandlerAsync(root_DefaultCommandHadler, null, quitSignal);

            var twin = await deviceClient.GetTwinAsync();

            var reportADate = CreateAck("aDate", DateTime.Now, 200, 1);
            await deviceClient.UpdateReportedPropertiesAsync(reportADate);

            //TwinCollection reported = new TwinCollection();
            //reported["aDateTime"] = "1010-10-10T10:10";
            //reported["aDate"] = "1010-10-10";
            //await deviceClient.UpdateReportedPropertiesAsync(reported);

            await Task.Run(async() =>
            {
                while (!quitSignal.IsCancellationRequested)
                {
                    logger.LogInformation("not Sending Telemetry");
                    await Task.Delay(5000);
                }
            });
        }
示例#3
0
        public async Task RunAsync(string connectionString, ILogger logger, CancellationToken quitSignal)
        {
            this.logger = logger;

            deviceClient = await DeviceClientFactory.CreateDeviceClientAsync(connectionString, logger, modelId);

            var twin = await deviceClient.GetTwinAsync();

            await Task.Run(async() =>
            {
                while (!quitSignal.IsCancellationRequested)
                {
                    await deviceClient.SendEventAsync(
                        new Message(
                            Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(
                                                       new {
                        People = new {
                            personName = "rido",
                            isValid    = true,
                            birthday   = DateTime.Now.ToUniversalTime()
                        }
                    }
                                                       )))
                    {
                        ContentEncoding = "utf-8",
                        ContentType     = "application/json"
                    });

                    logger.LogInformation("Sending Telemetry");
                    await Task.Delay(5000);
                }
            });
        }
示例#4
0
        private async Task <DigitalTwinClient> CreateDigitalTwinsClientAsync()
        {
            var deviceClient = await DeviceClientFactory.CreateDeviceClientAsync(connectionString, logger);

            deviceClient.SetConnectionStatusChangesHandler((ConnectionStatus status, ConnectionStatusChangeReason reason) => logger.LogWarning($"Connection status changed: {status} {reason}"));
            var digitalTwinClient = new DigitalTwinClient(deviceClient);

            return(digitalTwinClient);
        }
        //Fact]
        public async Task DirectWithSas()
        {
            ConnectionStatus reportedStatus = ConnectionStatus.Disconnected;
            var dc = await DeviceClientFactory.CreateDeviceClientAsync("HostName=e2e-test-hub.azure-devices.net;DeviceId=test-sas-01;SharedAccessKey=pRNwmc8UU0fH6vnTZ50PmfqGffii5fWWLNfdQOaBsu8=");

            dc.SetConnectionStatusChangesHandler((ConnectionStatus status, ConnectionStatusChangeReason reason) => reportedStatus = status);
            await dc.OpenAsync();

            Assert.Equal(ConnectionStatus.Connected, reportedStatus);
        }
 public void InvalidConnectingString(string connectionString)
 {
     try
     {
         var deviceFactory = DeviceClientFactory.CreateDeviceClientAsync(connectionString);
     }
     catch (ApplicationException)
     {
         Assert.Equal(ConnectionStringType.Invalid, DeviceClientFactory.Instance.connectionStringType);
     }
 }
示例#7
0
        static async Task Main(string[] args)
        {
            string       connectionString = Environment.GetEnvironmentVariable("CS");
            string       modelId          = "dtmi:com:example:TemperatureController;1";
            DeviceClient dc = await DeviceClientFactory.CreateDeviceClientAsync(connectionString, modelId);

            await dc.OpenAsync();

            Console.WriteLine("Connected");
            await Task.Delay(500);

            await dc.CloseAsync();
        }
示例#8
0
        public async Task RunAsync(string connectionString, ILogger logger, CancellationToken quitSignal)
        {
            this.logger  = logger;
            deviceClient = await DeviceClientFactory.CreateDeviceClientAsync(connectionString, logger, modelId);

            await deviceClient.SetMethodDefaultHandlerAsync(DefaultCommandHadlerAsync, null, quitSignal);

            await deviceClient.SetDesiredPropertyUpdateCallbackAsync(DesiredPropertyUpdateCallback, null, quitSignal);

            var reported = new TwinCollection();

            reported["serialNumber"]     = serialNumber;
            reported["baseSerialNumber"] = serialNumber;
            await deviceClient.UpdateReportedPropertiesAsync(reported);

            var reportedInterface01 = new TwinCollection();

            reportedInterface01["myinterface01"] = new {
                __t          = "c",
                serialNumber = serialNumber
            };
            await deviceClient.UpdateReportedPropertiesAsync(reportedInterface01);

            var reportedInterface02 = new TwinCollection();

            reportedInterface02["myinterface02"] = new {
                __t          = "c",
                serialNumber = serialNumber
            };
            await deviceClient.UpdateReportedPropertiesAsync(reportedInterface02);

            await Task.Run(async() =>
            {
                while (!quitSignal.IsCancellationRequested)
                {
                    await deviceClient.SendEventAsync(
                        new Message(
                            Encoding.UTF8.GetBytes(
                                "{" +
                                "\"workingSet\" : " + Environment.WorkingSet +
                                "}"))
                    {
                        ContentEncoding = "utf-8",
                        ContentType     = "application/json"
                    });

                    logger.LogInformation("Sending workingset ");
                    await Task.Delay(Convert.ToInt32(refreshInterval) * 1000);
                }
            });
        }
示例#9
0
        public async Task RunDeviceAsync()
        {
            var deviceClient = await DeviceClientFactory.CreateDeviceClientAsync(_connectionString, _logger);

            var deviceInformation = new DeviceInformation(deviceClient);
            await deviceInformation.UpdatePropertiesAsync();

            var s1Sensor = new S1Sensor(deviceClient);
            await s1Sensor.SyncTwinPropertiesAsync();

            await s1Sensor.RegisterCommandsAsync();

            await s1Sensor.EnterTelemetryLoopAsync(_quitSignal);
        }
示例#10
0
        public async Task RunAsync(string connectionString, ILogger logger, CancellationToken quitSignal)
        {
            this.logger = logger;

            //deviceClient = DeviceClient.CreateFromConnectionString(connectionString,
            //  TransportType.Mqtt, new ClientOptions { ModelId = modelId });
            temperatureSeries.Add(DateTime.Now, CurrentTemperature);
            deviceClient = await DeviceClientFactory.CreateDeviceClientAsync(connectionString, logger, modelId);

            await deviceClient.SetDesiredPropertyUpdateCallbackAsync(DesiredPropertyUpdateCallback, this, quitSignal);

            await deviceClient.SetMethodHandlerAsync("getMaxMinReport", root_getMaxMinReportCommandHadler, this);

            var twin = await deviceClient.GetTwinAsync();

            double targetTemperature = GetPropertyValue <double>(twin.Properties.Desired, "targetTemperature");

            if (targetTemperature > 0)
            {
                await AckDesiredPropertyReadAsync("targetTemperature", targetTemperature, 200, "property synced", twin.Properties.Desired.Version);
            }



            await this.ProcessTempUpdateAsync(targetTemperature);

            await Task.Run(async() =>
            {
                while (!quitSignal.IsCancellationRequested)
                {
                    temperatureSeries.Add(DateTime.Now, CurrentTemperature);

                    await deviceClient.SendEventAsync(
                        new Message(
                            Encoding.UTF8.GetBytes(
                                "{" +
                                "\"temperature\": " + CurrentTemperature + "," +
                                "\"workingSet\" : " + Environment.WorkingSet +
                                "}"))
                    {
                        ContentEncoding = "utf-8",
                        ContentType     = "application/json"
                    });

                    logger.LogInformation("Sending CurrentTemperature and workingset " + CurrentTemperature);
                    await Task.Delay(1000);
                }
            });
        }
示例#11
0
        public async Task RunAsync(string connectionString, ILogger logger, CancellationToken quitSignal)
        {
            this.logger = logger;

            deviceClient = await DeviceClientFactory.CreateDeviceClientAsync(connectionString, logger, modelId);

            await deviceClient.SetDesiredPropertyUpdateCallbackAsync(DesiredPropertyUpdateCallback, this, quitSignal);

            await deviceClient.SetMethodDefaultHandlerAsync(root_DefaultCommandHadler, null, quitSignal);

            var twin = await deviceClient.GetTwinAsync();

            TwinCollection reported = new TwinCollection();

            reported["Owner"] = new {
                personName = "owner",
                birthday   = DateTime.Now,
                isValid    = true
            };
            await deviceClient.UpdateReportedPropertiesAsync(reported);

            await Task.Run(async() =>
            {
                while (!quitSignal.IsCancellationRequested)
                {
                    await deviceClient.SendEventAsync(
                        new Message(
                            Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(
                                                       new {
                        People = new {
                            personName = "rido",
                            isValid    = true,
                            birthday   = DateTime.Now.ToUniversalTime()
                        }
                    }
                                                       )))
                    {
                        ContentEncoding = "utf-8",
                        ContentType     = "application/json"
                    });

                    logger.LogInformation("Sending Telemetry");
                    await Task.Delay(5000);
                }
            });
        }
        public async Task RunAsync(string connectionString, ILogger logger, CancellationToken quitSignal)
        {
            this.logger = logger;

            deviceClient = await DeviceClientFactory.CreateDeviceClientAsync(connectionString, logger, modelId);

            await deviceClient.SetDesiredPropertyUpdateCallbackAsync(DesiredPropertyUpdateCallback, this, quitSignal);

            var twin = await deviceClient.GetTwinAsync();

            logger.LogInformation(twin.ToJson());

            await Task.Run(async() =>
            {
                while (!quitSignal.IsCancellationRequested)
                {
                    await UpdateClient();
                    await Task.Delay(1000);
                }
            });
        }