Skip to content

Commit

Permalink
Merge pull request #5059 from PokemonGoF/change_subscribe_to_on_conne…
Browse files Browse the repository at this point in the history
…ct_mqtt

Move subscribe into on_connect.
  • Loading branch information
solderzzc authored Sep 1, 2016
2 parents 79469dc + 67c3b85 commit 7f53d65
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions pokemongo_bot/event_handlers/social_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,25 @@ def __init__(self, bot, clientid=None):
self.bot.mqtt_pokemon_list = []
self._mqttc = None
def mqtt_on_connect(self, mqttc, obj, flags, rc):
if rc is 0:
self._mqttc.subscribe("pgo/#", 1)
if DEBUG_ON:
print "rc: "+str(rc)
def mqtt_on_message(self, mqttc, obj, msg):
#msg.topic+" "+str(msg.qos)+" "+str(msg.payload)]
pokemon = json.loads(msg.payload)
if DEBUG_ON:
print 'on message: {}'.format(pokemon)
print 'on message: {}'.format(msg.payload)
pokemon = json.loads(msg.payload)
if pokemon and 'encounter_id' in pokemon:
new_list = [x for x in self.bot.mqtt_pokemon_list if x['encounter_id'] is pokemon['encounter_id']]
if not (new_list and len(new_list) > 0):
self.bot.mqtt_pokemon_list.append(pokemon)
def on_disconnect(self,client, userdata, rc):
self._mqttc.unsubscribe("pgo/#")
if DEBUG_ON:
print 'on_disconnect'
if rc != 0:
print("Unexpected disconnection.")
print "Unexpected disconnection."
def mqtt_on_publish(self, mqttc, obj, mid):
if DEBUG_ON:
print "mid: "+str(mid)
Expand All @@ -48,17 +51,15 @@ def connect_to_mqtt(self):
if DEBUG_ON:
print 'connect again'
self._mqttc = mqtt.Client(None)
if self._mqttc:
self._mqttc.on_message = self.mqtt_on_message
self._mqttc.on_connect = self.mqtt_on_connect
self._mqttc.on_subscribe = self.mqtt_on_subscribe
self._mqttc.on_publish = self.mqtt_on_publish
self._mqttc.on_disconnect = self.on_disconnect
self._mqttc.on_message = self.mqtt_on_message
self._mqttc.on_connect = self.mqtt_on_connect
self._mqttc.on_subscribe = self.mqtt_on_subscribe
self._mqttc.on_publish = self.mqtt_on_publish
self._mqttc.on_disconnect = self.on_disconnect

self._mqttc.connect("broker.pikabot.org", 1883, 60)
# Enable this line if you are doing the snip code, off stress
self._mqttc.subscribe("pgo/#", 1)
# self._mqttc.loop_start()
self._mqttc.connect("broker.pikabot.org", 1883, 60)
# Enable this line if you are doing the snip code, off stress
# self._mqttc.loop_start()
except TypeError:
print 'Connect to mqtter error'
return
Expand Down

0 comments on commit 7f53d65

Please sign in to comment.