mobilityhouse / ocpp

Python implementation of the Open Charge Point Protocol (OCPP).
MIT License
782 stars 306 forks source link

Key name naming style for AnyType data #644

Closed m4su6747 closed 1 week ago

m4su6747 commented 4 months ago

Hello,

I tried to collect data using customData or DataTransfer.

Here is an example:

request = call.DataTransfer(
    vendor_id="my.vendor.id",
    message_id="dataCollection",
    data={
        "camelCaseKey": 1,
        "snake_case_key": 2,
        "SCREAMING_CASE_KEY": 3,
        "iso15118eMAID": "Desired key name:iso15118eMAID",
        "iso15118Emaid": "camelCase iso15118eMAID",
        "iso15118_emaid": "snake_case iso15118eMAID"
    }
)
response = await cp.call(request)

According to the document:

OCPP uses a camelCase naming scheme for the keys in the payload. Python, on the other hand, uses snake_case.

Therefore this ocpp package converts all keys in messages from camelCase to snake_case and vice versa to make sure you can write Pythonic code.

Since all the data I send will pass through the camelCase/snake_case conversion, the outgoing message will be:

INFO:ocpp:CP_001: send [2,"15dd2db7-6338-4d98-9de7-29f20a50894c","DataTransfer",{"vendorId":"eTreego","messageId":"dataCollection","data":{"camelCaseKey":1,"snakeCaseKey":2,"SCREAMINGCASEKEY":3,"iso15118eMAID":"Desired key name:iso15118eMAID","iso15118Emaid":"snake_case iso15118eMAID"}}]

And the received payload structure in python becomes:

{
    "vendor_id": "my.vendor.id",
    "message_id": "dataCollection",
    "data": {
        "camel_case_key": 1,
        "snake_case_key": 2,
        "screamingcasekey": 3,
        "iso15118e_maid": "Desired key name:iso15118eMAID",
        "iso15118_emaid": "snake_case iso15118eMAID",
    }
}

I'm wondering whether it should skip the conversion, or should OCPP be restricted to using camelCase key names even for AnyType data?

jainmohit2001 commented 1 week ago

Hi @m4su6747, I believe the conversion is required due to that fact that we can't enforce type hints (like Typescript) for AnyType data in python. Moreover, this conversion is a safe guard which allows developers to pass in the both snakeCase and camelCase keys without worrying too much about the OCPP format compliance.

m4su6747 commented 1 week ago

Thanks for repley But, with the conversion, we can only send key in camelCaseKey.

Here I'm trying to send "snake_case_key": 2, but it becomes "snakeCaseKey":2 when sending out. Receiver can only get camelCaseKey style key, not snake_case_key I desired. And also like SCREAMING_CASE_KEY or other style won't be adoptable for AnyType data with the conversion.