Пример #1
0
        protected override async Task RunContinuouslyAsync(IMqttClient client, TimeSpan delay, CancellationToken stoppingToken)
        {
            var max44009 = new Max44009("/dev/i2c-1", 0x4A);

            max44009.Reset();

            float oldLux         = 0;
            var   nextReportTime = DateTimeOffset.MinValue;

            while (!stoppingToken.IsCancellationRequested)
            {
                var lux = max44009.Read();
                var now = DateTimeOffset.UtcNow;
                if ((now > nextReportTime) || ShouldReport(oldLux, lux))
                {
                    await SendReading(client, lux, stoppingToken).ConfigureAwait(false);

                    oldLux         = lux;
                    nextReportTime = now.AddSeconds(60);
                }
                await Task.Delay(delay, stoppingToken).ConfigureAwait(false);
            }
        }
Пример #2
0
        private static async Task Main()
        {
            CancellationToken ct = default;

            Console.WriteLine("Hello Max44009!");
            try
            {
                var max44009 = new Max44009("/dev/i2c-1", 0x4A);
                max44009.Reset();

                while (!ct.IsCancellationRequested)
                {
                    var lux = max44009.Read();
                    Console.WriteLine(FormattableString.Invariant($"Lux = {lux}"));
                    await Task.Delay(1000, ct).ConfigureAwait(false);
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
            {
                Console.WriteLine(FormattableString.Invariant($"Error: {ex}"));
            }
        }