[Mosquitto-users] python daemon - on_message callback not fired

2013-02-04 Thread Alexander Bolotnov
Hi,

I'm trying to write a little mosquitto python daemon that will run in
background and fire actions on messages. Code quoted below. My problem is
that on_message will not get hit. I've tried referencing both the Class
method and unbound method outside of Client class - no luck. Self-tests
message will happily get posted but not received. Can someone please direct
me towards correct usage?

#!/usr/bin/python
import mosquittofrom daemon import runnerfrom subprocess import
callimport config
class Client():

mc = None

def __init__(self):
self.stdin_path = '/dev/null'
self.stdout_path = 'Mosquitto.log'
self.stderr_path = '/dev/tty'
self.pidfile_path =  '/tmp/foo.pid'
self.pidfile_timeout = 5
print('Class initialization...')
if not Client.mc:
print('Creating an instance of MQ client...')
try:
Client.mc = mosquitto.Mosquitto(config.DEVICE_NAME)
Client.mc.connect(host = config.MQ_BROKER_ADDRESS)
print('Successfully created MQ client...')

print('Subscribing to topics...')
for topic in config.MQ_TOPICS:
result = Client.mc.subscribe(topic, 0)
print('Settings up callbacks...')
Client.mc.on_message = Client.on_message
print('Done setting up callbacks')

print('Sending self-test message...')
Client.send_message(config.MQ_TEST_TOPIC,config.MQ_TEST_MESSAGE)
print('Finished initialization...')

except Exception as e:
print('Failed creating MQ client: %s' % e.message)
@staticmethod
def run():
print('Entering running state...')

@staticmethod
def on_message(self, mosq, obj, msg):
call(['aplay','new.wav'])
print("Message received on topic "+msg.topic+" "+msg.payload)

@staticmethod
def send_message(topic, message):
res = Client.mc.publish(topic, message)
print(res)

app = Client()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()


And the config is (just in case):

#general settings
DEVICE_NAME = 'goof'
#MQ settings
MQ_BROKER_ADDRESS = '192.168.0.107'
MQ_TOPICS = [
'voice/all',
'command/all',
'voice/goof',
'command/goof',
'test']
MQ_TEST_TOPIC = 'test'
MQ_TEST_MESSAGE = 'This is a self-test message'


Sasha Bolotnov
www.bolotnov.info
-- 
Mailing list: https://launchpad.net/~mosquitto-users
Post to : mosquitto-users@lists.launchpad.net
Unsubscribe : https://launchpad.net/~mosquitto-users
More help   : https://help.launchpad.net/ListHelp


Re: [Mosquitto-users] python daemon - on_message callback not fired

2013-02-04 Thread Roger Light
Dear Alexander,

> My problem is that on_message will not get hit.

You aren't calling the network processing loop anywhere so only
outgoing communication will happen. You should use loop()
periodically, loop_start() once to run the network loop in a thread in
the background, or loop_forever() to call the network loop in a
blocking call. I guess that the latter is what you want without
knowing the details of the daemon module:

def run():
print('Entering running state...')
Client.mc.loop_forever()

Cheers,

Roger

-- 
Mailing list: https://launchpad.net/~mosquitto-users
Post to : mosquitto-users@lists.launchpad.net
Unsubscribe : https://launchpad.net/~mosquitto-users
More help   : https://help.launchpad.net/ListHelp