9
9
using System . Device . I2c ;
10
10
using Iot . Device . Pcx857x ;
11
11
using Iot . Device . CharacterLcd ;
12
+ using Microsoft . Extensions . Logging ;
12
13
13
14
namespace Almostengr . ThermometerPi . Api . Workers
14
15
{
15
16
public class LcdDisplayWorker : BackgroundService
16
17
{
17
18
private readonly ITemperatureReadingService _temperatureReadingService ;
19
+ private readonly ILogger < LcdDisplayWorker > _logger ;
18
20
private const int DelaySeconds = 5 ;
19
21
private Lcd1602 lcd ;
20
22
21
- public LcdDisplayWorker ( IServiceScopeFactory factory )
23
+ public LcdDisplayWorker ( IServiceScopeFactory factory ,
24
+ ILogger < LcdDisplayWorker > logger )
22
25
{
23
26
_temperatureReadingService = factory . CreateScope ( ) . ServiceProvider . GetRequiredService < ITemperatureReadingService > ( ) ;
27
+ _logger = logger ;
24
28
}
25
29
26
30
protected override async Task ExecuteAsync ( CancellationToken stoppingToken )
27
31
{
28
- using I2cDevice i2c = I2cDevice . Create ( new I2cConnectionSettings ( 1 , 0x27 ) ) ;
29
- using Pcf8574 driver = new Pcf8574 ( i2c ) ;
32
+ try
33
+ {
34
+ using I2cDevice i2c = I2cDevice . Create ( new I2cConnectionSettings ( 1 , 0x27 ) ) ;
35
+ using Pcf8574 driver = new Pcf8574 ( i2c ) ;
30
36
31
- lcd = new Lcd1602 ( registerSelectPin : 0 ,
32
- enablePin : 2 ,
33
- dataPins : new int [ ] { 4 , 5 , 6 , 7 } ,
34
- backlightPin : 3 ,
35
- backlightBrightness : 1f ,
36
- readWritePin : 1 ,
37
- controller : new GpioController ( PinNumberingScheme . Logical , driver ) ) ;
37
+ lcd = new Lcd1602 ( registerSelectPin : 0 ,
38
+ enablePin : 2 ,
39
+ dataPins : new int [ ] { 4 , 5 , 6 , 7 } ,
40
+ backlightPin : 3 ,
41
+ backlightBrightness : 1f ,
42
+ readWritePin : 1 ,
43
+ controller : new GpioController ( PinNumberingScheme . Logical , driver ) ) ;
38
44
39
- while ( ! stoppingToken . IsCancellationRequested )
40
- {
41
- TemperatureDto interiorTemp = await _temperatureReadingService . GetLatestInteriorReadingAsync ( ) ;
45
+ while ( ! stoppingToken . IsCancellationRequested )
46
+ {
47
+ TemperatureDto interiorTemp = await _temperatureReadingService . GetLatestInteriorReadingAsync ( ) ;
42
48
43
- lcd . Clear ( ) ;
44
- string output = string . Empty ;
49
+ lcd . Clear ( ) ;
50
+ string output = string . Empty ;
45
51
46
- DisplayLcdText (
47
- interiorTemp != null ?
48
- $ "In: { interiorTemp . Fahrenheit . ToString ( ) } F { interiorTemp . Celsius . ToString ( ) } C" :
49
- "No Data" ,
50
- DateTime . Now . ToString ( "ddd MM/dd HH:mm" )
51
- ) ;
52
+ DisplayLcdText (
53
+ interiorTemp != null ?
54
+ $ "In: { interiorTemp . Fahrenheit . ToString ( ) } F { interiorTemp . Celsius . ToString ( ) } C" :
55
+ "No Data" ,
56
+ DateTime . Now . ToString ( "ddd MM/dd HH:mm" )
57
+ ) ;
52
58
53
- await Task . Delay ( TimeSpan . FromSeconds ( DelaySeconds ) , stoppingToken ) ;
59
+ await Task . Delay ( TimeSpan . FromSeconds ( DelaySeconds ) , stoppingToken ) ;
54
60
55
- TemperatureDto minInteriorTemp = await _temperatureReadingService . GetMinInteriorReadingAsync ( ) ;
56
- TemperatureDto maxInteriorTemp = await _temperatureReadingService . GetMaxInteriorReadingAsync ( ) ;
61
+ TemperatureDto minInteriorTemp = await _temperatureReadingService . GetMinInteriorReadingAsync ( ) ;
62
+ TemperatureDto maxInteriorTemp = await _temperatureReadingService . GetMaxInteriorReadingAsync ( ) ;
57
63
58
- output = string . Empty ;
64
+ output = string . Empty ;
59
65
60
- DisplayLcdText (
61
- minInteriorTemp != null ?
62
- $ "Min: { minInteriorTemp . Fahrenheit . ToString ( ) } F { minInteriorTemp . Celsius . ToString ( ) } C" :
63
- string . Empty ,
64
- maxInteriorTemp != null ?
65
- $ "Max: { maxInteriorTemp . Fahrenheit . ToString ( ) } F { maxInteriorTemp . Celsius . ToString ( ) } C" :
66
- string . Empty
67
- ) ;
66
+ DisplayLcdText (
67
+ minInteriorTemp != null ?
68
+ $ "Min: { minInteriorTemp . Fahrenheit . ToString ( ) } F { minInteriorTemp . Celsius . ToString ( ) } C" :
69
+ string . Empty ,
70
+ maxInteriorTemp != null ?
71
+ $ "Max: { maxInteriorTemp . Fahrenheit . ToString ( ) } F { maxInteriorTemp . Celsius . ToString ( ) } C" :
72
+ string . Empty
73
+ ) ;
68
74
69
- await Task . Delay ( TimeSpan . FromSeconds ( DelaySeconds ) , stoppingToken ) ;
75
+ await Task . Delay ( TimeSpan . FromSeconds ( DelaySeconds ) , stoppingToken ) ;
76
+ }
77
+ }
78
+ catch ( Exception ex )
79
+ {
80
+ _logger . LogError ( ex . Message ) ;
70
81
}
71
82
}
72
83
@@ -79,5 +90,5 @@ private void DisplayLcdText(string line1 = "No data", string line2 = "")
79
90
lcd . Write ( line2 ) ;
80
91
}
81
92
82
- } // end class
93
+ } // end class
83
94
}
0 commit comments