Skip to content

Commit

Permalink
Allow search to be toggled on/off from the UI (PokemonGoF#2756)
Browse files Browse the repository at this point in the history
  • Loading branch information
chuyskywalker authored Jul 31, 2016
1 parent c360bec commit 7fd5fde
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 12 deletions.
31 changes: 27 additions & 4 deletions pogom/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,42 @@ def __init__(self, import_name, **kwargs):
self.route("/loc", methods=['GET'])(self.loc)
self.route("/next_loc", methods=['POST'])(self.next_loc)
self.route("/mobile", methods=['GET'])(self.list_pokemon)
self.route("/search_control", methods=['GET'])(self.get_search_control)
self.route("/search_control", methods=['POST'])(self.post_search_control)

def set_search_control(self, control):
self.search_control = control

def get_search_control(self):
return jsonify({'status': self.search_control.is_set()})

def post_search_control(self):
args = get_args()
if not args.search_control:
return 'Search control is disabled', 403
action = request.args.get('action','none')
if action == 'on':
self.search_control.set()
log.info('Search thread resumed')
elif action == 'off':
self.search_control.clear()
log.info('Search thread paused')
else:
return jsonify({'message':'invalid use of api'})
return self.get_search_control()

def fullmap(self):
args = get_args()
display = "inline"
if args.fixed_location:
display = "none"
fixed_display = "none" if args.fixed_location else "inline"
search_display = "inline" if args.search_control else "none"

return render_template('map.html',
lat=config['ORIGINAL_LATITUDE'],
lng=config['ORIGINAL_LONGITUDE'],
gmaps_key=config['GMAPS_KEY'],
lang=config['LOCALE'],
is_fixed=display
is_fixed=fixed_display,
search_control=search_display
)

def raw_data(self):
Expand Down
13 changes: 8 additions & 5 deletions pogom/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,23 +125,26 @@ def login(args, position):
#
# Search Threads Logic
#
def create_search_threads(num):
def create_search_threads(num, search_control):
search_threads = []
for i in range(num):
t = Thread(target=search_thread, name='search_thread-{}'.format(i), args=(search_queue,))
t = Thread(target=search_thread, name='search_thread-{}'.format(i), args=(search_queue,search_control,))
t.daemon = True
t.start()
search_threads.append(t)


def search_thread(q):
def search_thread(q, search_control):
threadname = threading.currentThread().getName()
log.debug("Search thread {}: started and waiting".format(threadname))
while True:

# Get the next item off the queue (this blocks till there is something)
i, step_location, step, lock = q.get()

# Pause if searching is disabled
search_control.wait()

# If a new location has been set, just mark done and continue
if 'NEXT_LOCATION' in config:
log.debug("{}: new location waiting, flushing queue".format(threadname))
Expand Down Expand Up @@ -180,9 +183,9 @@ def search_thread(q):
#
# Search Overseer
#
def search_loop(args):
def search_loop(args, search_control):
i = 0
while True:
while search_control.wait():
log.info("Search loop {} starting".format(i))
try:
search(args, i)
Expand Down
3 changes: 3 additions & 0 deletions pogom/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ def get_args():
parser.add_argument('-os', '--only-server',
help='Server-Only Mode. Starts only the Webserver without the searcher.',
action='store_true', default=False)
parser.add_argument('-nsc','--no-search-control',
help='Disables search control',
action='store_false', dest='search_control', default=True)
parser.add_argument('-fl', '--fixed-location',
help='Hides the search bar for use in shared maps.',
action='store_true', default=False)
Expand Down
12 changes: 9 additions & 3 deletions runserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
logging.basicConfig(format='%(asctime)s [%(threadName)14s][%(module)14s] [%(levelname)7s] %(message)s')
log = logging.getLogger()

from threading import Thread
from threading import Thread, Event
from flask_cors import CORS

from pogom import config
Expand Down Expand Up @@ -83,12 +83,16 @@
os.remove(args.db)
create_tables(db)

# Control the search status (running or not) across threads; set it "on"
search_control = Event()
search_control.set()

if not args.only_server:
# Gather the pokemons!
if not args.mock:
log.debug('Starting a real search thread and {} search runner thread(s)'.format(args.num_threads))
create_search_threads(args.num_threads)
search_thread = Thread(target=search_loop, args=(args,))
create_search_threads(args.num_threads, search_control)
search_thread = Thread(target=search_loop, args=(args,search_control,))
else:
log.debug('Starting a fake search thread')
insert_mock_data()
Expand All @@ -101,6 +105,8 @@
if args.cors:
CORS(app);

app.set_search_control(search_control)

config['ROOT_PATH'] = app.root_path
config['GMAPS_KEY'] = args.gmaps_key
config['REQ_SLEEP'] = args.scan_delay
Expand Down
18 changes: 18 additions & 0 deletions static/js/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,16 @@ function createSearchMarker() {
return marker;
}

var searchControlURI = 'search_control';
function searchControl(action) {
$.post(searchControlURI + '?action='+encodeURIComponent(action));
}
function updateSearchStatus() {
$.getJSON(searchControlURI).then(function(data){
$('#search-switch').prop('checked', data.status);
})
}

function initSidebar() {
$('#gyms-switch').prop('checked', Store.get('showGyms'));
$('#pokemon-switch').prop('checked', Store.get('showPokemon'));
Expand All @@ -343,6 +353,9 @@ function initSidebar() {
var searchBox = new google.maps.places.SearchBox(document.getElementById('next-location'));
$("#next-location").css("background-color", $('#geoloc-switch').prop('checked') ? "#e0e0e0" : "#ffffff");

updateSearchStatus();
setInterval(updateSearchStatus,5000);

searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();

Expand Down Expand Up @@ -1336,4 +1349,9 @@ $(function() {
else
Store.set('geoLocate', this.checked);
});

$('#search-switch').change(function() {
searchControl(this.checked?'on':'off');
});

});
11 changes: 11 additions & 0 deletions templates/map.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ <h3>Follow location</h3>
</div>
</div>

<div class="form-control switch-container" style="display:{{search_control}}">
<h3>Search</h3>
<div class="onoffswitch">
<input id="search-switch" type="checkbox" name="search-switch" class="onoffswitch-checkbox" checked/>
<label class="onoffswitch-label" for="search-switch">
<span class="switch-label" data-on="On" data-off="Off"></span>
<span class="switch-handle"></span>
</label>
</div>
</div>

<hr width="100%" style="display:{{is_fixed}}" >

<p>Select markers to be visible on map.</p>
Expand Down

0 comments on commit 7fd5fde

Please sign in to comment.