Skip to content

Commit

Permalink
WIP create result bldgs
Browse files Browse the repository at this point in the history
  • Loading branch information
dibaunaumh committed Jun 5, 2015
1 parent a571d8d commit f0f3e4b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
34 changes: 30 additions & 4 deletions fcs_aux/mies/residents/acting/flow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime
import random
from mies.buildings.constants import DEFAULT_BLDG_ENERGY
from mies.buildings.model import logging
from mies.celery import app
from mies.mongoconfig import get_db

Expand All @@ -17,6 +18,8 @@ def update_action_status(bldg, action_status):
"actions": actions
}
})
logging.info("Updated bldg {} action status: {}".format(
bldg["address"], action_status))


def add_new_action_status(bldg, action_status):
Expand All @@ -31,20 +34,43 @@ def add_new_action_status(bldg, action_status):
"actions": actions
}
})
logging.info("Added new action status to bldg {}: {}".format(
bldg["address"], action_status))


def update_bldg_processed_status(bldg, energy_change):
# TODO have a Bldg class & move the method there
curr_bldg_energy = bldg["energy"] or DEFAULT_BLDG_ENERGY
change = {
"processed": (energy_change < 0),
"energy": curr_bldg_energy + energy_change
}
db = get_db()
db.buildings.update({
"_id": bldg["_id"]
}, {
"$set": {
"processed": (energy_change < 0),
"energy": curr_bldg_energy + energy_change
}
"$set": change
})
logging.info("Updated bldg {} processed status: {}".format(
bldg["address"], change))


def update_bldg_with_results(bldg, content_type, payload):
# TODO have a Bldg class & move the method there
change = {}
if content_type and content_type != bldg["contentType"]:
change["contentType"] = content_type
if payload:
bldg["payload"].update(payload)
change["payload"] = bldg["payload"]

db = get_db()
db.buildings.update({
"_id": bldg["_id"]
}, {
"$set": change
})
logging.info("Updated bldg {} with results".format(bldg["address"]))


class ActingBehavior:
Expand Down
29 changes: 23 additions & 6 deletions fcs_aux/mies/residents/life/event.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
from collections import defaultdict
from celery.utils.log import get_task_logger
from mies.buildings.model import remove_occupant, add_occupant, load_bldg
from mies.buildings.model import remove_occupant, add_occupant, load_bldg, create_buildings
from mies.celery import app
from mies.residents.acting.flow import update_bldg_with_results
from mies.residents.model import Resident

logging = get_task_logger(__name__)


def create_result_bldgs(results):
bldgs_to_create = []
for r in results:
def create_result_bldgs(curr_bldg, action_results):
for r in action_results:
key = r.get("key")
content_type = r.get("content-type")
payload = r.get("payload")
placement_hints = r.get("placement_hints")

# TODO create bldgs
if placement_hints.get("new_bldg"):
flr = curr_bldg["flr"] # same_flr is the default
if placement_hints.get("flr_above"):
flr_number = int(curr_bldg["flr"][1:]) + 1
flr = "l{}".format(flr_number)
# same_location is the default
position_hints = {
"near_x": curr_bldg["x"],
"near_y": curr_bldg["y"],
}
create_buildings.s(content_type, [key], [payload], flr, position_hints).apply_async()

else:
# just update the current bldg
update_bldg_with_results(curr_bldg, content_type, payload)



@app.task(ignore_result=True)
Expand Down Expand Up @@ -59,7 +76,7 @@ def handle_life_event(resident):
return
else:
# yay, we have results
create_result_bldgs(action_result)
create_result_bldgs(curr_bldg, action_result)

# if we got here, it means that no action is still pending
resident.finish_processing(action_status, curr_bldg)
Expand Down

0 comments on commit f0f3e4b

Please sign in to comment.