Skip to content
Snippets Groups Projects
Select Git revision
  • f193cae592e9edb7e4b81d01e17b76700a66155f
  • main default
2 results

planner.py

Blame
  • planner.py 2.24 KiB
    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    """
    Created on: 16/01/2023
    @author: Andoni Aranguren Ubierna
    -- Adaptations: 02/2023 @author: Sergio Campos
    
    """
    import time
    import datetime
    
    from src import utils, constants
    
    import random as rnd
    
    
    def plan_similar_plan_detail_shifting_end_actionnt(otp_parameters, intermediate_nodes, DDBB_params):
        original_place = otp_parameters["toPlace"].split(",")
        shifting = constants.DEFAULT_SHIFTING
        utils.get_best_order_plan_detail(otp_parameters, intermediate_nodes)
        for tries in range(0, constants.DEFAULT_MAX_TRIES_TO_RELOCATE_ENDING_actionNT):
            new_place = [str(float(original_place[0]) + shifting * rnd.randint(-10, 10)),
                         str(float(original_place[1]) + shifting * rnd.randint(-10, 10))]
            otp_parameters["toPlace"] = ",".join([new_place[0], new_place[1]])
            DDBB_params["to_lat"] = new_place[0]
            DDBB_params["to_lon"] = new_place[1]
            time.sleep(0.01)
            response, errors = utils.get_best_order_plan_detail(otp_parameters, intermediate_nodes)
            shifting *= constants.DEFAULT_SHIFTING_AMOUNT
            if response:
                return response, None
        return None, constants.ERROR_EXCEPTION
    
    
    def planner_plan_detail(cnx, request):
        # Generate the plan json structure
        plan_json = constants.plan_JSON_TEMPLATE
        plan_json["date"] = datetime.date.today().strftime("%Y-%m-%d")
    
        # Read all parameters to call otp and also so we can store things into DDBB
        otp_parameters, DDBB_params, intermediate_nodes, missing_params = utils.get_otp_ddbb_parameters(cnx, request.args)
        if missing_params:
            return [{"error": "Missing or incorrectly formatted params: " + missing_params}, 415]
    
        response, errors = utils.get_best_order_plan_detail(otp_parameters, intermediate_nodes)
        if errors and errors["error"]["msg"] == constants.DEFAULT_OTP_TOO_CLOSE:
            response, errors = plan_similar_plan_detail_shifting_end_actionnt(otp_parameters, intermediate_nodes, DDBB_params)
    
        if errors:
            return constants.ERROR_EXCEPTION
    
        # Include the DDBB parameters so we can later on save everything easily in the DDBB
        response["DDBBParameters"] = DDBB_params
        plan_json["plan_details"][DDBB_params["time_slot"]] = response
    
        return plan_json