[RELEASE] Execute user scripts on specific XBMC actions (play starts/stops...)

  Thread Rating:
  • 3 Votes - 4.33 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Post Reply
hindibuff Offline
Member
Posts: 91
Joined: Aug 2011
Reputation: 0
Post: #136
Hi,
Love the idea but complete newbie trying to implement this on an android box. Is there a gui or a walk--through how to go about configuring this??
find quote
KenV99 Offline
Senior Member
Posts: 245
Joined: Nov 2013
Reputation: 8
Post: #137
For my version, you can start here: https://github.com/KenV99/service.xbmc.callbacks2/wiki
find quote
hindibuff Offline
Member
Posts: 91
Joined: Aug 2011
Reputation: 0
Post: #138
Awesome. I am trying to automate importing imdb/trakt watchlist in Genesis addon. Wondering if this will work for that. will take a stab at it. Thanks again.
find quote
joachimpr Offline
Junior Member
Posts: 2
Joined: Aug 2014
Reputation: 0
Post: #139
Hi Pilluli,

Thanks so much for this plugin. AWESOME concept and it will help me a lot if I can get it working. I have literally tried everything now including all the suggestions in this thread.

What I am trying to do:
I have an 8 channel relay board linked to the GPIO pins on the Pi. I have written 3 python scripts: raspbmc_on.py to turn the switch on, raspbmc_off.py to turn it off and raspbmc_off_timer.py to turn the switch off after 10 minutes.

The code:

raspbmc_on.py
Code:
#!/usr/bin/env python

import RPi.GPIO as GPIO
import time
import random

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

GPIO.setup(3, GPIO.OUT)
GPIO.setup(5, GPIO.OUT)
GPIO.setup(7, GPIO.OUT)

color = random.randrange(1,5)
print color

if color == 1:
    GPIO.output(7, False)
    GPIO.output(3, False)
    GPIO.output(5, False)

elif color == 2:
    GPIO.output(7, False)
    GPIO.output(3, True)
    GPIO.output(5, False)

elif color == 3:
    GPIO.output(7, False)
    GPIO.output(3, False)
    GPIO.output(5, True)

elif color == 4:
    GPIO.output(7, True)
    GPIO.output(3, False)
    GPIO.output(5, False)
    
elif color == 5:
    GPIO.output(7, False)
    GPIO.output(3, True)
    GPIO.output(5, False)    
else:
    GPIO.output(7, False)
    GPIO.output(3, False)
    GPIO.output(5, False)

raspbmc_off.py
Code:
#!/usr/bin/env python

import RPi.GPIO as GPIO
import time


GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

GPIO.setup(3, GPIO.OUT)
GPIO.setup(5, GPIO.OUT)
GPIO.setup(7, GPIO.OUT)

color = "off"

if color == "red":
    GPIO.output(3, False)
    GPIO.output(5, True)
    GPIO.output(7, True)

elif color == "green":
    GPIO.output(5, False)
    GPIO.output(3, True)
    GPIO.output(7, True)
    
elif color == "blue":
    GPIO.output(7, False)
    GPIO.output(3, True)
    GPIO.output(5, True)
    
elif color == "white":
    GPIO.output(7, False)
    GPIO.output(3, False)
    GPIO.output(5, False)

elif color == "tq":
    GPIO.output(7, False)
    GPIO.output(3, True)
    GPIO.output(5, False)

elif color == "purple":
    GPIO.output(7, False)
    GPIO.output(3, False)
    GPIO.output(5, True)

elif color == "yellow":
    GPIO.output(7, True)
    GPIO.output(3, False)
    GPIO.output(5, False)
    
else:
    GPIO.output(7, True)
    GPIO.output(3, True)
    GPIO.output(5, True)

raspbmc_off_timer.py
Code:
#!/usr/bin/env python

import RPi.GPIO as GPIO
import time
import datetime


GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

GPIO.setup(3, GPIO.OUT)
GPIO.setup(5, GPIO.OUT)
GPIO.setup(7, GPIO.OUT)


i = datetime.datetime.now()
scriptstartdatetime = ("%s" % i)

i = datetime.datetime.now()
currentdatetime = i
offdatetime = i + datetime.timedelta(minutes = 10)

while(currentdatetime < offdatetime):
    time.sleep(1)
    i = datetime.datetime.now()
    currentdatetime = i
    print (currentdatetime)


GPIO.output(7, True)
GPIO.output(3, True)
GPIO.output(5, True)


I tried executing the python scripts directly from your plugin and it did not work. I then proceeded to try and run the python scripts through command shell and it did not work either as you need to execute the scripts with SUDO. Yes I did make them executable with CHMOD.

I then proceeded to create 3 new sh scripts. exec_raspbmc_on.sh which executes raspbmc_on.py with SUDO, exec_raspbmc_off.sh and exec_raspbmc_off_timer.sh to do the same with their matching python scripts.

The code for the sh scripts:

exec_raspbmc_on.sh
Code:
#!/bin/bash
sudo /home/pi/raspbmc_on.py

exec_raspbmc_off.sh
Code:
#!/bin/bash
sudo /home/pi/raspbmc_off.py

exec_raspbmc_off_timer.sh
Code:
#!/bin/bash
sudo /home/pi/raspbmc_off_timer.py

After I did this I could execute the sh scripts through command line without SUDO which in turn executed their respective python scripts with SUDO and it worked perfectly. The switches turn on and off as expected.

The problem is that when I pointed your plugin to those sh scripts, once again it did not work.

I even tried updating the settings.xml file manually to point directly to the python scripts and I even tried setting the value to "SUDO /home/pi/raspbmc_on.py" and nothing.

I tried creating python scripts which is the same as the sh scripts which executes the on off python scripts with SUDO. Once again, executing these without sudo in terminal executes the on off scripts with sudo and everything works yet hooking them up with the plugin does not work.

I also checked the XBMC log and opened your code and I cannot find the log entries that you write to the xbmc log anywhere in my xbmc log? It looks like its not even trying to execute the scripts yet the addon is installed, enabled and configured?

I am relatively new to linux and this whole python raspberry pi adventure and I know the code is not optimal but I will optimize it once I can get it running. I am sure I am probably missing something obvious but for the life of me it has been a 3 day battle and I just cannot get it working.

Please Please PLEASE help me to get this running.

Thanks in advance.
J
(This post was last modified: 2014-08-27 22:09 by joachimpr.)
find quote
kulgan Offline
Junior Member
Posts: 6
Joined: Aug 2014
Reputation: 0
Post: #140
Hi,


I'm trying to use this addon but with version 0.5 I've an error :
21:50:28 T:2934338640 DEBUG: XBMC callbacks: screensaver starts
21:50:28 T:2934338640 DEBUG: XBMC callbacks: Going to execute script = "/storage/onScreenSaverActivate.sh"
21:50:28 T:2934338640 DEBUG: XBMC callbacks: ERROR executing script when screensaver starts

while /storage/onScreenSaverActivate.sh can successfully executed manually.

Could you help me?

Thanks


--------------------------------------------------------------------------------------------------------------
I've also tried KenV99 kork, but I didn't suceed in installing script.module.monitorext
(This post was last modified: 2014-08-31 22:22 by kulgan.)
find quote
KenV99 Offline
Senior Member
Posts: 245
Joined: Nov 2013
Reputation: 8
Post: #141
For my fork, please ensure that you installed from the 'allincluded' branch:
https://github.com/KenV99/service.xbmc.c...cluded.zip

If you are still having an issue after checking that, please post back.

EDIT: Sorry, you seem to have deleted that bit about my fork. You can disregard the above as you see fit.
(This post was last modified: 2014-08-31 22:23 by KenV99.)
find quote
kulgan Offline
Junior Member
Posts: 6
Joined: Aug 2014
Reputation: 0
Post: #142
Yes I found that script.module.monitorext , so I 'he tried to fix by myself, so i've edited my post.

allincluded installed successfully, will now test call to my script

Thanks


-----------------------------------
edit:
all tests ok

Thanks
(This post was last modified: 2014-09-01 08:20 by kulgan.)
find quote
kulgan Offline
Junior Member
Posts: 6
Joined: Aug 2014
Reputation: 0
Post: #143
Hi,

I using the addon fro several day now, and I've found a strange behavior :
The context: I've 2 profiles children/master (to select what movie children can access).
xbmc autolog in children profile.

my usecase :
if I swicth from children to Master profile, I want to automatically switch back to children when screensaver is activated.
Do do so, I've created in Master profile a rule a screensaveractivated to reboot XBMC.

The probleme :
if I start xbmc, switch from children to master, at screensaveractivation, nothing happen despite my rules.
if Istart xbmc, switch from children to master, check in addon configuration that everything is OK, at screensaveractivation, xbmc is rebooted (as expected)
So it look like, when switching profile, the addon is not loading rules of the profile.

Thanks
find quote
kulgan Offline
Junior Member
Posts: 6
Joined: Aug 2014
Reputation: 0
Post: #144
found a way to workaround this behavior by changing addon start type to login
(This post was last modified: 2014-09-05 19:15 by kulgan.)
find quote
djgreedo Offline
Junior Member
Posts: 13
Joined: May 2012
Reputation: 0
Post: #145
I'm interested in using this plugin. Is there any way to detect the CEC active/inactive status?

I want to disable/enable the Xbox One media remote control based on whether the XBMC is the active HDMI device. The goal is to have XBMC react to the Xbox One remote when it is active, but ignore it when I'm actually using the remote to control the Xbox.

(I have a RPi XBMC/Openelec plugged into the HDMI-In of the Xbox One, and the Xbox One sends activate/deactivate commands to the XBMC via HDMI-CEC)
find quote
Ferdi-T Offline
Senior Member
Posts: 202
Joined: Jul 2011
Reputation: 1
Post: #146
This seems exactly what I've been looking for, for controlling my lights through XBMC. I'll install the fork tonight.

I'm wondering how I'm going to pass a parameter to a bash (as I want to dim lights on video playback but not music playback) Would this be correct input?

type: script
Script executable: /home/pi/scripts/dimlights.sh
parameters: {movie:episode}

And am I right there's no way to call a script on http video streaming? Not a biggy, just wondering.
find quote
Ferdi-T Offline
Senior Member
Posts: 202
Joined: Jul 2011
Reputation: 1
Post: #147
Ok, worked partially: plugin now controls my lights, but also dims when I play music. I'm still tryintg to figure out how to run the script on video only, but at least it's doing something today it wasn't doing yesterday. :-)

As for distinguishing between music and video: is it right to assume that it's not possible to pass a parameter to sh? Because that was my conclusion after a couple of hours of tinkering.

So I tried making a python-script, which checks the media and then points towards the bash. Obviously it's not working, my python-knowledge is close to zero.

Code:
#!/usr/bin/env python

import sys
if  not  'music' in sys.argv[1]:
   import subprocess
   subprocess.call([./blauw.sh']
0

yields...

Code:
File "blauw2.py", line 6
    subprocess.call([./blauw.sh']
                     ^
SyntaxError: invalid syntax

Anyone seeing what I'm messing up?
(This post was last modified: 2014-10-01 22:42 by Ferdi-T.)
find quote
KenV99 Offline
Senior Member
Posts: 245
Joined: Nov 2013
Reputation: 8
Post: #148
You are missing both a beginning single quote and an end parentheses and do not need square brackets.

Try: subprocess.call('./blauw.sh')

I'm on windows, but if I am correct, bin/bash takes any command line parameters and places them into the variables $1, $2, etc so you could skip python and write a shell script if you are more comfortable, but if you didn't know about parsing the command line, that is not likely the case.

If you are using my branch, if playback is a video stream, then the first argument passed will be type=stream... In its current state, the addon cannot distinguish between streaming audio and local audio files.

(2014-09-26 06:15)djgreedo Wrote:  I'm interested in using this plugin. Is there any way to detect the CEC active/inactive status?

I want to disable/enable the Xbox One media remote control based on whether the XBMC is the active HDMI device. The goal is to have XBMC react to the Xbox One remote when it is active, but ignore it when I'm actually using the remote to control the Xbox.

(I have a RPi XBMC/Openelec plugged into the HDMI-In of the Xbox One, and the Xbox One sends activate/deactivate commands to the XBMC via HDMI-CEC)

In short, no. There is no way directly in the python version/libraries that XBMC provides to detect CEC changes.
(This post was last modified: 2014-10-01 23:42 by KenV99.)
find quote
Ferdi-T Offline
Senior Member
Posts: 202
Joined: Jul 2011
Reputation: 1
Post: #149
Hi Ken,

Thanks for the pointer. Very much appreciated. This is my very first Python-script and I'm glad it worked. Didn't know how to handle arguments in sh, just used some previous post as reference. Calling sh from py is probably lazy programming, but at least it works. :-)

One more question though. Right now everything works when playing a movie: start, stop, pause and resume all trigger a separate action. Starting music does nothing, as intended. The script that correctly distinguishes between music and video during the start of playback is:

Code:
#!/usr/bin/env python
#import then run
import sys
if  not  'music' in sys.argv[1]:
    import subprocess
    subprocess.call('/home/pi/milight/blauw.sh')
0

However, pausing, resuming and ending music still triggers the stop-action (lights on, which is a bit weird during daytime). Could the problem we that stopping music causes the value in sys.argv to change before the scripts executes?

Current script for ending playback runs on both music and video:

Code:
#!/usr/bin/env python
#import then run
import sys
if  not  'music' in sys.argv[1]:
    import subprocess
    subprocess.call('/home/pi/milight/withalf.sh')
0

Perhaps I should tell it it to look at if/video, instead of ifnot/music? I already tried putting a variable in line 4, but am not sure about the syntax.

Code:
if  any ('movie', 'episode') in sys.argv[1]:
(This post was last modified: 2014-10-02 10:25 by Ferdi-T.)
find quote
KenV99 Offline
Senior Member
Posts: 245
Joined: Nov 2013
Reputation: 8
Post: #150
XBMC calls the same stop and end events, regardless of what type of file was playing. Due to this, the addon as currently written does not pass the type of file that was playing back out on the stop or end events.

You would need to find a way to store state information in between calls of your python script such that your start script stores the fact that light were turned off and then your stop script only turns the lights on if the lights were turned off. Given that these are individual scripts running separately, one simple way would be to create a file when the lights turn off, check for it's existence during the off script and only turn the lights on if exists. Finally, you would delete the file after the lights come on.

Something like this:
Code:
# Lights off
import sys
if not 'music' in sys.argv[1]:
    import subprocess
    subprocess.call('/home/pi/milight/blauw.sh')
    open('/home/pi/milight/lightsoff.txt', 'a').close()
Code:
# Lights on
import os
if os.path.isfile('/home/pi/milight/lightsoff.txt'):
    import subprocess
    subprocess.call('/home/pi/milight/withalf.sh')
    os.remove('/home/pi/milight/lightsoff.txt')

Make sure that you have appropriate permissions set for creating and deleting files. Since you are reading/writing to '/home', you shouldn't have issues, but that's what I would look at if you do.

Note for more complicated information, you would 'pickle' the data and save it, but I think that's overkill in this instance.
find quote
Post Reply