API updates for functional code
This commit is contained in:
53
house-plant-api/Services/BluetoothService.cs
Normal file
53
house-plant-api/Services/BluetoothService.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
namespace house_plant_api.Services
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using house_plant_api.Models;
|
||||
using InTheHand.Bluetooth;
|
||||
|
||||
public class BluetoothService
|
||||
{
|
||||
private readonly Guid _serviceUuid = Guid.Parse("12345678-1234-1234-1234-123456789abc");
|
||||
private readonly Guid _characteristicUuid = Guid.Parse("abcd1234-5678-1234-5678-abcdef123456");
|
||||
|
||||
public async Task<IReadOnlyCollection<BluetoothDevice>> ScanDevicesAsync()
|
||||
{
|
||||
// Scan for BLE devices
|
||||
return await Bluetooth.ScanForDevicesAsync();
|
||||
}
|
||||
|
||||
public async Task<int> GetSoilMoistureAsync(string deviceName)
|
||||
{
|
||||
// 1. Scan for devices and match by name
|
||||
var devices = await Bluetooth.ScanForDevicesAsync();
|
||||
var targetDevice = devices.FirstOrDefault(d => d.Name == deviceName);
|
||||
|
||||
if (targetDevice == null)
|
||||
throw new Exception($"Device '{deviceName}' not found.");
|
||||
|
||||
// 2. Connect and read moisture
|
||||
await targetDevice.Gatt.ConnectAsync();
|
||||
try
|
||||
{
|
||||
var services = await targetDevice.Gatt.GetPrimaryServicesAsync();
|
||||
var targetService = services.FirstOrDefault(s => s.Uuid == _serviceUuid);
|
||||
if (targetService == null)
|
||||
throw new Exception("Moisture service not found.");
|
||||
|
||||
var characteristics = await targetService.GetCharacteristicsAsync();
|
||||
var targetCharacteristic = characteristics.FirstOrDefault(c => c.Uuid == _characteristicUuid);
|
||||
if (targetCharacteristic == null)
|
||||
throw new Exception("Moisture characteristic not found.");
|
||||
|
||||
var value = await targetCharacteristic.ReadValueAsync();
|
||||
return BitConverter.ToInt32(value, 0);
|
||||
}
|
||||
finally
|
||||
{
|
||||
targetDevice.Gatt.Disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
101
house-plant-api/Services/CachedBluetoothService.cs
Normal file
101
house-plant-api/Services/CachedBluetoothService.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
namespace house_plant_api.Services
|
||||
{
|
||||
using house_plant_api.Context;
|
||||
using house_plant_api.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class CachedBluetoothService
|
||||
{
|
||||
private readonly BluetoothService _bluetoothService;
|
||||
private readonly IDbContextFactory<AppDbContext> _dbContextFactory;
|
||||
|
||||
public CachedBluetoothService(
|
||||
BluetoothService bluetoothService,
|
||||
IDbContextFactory<AppDbContext> dbContextFactory)
|
||||
{
|
||||
_bluetoothService = bluetoothService;
|
||||
_dbContextFactory = dbContextFactory;
|
||||
}
|
||||
|
||||
public List<Device> GetTrackedDevices()
|
||||
{
|
||||
// If you're using an IDbContextFactory:
|
||||
using var dbContext = _dbContextFactory.CreateDbContext();
|
||||
// Or if you're injecting a scoped dbContext (see note below):
|
||||
// var dbContext = _dbContext;
|
||||
|
||||
return dbContext.Devices.ToList();
|
||||
}
|
||||
|
||||
public async Task<List<Device>> GetTrackedDevicesAsync()
|
||||
{
|
||||
using var dbContext = _dbContextFactory.CreateDbContext();
|
||||
return await dbContext.Devices.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task PollDevicesAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var devices = await _bluetoothService.ScanDevicesAsync();
|
||||
|
||||
// Create a fresh AppDbContext instance
|
||||
using var dbContext = _dbContextFactory.CreateDbContext();
|
||||
|
||||
foreach (var device in devices)
|
||||
{
|
||||
var existingDevice = dbContext.Devices
|
||||
.FirstOrDefault(d => d.Uuid == device.Id.ToString());
|
||||
|
||||
if (existingDevice == null)
|
||||
{
|
||||
dbContext.Devices.Add(new Device
|
||||
{
|
||||
Name = device.Name,
|
||||
Nickname = "Pakkun Flower",
|
||||
Uuid = device.Id.ToString(),
|
||||
LastSeen = DateTime.Now
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
existingDevice.LastSeen = DateTime.Now;
|
||||
|
||||
try
|
||||
{
|
||||
var moisture = await _bluetoothService.GetSoilMoistureAsync(device.Name);
|
||||
existingDevice.Moisture = moisture;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Swallow exceptions from reading moisture
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error during polling: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteDeviceAsync(string name)
|
||||
{
|
||||
using var dbContext = _dbContextFactory.CreateDbContext();
|
||||
var device = dbContext.Devices.FirstOrDefault(d => d.Name == name);
|
||||
|
||||
if (device == null)
|
||||
return false;
|
||||
|
||||
dbContext.Devices.Remove(device);
|
||||
await dbContext.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
103
house-plant-api/Services/DevicePollingService.cs
Normal file
103
house-plant-api/Services/DevicePollingService.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
namespace house_plant_api.Services
|
||||
{
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using InTheHand.Bluetooth;
|
||||
using house_plant_api.Context;
|
||||
using house_plant_api.Models;
|
||||
|
||||
public class DevicePollingService : BackgroundService
|
||||
{
|
||||
private readonly ILogger<DevicePollingService> _logger;
|
||||
private readonly BluetoothService _bluetoothService;
|
||||
private readonly IDbContextFactory<AppDbContext> _dbContextFactory;
|
||||
private readonly TimeSpan _pollingInterval = TimeSpan.FromSeconds(30);
|
||||
|
||||
public DevicePollingService(
|
||||
ILogger<DevicePollingService> logger,
|
||||
BluetoothService bluetoothService,
|
||||
IDbContextFactory<AppDbContext> dbContextFactory)
|
||||
{
|
||||
_logger = logger;
|
||||
_bluetoothService = bluetoothService;
|
||||
_dbContextFactory = dbContextFactory;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
await PollDevicesAsync();
|
||||
await Task.Delay(_pollingInterval, stoppingToken);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task PollDevicesAsync()
|
||||
{
|
||||
_logger.LogInformation("Polling devices for moisture data...");
|
||||
|
||||
try
|
||||
{
|
||||
// 1. Scan for BLE devices
|
||||
var devices = await _bluetoothService.ScanDevicesAsync();
|
||||
|
||||
// 2. Open a new DB context for each polling run
|
||||
using var dbContext = _dbContextFactory.CreateDbContext();
|
||||
|
||||
// 3. Filter devices that have "SoilSensor" in the name (if desired)
|
||||
var soilDevices = devices
|
||||
.Where(d => !string.IsNullOrEmpty(d.Name) && d.Name.Contains("SoilSensor", StringComparison.OrdinalIgnoreCase))
|
||||
.ToList();
|
||||
|
||||
// 4. For each device, attempt to get moisture data and store/update in DB
|
||||
foreach (var device in soilDevices)
|
||||
{
|
||||
var existingDevice = dbContext.Devices
|
||||
.FirstOrDefault(d => d.Uuid == device.Id.ToString());
|
||||
|
||||
if (existingDevice == null)
|
||||
{
|
||||
// Add new device to DB
|
||||
existingDevice = new Device
|
||||
{
|
||||
Name = device.Name,
|
||||
Nickname = "Pakkun Flower",
|
||||
Uuid = device.Id.ToString(),
|
||||
LastSeen = DateTime.Now
|
||||
};
|
||||
dbContext.Devices.Add(existingDevice);
|
||||
}
|
||||
else
|
||||
{
|
||||
existingDevice.LastSeen = DateTime.Now;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Attempt to read moisture from the device
|
||||
var moisture = await _bluetoothService.GetSoilMoistureAsync(device.Name);
|
||||
existingDevice.Moisture = moisture;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// If reading fails, log and continue
|
||||
_logger.LogWarning(ex, $"Failed to read moisture from {device.Name}");
|
||||
}
|
||||
}
|
||||
|
||||
// 5. Save changes
|
||||
await dbContext.SaveChangesAsync();
|
||||
_logger.LogInformation("Polling cycle complete.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error during device polling.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user