Skip to content

Commit

Permalink
Allow social event exchange anonymous betweed clients, MQTT broker wi…
Browse files Browse the repository at this point in the history
…ll hide users' ip and personal information.
  • Loading branch information
solderzzc committed Aug 10, 2016
1 parent 8b5af8d commit 4c324ad
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions configs/config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"location": "SOME_LOCATION",
"gmapkey": "GOOGLE_MAPS_API_KEY",
"libencrypt_location": "",
"enable_social": false,
"tasks": [
{
"type": "HandleSoftBan"
Expand Down
8 changes: 8 additions & 0 deletions pokecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions pokemongo_bot/event_handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions pokemongo_bot/event_handlers/social_handler.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

1 comment on commit 4c324ad

@perduta
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such a nice idea and great code! Thank you for doing so much good to open source!

Please sign in to comment.