DFRobotGravityDOMeter sensor;
public override Task Initialize()
{
Resolver.Log.Info("Initialize...");
sensor = new DFRobotGravityDOMeter(Device.Pins.A01);
var consumer = DFRobotGravityDOMeter.CreateObserver(
handler: result =>
{
string oldValue = (result.Old is { } old) ? $"{old.MilligramsPerLiter:n0}" : "n/a";
string newValue = $"{result.New.MilligramsPerLiter:n0}";
Resolver.Log.Info($"New: {newValue}mg/l, Old: {oldValue}mg/l");
},
filter: null
);
sensor.Subscribe(consumer);
sensor.Updated += (sender, result) =>
{
string oldValue = (result.Old is { } old) ? $"{old.MilligramsPerLiter}mg/l" : "n/a";
Resolver.Log.Info($"Updated - New: {result.New.MilligramsPerLiter:n0}mg/l, Old: {oldValue}");
};
return Task.CompletedTask;
}
public override async Task Run()
{
Resolver.Log.Info("Run...");
await ReadSensor();
sensor.StartUpdating(TimeSpan.FromSeconds(2));
}
protected async Task ReadSensor()
{
var concentration = await sensor.Read();
Resolver.Log.Info($"Initial concentration: {concentration.MilligramsPerLiter:N0}mg/l");
}