PepperDash / Essentials

The Essentials Application Framework Libraries
MIT License
132 stars 75 forks source link

[BUG]-Verify all devices implementing IUsageTracking initializes usage tracker #1174

Open jkdevito opened 7 months ago

jkdevito commented 7 months ago

Was this bug identified in a specific build version? Essentials 1.16.0-rc-2374

Describe the bug Attempting to invoke UsageTracker.StartDeviceUsage() on a source with "type": "laptop" will cause a null exception.

Stacktrace

Include a stack trace of the exception if possible.

Error: SimplSharpPro.exe [App 1] # 2024-02-06 12:50:22  # Exception:BeginInvoke- Unhandled exception: - System.NullReferenceException: NullReferenceException
   at PepperDash.Essentials.Plugin.Room.RoomController.<>c__DisplayClass18.<RunRouteAction>b__17(Object o)
   at Crestron.SimplSharp.UserThreadStartDeleg 12. Error: SimplSharpPro.exe [App 1] # 2024-02-06 12:50:22  # ateWrapper.a(Object A_0)
   at System.Threading.Timer.ring()

To Reproduce Steps to reproduce the behavior:

  1. Load system using Essentials Room (or api-room) with a source using the configuration:
    {
    "key": "laptop-1",
    "name": "HDMI 1",
    "type": "laptop",
    "group": "pc",
    "uid": 7,
    "id": "24db1d75-9444-45e0-b105-0fdf422dcbef",
    "properties": {
        "isDefault": false
    }
    }
  2. Routing the source using the RunRouteAction method will attempt to start the device usage tracker, causing the null reference exception.

Additional context Added a null reference check on the UsageTracker to RunRouteAction to prevent the exception:

// Start usage timer on routed source
if (sourceListItem.SourceDevice is IUsageTracking && (sourceListItem.SourceDevice as IUsageTracking).UsageTracker != null)
{
    (sourceListItem.SourceDevice as IUsageTracking).UsageTracker.StartDeviceUsage();
}