示例#1
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            // Get a BackgroundTaskDeferral and hold it forever if initialization is sucessful.
            _deferral = taskInstance.GetDeferral();
            try
            {
                await _station.InitAsync();
            }
            catch (Exception e)
            {
                Debug.WriteLine("I2C initialization failed: " + e.Message);
                _deferral.Complete();
                return;
            }
            AppServiceBridge.RequestReceived += AppServiceRequestHandler;
            await AppServiceBridge.InitAsync();

            await _client.InitAsync();

            taskInstance.Canceled += (IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason) =>
            {
                Debug.WriteLine("Cancelled: reason " + reason);
                _deferral.Complete();
            };

            MemoryManager.AppMemoryUsageIncreased += MemoryManager_AppMemoryUsageIncreased;

            _timer = ThreadPoolTimer.CreatePeriodicTimer(LogSensorDataAsync, TimeSpan.FromSeconds(5));
            LogSensorDataAsync(null);
        }
示例#2
0
        private async Task SendPropertyChange(IEnumerable changedProperties, object userContext)
        {
            ValueSet properties = new ValueSet();

            foreach (var prop in changedProperties)
            {
                var pair  = (KeyValuePair <string, object>)prop;
                var value = pair.Value as JValue;
                if (value == null)
                {
                    Debug.WriteLine("Twin key " + pair.Key + " has unsupported type");
                    continue;
                }
                properties.Add(pair.Key, pair.Value.ToString());
            }
            await AppServiceBridge.SendMessageAsync(properties);
        }
示例#3
0
        private async void LogSensorDataAsync(ThreadPoolTimer timer)
        {
            var temperature = _station.ReadTemperature();
            var humidity    = _station.ReadHumidity();
            var pressure    = _station.ReadPressure();

            await _client.LogDataAsync(temperature, humidity, pressure);

            await AppServiceBridge.SendMessageAsync(new ValueSet
            {
                ["temperature"] = temperature,
                ["humidity"]    = humidity,
                ["pressure"]    = pressure
            });

            Debug.WriteLine("Logged data");
        }