protoserver.app module#
- class protoserver.app.CommandMessageForm(*args, **kwargs)[source]#
Bases:
FlaskForm
FlaskForm for creating a Command Message.
- Variables:
message_id (
StringField
) – Unique identifier for the message.correlation_id (
StringField
) – Identifier for correlating messages.vehicle_id (
StringField
) – Identifier for the associated vehicle.type (
SelectField
) – Type of the message (e.g., command).priority (
StringField
) – Priority level of the message.provisioning_state (
SelectField
) – Provisioning state of the message.version (
StringField
) – Version of the message.packet_status (
SelectField
) – Status of the packet (e.g., Live).subtype (
SelectField
) – Subtype of the command message.
For Future Changes
This form assumes that the fields message_id, correlation_id, vehicle_id, type, priority, provisioning_state, version, packet_status, and subtype are present in future proto versions of CommandMessage as in version TMCVP 6.3. If any of these headers change, the code needs to be modified.
- protoserver.app.get_additional_fields()[source]#
A function to handle the
/get_additional_fields
route and return additional fields HTML based on the request data.This function is called on subtype dropdown change
131$('#{{ form.subtype.id }}').change( function() { 132var selectedSubtype = $(this).val(); 133var numEntries = $('#{{ form.numEntries.id }}').val(); 134 135$.ajax({ 136 url: '/get_additional_fields', 137 method: 'POST', 138 headers: { 139 'X-CSRFToken': csrf_token 140 }, 141 data: { 'subtype': selectedSubtype, 'numEntries': numEntries }, 142 success: function (response) { 143 $('#additionalFieldsContainer').html(response.additional_fields_html); 144 } 145 }); 146})
- protoserver.app.send_command()[source]#
Generate and send a command message based on the form data.
This function extracts the subtype from the form data and generates a command message using the
generate_command_message()
function. The generated message is then serialized and published to the appropriate MQTT topic.- Returns:
JSON object containing the response with status, command message as a dictionary, and the hex representation of the serialized message.
- Return type:
(str)
- protoserver.app.generateDynamicForm(payload)[source]#
Generate a dynamic form class based on the given payload.
Works almost same as
utils.fill_message()
.- Parameters:
payload (google.protobuf.message.Message) – The message fields lled into the form.
- Returns:
The generated form class.
- Return type:
DynamicForm (
FlaskForm
)
- protoserver.app.generate_command_message(subtype, form)[source]#
Generate a Command Message based on the provided subtype and form input.
This function uses
google.protobuf.json_format.ParseDict()
for parsing the form data, asrequest.form
is anImmutableMultiDict
object.- Parameters:
- Returns:
The generated CommandMessage.
- Return type:
tmcvp_command_message_pb2.CommandMessage
Works almost same as
commander.generate_command_message()
.
- protoserver.app.fill_payload(message, form)[source]#
Fills the payload message with data from the form.
Literally the same as
utils.fill_message()
Warning
Use
google.protobuf.json_format.ParseDict()
instead.
- protoserver.app.handle_mytopic(client, userdata, message)[source]#
Callback function for handling MQTT messages on a specific topic.
This function decodes the payload of the received MQTT message based on the topic and emits the decoded information to the connected clients through Socket.IO.
If the decoding fails, a warning message is emitted, and the payload is marked as “Parsing Failed” in the Socket.IO message.
The corresponding socket.io event is “mqtt_message” which is handled as
195const socket = io.connect('http://' + document.domain + ':' + location.port); 196socket.on('mqtt_message', function(data) { 197 showToast({ 198 header: 'MQTT Response received on', 199 data: data.topic 200 }); 201 $('#responseHex').html(data.messageHex); 202 $('#response').html(data.message); 203});
- protoserver.app.decode_response(rcvdMsg)[source]#
Decode command response received in the MQTT message.
- Parameters:
rcvdMsg (bytes) – The received MQTT message in bytes.
This is equivalent to
protoserver.commander.decode_response()
Caution
This function assumes that the fields
message_id
,correlation_id
,vehicle_id
,type
,subtype
,priority
,provisioning_state
,version
,time_stamp
,packet_status
andreturn_code
are present in future proto versions of CommandResponseMessage as in version TMCVP 6.3. If any of these fields change, the code needs to be modified. However, it is agnostic to changescommandResponsePayload
.
- protoserver.app.decode_telemetry(topic, rcvdMsg)[source]#
Decodes different types of telemetry messages received in MQTT Protobuf.
- protoserver.app.decode_vehicleevents(rcvdMsg)[source]#
Decodes different types of vehicle events messages received in MQTT Protobuf.
- Parameters:
rcvdMsg (bytes) – The received MQTT message in bytes.
- Returns:
A table representation of the vehicle events message.
- Return type:
(str)
Example:
def decode_vehicleevents(rcvdMsg): vehicle_event_message = tmcvp_vehicleevent_message_pb2.VehicleEventMessage() vehicle_event_message.ParseFromString(rcvdMsg) print(utils.MessageToTable(vehicle_event_message)) rcvdMsg = b"\n$2404c70e-41a6-4908-97d9-2e3c33073db7\x12\x02NA\x1a\x11ACCDEV14012076255 \x04(\x142\x0118\x02B\x056.3.0J\x08\x08\x98\xb6\xe3\xb0\x06\x106PLZ'\xaa\x01$\x08\x80\xa4\xa7\xda\x06\x10\x80\xa4\xa7\xda\x06\x18\xf7\xc1\xd7/ \xa0\xa7\xe5\x15(\x012\x08\x08\x98\xb6\xe3\xb0\x06\x10h8\x01" decode_vehicleevents(rcvdMsg)
- protoserver.app.TmcvpMQTTProtobufServer()[source]#
This function is for wsgi server that runs the app
Examples
Using Gunicorn
$ gunicorn -w 4 -b 0.0.0.0 'protoserver.app:TmcvpMQTTProtobufServer()' # or $ gunicorn -w 4 -b 0.0.0.0 tmcvp-server
Using Waitress
$ waitress-serve --call protoserver.app:TmcvpMQTTProtobufServer # or equivalently $ waitress-serve --call tmcvp-server
- Returns:
Flask application object
- Return type:
app (flask.Flask)