Skip to content

Commit cbfce0f

Browse files
authored
Merge pull request #331 from OpenShock/feature/device-paired-signal
feat: emit DeviceUpdateType.Paired when a device consumes a pair code
2 parents 30e227a + b221847 commit cbfce0f

3 files changed

Lines changed: 19 additions & 7 deletions

File tree

‎API/Controller/Device/Pair.cs‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,18 @@ private async Task<IActionResult> PairInternal(string pairCode)
5454
if (pair is null) return Problem(PairError.PairCodeNotFound);
5555
await devicePairs.DeleteAsync(pair);
5656

57-
var deviceToken = await _db.Devices.Where(x => x.Id == pair.Id).Select(x => x.Token).FirstOrDefaultAsync();
58-
if (deviceToken is null) throw new Exception("Device not found for pair code");
57+
var device = await _db.Devices.Where(x => x.Id == pair.Id).Select(x => new { x.Token, x.OwnerId }).FirstOrDefaultAsync();
58+
if (device is null) throw new Exception("Device not found for pair code");
5959

60-
return LegacyDataOk(deviceToken);
60+
try
61+
{
62+
await _deviceUpdateService.UpdateDevice(device.OwnerId, pair.Id, DeviceUpdateType.Paired);
63+
}
64+
catch (Exception ex)
65+
{
66+
_logger.LogWarning(ex, "Failed to emit paired update for device {DeviceId}", pair.Id);
67+
}
68+
69+
return LegacyDataOk(device.Token);
6170
}
62-
}
71+
}

‎API/Controller/Device/_ApiController.cs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Asp.Versioning;
22
using Microsoft.AspNetCore.Authorization;
33
using Microsoft.AspNetCore.Mvc;
4+
using OpenShock.API.Services.DeviceUpdate;
45
using OpenShock.Common;
56
using OpenShock.Common.Authentication;
67
using OpenShock.Common.OpenShockDb;
@@ -21,12 +22,14 @@ public sealed partial class DeviceController : OpenShockControllerBase
2122
{
2223
private readonly OpenShockContext _db;
2324
private readonly IRedisConnectionProvider _redis;
25+
private readonly IDeviceUpdateService _deviceUpdateService;
2426
private readonly ILogger<DeviceController> _logger;
2527

26-
public DeviceController(OpenShockContext db, IRedisConnectionProvider redis, ILogger<DeviceController> logger)
28+
public DeviceController(OpenShockContext db, IRedisConnectionProvider redis, IDeviceUpdateService deviceUpdateService, ILogger<DeviceController> logger)
2729
{
2830
_db = db;
2931
_redis = redis;
32+
_deviceUpdateService = deviceUpdateService;
3033
_logger = logger;
3134
}
3235
}

‎Common/Models/DeviceUpdateType.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ public enum DeviceUpdateType
55
Created, // Whenever a new device is created
66
Updated, // Whenever name or something else directly related to the device is updated
77
ShockerUpdated, // Whenever a shocker is updated, name or limits for a person
8-
Deleted // Whenever a device is deleted
9-
8+
Deleted, // Whenever a device is deleted
9+
Paired // Whenever a device consumes a pair code
1010
}

0 commit comments

Comments
 (0)