Skip to content

Commit e6d3760

Browse files
committed
Send own pubsub message for online status updates instead of using json set
1 parent fdd5df8 commit e6d3760

File tree

7 files changed

+46
-7
lines changed

7 files changed

+46
-7
lines changed

API/Realtime/RedisSubscriberService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public RedisSubscriberService(
4848
public async Task StartAsync(CancellationToken cancellationToken)
4949
{
5050
await _subscriber.SubscribeAsync(RedisChannels.KeyEventExpired, (_, message) => { LucTask.Run(() => RunLogic(message, false)); });
51-
await _subscriber.SubscribeAsync(RedisChannels.KeyEventJsonSet, (_, message) => { LucTask.Run(() => RunLogic(message, true)); });
51+
await _subscriber.SubscribeAsync(RedisChannels.DeviceOnlineStatus, (_, message) => { LucTask.Run(() => RunLogic(message, true)); });
5252
await _subscriber.SubscribeAsync(RedisChannels.KeyEventDel, (_, message) => { LucTask.Run(() => RunLogic(message, false)); });
5353
}
5454

Common/Services/RedisPubSub/IRedisPubService.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ namespace OpenShock.Common.Services.RedisPubSub;
55

66
public interface IRedisPubService
77
{
8+
/// <summary>
9+
/// Used when a device comes online or changes its connection details like, gateway, firmware version, etc.
10+
/// </summary>
11+
/// <param name="deviceId"></param>
12+
/// <returns></returns>
13+
public Task SendDeviceOnlineStatus(Guid deviceId);
14+
815
/// <summary>
916
/// General shocker control
1017
/// </summary>

Common/Services/RedisPubSub/RedisChannels.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public static class RedisChannels
1111
public static readonly RedisChannel DeviceControl = new("msg-device-control", RedisChannel.PatternMode.Literal);
1212
public static readonly RedisChannel DeviceCaptive = new("msg-device-control-captive", RedisChannel.PatternMode.Literal);
1313
public static readonly RedisChannel DeviceUpdate = new("msg-device-update", RedisChannel.PatternMode.Literal);
14+
public static readonly RedisChannel DeviceOnlineStatus = new("msg-device-online-status", RedisChannel.PatternMode.Literal);
1415

1516
// OTA
1617
public static readonly RedisChannel DeviceOtaInstall = new("msg-device-ota-install", RedisChannel.PatternMode.Literal);

Common/Services/RedisPubSub/RedisPubService.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ public RedisPubService(IConnectionMultiplexer connectionMultiplexer)
1818
_subscriber = connectionMultiplexer.GetSubscriber();
1919
}
2020

21+
public Task SendDeviceOnlineStatus(Guid deviceId)
22+
{
23+
var redisMessage = new DeviceUpdatedMessage
24+
{
25+
Id = deviceId
26+
};
27+
28+
return _subscriber.PublishAsync(RedisChannels.DeviceOnlineStatus, JsonSerializer.Serialize(redisMessage));
29+
}
30+
2131
/// <inheritdoc />
2232
public Task SendDeviceControl(Guid sender, IDictionary<Guid, IList<ControlMessage.ShockerControlInfo>> controlMessages)
2333
{

LiveControlGateway/Controllers/DeviceControllerBase.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using OpenShock.Common.OpenShockDb;
1515
using OpenShock.Common.Problems;
1616
using OpenShock.Common.Redis;
17+
using OpenShock.Common.Services.RedisPubSub;
1718
using OpenShock.Common.Utils;
1819
using OpenShock.LiveControlGateway.LifetimeManager;
1920
using OpenShock.LiveControlGateway.Websocket;
@@ -44,6 +45,7 @@ public abstract class DeviceControllerBase<TIn, TOut> : FlatbuffersWebsocketBase
4445
private readonly IRedisConnectionProvider _redisConnectionProvider;
4546
private readonly IDbContextFactory<OpenShockContext> _dbContextFactory;
4647
private readonly LCGConfig _lcgConfig;
48+
private readonly IRedisPubService _redisPubService;
4749

4850
private readonly Timer _keepAliveTimeoutTimer = new(Duration.DeviceKeepAliveInitialTimeout);
4951
private DateTimeOffset _connected = DateTimeOffset.UtcNow;
@@ -79,14 +81,15 @@ protected DeviceControllerBase(
7981
ISerializer<TOut> outgoingSerializer,
8082
IRedisConnectionProvider redisConnectionProvider,
8183
IDbContextFactory<OpenShockContext> dbContextFactory,
82-
IServiceProvider serviceProvider, LCGConfig lcgConfig
83-
84+
IServiceProvider serviceProvider, LCGConfig lcgConfig,
85+
IRedisPubService redisPubService
8486
) : base(logger, lifetime, incomingSerializer, outgoingSerializer)
8587
{
8688
_redisConnectionProvider = redisConnectionProvider;
8789
_dbContextFactory = dbContextFactory;
8890
ServiceProvider = serviceProvider;
8991
_lcgConfig = lcgConfig;
92+
_redisPubService = redisPubService;
9093
_keepAliveTimeoutTimer.Elapsed += async (sender, args) =>
9194
{
9295
Logger.LogInformation("Keep alive timeout reached, closing websocket connection");
@@ -161,11 +164,18 @@ await deviceOnline.InsertAsync(new DeviceOnline
161164
ConnectedAt = _connected,
162165
UserAgent = _userAgent
163166
}, Duration.DeviceKeepAliveTimeout);
167+
168+
169+
await _redisPubService.SendDeviceOnlineStatus(CurrentDevice.Id);
164170
return;
165171
}
166172

173+
// We cannot rely on the json set anymore, since that also happens with uptime and latency
174+
// as we dont want to send a device online status every time, we will do it here
167175
online.Uptime = uptime;
168176
online.Latency = latency;
177+
178+
var sendOnlineStatusUpdate = false;
169179

170180
if (online.FirmwareVersion != _firmwareVersion ||
171181
online.Gateway != _lcgConfig.Lcg.Fqdn ||
@@ -177,9 +187,16 @@ await deviceOnline.InsertAsync(new DeviceOnline
177187
online.ConnectedAt = _connected;
178188
online.UserAgent = _userAgent;
179189
Logger.LogInformation("Updated details of online device");
190+
191+
sendOnlineStatusUpdate = true;
180192
}
181193

182194
await deviceOnline.UpdateAsync(online, Duration.DeviceKeepAliveTimeout);
195+
196+
if (sendOnlineStatusUpdate)
197+
{
198+
await _redisPubService.SendDeviceOnlineStatus(CurrentDevice.Id);
199+
}
183200
}
184201

185202
/// <inheritdoc />

LiveControlGateway/Controllers/DeviceV1Controller.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using OpenShock.Common.Models;
99
using OpenShock.Common.OpenShockDb;
1010
using OpenShock.Common.Services.Ota;
11+
using OpenShock.Common.Services.RedisPubSub;
1112
using OpenShock.Common.Utils;
1213
using OpenShock.Serialization.Deprecated.DoNotUse.V1;
1314
using OpenShock.Serialization.Types;
@@ -38,15 +39,16 @@ public sealed class DeviceV1Controller : DeviceControllerBase<HubToGatewayMessag
3839
/// <param name="userHubContext"></param>
3940
/// <param name="serviceProvider"></param>
4041
/// <param name="lcgConfig"></param>
42+
/// <param name="redisPubService"></param>
4143
public DeviceV1Controller(
4244
ILogger<DeviceV1Controller> logger,
4345
IHostApplicationLifetime lifetime,
4446
IRedisConnectionProvider redisConnectionProvider,
4547
IDbContextFactory<OpenShockContext> dbContextFactory,
4648
IHubContext<UserHub, IUserHub> userHubContext,
47-
IServiceProvider serviceProvider, LCGConfig lcgConfig)
49+
IServiceProvider serviceProvider, LCGConfig lcgConfig, IRedisPubService redisPubService)
4850
: base(logger, lifetime, HubToGatewayMessage.Serializer, GatewayToHubMessage.Serializer, redisConnectionProvider,
49-
dbContextFactory, serviceProvider, lcgConfig)
51+
dbContextFactory, serviceProvider, lcgConfig, redisPubService)
5052
{
5153
_userHubContext = userHubContext;
5254
}

LiveControlGateway/Controllers/DeviceV2Controller.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using OpenShock.Common.Models;
1111
using OpenShock.Common.OpenShockDb;
1212
using OpenShock.Common.Services.Ota;
13+
using OpenShock.Common.Services.RedisPubSub;
1314
using OpenShock.Common.Utils;
1415
using OpenShock.Serialization.Gateway;
1516
using OpenShock.Serialization.Types;
@@ -42,15 +43,16 @@ public sealed class DeviceV2Controller : DeviceControllerBase<HubToGatewayMessag
4243
/// <param name="userHubContext"></param>
4344
/// <param name="serviceProvider"></param>
4445
/// <param name="lcgConfig"></param>
46+
/// <param name="redisPubService"></param>
4547
public DeviceV2Controller(
4648
ILogger<DeviceV2Controller> logger,
4749
IHostApplicationLifetime lifetime,
4850
IRedisConnectionProvider redisConnectionProvider,
4951
IDbContextFactory<OpenShockContext> dbContextFactory,
5052
IHubContext<UserHub, IUserHub> userHubContext,
51-
IServiceProvider serviceProvider, LCGConfig lcgConfig)
53+
IServiceProvider serviceProvider, LCGConfig lcgConfig, IRedisPubService redisPubService)
5254
: base(logger, lifetime, HubToGatewayMessage.Serializer, GatewayToHubMessage.Serializer, redisConnectionProvider,
53-
dbContextFactory, serviceProvider, lcgConfig)
55+
dbContextFactory, serviceProvider, lcgConfig, redisPubService)
5456
{
5557
_userHubContext = userHubContext;
5658
_pingTimer = new Timer(PingTimerElapsed, null, Duration.DevicePingInitialDelay, Duration.DevicePingPeriod);

0 commit comments

Comments
 (0)