pseudo-channel icon indicating copy to clipboard operation
pseudo-channel copied to clipboard

Multiple "channels" with multiple daily_schedule tables and the ability to specify which one on startup

Open MoeFwacky opened this issue 7 years ago • 32 comments

So, with the addition of the immediate start of playback when the script is launched, I got the idea that with multiple daily_schedule tables in the database (called from individual xml files), and the right setup, the function could be used to set up multiple channels by triggering the script off, then on again with a different daily_schedule table. Basically allows for multiple channels without having to run more than one instance of the script or one RasPlex device.

MoeFwacky avatar Aug 12 '17 17:08 MoeFwacky

This is an incredible idea. I'd imagine the way to get it to work without typing in cli commands is to have some sort of external trigger - ideally, an IR Remote circuit that can catch/decode your TV remote button presses so it can change channels when your press channel + / - and loop back around like real tv. I suppose the quickest/dirtiest way I can think of is to have the setting in the config: "numberOfChannels = 5". Then I can have the app create the 5 schedules, 5 queues and look for the corresponding 5 XML files for the channels. At that point, some kind of external trigger could pass a "1" or "0" to the app telling it to change channels up/down respectively. We'll have to keep on this one! Not only would it be energy efficient & further the life of your plex hard drives, it of course would give you a lot more options for your experience, a movie channel, a cartoon-network of sorts, a channel for james bond movies only, a 90's channel, MTV channel playing music videos (as you mentioned you already do). This is a fantastic idea. Let's keep discussing it. :)

justinemter avatar Aug 12 '17 20:08 justinemter

I was thinking of grabbing a remote like this one since it's small and simple and doesn't require line-of-sight.

MoeFwacky avatar Aug 12 '17 21:08 MoeFwacky

Oh yeah! I just happened to quickly tap the reviews and saw it had some pretty bad ones for some reason, but yes, this is the idea! I've never intercepted usb signals in this way, definitely something to look into. :)

justinemter avatar Aug 12 '17 22:08 justinemter

Ok, just for reference and to keep this idea going strong, here is a lib I came across when briefly researching interfacing with usb via python: https://github.com/walac/pyusb. I'll try and setup a py script that listens for arguments. I'll create another repo where we can tinker with this tech (if it's not overboard). I have a feeling that this is going to be easier than you think.

justinemter avatar Aug 12 '17 22:08 justinemter

You seem pretty confident, so I pulled the trigger on that remote (for less than $10, why not). Could be a pretty exciting addition.

MoeFwacky avatar Aug 12 '17 23:08 MoeFwacky

Something to consider, is that this may complicate the last played episode data. If each xml is calling the same data, and a show is scheduled on two different "channels," will it advance the show one episode per time scheduled across channels?

MoeFwacky avatar Aug 12 '17 23:08 MoeFwacky

I suppose a quick and dirty solution is to associate an XML with a "shows" table. So as of now, the "shows" table saves the "prevEpisodeTitle" which is all there is for a "queue". So everyday the app generates a daily schedule it checks that "shows" table for the "prevEpisodeTitle". So if there is a channel two, we would just have a "shows2" table or something. So whenever the second channel tries to generate the new daily schedule it will do so using the "queue" from the "shows2" table. Unfortunately there is a lot in this already that wreaks of "legacy" code... It's fun to see the project evolve step by step, but now that we're here at this important point, I can see how things should be done *right. It's a tough call - do we keep hacking at this script or do we move PseudoChannel to Django... I suppose this a good reason to iron out all the bugs in this alpha version, and then get really nuts with the Django (beta) version. It just sucks cause this is my first Django project, and I've gotten pretty far but not nearly as far as the cli app and I get a bit frustrated at that fact. But yes, we need to figure out how to handle the independent queues for each channel.

justinemter avatar Aug 12 '17 23:08 justinemter

Yeah this might be a thing to wait on the Django port for, given the potential complexities it introduces.

Edit: though my remote control did come in the mail today, will be neat once this thing can do stuff

MoeFwacky avatar Aug 15 '17 21:08 MoeFwacky

So, on the remote control angle. I was poking around with openbox, and it looks like it's relatively trivial to map keys to shell scripts. I've already set one up on the home button that restarts lightdm and chromium on the status screen as a test. Going to try and set up the power button to run the pseudo channel startstop script tomorrow.

MoeFwacky avatar Aug 19 '17 10:08 MoeFwacky

Ok cool. Yeah I spent a good amount of time revising that startstop.sh script to work as well as I could. Keep in mind you need to set the cron job to trigger to update the daily schedule if your app isn't running. I need to go back and revise that script too. If you're confident that your app is running pretty good, you can set the cron task and use the startstop script exclusively. Next step is to think about / implement multi-channel. Although an out-of-the-box way to get it working, as you mentioned is to just have multiple instances of the app in different directories. I could whip up a script that sits outside of all the channel directories and cycles through them when your remote button is pressed. This could be a nice short term solution. Tell me what you think.

justinemter avatar Aug 19 '17 12:08 justinemter

Yeah, that could be workable, maybe with two versions of the directory switching script (one for up and one for down). I know I could set up simple scripts to assign buttons to run -u and -xml, but I'm not sure how that would work with multiple instances. Those are nice-to-haves, but I figure I may as well use as many of the buttons as possible.

MoeFwacky avatar Aug 19 '17 17:08 MoeFwacky

Ahh, assigning "-u" to a button is a great idea too! Good stuff. Also, I've been revising the readme a bit and want to make your name more prominent in "acknowledgements" but need to figure out how other people do that. So for now I just kind of squeezed you in at the bottom in the rough draft. Furthermore, I have an issue with my new XML. I have a few time blocks in <saturdays> & <sundays>... my times aren't being scheduled correctly so I need to further push back the alpha until that's fixed. So hopefully I can get back to it tomorrow or Monday.

justinemter avatar Aug 19 '17 19:08 justinemter

Cool. The readme looks good. I'd like to write an update to the FakeTV blog to point people to pseudo-channel instead of the old script, and include the remote setup, once I get it fine tuned.

Speaking of setting up the remote, I am having an issue with the startstop.sh file. I'm getting the following output over and over when I run it in the ssh terminal:

startstop.sh: 69: startstop.sh: +1]: not found
PseudoChannel.py -m -xml -r @:  is still running

MoeFwacky avatar Aug 19 '17 21:08 MoeFwacky

hmm, make sure you are testing it from your terminal first. Go ahead and cd into the pseudo-channel dir and run ./startstop.sh. If your app isn't already running, it will start it and create a running.pid file in the same directory and write the pid to it. The next time your run ./startstop it will look to see if that .pid file exists if so it knows there is an instance running and it will read it and kill it, then delete that file. That's how it knows. The error you are seeing is odd and hard to understand. It does, however, look as if you may have altered the original code.

justinemter avatar Aug 19 '17 21:08 justinemter

Actually, at line 69 I updated this: $COUNTER=$[$COUNTER +1]. I haven't gotten this error but that might have been it.

justinemter avatar Aug 19 '17 21:08 justinemter

I was missing the $ sign for the bash var. I think that makes it work. Sorry man, I haven't seen that error for some reason even though I'm running that script exclusively. Weird.

justinemter avatar Aug 19 '17 21:08 justinemter

Started up good with the change, but attempting to stop it doesn't appear to work.

pi@controller:~/pseudo-channel $ sudo sh startstop.sh
Started PseudoChannel.py -m -xml -r @ Process: 6678
Created running.pid file in . dir
pi@controller:~/pseudo-channel $ sudo sh startstop.sh
Deleted running.pid file in . dir
startstop.sh: 57: kill: Illegal number:
PseudoChannel.py -m -xml -r @:  is still running
startstop.sh: 69: startstop.sh: 1=$[1: not found

MoeFwacky avatar Aug 19 '17 21:08 MoeFwacky

Can you confirm that this is working in your terminal by hand executing the script manually? If you are jumping to your remote stuff, it is very possible that you need to cd into the pseudo-channel dir before doing anything. So like a cron task, if you execute a script, if you don't cd into the directory to execute it then it get's lost in the filesystem. So perhaps the script can't find the local ./running.pid file when executing the script from your remote app.

justinemter avatar Aug 19 '17 22:08 justinemter

yeah, this is running it from terminal in the folder

MoeFwacky avatar Aug 19 '17 22:08 MoeFwacky

Try it now. I am confused why this doesn't work for you. I thought it might be due to that initial $ but now think I may have been wrong and removed it. Bash is goofy.

justinemter avatar Aug 19 '17 22:08 justinemter

Still no luck. It starts fine like this

pi@controller:~/pseudo-channel $ sudo sh startstop.sh
Started PseudoChannel.py -m -xml -r @ Process: 7622
Created running.pid file in . dir

and in top it shows as expected (high CPU because it just started up)

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
 7622 root      20   0   22248  18424   7060 S 80.1  4.1   0:11.53 python

But when I run it again to stop it, I get this until I force terminate

pi@controller:~/pseudo-channel $ sudo sh startstop.sh
Deleted running.pid file in . dir
startstop.sh: 57: kill: Illegal number:
PseudoChannel.py -m -xml -r @:  is still running
startstop.sh: 69: startstop.sh: +1]: not found
PseudoChannel.py -m -xml -r @:  is still running
startstop.sh: 69: startstop.sh: +1]: not found
PseudoChannel.py -m -xml -r @:  is still running
startstop.sh: 69: startstop.sh: +1]: not found
PseudoChannel.py -m -xml -r @:  is still running
startstop.sh: 69: startstop.sh: +1]: not found
PseudoChannel.py -m -xml -r @:  is still running
startstop.sh: 69: startstop.sh: +1]: not found
PseudoChannel.py -m -xml -r @:  is still running
startstop.sh: 69: startstop.sh: +1]: not found
PseudoChannel.py -m -xml -r @:  is still running

Also, after I've run the second time, I get this Error creating play queue dialogue on the screen, though it doesn't prevent playback.

MoeFwacky avatar Aug 19 '17 22:08 MoeFwacky

I'm also just noticing that it looks like the script is running multiple times

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
 7529 root      20   0   41224  36892   6788 R 37.4  8.3   1:13.01 python
 7622 root      20   0   41224  37020   6916 R 37.1  8.3   1:11.53 python
 6678 root      20   0   56360  43160   6916 R 12.1  9.7   5:18.01 python

Playback is starting up twice, likely because they're being triggered by one, then the other.

MoeFwacky avatar Aug 19 '17 22:08 MoeFwacky

Ahh, I'm getting the same errors when running the process via sudo sh & also with just sh. However when I execute the script via, ./startstop (without sudo and without sh) it seems to work fine. Try that. Also, before doing this, run pgrep python and execute, sudo kill <pid> for all the processes that come up.

I think when you run sh it is not looking for the running.pid file in the local dir. Is there a reason you want to run the script as sudo and using the sh command?

justinemter avatar Aug 19 '17 22:08 justinemter

I've been running the pseudo channel script as sudo because if I don't it errors out. I suppose there's probably a better solution though.

pi@controller:~/pseudo-channel $ python PseudoChannel.py -xml -r
Traceback (most recent call last):
  File "PseudoChannel.py", line 697, in <module>
    pseudo_channel = PseudoChannel()
  File "PseudoChannel.py", line 54, in __init__
    logging.basicConfig(filename="pseudo-channel.log", level=logging.INFO)
  File "/usr/lib/python2.7/logging/__init__.py", line 1540, in basicConfig
    hdlr = FileHandler(filename, mode)
  File "/usr/lib/python2.7/logging/__init__.py", line 911, in __init__
    StreamHandler.__init__(self, self._open())
  File "/usr/lib/python2.7/logging/__init__.py", line 936, in _open
    stream = open(self.baseFilename, self.mode)
IOError: [Errno 13] Permission denied: '/home/pi/pseudo-channel/pseudo-channel.log'

MoeFwacky avatar Aug 19 '17 22:08 MoeFwacky

./startstop doesn't seem to work for me. Not sure what I'm doing wrong

pi@controller:~/pseudo-channel $ ./startstop
-bash: ./startstop: No such file or directory

pi@controller:~/pseudo-channel $ ./startstop startstop.sh
-bash: ./startstop: No such file or directory

pi@controller:~/pseudo-channel $ ./startstop /home/pi/pseudo/startstop.sh
-bash: ./startstop: No such file or directory

pi@controller:~/pseudo-channel $ dir | grep startstop
generate_daily_sched.sh  PseudoChannel.py     README.md         startstop.sh

MoeFwacky avatar Aug 19 '17 22:08 MoeFwacky

I have a feeling you are not currently the pi user but are logged in as root. If you run whoami you can see who you are at the moment. We're close.

justinemter avatar Aug 19 '17 22:08 justinemter

Comes back as pi

Sorry for the delay, had to run to the store.

MoeFwacky avatar Aug 20 '17 00:08 MoeFwacky

pi@controller:~/pseudo-channel $ ./startstop -bash: ./startstop: No such file or directory

pi@controller:~/pseudo-channel $ ./startstop startstop.sh -bash: ./startstop: No such file or directory

pi@controller:~/pseudo-channel $ ./startstop /home/pi/pseudo/startstop.sh -bash: ./startstop: No such file or directory

Try: ./startstop.sh. It needs the .sh

Also ensure that the bash file is executable: chmod +x ./startstop.sh

justinemter avatar Aug 20 '17 04:08 justinemter

Just did ./startstop.sh and it works as expected. Going to try keybinding that to the remote now. edit: i should clarify, I did sudo ./startstop.sh, without sudo it didn't start.

MoeFwacky avatar Aug 20 '17 04:08 MoeFwacky

So the challenge i'm hitting now is that I can't run ./startstop.sh from within openbox and trying to run the full path (even in an ssh terminal) doesn't seem to actually start the script and it gives this output even though the script isn't currently running beforehand

pi@controller:~/.config/openbox $ sudo ~/pseudo-channel/startstop.sh
Deleted running.pid file in . dir
/home/pi/pseudo-channel/startstop.sh: line 57: kill: (24065) - No such process
PseudoChannel.py -m -xml -r @: 24065 has finished
pi@controller:~/.config/openbox $ pgrep python
pi@controller:~/.config/openbox $

MoeFwacky avatar Aug 20 '17 05:08 MoeFwacky