import paho.mqtt.client as PahoMQTT
import time
import json

class Subscriber:
        def __init__(self, clientID,broker,topic,myOnMessageReceived):
            self.clientID = clientID
            # create an instance of paho.mqtt.client
            self._paho_mqtt = PahoMQTT.Client(clientID, False) 

            # register the callback
            self._paho_mqtt.on_connect = self.myOnConnect
            self._paho_mqtt.on_message = myOnMessageReceived

            self.topic = topic
            #self.messageBroker = 'mqtt.eclipse.org'
            self.messageBroker = broker[0]
            self.port=broker[1]



        def start (self):
            #manage connection to broker
            self._paho_mqtt.connect(self.messageBroker, self.port)
            self._paho_mqtt.loop_start()
            # subscribe for a topic
            self._paho_mqtt.subscribe(self.topic, 2)

        def stop (self):
            self._paho_mqtt.unsubscribe(self.topic)
            self._paho_mqtt.loop_stop()
            self._paho_mqtt.disconnect()

        def myOnConnect (self, paho_mqtt, userdata, flags, rc):
                pass
            #print ("Connected to %s with result code: %d" % (self.messageBroker, rc))

       
            

