Beispiel #1
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            m_Deferral             = taskInstance.GetDeferral();
            taskInstance.Canceled += TaskInstanceCanceled;

            // initialize I2C
            var i2cSettings = new I2cConnectionSettings(TSL2561.Address)
            {
                BusSpeed    = I2cBusSpeed.FastMode,
                SharingMode = I2cSharingMode.Shared,
            };
            var selector          = I2cDevice.GetDeviceSelector(m_DeviceSelector);
            var deviceInformation = await DeviceInformation.FindAllAsync(selector);

            m_Device = await I2cDevice.FromIdAsync(deviceInformation[0].Id, i2cSettings);

            // initialize sensor
            m_Sensor = new TSL2561(m_Device);
            m_Sensor.SetTiming(m_Gain, m_Timing);
            m_Sensor.PowerUp();

            // initialize MQTT
            try
            {
                m_Client = new MqttClient(m_Host);
                m_Client.Connect(m_ClientId);
            }
            catch (MqttConnectionException ex)
            {
                // ignore connection exception and retry to connect later
                Debug.WriteLine("Cannot connect to MQTT broker: " + ex.Message);
                m_Client = null;
            }

            var appService = taskInstance.TriggerDetails as AppServiceTriggerDetails;

            if (appService != null && appService.Name == m_ServiceName)
            {
                m_AppServiceConnection = appService.AppServiceConnection;
                m_AppServiceConnection.RequestReceived += OnRequestReceived;
            }

            m_Client.Publish(m_ServiceTopic, Encoding.UTF8.GetBytes(DateTime.Now.ToUniversalTime().ToString("O")), m_QoS, true);

            // start timer
            m_Timer = new Timer(LuxProvider, null, m_TimerDueTime, m_TimerInterval);
        }
Beispiel #2
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            m_Deferral = taskInstance.GetDeferral();
            taskInstance.Canceled += TaskInstanceCanceled;

            // initialize I2C
            var i2cSettings = new I2cConnectionSettings(TSL2561.Address)
            {
                BusSpeed = I2cBusSpeed.FastMode,
                SharingMode = I2cSharingMode.Shared,
            };
            var selector = I2cDevice.GetDeviceSelector(m_DeviceSelector);
            var deviceInformation = await DeviceInformation.FindAllAsync(selector);
            m_Device = await I2cDevice.FromIdAsync(deviceInformation[0].Id, i2cSettings);

            // initialize sensor
            m_Sensor = new TSL2561(m_Device);
            m_Sensor.SetTiming(m_Gain, m_Timing);
            m_Sensor.PowerUp();

            // initialize MQTT
            try
            {
                m_Client = new MqttClient(m_Host);
                m_Client.Connect(m_ClientId);
            }
            catch (MqttConnectionException ex)
            {
                // ignore connection exception and retry to connect later
                Debug.WriteLine("Cannot connect to MQTT broker: " + ex.Message);
                m_Client = null;
            }

            var appService = taskInstance.TriggerDetails as AppServiceTriggerDetails;
            if (appService != null && appService.Name == m_ServiceName)
            {
                m_AppServiceConnection = appService.AppServiceConnection;
                m_AppServiceConnection.RequestReceived += OnRequestReceived;
            }

            m_Client.Publish(m_ServiceTopic, Encoding.UTF8.GetBytes(DateTime.Now.ToUniversalTime().ToString("O")), m_QoS, true);

            // start timer
            m_Timer = new Timer(LuxProvider, null, m_TimerDueTime, m_TimerInterval);
        }