How to get Plugin to recieve events?

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
thug
Plugin Developer
Posts: 64
Joined: Sat Jul 22, 2006 2:37 pm
Location: Australia

How to get Plugin to recieve events?

Post by thug » Wed Jan 03, 2007 3:26 pm

Warning ... stoopid noob question follows!! :roll:

I need my plugin to receive the 'Main' events, or any/every other I suppose.

How can I get my plugin to internally receive either all generated events, a matching / wildcard set or all 'Main' & 'System' events like 'Main.OnInit' and 'System.Suspend'?

Cheers and thank you in advance for your help! :lol:

User avatar
Bitmonster
Site Admin
Posts: 2239
Joined: Mon Feb 06, 2006 10:28 pm

Post by Bitmonster » Wed Jan 03, 2007 3:39 pm

Currently there is no "hook" you can use inside a plugin to get all or some events of other plugins. At least its not defined in a fixed way (in Python you could actually hook everything at runtime, if you know the method you need to overwrite). So currently I would suggest to use an "event sink action", that you can add to the tree and assign all needed events to. This should be workable so far to try out whatever you want to do with your plugin. Later we can think about a plugin function like RegisterEventHook or something, that you can use directly inside your plugin.

Do you think about capturing the events to let them show on your serial LCD?

thug
Plugin Developer
Posts: 64
Joined: Sat Jul 22, 2006 2:37 pm
Location: Australia

Post by thug » Wed Jan 03, 2007 11:46 pm

Do you think about capturing the events to let them show on your serial LCD?
No, not show them directly, but when I have my serial lcd plugin loaded, and I send my machine into S3 standby i get this horrid exception error as follows :-

Code: Select all

System.QuerySuspend
System.Suspend
System.Idle
Exception in thread Thread-206:
Traceback (most recent call last):
  File "threading.pyc", line 460, in __bootstrap
  File "threading.pyc", line 625, in run
  File "23", line 24, in UpdateMyScreen
  File "plugins/VFD/__init__.py", line 400, in __call__
  File "plugins/VFD/__init__.py", line 330, in UpdateVFD
  File "plugins/VFD/__init__.py", line 357, in VFD_String
  File "C:\Program Files\EventGhost\eg\WinAPI\serial\serialwin32.py", line 240, in write
SerialTimeoutException: Write timeout

Exception in thread Thread-207:
Traceback (most recent call last):
  File "threading.pyc", line 460, in __bootstrap
  File "threading.pyc", line 625, in run
  File "23", line 24, in UpdateMyScreen
  File "plugins/VFD/__init__.py", line 400, in __call__
  File "plugins/VFD/__init__.py", line 330, in UpdateVFD
  File "plugins/VFD/__init__.py", line 357, in VFD_String
  File "C:\Program Files\EventGhost\eg\WinAPI\serial\serialwin32.py", line 238, in write
error: (995, 'GetOverlappedResult', 'The I/O operation has been aborted because of either a thread exit or an application request.')
System.ResumeAutomatic
System.Resume
I know why it happens, my plugin is getting used (serial port routines) whilst the system is going into standby.

I just wanted my plugin to be notified (directly) of any System.Suspend messages so that the system can pause any updates sent to the serial port, or even close the serial port and reopen when resumed, that would be cleaner than anyone having to code / capture / cater for it themselves via an "event sink action" (if i understand what you mean by an "event sink action").

I could hijack the code from the PowerBroadcastNotifier.py file for my plugin to get the suspend / resume events, you OK with that? :wink:

User avatar
Bitmonster
Site Admin
Posts: 2239
Joined: Mon Feb 06, 2006 10:28 pm

Post by Bitmonster » Thu Jan 04, 2007 12:02 am

Currently you can't add another handler the way PowerBroadcastNotifier.py does it, because it would replace the handler that ist registered for WM_POWERBROADCAST. So PowerBroadcastNotifier.py wouldn't work anymore. I can fix this, so multiple handlers can work on the same message (should be done anyhow).

But there is another problem:
Your plugin runs in another thread than the message that is received by PowerBroadcastNotifier. So this might arrise issues, because the system shuts down, before your code can execute everything to shut down the serial port.

I need some time to think about it. Maybe tomorrow...

thug
Plugin Developer
Posts: 64
Joined: Sat Jul 22, 2006 2:37 pm
Location: Australia

Post by thug » Thu Jan 04, 2007 12:21 am

Hey, take all the time you need. :P

I got some work to do on this plugin anyway, it can wait! 8)

Cheers!

User avatar
Bitmonster
Site Admin
Posts: 2239
Joined: Mon Feb 06, 2006 10:28 pm

Post by Bitmonster » Sat Jan 06, 2007 6:06 pm

Ok, I made a quick solution in 839.

You can now call the new eg.Bind and eg.Unbind. If you want a notification of the System.Suspend event for example, you can write this test code:

Code: Select all

def OnSuspend(event):
    print "System is going into Suspend."

eg.Bind("System.Suspend", OnSuspend)
That's all. It will work from plugins and PythonScripts. The OnSuspend handler will be called from the ActionThread, so there shouldn't be any threading issues, because this is the same thread as nearly all plugin code is called from. As long as your OnSuspend handler runs its job, EG won't let the system go into standby.

This mechanism works for all EG events, but the event must be "fully qualified", so wildcards won't work. If your handler returns "True", EG will not let this event go to the normal processing. But this should be avoided. The "event" parameter is the object, that is described as eg.event in the wiki:
http://www.eventghost.org/wiki/Scripting#eg.event
You can even modifiy this object, but this is beyond the current scope.

Once you are through with your hook, you must call eg.Unbind to delete the handler. The parameters are the same, so you have to supply the same eventstring and the same callable object:

eg.Unbind("System.Suspend ", OnSuspend)
So this should be called in your __stop__ handler.

thug
Plugin Developer
Posts: 64
Joined: Sat Jul 22, 2006 2:37 pm
Location: Australia

Post by thug » Sun Jan 07, 2007 1:12 pm

holy cr@p! That works supreme! (But you already knew that) :lol:

Post Reply