Python client - subclassing [message #1233523] |
Sun, 19 January 2014 17:29  |
Eclipse User |
|
|
|
The example that ships with the Python client does this:
class MyMQTTClass:
def __init__(self, clientid=None):
self._mqttc = mqtt.Client(clientid)
Before I saw the example I had developed a module that subclasses the client like:
class MqClient(client.Client):
def __init__(self, name):
client.Client.__init__(self, name)
Is there any reason why the subclassing method shown in the repo example is preferable to the one I'm using? Mine seems to work fine at this point.
The major difference is that the repo example creates an internal _mqttc object where my method subclasses the client
The (admittedly minor) advantage of my approach is that I can do:
mqClient = MqClient({'name':'test123', etc etc})
mqClient.subscribe(mytopic, myqos)
as opposed to the repo example requiring:
mqClient = MyClient('test123')
mqClient._mqtcc.subscribe(mytopic, myqos)
|
|
|
|
|
Re: Python client - subclassing [message #1718416 is a reply to message #1233523] |
Wed, 23 December 2015 08:47   |
Eclipse User |
|
|
|
Sorry for resurrecting an old thread.
Any advice on how to subclass the Client?
I'm having troubles with callbacks.
This does not work for example:
class MQTTClient(mqtt.Client):
def __init__(self, client_id='', clean_session=True, userdata=None, protocol=mqtt.MQTTv31):
super(MQTTClient, self).__init__(client_id, clean_session, userdata, protocol)
self.on_message = self.on_message_cb
def on_message_cb(self, userdata, message):
print mesage.payload
The best thing would be if on_message would not be defined in __init__ but it would be an emptu funtion in mqtt.Client. Then you would just redefine the function in your own class.
|
|
|
|
Powered by
FUDForum. Page generated in 0.04467 seconds