TECHS-Technological-Solutions / ocpi

Python implementation of the Open Charge Point Interface (OCPI)
MIT License
49 stars 15 forks source link

Crud get, module command, get command_result #50

Closed andrewdubinyak closed 1 month ago

andrewdubinyak commented 1 year ago

Hello.

How to understand for what command I need to return "command_result" in the case when we have 2 Chargers on one Location with the same auth_token and two chargers send START_SESSION?

`async def send_command_result(response_url: str, command: CommandType, auth_token: str, crud: Crud, adapter: Adapter): client_auth_token = await crud.do(ModuleID.commands, RoleEnum.cpo, Action.get_client_token, auth_token=auth_token, version=VersionNumber.v_2_2_1)

for _ in range(150):  # check for 5 mins
    # since command has no id, 0 is used for id parameter of crud.get
    command_result = await crud.get(ModuleID.commands, RoleEnum.cpo, 0,
                                    auth_token=auth_token, version=VersionNumber.v_2_2_1, command=command)
    if command_result:
        break
    await sleep(2)

if not command_result:
    command_result = CommandResult(result=CommandResultType.failed)
else:
    command_result = adapter.command_result_adapter(command_result, VersionNumber.v_2_2_1)

async with httpx.AsyncClient() as client:
    authorization_token = f'Token {encode_string_base64(client_auth_token)}'
    await client.post(response_url, json=command_result.dict(), headers={'authorization': authorization_token})
andrewdubinyak commented 1 year ago

A good solution will be add to "modules/commands/v_2_2_1/api/cpo.py" "command_data: dict" and get from this command_data.response_url it will allow us in crud.get method getting command_result by "command_data"

async def send_command_result(command_data: dict, command: CommandType, auth_token: str, crud: Crud, adapter: Adapter):
    client_auth_token = await crud.do(ModuleID.commands, RoleEnum.cpo, Action.get_client_token,
                                      auth_token=auth_token, version=VersionNumber.v_2_2_1)

    for _ in range(150):  # check for 5 mins
        # since command has no id, 0 is used for id parameter of crud.get
        command_result = await crud.get(ModuleID.commands, RoleEnum.cpo, 0,
                                        auth_token=auth_token, version=VersionNumber.v_2_2_1, command=command, command_data=command_data)
        if command_result:
            break
        await sleep(2)

    if not command_result:
        command_result = CommandResult(result=CommandResultType.failed)
    else:
        command_result = adapter.command_result_adapter(command_result, VersionNumber.v_2_2_1)

    async with httpx.AsyncClient() as client:
        authorization_token = f'Token {client_auth_token}'
        await client.post(command_data.response_url, json=command_result.dict(), headers={'authorization': authorization_token})
@router.post("/{command}", response_model=OCPIResponse)
async def receive_command(request: Request, command: CommandType, data: dict, background_tasks: BackgroundTasks,
                          crud: Crud = Depends(get_crud), adapter: Adapter = Depends(get_adapter)):
    auth_token = get_auth_token(request)

    try:
        command_data = await apply_pydantic_schema(command, data)
    except ValidationError as exc:
        return JSONResponse(
            status_code=fastapistatus.HTTP_422_UNPROCESSABLE_ENTITY,
            content={'detail': jsonable_encoder(exc.errors())}
        )

    try:
        if hasattr(command_data, 'location_id'):
            await crud.get(ModuleID.locations, RoleEnum.cpo, command_data.location_id, auth_token=auth_token,
                           version=VersionNumber.v_2_2_1)

        command_response = await crud.do(ModuleID.commands, RoleEnum.cpo, Action.send_command, command_data.dict(),
                                         command=command, auth_token=auth_token, version=VersionNumber.v_2_2_1)

        background_tasks.add_task(send_command_result, command_data=command_data,
                                  command=command, auth_token=auth_token, crud=crud, adapter=adapter)

        return OCPIResponse(
            data=[adapter.command_response_adapter(command_response).dict()],
            **status.OCPI_1000_GENERIC_SUCESS_CODE,
        )