Esempio n. 1
0
        private async Task RunApplication()
        {
            _logging.LogMessage("Initializing Application", LoggingLevel.Verbose);

            //Set up IoT Hub Device
            if (!await _azureIoTHubService.Init(_tpmDevice.GetConnectionString()))
            {
                _logging.LogMessage("The application was not initialized. Please make sure that the TPM service is properly set up on Device 0. Then restart the application.", LoggingLevel.Error);
                return;
            }

            await InitConfig();

            await _cameraService.Init(_config.CameraIndex, _config.CameraResolution);

            _cognitiveService.Init(_config.CognitiveVisionAPIKey);

            //Run the program
            _isRunning = true;
            do
            {
                _isInterruptRequested = false;
                if (_cameraService.IsInitialized && _azureIoTHubService.Connected &&
                    !string.IsNullOrEmpty(_config.EventId) && !string.IsNullOrEmpty(_config.CognitiveVisionAPIKey))
                {
                    using (var captureStream = new InMemoryRandomAccessStream())
                    {
                        await _cameraService.TakePhotoToStream(captureStream);

                        if (captureStream.Position != 0)
                        {
                            string text = await _cognitiveService.ProcessPhotoFromStream(captureStream);

                            if (!string.IsNullOrEmpty(text))
                            {
                                await _azureIoTHubService.SendDeviceToCloudMessageAsync(new NoteMessage()
                                {
                                    EventId = _config.EventId,
                                    Note    = text,
                                    Type    = "Note"
                                });
                            }
                            else
                            {
                                _logging.LogMessage("No note was found.", LoggingLevel.Verbose);
                            }
                        }
                    }
                }
                else
                {
                    _isInterruptRequested = true;
                    _config.IntervalPhoto = 5;
                }
                await Task.Delay(_config.IntervalPhoto * 1000);
            }while (!_isInterruptRequested);

            //Program interrupted, dispose the services
            if (!_azureIoTHubService.Connected)
            {
                _azureIoTHubService.Dispose();
            }
            _cameraService.Dispose();

            _isRunning = false;

            //Restart
            await RunApplication();
        }
 protected override void OnDestroy()
 {
     base.OnDestroy();
     _cameraService.Dispose();
 }