Kodi Community Forum

Full Version: [RELEASE] Execute user scripts on specific XBMC actions (play starts/stops...)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
(2014-06-06, 11:45)schumi2004 Wrote: [ -> ]Can this script also launch scripts/commands on profile switching?

No, as far as I know there is no API to receive a profile switch in xbmc. Regards,
Hello,

Would it be possible to make your script receive sleep calls?
(2014-06-06, 21:00)pilluli Wrote: [ -> ]
(2014-06-06, 11:45)schumi2004 Wrote: [ -> ]Can this script also launch scripts/commands on profile switching?

No, as far as I know there is no API to receive a profile switch in xbmc. Regards,

You could sort of hack it.

You could have a member variable which is the translated path of special://profile

In your def _daemon method you could then check whether this has changed, if it has then store the latest value and trigger the profile changed script.

On a different note, given that people seems to be adding code into your script to call XBMC actions, would it be worth adding that ability from with the script itself?
The problem I see with that approach is that the deamon would have to be executed frequently to detect a profile change. Right now the approach is to not pull frequently xbmc but rely on notifications to do the job... I will see if changing the profile triggers other things (such as a xbmc starts)

About the xbmc actions, i am not sure what you mean. what do you have in mind?
If you run the service at login rather than startup you could trigger it off that

Code:
<extension point="xbmc.service" library="default.py" start="login">

My other point was that at the moment you can trigger a script but some people are wanting to trigger xbmc builtins and are currently editing the local copy of the script in order to do so, eg

Code:
xbmc.executebuiltin("xbmc.RunScript(script.tv.show.next.aired)")

What would be good would be if you could actually enter the

Code:
"xbmc.RunScript(script.tv.show.next.aired)"

bit in the addon settings.rather than having to edit the code.
Thanks, I will try the login trigger. Sounds very good and neat.

About the action bit I will think of it. I don't see a clear way to mix builtins and scripts without cluttering too much the add on and having 2 different settings for scripts or builtins...
Great add-on. Three quick questions.

Firstly, what exactly does the idle_time command setting do in the addon? The default i believe is set to 10 minutes. What exactly does this mean? I set it to 1 minute, but i don't actually know what it does! If i hardcode it in settings.xml and set it to 0, what effect does that have? Can't find an explanation anywhere in the addon.

Secondly, what happens if i press pause [which executes script1] and play [which executes script2] in quick succession, will it wait for script 1 to finish before executing script 2? Or will it run them both at the same time if script1 hasn't finished yet?

Thirdly, is there any way to distinguish between the type of media being played by XBMC?
So for example,

if "video" content resumes or starts (TV Shows, Movie, YouTube Stream etc), i want to run LightsOff.sh
if "audio" content resumes or starts (Music from music library, internet radio stream etc), i want to run LightsOn.sh

I'd like to keep my ambient lights when i'm relaxing and listening to music, but if a tv show/movie/any video content is playing back, i want to turn the lights off using the addon. Would this be possible with your add on ? : )

Thanks so much
(2014-06-24, 06:16)invision Wrote: [ -> ]Firstly, what exactly does the idle_time command setting do in the addon? The default i believe is set to 10 minutes. What exactly does this mean? I set it to 1 minute, but i don't actually know what it does! If i hardcode it in settings.xml and set it to 0, what effect does that have? Can't find an explanation anywhere in the addon.

idle_time is the time you consider XBMC being idle Smile Seriously, after "idle_time" minutes of XBMC doing nothing, the "script_idle" will be called.

Quote:Secondly, what happens if i press pause [which executes script1] and play [which executes script2] in quick succession, will it wait for script 1 to finish before executing script 2? Or will it run them both at the same time if script1 hasn't finished yet?

Right now this service addon *does* wait for the script to finish so in theory script 2 will be executed *after* script 1 is finished.

Quote:Thirdly, is there any way to distinguish between the type of media being played by XBMC?
So for example,

if "video" content resumes or starts (TV Shows, Movie, YouTube Stream etc), i want to run LightsOff.sh
if "audio" content resumes or starts (Music from music library, internet radio stream etc), i want to run LightsOn.sh

I'd like to keep my ambient lights when i'm relaxing and listening to music, but if a tv show/movie/any video content is playing back, i want to turn the lights off using the addon. Would this be possible with your add on ? : )

It should be possible,right now the addon calls the script with an argument saying "video" or "music". You can read the argument in your script like this (in python):

Code:
#!/usr/bin/env python

import sys
if  'music' in sys.argv[1]:
  # do things
pilluli,

I wrote a script module which extends the xbmc.Monitor class that you might want to look into. It provides onStereoModeChanged(), onProfileChanged() and onPlaybackStarted() events in the monitor class in addition to it's standard events. There are some extra things you need to do when you subclassing it instead of the xbmc.Monitor. Below are links to the module and an example script that subclasses from the module. Each of the events are monitored in separate threads and the polling intervals can be set individually. The onPlayBackStarted event was added because in the current release, that event isn't fired in player for external players.

Module: https://github.com/KenV99/script.module.monitorext
Example Script: https://github.com/KenV99/script.monitorextexample

If you get a chance, let me know what you think or if you have any questions or if there are any other events you might want to see added. I'd be happy to fork your current and write it in if you wish...

Ken
Hi KenV99,

Many thanks for the info! It looks excellent and a great addition for this add on. I will take a deeper look into it but of course feel free to also fork and play with it!!
Thanks so much for your help pilluli.

I made this .py file, called it resumeandstart.py, and have it execute when "Play Starts" and when "Player Resumes". Any reason why it isn;'t working? I'm sure something is wrong with my code, just don't know what it is (as you can tell i have never used python before!).

Code:
#!/usr/bin/env python

import sys
import subprocess

if  'music' in sys.argv[1]:
subprocess.call(["/home/xbmc/.xbmc/scripts/lightson.sh"])

if  'video' in sys.argv[1]:
subprocess.call(["/home/xbmc/.xbmc/scripts/lightsoff.sh"])

If i get your addon to run lightsoff.sh and lightson.sh instead of this middle man .py file, they execute fine. I suspect something is wrong with my python coding.
Thanks so much
invision,
Python is all about indentation, your subprocess calls need to be indented for python to understand that they are part of the if statements. Take a look at https://www.python.org/about/gettingstarted/ if your interested in learning the language. If not this should fix it.

Code:
#!/usr/bin/env python

import sys
import subprocess

if  'music' in sys.argv[1]:
    subprocess.call(["/home/xbmc/.xbmc/scripts/lightson.sh"])

if  'video' in sys.argv[1]:
    subprocess.call(["/home/xbmc/.xbmc/scripts/lightsoff.sh"])
Hi,

I ended up reusing some of Pilulli's code rather than actually forking it.

I have a version which implements monitoring changes in Stereoscopic Mode, Profile Changes and can monitor playback starts/stops in external players (only use this last option if really necessary as it introduces some latency issues). I also included options for running regular scripts with arguments and using the shell. I also added the ability to run python scripts from within the xbmc environment which should give better access to xbmc information in the script. In addition, XBMC builtins and http:// commands can be run. I also added options for what information should be passed as arguments back to the caller. You can test each script right from the settings page, but runtime arguments (like media type) won't be sent.

You can download the alpha here: https://github.com/KenV99/service.xbmc.c...llincluded

I included a small python script for testing called 'testme.py' which just displays in XBMC the arguments that are being used.
Let me know if things don't work or you find bugs, etc. I only have a windows system to test on, so I'd be interested in hearing if things work out ok on other OS's.
______________________
I made a few small revisions as of 5:30p EST...
Hi KenV99,

is it possible to send simply http telegrams as notifications without script?
In case of "action" (player start/stop/pause-resume) send ip:port:"message". For "message" a simply number will be enough (1 = system start; 2.* = player start *media; 3 = stop; 4 = pause; 5 = resume...)
My problem is to get the actual status of xbmc without json request (because idle time).

Or - is there any documentation for your script to build my requested features?

Thanks in advanced!


EDIT:

Ok, i will try with python script. At the moment my server is down, so i can't try if it works or not.

my test script look like this:

Code:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.settimeout(2.0)
sock.connect(("192.168.70.249", 3654))
sock.settimeout(1.0)
msg = ''
sock.sendall(msg)

Is this ok for a simply message?
(2014-07-18, 08:52)prime0n Wrote: [ -> ]Hi KenV99,

is it possible to send simply http telegrams as notifications without script?
In case of "action" (player start/stop/pause-resume) send ip:port:"message". For "message" a simply number will be enough (1 = system start; 2.* = player start *media; 3 = stop; 4 = pause; 5 = resume...)
My problem is to get the actual status of xbmc without json request (because idle time).

Or - is there any documentation for your script to build my requested features?

Thanks in advanced!


EDIT:

Ok, i will try with python script. At the moment my server is down, so i can't try if it works or not.

my test script look like this:

Code:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.settimeout(2.0)
sock.connect(("192.168.70.249", 3654))
sock.settimeout(1.0)
msg = ''
sock.sendall(msg)

Is this ok for a simply message?

I agree with the conclusion you have arrived at - the http messaging that I implemented from within the script has some limitations. It doesn't use a raw socket to send messages. It requires a message formatted the same way you might put in a browser ("http://192.168.70.249:3654/mypageaddr?msg=1") and it isn't set up to pass parameters like 'media'. So you do need to implement that as your own script, as you have done. You'll need to pull in the arguments as a list with arglist = sys.argv if you want to pass 'media' to when the player starts...
Exactly how you setup the socket is obviously dependent on what the 'server' is expecting, so I won't be of much help there.
Good luck!
Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23