Working with multiple calls to the loop method

The following lines declare the process_incoming_commands method that is part of the VehicleCommandProcessor class. You have to add these lines to the existing vehicle_mqtt_client.py Python file. The code file for the sample is included in the mqtt_python_gaston_hillar_04_01 folder, in the vehicle_mqtt_client.py file:

    def process_incoming_commands(self):
self.client.loop()

The process_incoming_commands method calls the loop method for the MQTT client and ensures communication with the MQTT server is carried out. Think about the call to the loop method as synchronizing your mailbox. Any pending messages to be published in the outgoing box will be sent, any incoming messages will arrive to the inbox, and the events that we have previously analyzed will be fired. This way, the vehicle command processor will receive messages and process commands.

Finally, the following lines declare the main block of code. You have to add these lines to the existing vehicle_mqtt_client.py Python file. The code file for the sample is included in the mqtt_python_gaston_hillar_04_01 folder, in the vehicle_mqtt_client.py file:

if __name__ == "__main__":
vehicle = Vehicle("vehiclepi01")
vehicle_command_processor = VehicleCommandProcessor("vehiclepi01",
vehicle)
while True:
# Process messages and the commands every 1 second
vehicle_command_processor.process_incoming_commands()
time.sleep(1)

The __main__ method creates an instance of the Vehicle class, named vehicle, with "vehiclepi01" as the value for the name argument. The next line creates an instance of the VehicleCommandProcessor class, named vehicle_command_processor, with "vehiclepi01" and the previously created Vehicle instance, X, as the values for the name and vehicle arguments. This way, vehicle_command_processor will delegate the execution of the commands to instance methods in vehicle.

The constructor for the VehicleCommandProcessor class will subscribe to the vehicles/vehiclepi01/commands topic in the MQTT server, and therefore, we must publish messages to this topic in order to send the commands that the code will process. Whenever a command is successfully processed, new messages will be published to the vehicles/vehiclepi01/executedcommands topic. Hence, we must subscribe to this topic to check the commands that the vehicle has executed.

The while loop calls the vehicle_command_processor.process_commands method and sleeps for one second. The process_commands method calls the loop method for the MQTT client and ensures communication with the MQTT server is carried out.

There is also a threaded interface that we can run by calling the loop_start method for the MQTT client. This way, we can avoid multiple calls to the loop method. However, we call the loop method to make it easier to debug the code and understand how everything works under the hood. We will work with the threaded interfaces in Chapter 5Testing and Improving our Vehicle Control Solution in Python.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.2.15