mosquitto-users team mailing list archive
-
mosquitto-users team
-
Mailing list archive
-
Message #00167
python daemon - on_message callback not fired
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
Follow ups