From 4c324adb945e0e37259b09618be089feec5e5ea3 Mon Sep 17 00:00:00 2001 From: Simba Zhang Date: Tue, 9 Aug 2016 23:26:05 -0700 Subject: [PATCH 1/2] Allow social event exchange anonymous betweed clients, MQTT broker will hide users' ip and personal information. --- configs/config.json.example | 1 + pokecli.py | 8 ++++ pokemongo_bot/__init__.py | 5 ++- pokemongo_bot/event_handlers/__init__.py | 1 + .../event_handlers/social_handler.py | 45 +++++++++++++++++++ requirements.txt | 1 + 6 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 pokemongo_bot/event_handlers/social_handler.py diff --git a/configs/config.json.example b/configs/config.json.example index 08c844218c..3ef5362fc7 100644 --- a/configs/config.json.example +++ b/configs/config.json.example @@ -5,6 +5,7 @@ "location": "SOME_LOCATION", "gmapkey": "GOOGLE_MAPS_API_KEY", "libencrypt_location": "", + "enable_social": false, "tasks": [ { "type": "HandleSoftBan" diff --git a/pokecli.py b/pokecli.py index f59ae3acb9..663e9583c4 100644 --- a/pokecli.py +++ b/pokecli.py @@ -403,6 +403,14 @@ def _json_loader(filename): type=bool, default=True ) + add_config( + parser, + load, + long_flag="--enable_social", + help="Enable social event exchange between bot", + type=bool, + default=True + ) # Start to parse other attrs config = parser.parse_args() diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index 31e7eb0bd1..9c007035a9 100644 --- a/pokemongo_bot/__init__.py +++ b/pokemongo_bot/__init__.py @@ -25,7 +25,7 @@ from human_behaviour import sleep from item_list import Item from metrics import Metrics -from pokemongo_bot.event_handlers import LoggingHandler, SocketIoHandler, ColoredLoggingHandler +from pokemongo_bot.event_handlers import LoggingHandler, SocketIoHandler, ColoredLoggingHandler, SocialHandler from pokemongo_bot.socketio_server.runner import SocketIoRunner from pokemongo_bot.websocket_remote_control import WebsocketRemoteControl from pokemongo_bot.base_dir import _base_dir @@ -93,7 +93,8 @@ def _setup_event_system(self): handlers.append(ColoredLoggingHandler()) else: handlers.append(LoggingHandler()) - + if self.config.enable_social: + handlers.append(SocialHandler()) if self.config.websocket_server_url: if self.config.websocket_start_embedded_server: self.sio_runner = SocketIoRunner(self.config.websocket_server_url) diff --git a/pokemongo_bot/event_handlers/__init__.py b/pokemongo_bot/event_handlers/__init__.py index a4dd6fce7f..68c2636b12 100644 --- a/pokemongo_bot/event_handlers/__init__.py +++ b/pokemongo_bot/event_handlers/__init__.py @@ -1,3 +1,4 @@ from logging_handler import LoggingHandler from socketio_handler import SocketIoHandler from colored_logging_handler import ColoredLoggingHandler +from social_handler import SocialHandler diff --git a/pokemongo_bot/event_handlers/social_handler.py b/pokemongo_bot/event_handlers/social_handler.py new file mode 100644 index 0000000000..238e2ad4c7 --- /dev/null +++ b/pokemongo_bot/event_handlers/social_handler.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from pokemongo_bot.event_manager import EventHandler +import thread +import paho.mqtt.client as mqtt +class MyMQTTClass: + def __init__(self, clientid=None): + self._mqttc = mqtt.Client(clientid) + self._mqttc.on_message = self.mqtt_on_message + #self._mqttc.on_connect = self.mqtt_on_connect + #self._mqttc.on_publish = self.mqtt_on_publish + #self._mqttc.on_subscribe = self.mqtt_on_subscribe + #def mqtt_on_connect(self, mqttc, obj, flags, rc): + #print "rc: "+str(rc) + def mqtt_on_message(self, mqttc, obj, msg): + print msg.topic+" "+str(msg.qos)+" "+str(msg.payload) + #def mqtt_on_publish(self, mqttc, obj, mid): + #print "mid: "+str(mid) + #def mqtt_on_subscribe(self, mqttc, obj, mid, granted_qos): + # print "Subscribed: "+str(mid)+" "+str(granted_qos) + #def mqtt_on_log(self, mqttc, obj, level, string): + # print string + def publish(self, channel, message): + self._mqttc.publish(channel, message) + def connect_to_mqtt(self): + self._mqttc.connect("test.mosca.io", 1883, 60) + self._mqttc.subscribe("$GOF/Social/#", 0) + def run(self): + self._mqttc.loop_forever() +class SocialHandler(EventHandler): + def __init__(self): + self.mqttc = MyMQTTClass() + self.mqttc.connect_to_mqtt() + thread.start_new_thread(self.mqttc.run) + def handle_event(self, event, sender, level, formatted_msg, data): + #sender_name = type(sender).__name__ + if formatted_msg: + message = "[{}] {}".format(event, formatted_msg) + else: + message = '{}: {}'.format(event, str(data)) + if event == 'catchable_pokemon': + self.mqttc.publish("$GOF/Social/Catchable", message) + #print 'have catchable_pokemon' + #print message diff --git a/requirements.txt b/requirements.txt index 76b1d15a7b..af996ef87f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -22,3 +22,4 @@ mock==2.0.0 timeout-decorator==0.3.2 raven==5.23.0 demjson==2.2.4 +paho-mqtt==1.2 From 53c9ff720c5fd0f2cd438bd2c016c9be6ce7060a Mon Sep 17 00:00:00 2001 From: Simba Zhang Date: Wed, 10 Aug 2016 23:44:25 -0700 Subject: [PATCH 2/2] Added sub message channel for pokemon id. --- pokemongo_bot/__init__.py | 2 +- pokemongo_bot/event_handlers/social_handler.py | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index 9c007035a9..6e7d59eb30 100644 --- a/pokemongo_bot/__init__.py +++ b/pokemongo_bot/__init__.py @@ -94,7 +94,7 @@ def _setup_event_system(self): else: handlers.append(LoggingHandler()) if self.config.enable_social: - handlers.append(SocialHandler()) + handlers.append(SocialHandler(self)) if self.config.websocket_server_url: if self.config.websocket_start_embedded_server: self.sio_runner = SocketIoRunner(self.config.websocket_server_url) diff --git a/pokemongo_bot/event_handlers/social_handler.py b/pokemongo_bot/event_handlers/social_handler.py index 238e2ad4c7..45f3d1da12 100644 --- a/pokemongo_bot/event_handlers/social_handler.py +++ b/pokemongo_bot/event_handlers/social_handler.py @@ -1,6 +1,4 @@ # -*- coding: utf-8 -*- -from __future__ import unicode_literals - from pokemongo_bot.event_manager import EventHandler import thread import paho.mqtt.client as mqtt @@ -25,11 +23,12 @@ def publish(self, channel, message): self._mqttc.publish(channel, message) def connect_to_mqtt(self): self._mqttc.connect("test.mosca.io", 1883, 60) - self._mqttc.subscribe("$GOF/Social/#", 0) + self._mqttc.subscribe("$GOF/All/#", 0) def run(self): self._mqttc.loop_forever() class SocialHandler(EventHandler): - def __init__(self): + def __init__(self, bot): + self.bot = bot self.mqttc = MyMQTTClass() self.mqttc.connect_to_mqtt() thread.start_new_thread(self.mqttc.run) @@ -40,6 +39,9 @@ def handle_event(self, event, sender, level, formatted_msg, data): else: message = '{}: {}'.format(event, str(data)) if event == 'catchable_pokemon': - self.mqttc.publish("$GOF/Social/Catchable", message) + self.mqttc.publish("$GOF/All/Catchable", str(data)) + print data + if data['pokemon_id']: + self.mqttc.publish("$GOF/PokeID/"+str(data['pokemon_id']), str(data)) #print 'have catchable_pokemon' - #print message + print message