MCPcopy Create free account
hub / github.com/c-jimenez/open-ocpp / gen_ocpp_message

Function gen_ocpp_message

tools/json2cpp/json2cpp.py:614–684  ·  view source on GitHub ↗

Generate the code corresponding to an OCPP message @param message: Name of the OCPP message @type message: string @param templates: Code templates @type templates: {string,string} @param params: Command line parameters @type params: Parame

(message, templates, params)

Source from the content-addressed store, hash-verified

612 type_impl.close()
613
614def gen_ocpp_message(message, templates, params) -> None:
615 '''
616 Generate the code corresponding to an OCPP message
617
618 @param message: Name of the OCPP message
619 @type message: string
620
621 @param templates: Code templates
622 @type templates: {string,string}
623
624 @param params: Command line parameters
625 @type params: Parameters
626 '''
627
628 # Message object
629 ocpp_version_suffix = params.ocpp_version[4:]
630
631 msg = Message()
632 msg.name = f"{message}"
633
634 # Parse input files
635 request = json.load(open(os.path.join(params.input_dir, f"{ocpp_message}Request.json")))
636 parse_message_contents(msg.request, request, ocpp_version_suffix)
637 response = json.load(open(os.path.join(params.input_dir, f"{ocpp_message}Response.json")))
638 parse_message_contents(msg.response, response, ocpp_version_suffix)
639
640 msg.id = request["$id"]
641 msg.comment = request["comment"]
642 msg.types = {f"{ocpp_message}Req": msg.request, f"{ocpp_message}Conf": msg.response}
643
644 # Required types
645 msg.requires = msg.request.requires
646 for required_type in msg.response.requires:
647 if not required_type in msg.requires:
648 msg.requires.append(required_type)
649
650 # Generate associated type
651 associated_types = {}
652 for type in msg.request.associated_types:
653 associated_types[type.name] = type
654 for type in msg.response.associated_types:
655 associated_types[type.name] = type
656 for associated_type in associated_types.values():
657 if associated_type.basic_type == "enum":
658 gen_ocpp_enum(msg, associated_type, templates, params, ocpp_version_suffix)
659 else:
660 gen_ocpp_type(msg, associated_type, associated_types, templates, params, ocpp_version_suffix)
661
662 # Generate message header file
663 msg_header_path = os.path.join(params.messages_dir, ocpp_message + ocpp_version_suffix + ".h")
664 msg_header = open(msg_header_path, "wt")
665
666 env = jinja2.Environment()
667 template = env.from_string(templates["msg_header"])
668 rendered_template = template.render(msg = msg, ocpp_version_namespace = params.ocpp_version, ocpp_version_suffix = ocpp_version_suffix)
669
670 msg_header.write(rendered_template)
671 msg_header.close()

Callers 1

json2cpp.pyFile · 0.85

Calls 8

MessageClass · 0.85
parse_message_contentsFunction · 0.85
gen_ocpp_enumFunction · 0.85
gen_ocpp_typeFunction · 0.85
appendMethod · 0.80
writeMethod · 0.80
closeMethod · 0.80
loadMethod · 0.45

Tested by

no test coverage detected