1414using OpenShock . Common . OpenShockDb ;
1515using OpenShock . Common . Problems ;
1616using OpenShock . Common . Redis ;
17+ using OpenShock . Common . Services . RedisPubSub ;
1718using OpenShock . Common . Utils ;
1819using OpenShock . LiveControlGateway . LifetimeManager ;
1920using 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 />
0 commit comments