maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   Dbus Help (https://talk.maemo.org/showthread.php?t=63550)

xur17 2010-10-08 18:47

Dbus Help
 
I am trying to monitor a dbus signal, and make my n900 output some text when a specific signal is received.

I am using python. This is the signal that I am monitoring for (any presence change for a user on my jabber account). I got this using the dbus-monitor command:

Code:

signal sender=:1.496 -> dest=(null destination) serial=3979 path=/org/freedesktop/Telepathy/Connection/gabble/jabber/username_40gmail_2ecom_2fMaemo; interface=org.freedesktop.Telepathy.Connection.Interface.SimplePresence; member=PresencesChanged
  array [
      dict entry(
        uint32 209
        struct {
            uint32 2
            string "available"
            string "243"
        }
      )
  ]

Here is my code:

Code:

#! /usr/bin/python
import gobject, dbus
from dbus.mainloop.glib import DBusGMainLoop

def handle_presence():
    print "status changed"

if __name__ == '__main__':
        print "starting"
        DBusGMainLoop(set_as_default=True)
        bus = dbus.SystemBus()
        bus.add_signal_receiver(handle_presence, 'PresencesChanged', 'org.freedesktop.Telepathy.Connection.Interface.SimplePresence', None, '/org/freedesktop/Telepathy/Connection/gabble/jabber/username_40gmail_2ecom_2fMaemo')
        gobject.MainLoop().run()

As far as I can tell, this should work, but nothing is outputted.

kureyon 2010-10-08 19:46

Re: Dbus Help
 
Have you used dbus-monitor to confirm that the events you expect are being triggered?

xur17 2010-10-08 19:51

Re: Dbus Help
 
Quote:

Originally Posted by kureyon (Post 836715)
Have you used dbus-monitor to confirm that the events you expect are being triggered?

Yes, and they definitely are. I manually changed a user's presence that is on my buddy list while running dbus-monitor. I copied that exact line into this post, and made my script using it.

daperl 2010-10-08 21:35

Re: Dbus Help
 
Change

bus = dbus.SystemBus()

to

bus = dbus.SessionBus()

and all should be good.

xur17 2010-10-08 22:01

Re: Dbus Help
 
Quote:

Originally Posted by daperl (Post 836779)
Change

bus = dbus.SystemBus()

to

bus = dbus.SessionBus()

and all should be good.

Thank you! I have been playing around with it for hours, and this fixed it for me!

xur17 2010-10-08 22:31

Re: Dbus Help
 
Do you know what type is passed to handle_presence?

nicolai 2010-10-08 22:40

Re: Dbus Help
 
http://telepathy.freedesktop.org/spe...esencesChanged

daperl 2010-10-08 22:54

Re: Dbus Help
 
I changed your code to this:

Code:

def handle_presence(*args):
    print "status changed",args

and here's the output:

Code:

status changed (dbus.Dictionary({dbus.UInt32(1L): dbus.Struct((dbus.UInt32(2L), dbus.String(u'available'), dbus.String(u'')), signature=None)}, signature=dbus.Signature('u(uss)')),)
So, it looks like a one-value tuple with a dbus.Dictionary.

But I noticed that no events occur when the account is disabled, so maybe you also want to listen for this:

Code:

bus.add_signal_receiver(handle_presence, 'StatusChanged', 'org.freedesktop.Telepathy.Connection', None, '/org/freedesktop/Telepathy/Connection/gabble/jabber/username_40gmail_2ecom_2fMaemo')
Here's the output when I swap that with the other receiver call:

enabled:

Code:

status changed (dbus.UInt32(1L), dbus.UInt32(1L))
status changed (dbus.UInt32(0L), dbus.UInt32(1L))

disabled:

Code:

status changed (dbus.UInt32(2L), dbus.UInt32(1L))

xur17 2010-10-09 00:13

Re: Dbus Help
 
What variable type is args?

I receive the following when I do print args:

Code:

(dbus.Dictionary({dbus.UInt32(3L): dbus.Struct((dbus.UInt32(4L), dbus.String(u'xa'), dbus.String(u"I'm away from my computer")), signature=None)}, signature=dbus.Signature('u(uss)')),)
It appears that it is a structure inside of a dictionary. How do I set a variable to the value of the status ("I'm away from my computer")?

daperl 2010-10-09 01:17

Re: Dbus Help
 
I think all is answered by following nicolai's link and the dbus-python tutorial. According to the Data Types section, dbus.Dictionary is a sub-class of dict.

Remove the '*' from '*arg' and arg should just be a dbus.Dictionary.

xur17 2010-10-09 01:56

Re: Dbus Help
 
Quote:

Originally Posted by daperl (Post 836865)
I think all is answered by following nicolai's link and the dbus-python tutorial. According to the Data Types section, dbus.Dictionary is a sub-class of dict.

Remove the '*' from '*arg' and arg should just be a dbus.Dictionary.

I still don't understand exactly how to access the dictionary variable. Is a dbus.dictionary variable the same as a regular dictionary variable?

I have been playing around with my handle_presence function (here's what it looks like). Can you give me a little more help to get this to work?:

Code:

def handle_presence(dictionary):
        print type(dictionary)
        print dictionary.keys()
        for x in dictionary.keys():
                print x
                print type(dictionary[x])
                print dictionary[x]
        print "status changed"

I really appreciate your help.

xur17 2010-10-09 04:09

Re: Dbus Help
 
I ended up getting it to work by using the db2p function at the bottom of this page: http://telepathy.freedesktop.org/wik...rial%20Example

Then I searched for a particular string in the message (that I set).

I feel like there should be an easier / cleaner way to do this, but what I did worked for me.

xur17 2010-10-09 19:57

Re: Dbus Help
 
Does anyone know how to send a message to a user via a dbus command? It appears that I need to create a channel first, but I am at a loss on how to do this.

This is what I have so far, but I am stuck after that:

Code:

bus = dbus.SessionBus()
account = bus.get_object('org.freedesktop.Telepathy.AccountManager', '/org/freedesktop/Telepathy/Connection/theonering/gv/username_40gmail_2ecom/channel6')

Do I need to do something like this?:

Code:

account.Set('org.freedesktop.Telepathy.Channel.Interface.Messages', 'Send', dbus.Struct(( dbus.UInt32(0) ,  presence_text,  ""), signature='uss'), dbus_interface='org.freedesktop.DBus.Properties')
I am trying to use the example at the bottom of this page, but I am not sure how to adapt it to actually send a message to a user:

http://wiki.maemo.org/Adventures_with_D-Bus


All times are GMT. The time now is 06:10.

vBulletin® Version 3.8.8