smash some bugs
parent
26c3217275
commit
d8b5bc430f
64
server.py
64
server.py
|
@ -13,10 +13,15 @@ import pika
|
||||||
from params import rmq_params, socket_params
|
from params import rmq_params, socket_params
|
||||||
|
|
||||||
def checkpoint(message):
|
def checkpoint(message):
|
||||||
"""Prints [CHeckpoint] <message>
|
"""Prints [Checkpoint] <message>
|
||||||
"""
|
"""
|
||||||
print("[Checkpoint] {}".format(message))
|
print("[Checkpoint] {}".format(message))
|
||||||
|
|
||||||
|
def error(message):
|
||||||
|
"""Prints [ERROR] <message>
|
||||||
|
"""
|
||||||
|
print("[ERROR] {}".format(message))
|
||||||
|
|
||||||
def get_info(id):
|
def get_info(id):
|
||||||
"""Looks up all data for an id
|
"""Looks up all data for an id
|
||||||
"""
|
"""
|
||||||
|
@ -24,7 +29,7 @@ def get_info(id):
|
||||||
|
|
||||||
# Connect to mongodb & return user
|
# Connect to mongodb & return user
|
||||||
collection = pymongo.MongoClient().group23.bac_monitoring
|
collection = pymongo.MongoClient().group23.bac_monitoring
|
||||||
return collection.find({'id': 123456})[0]
|
return collection.find({'id': id})[0]
|
||||||
|
|
||||||
def add_drink(id):
|
def add_drink(id):
|
||||||
"""Adds 1 drink to the user
|
"""Adds 1 drink to the user
|
||||||
|
@ -66,13 +71,33 @@ def allowed_drinks(id):
|
||||||
"""
|
"""
|
||||||
## Calculate current BAC
|
## Calculate current BAC
|
||||||
# Lookup BAC for 1 drink
|
# Lookup BAC for 1 drink
|
||||||
|
|
||||||
|
# Account for database errors
|
||||||
|
try:
|
||||||
user_info = get_info(id)
|
user_info = get_info(id)
|
||||||
bac_lookup = {'M': {'100': .06, '120': .05, '140': .045, '160': .04,
|
except Exception as ex:
|
||||||
'180': .035, '200': .03, '220': .033, '240': .025},
|
error('User not found in database')
|
||||||
'F': {'100': .07, '120': .06, '140': .05, '160': .04,
|
return 0, 0
|
||||||
'180': .04, '200': .035, '220': .03, '240': .03}}
|
|
||||||
# Lookup gender & rounded weight
|
bac_lookup = {'M': {'80': .07, '100': .06, '120': .05, '140': .045,
|
||||||
|
'160': .04, '180': .035, '200': .03, '220': .033,
|
||||||
|
'240': .025, '260': .02, '280': .015, '300': .01,
|
||||||
|
'320': .005, '340': .003, '360': .002, '380': .001},
|
||||||
|
'F': {'80': .08, '100': .07, '120': .06, '140': .05,
|
||||||
|
'160': .04, '180': .04, '200': .035, '220': .03,
|
||||||
|
'240': .03, '260': .025, '280': .02, '300': .015,
|
||||||
|
'320': .01, '340': .003, '360': .002, '380': .001}}
|
||||||
|
# Round weight to the nearest increment of 20
|
||||||
weight = int(20 * round(float(user_info['weight']) / 20))
|
weight = int(20 * round(float(user_info['weight']) / 20))
|
||||||
|
|
||||||
|
# Lookup gender & rounded weight
|
||||||
|
if weight < 80:
|
||||||
|
checkpoint('Weight too low')
|
||||||
|
return 0, 9999
|
||||||
|
elif weight > 380:
|
||||||
|
checkpoint('ERROR: Weight too high. Using .001 for one drink.')
|
||||||
|
one_drink = .001
|
||||||
|
else:
|
||||||
one_drink = bac_lookup[user_info['gender']][str(weight)]
|
one_drink = bac_lookup[user_info['gender']][str(weight)]
|
||||||
|
|
||||||
# Add up BAC for each drink
|
# Add up BAC for each drink
|
||||||
|
@ -92,6 +117,9 @@ def allowed_drinks(id):
|
||||||
else:
|
else:
|
||||||
bac += drink_bac
|
bac += drink_bac
|
||||||
|
|
||||||
|
# Round to 2 places
|
||||||
|
bac = round(bac, 2)
|
||||||
|
|
||||||
# Debugging
|
# Debugging
|
||||||
checkpoint("BAC for \'{}\': {}".format(id, bac))
|
checkpoint("BAC for \'{}\': {}".format(id, bac))
|
||||||
|
|
||||||
|
@ -117,15 +145,23 @@ def order_callback(ch, method, properties, body):
|
||||||
body = body.decode('utf-8')
|
body = body.decode('utf-8')
|
||||||
client_params = ast.literal_eval(body)
|
client_params = ast.literal_eval(body)
|
||||||
|
|
||||||
# Extract things from client data
|
# Try to get host & port
|
||||||
id = int(client_params.get('id'))
|
try:
|
||||||
host = client_params.get('ip')
|
host = client_params.get('ip')
|
||||||
port = int(client_params.get('port'))
|
port = int(client_params.get('port'))
|
||||||
|
except Exception as ex:
|
||||||
|
error(ex)
|
||||||
|
return
|
||||||
|
|
||||||
|
# Try to get id
|
||||||
|
try:
|
||||||
|
id = int(client_params.get('id'))
|
||||||
|
# Add drink for user
|
||||||
add_drink(id)
|
add_drink(id)
|
||||||
drinks, time = allowed_drinks(id)
|
drinks, time = allowed_drinks(id)
|
||||||
|
except Exception as ex:
|
||||||
# Add drink for user
|
error(ex)
|
||||||
|
drinks, time = 0, 0
|
||||||
|
|
||||||
# Try to send response to client
|
# Try to send response to client
|
||||||
try:
|
try:
|
||||||
|
@ -138,7 +174,7 @@ def order_callback(ch, method, properties, body):
|
||||||
s.send(pickle.dumps((drinks, time)))
|
s.send(pickle.dumps((drinks, time)))
|
||||||
s.close()
|
s.close()
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
print(ex)
|
error(ex)
|
||||||
|
|
||||||
def reject_callback(ch, method, properties, body):
|
def reject_callback(ch, method, properties, body):
|
||||||
"""Remove most recent drink for user
|
"""Remove most recent drink for user
|
||||||
|
@ -154,8 +190,8 @@ def reject_callback(ch, method, properties, body):
|
||||||
def main():
|
def main():
|
||||||
"""Create & start 'drinks' queue for submitting drinks
|
"""Create & start 'drinks' queue for submitting drinks
|
||||||
"""
|
"""
|
||||||
# Temp TODO
|
# Remove drinks before we start
|
||||||
remove_drinks(123456)
|
remove_drinks()
|
||||||
|
|
||||||
# Connect to RMQ
|
# Connect to RMQ
|
||||||
credentials = pika.PlainCredentials(rmq_params['username'], rmq_params['password'])
|
credentials = pika.PlainCredentials(rmq_params['username'], rmq_params['password'])
|
||||||
|
|
|
@ -11,13 +11,13 @@ def checkpoint(message):
|
||||||
def main():
|
def main():
|
||||||
"""Adds new user to mongodb
|
"""Adds new user to mongodb
|
||||||
"""
|
"""
|
||||||
id = 123456
|
id = 444444
|
||||||
weight = 150
|
weight = 240
|
||||||
gender = 'F'
|
gender = 'M'
|
||||||
|
|
||||||
# Connect to mongodb & add user
|
# Connect to mongodb & add user
|
||||||
collection = pymongo.MongoClient().group23.bac_monitoring
|
collection = pymongo.MongoClient().group23.bac_monitoring
|
||||||
collection.update({'id': 123456}, {'$set': {'weight': weight, 'gender': gender}}, upsert=True)
|
collection.update({'id': id}, {'$set': {'weight': weight, 'gender': gender}}, upsert=True)
|
||||||
checkpoint("Added user \'{}\' with weight {} and gender {}"
|
checkpoint("Added user \'{}\' with weight {} and gender {}"
|
||||||
.format(id, weight, gender))
|
.format(id, weight, gender))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue