kmahon37 / plex-dvr-waker

Plex DVR Waker is a simple command-line tool for waking the computer before the next scheduled recording.
MIT License
8 stars 0 forks source link

[BUG] Not waking correctly when a schedule conflict #29

Open solstyce9 opened 4 years ago

solstyce9 commented 4 years ago

Describe the bug PlexDVRWaker isn't waking the system for the correct show when there is a scheduling conflict and the conflict starts before the prioritized show. I've seen this twice now for my 12:30pm recording of The Cat In The Hat (don't judge, it's for my son :-) Looking at the output of PlexDVRWaker.exe list, I don't see the 12:30pm recording in the list, so for some reason it's not being seen by PDW as scheduled.

To Reproduce Steps to reproduce the behavior:

Expected behavior PDW should wake the system at 12:30pm because the 12:30 show is the one Plex is going to record.

Actual behavior PDW wakes the system at 12n, for one of the lower priority shows. Then since I use Shutter to put the system to sleep after 15 mins of low CPU and low network, it falls asleep before 12:30 because there is no recording happening, and I miss the recording.

Desktop (please complete the following information):

Additional context Screenshot of my typical weekday 12n/12:30 conflicts - Nova, Waltons, and Cat In The Hat. 2nd column is Jul 7 and does not show conflicts because the recording time has passed so only shows the error for the missing Cat In The Hat because the system was asleep. Other 2 columns are tomorrow and the next day, so the conflicts are still shown in case I want to manually change them. Priority for these three shows is Cat In The Hat, then Nova, then Waltons. 2020-07-07_20-48-34

PDW log file excerpt showing sync running at noon with next show time being noon not 12:30p

2020-07-07T11:45:00      13376  PlexDvrWaker.exe add-task --offset=60 --task="Plex DVR Waker\DVR sync" --wakeup
2020-07-07T11:45:00      13376  Plex DVR Waker version: 2.1.1
2020-07-07T11:45:00      13376  .Net Core version: 3.1.4
2020-07-07T11:45:00      13376  Using Plex library database file:  C:\Users\media\AppData\Local\Plex Media Server\Plug-in Support\Databases\com.plexapp.plugins.library.db
2020-07-07T11:45:00      13376  Getting scheduled recordings
2020-07-07T11:45:00      13376    Loading subscriptions
2020-07-07T11:45:00      13376      Subscriptions loaded: 224
2020-07-07T11:45:00      13376    Loading EPG info for subscriptions
2020-07-07T11:45:00      13376      EPG info found: 200
2020-07-07T11:45:00      13376    Removing unschedulable and past items
2020-07-07T11:45:00      13376      Removed items: 28
2020-07-07T11:45:00      13376    Removing previously recorded TV shows
2020-07-07T11:45:00      13376      Removed TV shows: 24
2020-07-07T11:45:00      13376    Removing previously recorded movies
2020-07-07T11:45:00      13376      Removed movies: 0
2020-07-07T11:45:00      13376  Found 172 upcoming scheduled recordings starting at 7/7/2020 12:00:00 PM
2020-07-07T11:45:00      13376  Getting next Plex maintenance time
2020-07-07T11:45:00      13376  Plex maintenance is 1am to 6am every day
2020-07-07T11:45:00      13376  Next scheduled maintenance time is 7/8/2020 1:00:00 AM to 7/8/2020 6:00:00 AM
2020-07-07T11:45:00      13376  Creating/updating wakeup task: Plex DVR Waker\DVR wake
2020-07-07T11:45:00      13376    Creating/updating the task
2020-07-07T11:45:00      13376  Wakeup task scheduled for 7/7/2020 11:59:00 AM
2020-07-07T11:45:00      13376  Exit code: 0 (Success)
2020-07-07T11:59:00       9696  --------------------------------------------------------------
2020-07-07T11:59:00       9696  PlexDvrWaker.exe add-task --offset=60 --task="Plex DVR Waker\DVR wake" --wakeup --wakeup-delay=75
2020-07-07T11:59:00       9696  Plex DVR Waker version: 2.1.1
2020-07-07T11:59:00       9696  .Net Core version: 3.1.4
2020-07-07T11:59:00       9696  Using Plex library database file:  C:\Users\media\AppData\Local\Plex Media Server\Plug-in Support\Databases\com.plexapp.plugins.library.db
2020-07-07T11:59:00       9696  Waiting 75 seconds before refreshing the next scheduled recording time.
2020-07-07T12:00:00       7092  --------------------------------------------------------------
2020-07-07T12:00:00       7092  PlexDvrWaker.exe add-task --offset=60 --task="Plex DVR Waker\DVR sync" --wakeup
2020-07-07T12:00:00       7092  Plex DVR Waker version: 2.1.1
2020-07-07T12:00:00       7092  .Net Core version: 3.1.4
2020-07-07T12:00:00       7092  Using Plex library database file:  C:\Users\media\AppData\Local\Plex Media Server\Plug-in Support\Databases\com.plexapp.plugins.library.db
2020-07-07T12:00:00       7092  Getting scheduled recordings
2020-07-07T12:00:00       7092    Loading subscriptions
2020-07-07T12:00:00       7092      Subscriptions loaded: 224
2020-07-07T12:00:00       7092    Loading EPG info for subscriptions
2020-07-07T12:00:00       7092      EPG info found: 200
2020-07-07T12:00:00       7092    Removing unschedulable and past items
2020-07-07T12:00:00       7092      Removed items: 30
2020-07-07T12:00:00       7092    Removing previously recorded TV shows
2020-07-07T12:00:00       7092      Removed TV shows: 24
2020-07-07T12:00:00       7092    Removing previously recorded movies
2020-07-07T12:00:00       7092      Removed movies: 0
2020-07-07T12:00:00       7092  Found 170 upcoming scheduled recordings starting at 7/7/2020 2:00:00 PM
2020-07-07T12:00:00       7092  Getting next Plex maintenance time
2020-07-07T12:00:00       7092  Plex maintenance is 1am to 6am every day
2020-07-07T12:00:00       7092  Next scheduled maintenance time is 7/8/2020 1:00:00 AM to 7/8/2020 6:00:00 AM
2020-07-07T12:00:00       7092  Creating/updating wakeup task: Plex DVR Waker\DVR wake
2020-07-07T12:00:00       7092    Creating/updating the task
2020-07-07T12:00:01       7092  Wakeup task scheduled for 7/7/2020 1:59:00 PM
2020-07-07T12:00:01       7092  Exit code: 0 (Success)
2020-07-07T12:00:15       9696  Getting scheduled recordings
2020-07-07T12:00:15       9696    Loading subscriptions
2020-07-07T12:00:15       9696      Subscriptions loaded: 224
2020-07-07T12:00:15       9696    Loading EPG info for subscriptions
2020-07-07T12:00:15       9696      EPG info found: 200
2020-07-07T12:00:15       9696    Removing unschedulable and past items
2020-07-07T12:00:15       9696      Removed items: 30
2020-07-07T12:00:15       9696    Removing previously recorded TV shows
2020-07-07T12:00:15       9696      Removed TV shows: 24
2020-07-07T12:00:15       9696    Removing previously recorded movies
2020-07-07T12:00:15       9696      Removed movies: 0
2020-07-07T12:00:15       9696  Found 170 upcoming scheduled recordings starting at 7/7/2020 2:00:00 PM
2020-07-07T12:00:15       9696  Getting next Plex maintenance time
2020-07-07T12:00:15       9696  Plex maintenance is 1am to 6am every day
2020-07-07T12:00:15       9696  Next scheduled maintenance time is 7/8/2020 1:00:00 AM to 7/8/2020 6:00:00 AM
2020-07-07T12:00:15       9696  Creating/updating wakeup task: Plex DVR Waker\DVR wake
2020-07-07T12:00:15       9696    Creating/updating the task
2020-07-07T12:00:15       9696  Wakeup task scheduled for 7/7/2020 1:59:00 PM
2020-07-07T12:00:15       9696  Exit code: 0 (Success)

PDW list output

C:\PlexDvrWaker>PlexDvrWaker.exe list
Start Time              End Time                Title
---------------------   ---------------------   --------------------------------------------------
7/7/2020 10:00:00 PM    7/7/2020 10:30:00 PM    Seinfeld - S04E19 - The Implant
7/7/2020 10:30:00 PM    7/7/2020 11:00:00 PM    Seinfeld - S07E19 - The Wig Master
7/8/2020 1:30:00 AM     7/8/2020 2:00:00 AM     Nature Cat
7/8/2020 3:30:00 AM     7/8/2020 4:00:00 AM     Comics Unleashed With Byron Allen - S14E128 - 03-24-2020
7/8/2020 8:00:00 AM     7/8/2020 8:30:00 AM     Daniel Tiger's Neighborhood - S01E02 - Daniel Visits School; Daniel Visits the Doctor
7/8/2020 8:30:00 AM     7/8/2020 9:00:00 AM     Daniel Tiger's Neighborhood - S01E07 - Friends Help Each Other; Daniel Helps O Tell a Story
7/8/2020 9:00:00 AM     7/8/2020 9:30:00 AM     Sesame Street - S49E05 - Elmo's Factory
7/8/2020 10:00:00 AM    7/8/2020 10:30:00 AM    Daniel Tiger's Neighborhood - S02E11 - Daniel Can't Ride Trolley; Daniel Can't Get What He Wants
7/8/2020 10:30:00 AM    7/8/2020 11:00:00 AM    The Cat in the Hat Knows a Lot About That! - S03E05 - The Search for String; Mirror Mirror
7/8/2020 11:00:00 AM    7/8/2020 11:30:00 AM    Nature Cat - S01E24 - Ice Is Nice; Bird's Eye View
7/8/2020 12:00:00 PM    7/8/2020 1:00:00 PM     NOVA - S46E09 - First Horse Warriors
7/8/2020 12:00:00 PM    7/8/2020 1:00:00 PM     The Waltons - S06E11 - The Flight
7/8/2020 2:00:00 PM     7/8/2020 3:00:00 PM     General Hospital
7/8/2020 8:00:00 PM     7/8/2020 9:00:00 PM     The 100 - S07E08 - Anaconda
7/8/2020 9:00:00 PM     7/8/2020 10:00:00 PM    NOVA - S40E13 - Australia's First 4 Billion Years: Strange Creatures
7/8/2020 9:33:00 PM     7/8/2020 10:30:00 PM    Death in Paradise - S07E05 - Murder on the Day of the Dead
7/8/2020 10:00:00 PM    7/8/2020 10:30:00 PM    Seinfeld - S04E22 - The Handicap Spot
7/8/2020 10:30:00 PM    7/8/2020 11:00:00 PM    Seinfeld - S07E20 - The Calzone
7/9/2020 3:30:00 AM     7/9/2020 4:00:00 AM     Comics Unleashed With Byron Allen - S14E129 - 03-25-2020
7/9/2020 8:00:00 AM     7/9/2020 8:30:00 AM     Daniel Tiger's Neighborhood - S02E18 - Daniel Takes Care of Snowball; Margaret's Bathtime
7/9/2020 10:30:00 AM    7/9/2020 11:00:00 AM    The Cat in the Hat Knows a Lot About That! - S02E03 - Bounce; Timmy Tippy Toe
7/9/2020 10:30:00 AM    7/9/2020 11:00:00 AM    Daniel Tiger's Neighborhood - S02E10 - Miss Elaina Gets Hurt; Daniel Feels Better
7/9/2020 11:00:00 AM    7/9/2020 11:30:00 AM    Nature Cat - S01E26 - Stop That Squirrel; Onward and Pondward
7/9/2020 12:00:00 PM    7/9/2020 1:00:00 PM     NOVA - S46E10 - Lost Viking Army
7/9/2020 12:00:00 PM    7/9/2020 1:00:00 PM     The Waltons - S06E13 - The Children's Carol
7/9/2020 2:00:00 PM     7/9/2020 3:00:00 PM     General Hospital
7/9/2020 10:00:00 PM    7/9/2020 10:30:00 PM    Seinfeld - S08E21 - The Muffin Tops
7/9/2020 10:00:00 PM    7/9/2020 11:00:00 PM    NCIS: Los Angeles - S11E08 - Human Resources
7/9/2020 10:30:00 PM    7/9/2020 11:00:00 PM    Seinfeld - S07E21 - The Bottle Deposit - Part 1
kmahon37 commented 4 years ago

Thanks for the information and detail (and I'm not judging :-). The Plex schedule priority is currently not taken into account at all, and I purposely didn't support it (hence my disclaimer in the readme). That said, I can see it being useful, so I'll take a stab at it.

I can see the priority in Plex's database and it's easy enough to retrieve, but the harder part is determining conflicts the same way that Plex is doing it (I don't know if Plex stores the conflict data anywhere or if it's calculated on-the-fly). How many internal tuners does your "Hauppauge USB tuner for Xbox One" have? I'm guessing it only has one based on a quick search. I currently use an HDHomeRun with dual tuners, and so I can actually record two shows at once. I'm asking because I think I'll also need to take the number of tuners into account when calculating the priority and conflicts.

solstyce9 commented 4 years ago

Ah, sorry. I remember reading that some of the recording settings weren't supported, but didn't realize recording priority wasn't. There's my problem... :-)

Yeah, my tuner is a single tuner. My DVR setup started as an experiment - I had Plex Pass and an old OTA antenna, so I just asked for a USB tuner as a birthday present, and here I am. Plex DVR is interesting, but still has some serious usability gaps, so I don't think I'll be able to get my family on board with a quad tuner setup and external antenna for a while. So given the single tuner, I always have conflicts, but I think the time window for all my other conflicts completely overlap, so whether it's recording one show or the other doesn't matter as long as PDW wakes up my system and then Plex can figure it out. This is the only one where the preferred recording is significantly later than the lower priority conflict.

For now I went old school, and just created a manual task to wake at 12:29pm weekdays... And I can manage my conflicts to avoid this type of scenario. When / if you ever have a chance to work on this feature, I'd be happy to test it out, but I expect most people will have a dual or quad tuner and won't run into this problem, so no pressure. I really wish Plex would build auto-wake into PMS, but now that I've logged some time with the Plex DVR and run into all the weird bugs and hiccups and I see why it's not. My electricity bill and I thank you for making this possible! :-)

kmahon37 commented 4 years ago

Just wanted to give an update on this....

I was able to get something seemingly working for "simple" recording schedule conflicts based purely on start time, priority, and the number of tuners available. However, when I started testing recording schedules with multiple "complex" overlapping conflicts, my results would no longer match the conflicts that Plex identified, and thus would not be useful because it would not wakeup the computer at the time that Plex would want to record.

After looking through the Plex logs, it appears that Plex actually performs some additional magical logic in figuring out which shows to identify as conflicts. It looks like it may run something like a gaming-tree of calculations and may weight each show somehow before calculating a "score" of some sort. It seems like it also gives some weight to, say, recording two 30 minute shows instead of a 1 hour show, but that's just a guess. It also appears to make some kind of http call (most likely authenticated with your Plex credentials) to either perform the calculation or store the results, possibly.

After discovering Plex's magical conflict behavior from its logs, I poured over the Plex database again in an attempt to find anything related to the final recording schedule. However, there simply does not seem to be anything there I can use.

So, at this point, I'm not sure what else to do, because I don't know if my "simple" logic is worth releasing or not since it won't always match Plex's more "complex" conflicts. And I'm not sure how I could explain how/why the conflicts wouldn't match sometimes. I'll keep this bug open, and maybe someday I'll stumble upon an idea or something to make this work.

solstyce9 commented 4 years ago

Appreciate your efforts and the update!

The more I learn about Plex the more complicated I realize it is. I'm fine operating within its constraints now that I know what they are.

Hopefully at some point Plex will realize people want their systems to sleep when they're not being used and build this directly into the product. No reverse engineering magic required :-)