99using Api . Services . Models ;
1010using Api . Utilities ;
1111using Microsoft . EntityFrameworkCore ;
12+ using Microsoft . Extensions . Caching . Memory ;
1213
1314namespace Api . EventHandlers
1415{
@@ -23,11 +24,18 @@ public class MqttEventHandler : EventHandlerBase
2324
2425 private readonly Semaphore _updateRobotSemaphore = new ( 1 , 1 ) ;
2526
26- public MqttEventHandler ( ILogger < MqttEventHandler > logger , IServiceScopeFactory scopeFactory )
27+ private readonly IMemoryCache _cache ;
28+
29+ public MqttEventHandler (
30+ ILogger < MqttEventHandler > logger ,
31+ IServiceScopeFactory scopeFactory ,
32+ IMemoryCache cache
33+ )
2734 {
2835 _logger = logger ;
2936 // Reason for using factory: https://www.thecodebuzz.com/using-dbcontext-instance-in-ihostedservice/
3037 _scopeFactory = scopeFactory ;
38+ _cache = cache ;
3139
3240 Subscribe ( ) ;
3341 }
@@ -606,40 +614,77 @@ private async void OnIsarTaskUpdate(object? sender, MqttReceivedArgs mqttArgs)
606614 ) ;
607615 }
608616
617+ private async Task < string ? > GetRobotInstallationCode ( string robotIsarId )
618+ {
619+ if ( ! _cache . TryGetValue ( robotIsarId , out string ? installationCode ) )
620+ {
621+ var robot = await RobotService . ReadByIsarId ( robotIsarId ) ;
622+
623+ if ( robot == null )
624+ return null ;
625+ var cacheEntryOptions = new MemoryCacheEntryOptions
626+ {
627+ AbsoluteExpirationRelativeToNow = TimeSpan . FromDays ( 1 ) ,
628+ } ;
629+ _cache . Set (
630+ robotIsarId ,
631+ robot . CurrentInstallation . InstallationCode ,
632+ cacheEntryOptions
633+ ) ;
634+ installationCode = robot . CurrentInstallation . InstallationCode ;
635+ }
636+ return installationCode ;
637+ }
638+
609639 private async void OnIsarBatteryUpdate ( object ? sender , MqttReceivedArgs mqttArgs )
610640 {
611641 var batteryStatus = ( IsarBatteryMessage ) mqttArgs . Message ;
612642
613- var robot = await RobotService . ReadByIsarId ( batteryStatus . IsarId ) ;
643+ var installationCode = await GetRobotInstallationCode ( batteryStatus . IsarId ) ;
614644
615- if ( robot == null )
645+ if ( installationCode == null )
616646 return ;
617647
618- await RobotService . SendToSignalROnPropertyUpdate (
619- robot . Id ,
620- "batteryState" ,
621- batteryStatus . BatteryState
648+ await SignalRService . SendMessageAsync (
649+ "Robot telemetry updated" ,
650+ installationCode ,
651+ new UpdateRobotTelemetryMessage
652+ {
653+ IsarId = batteryStatus . IsarId ,
654+ TelemetryName = "batteryState" ,
655+ TelemetryValue = batteryStatus . BatteryState ,
656+ }
622657 ) ;
623- await RobotService . SendToSignalROnPropertyUpdate (
624- robot . Id ,
625- "batteryLevel" ,
626- batteryStatus . BatteryLevel
658+ await SignalRService . SendMessageAsync (
659+ "Robot telemetry updated" ,
660+ installationCode ,
661+ new UpdateRobotTelemetryMessage
662+ {
663+ IsarId = batteryStatus . IsarId ,
664+ TelemetryName = "batteryLevel" ,
665+ TelemetryValue = batteryStatus . BatteryLevel ,
666+ }
627667 ) ;
628668 }
629669
630670 private async void OnIsarPressureUpdate ( object ? sender , MqttReceivedArgs mqttArgs )
631671 {
632672 var pressureStatus = ( IsarPressureMessage ) mqttArgs . Message ;
633673
634- var robot = await RobotService . ReadByIsarId ( pressureStatus . IsarId ) ;
674+ var installationCode = await GetRobotInstallationCode ( pressureStatus . IsarId ) ;
635675
636- if ( robot == null )
676+ if ( installationCode == null )
637677 return ;
638678
639- await RobotService . SendToSignalROnPropertyUpdate (
640- robot . Id ,
641- "pressureLevel" ,
642- pressureStatus . PressureLevel
679+ await SignalRService . SendMessageAsync (
680+ "Robot telemetry updated" ,
681+ installationCode ,
682+ new UpdateRobotTelemetryMessage
683+ {
684+ IsarId = pressureStatus . IsarId ,
685+ TelemetryName = "pressureLevel" ,
686+ TelemetryValue = pressureStatus . PressureLevel ,
687+ }
643688 ) ;
644689 }
645690
0 commit comments