示例#1
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            // 
            // TODO: Insert code to start one or more asynchronous methods 
            //

            BackgroundTaskDeferral deferral = taskInstance.GetDeferral();

            this.tmp102 = new TMP102();

            // set and open the IoT client
            if (this.iotClient == null)
            {
                //this.iotClient = new IoTClient("raspberrypi2", Guid.NewGuid().ToString(), this.connectionString, this.eventHubEntity);
                this.iotClient = new IoTClientConnectTheDots("raspberrypi2", Guid.NewGuid().ToString(), this.connectionString, this.eventHubEntity);
            }

            if (!this.iotClient.IsOpen)
                this.iotClient.Open();


            bool isOpened = await this.tmp102.OpenAsync();
            
            IDictionary bag = new Dictionary<SensorType, float>();

            while (true)
            {
                float temperature = tmp102.Temperature();
                SensorType sensorType = SensorType.Temperature;

                if (!bag.Contains(sensorType))
                    bag.Add(sensorType, temperature);
                else
                    bag[sensorType] = temperature;

                if ((this.iotClient != null) && (this.iotClient.IsOpen))
                {
                    this.iotClient.SendAsync(bag);
                }

                await Task.Delay(5000);
            }

            deferral.Complete();
        }
示例#2
0
        public MainPage()
        {
            this.InitializeComponent();

            this.tmp102 = new TMP102();

            // set and open the IoT client
            if (this.iotClient == null)
            {
                //this.iotClient = new IoTClient("raspberrypi2", Guid.NewGuid().ToString(), this.connectionString, this.eventHubEntity);
                this.iotClient = new IoTClientConnectTheDots("raspberrypi2", Guid.NewGuid().ToString(), this.connectionString, this.eventHubEntity);
            }

            if (!this.iotClient.IsOpen)
                this.iotClient.Open();

            // just to start without UI :-)
            this.btnStart_Click(null, null);
        }