Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

Python Scripting and Triggered Events

If you have a question or need help, this is the place to be.
Post Reply
CrazyTimes
Posts: 10
Joined: Sat Mar 12, 2016 3:51 pm

Python Scripting and Triggered Events

Post by CrazyTimes »

Hi all, I'm trying to learn more about python and 'advanced' EG scripting in general and was hoping to get some pointers.

I'm currently getting tripped up trying to figure out how to write a script that is both the source of an event, but also then reads and reacts to said event. I'm not even sure this is possible with the way EG seems to process things.

The initial action I'm triggering in the script is "Player State" from the "SqueezeboxServer" plugin:

Code: Select all

eg.plugins.SqueezeboxServer.Player_State()
This produces an event showing if the player is either playing or paused. I would then do an IF or ELSE in the script based on the play state returned. This doesn't seem possible however because it appears EG queues the triggered events and only fires them after all macros have completed. Am I correct thinking/seeing this and this method isn't possible?

My next thought was that maybe the plugin itself could be modified to return a true/false or set a variable that could then be read by the script regardless of when the event fires.

This apprears to be the plugin code in question:

Code: Select all

    def __call__(self):
        if self.plugin.bSqueezeboxServerObjectCreated:
            STATE = self.plugin.sq.get_mode()
            if STATE in ('stop', 'pause'):
                self.plugin.TriggerEvent("Player Stopped/Paused")
                return
            else:
                    if STATE in ('play'):
                        self.plugin.TriggerEvent("Player Playing")
                        return
        else:
            self.plugin.TriggerEvent("Player Not Found")
But this is starting to get beyond my limited python/EG skill.

Any suggestions to point me in the right direction would be greatly appreciated.
blaher
Experienced User
Posts: 487
Joined: Thu Nov 17, 2011 1:27 am

Re: Python Scripting and Triggered Events

Post by blaher »

CrazyTimes wrote: I would then do an IF or ELSE in the script based on the play state returned.
What are you trying to do? Can't you just make 2 macros: 1 for play, and 1 for pause; along with their respective actions? Is the squeezebox plugin not giving you events?
CrazyTimes
Posts: 10
Joined: Sat Mar 12, 2016 3:51 pm

Re: Python Scripting and Triggered Events

Post by CrazyTimes »

Using 2 additional macros triggered by the SqueezeboxServer plugin was exactly something I was previously doing as a work around, but it then introduces timing issues with the overall flow of my sequencing because EG doesn't fire events as it goes and only sends them after everything previously running is done. My configuration requires a lot of 'Jump/Return's, which just compounds the issue.

That's why I'm trying to keep this specific "task" inside a single compact macro with python scripting. A simplified look of what I ultimately am trying to achieve would be:

Code: Select all

Main Macro: Stuff & Jump/Return-> Player State Macro: Do stuff based on player state-> Return to Main Macro
An alternative "just make it work" solution would be to:

Code: Select all

Main Macro Part 1: Stuff & Player State call: Triggered Event-> Playing Macro: Stuff & Jump-> Main Macro Part 2 OR Not Playing Macro: Stuff & Jump-> Main Macro Part 2
Which I'm desperately trying to avoid do to other complications this creates for me.

Thanks for taking the time to respond.
lbretth
Posts: 6
Joined: Sat Jan 10, 2015 7:44 am

Re: Python Scripting and Triggered Events

Post by lbretth »

Hi,

I'm no Python expert, just learning it myself. But I thought perhaps you could have both of the events that the plugin creates trigger the macro that contains your script as an action, then in the script use eg.event to evaluate which event was triggered and act accordingly. For example:

Code: Select all

event = eg.event.string
if event = "SqueezeboxExample.notplaying"
    #do this if not playing
elif event = "SqueezeboxExample.playing"
    #do this if playing
If that makes any sense?
User avatar
kgschlosser
Site Admin
Posts: 5190
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Python Scripting and Triggered Events

Post by kgschlosser »

have a look see at eg.EventString

it's the variable container for the event, it's a good way to have a single script handle multiple events

K
If you like the work I have been doing then feel free to Image
CrazyTimes
Posts: 10
Joined: Sat Mar 12, 2016 3:51 pm

Re: Python Scripting and Triggered Events

Post by CrazyTimes »

lbretth wrote:Hi,
I'm no Python expert, just learning it myself. But I thought perhaps you could have both of the events that the plugin creates trigger the macro that contains your script as an action, then in the script use eg.event to evaluate which event was triggered and act accordingly. For example:

Code: Select all

event = eg.event.string
if event = "SqueezeboxExample.notplaying"
    #do this if not playing
elif event = "SqueezeboxExample.playing"
    #do this if playing
If that makes any sense?
kgschlosser wrote:have a look see at eg.EventString
it's the variable container for the event, it's a good way to have a single script handle multiple events
K
Thanks for the pointers guys. I'm going to mess with the eventstring some more to see what I learn. The problem I'm still running into is it seems there's no way to have a single script create as well as process the event as the event itself is only registered at the completion of the macro containing the script.

-CT
blaher
Experienced User
Posts: 487
Joined: Thu Nov 17, 2011 1:27 am

Re: Python Scripting and Triggered Events

Post by blaher »

CrazyTimes wrote: the event itself is only registered at the completion of the macro containing the script.
Perhaps the timer plugin might be of use? If you separate the macros and use immediate timers, you won't have to wait for the macro to finish. In other words, trigger events, rather than use jumps.

If you post your .xml, maybe somebody can see a better solution with eyes on the problem.
lbretth
Posts: 6
Joined: Sat Jan 10, 2015 7:44 am

Re: Python Scripting and Triggered Events

Post by lbretth »

Hi CT,

Give this a go:

Code: Select all

state = eg.plugins.SqueezeboxServer.Player_State()  #doing this should set the state variable to the value returned by the plugin rather than creating an event
print "Squeezebox state: " + state   # used to debug what the above line is returning
if state= "Player Stopped/Paused"
    #do this if not playing
elif state = "Player Playing"
    #do this if playing
Let us know how you get on with that. It might still be necessary to alter the code of the plugin a little. But I'm not sure at this stage and don't have a Squeezebox to test with. If it doesn't work, please provide as much info as possible on what result you get.

Thx,

Luke
User avatar
kgschlosser
Site Admin
Posts: 5190
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Python Scripting and Triggered Events

Post by kgschlosser »

eg.scheduler.AddTask(time, function, params)


that's a good way to get the ball rolling at least 1/2 the way. lol.

i still don't believe it will actually post an event,


i know you can use threading to accomplish what you want. i have never tried to use it in a script tho, only in plugins.

i am gonna have to take a look see.

what would be the purpose of firing an event and having it process that event in the same script you are already processing something else in??

scripts are as such code snippets, they shouldn't take more than a millisecond to process something.

how much can happen in a millisecond??

K
If you like the work I have been doing then feel free to Image
Post Reply