OTA-Docker architecture design & source code

Architecture design

The main task of a Docker container: run the required application

Docker container mapping file directory: complete data sharing with local files, OTA-Client controls updates in the shared file directory and restarts the container

Finally, the SOTA application upgrade based on Docker is realized.

The design ideas of the overall architecture are as follows

  1. The terminal needs an OTA-Client, and the cloud needs an OTA-Server;
  2. The cloud OTA-Server is responsible for uploading new files, and the terminal's OTA-Client is responsible for subscribing to OTA-Server's updated files;
  3. The OTA-Client of the terminal is responsible for receiving the update file and placing the update file in the mapping directory (the shared data directory between the terminal and the container);
  4. After the OTA-Client of the terminal receives the update file, it detects whether there is a container running in the current image. If there is no container running, it binds the mapping directory to start the container and runs the startup script by itself; if there is a container running, it closes the currently running container. Then bind the mapping directory to start the container and run the startup script automatically;
  5. When the terminal's OTA-Client completes the restart of the container, the container application update is completed.

OTA protocol selection

OTA (Over-The-Air) upgrade is a technology that realizes remote device upgrade through wireless network. The specific implementation needs to follow certain protocol standards. MQTT and HTTP OTA are both commonly used OTA upgrade protocols.
The following will analyze and compare these two protocols in detail from the following aspects:

Transfer Protocol MQTT uses the MQTT protocol, while HTTP uses the HTTP protocol. The MQTT protocol is a lightweight message transmission protocol suitable for data transmission in low-bandwidth, high-latency, and unreliable network environments. The HTTP protocol is a protocol based on the request-response model and is used to transmit data on the Web. In terms of transmission efficiency, MQTT has higher data transmission efficiency than HTTP due to its lightweight transmission protocol.
Applicable scene MQTT is suitable for scenarios that require fast response and high real-time requirements, such as the Internet of Vehicles. The advantage of the MQTT protocol is that it can significantly reduce communication volume, reduce communication delays, and achieve rapid response. HTTP is more suitable for upgrades in scenarios where the network environment is relatively stable.
safety MQTT can improve the security of the upgrade process through encryption and authentication, while HTTP may have security issues during the transmission process, requiring additional security measures. For example, HTTPS can provide data encryption and authentication capabilities, making it more secure.
How OTA is implemented MQTT and HTTP are implemented slightly differently. MQTT usually uses the MQTT message queue as the transmission channel for upgrade packages, and the device can subscribe to the upgrade package from the MQTT Broker. HTTP usually uses an HTTP server as the source of the upgrade package, and the device can download the upgrade package through HTTP requests.
     综上所述,MQTT 和HTTP 各有优缺点,具体应用需要根据场景和要求进行选择。如果要求传输效率高、响应速度快、安全性好,则可以选择MQTT ;如果网络环境相对稳定,可以选择HTTP OTA。在本文中,将MQTT 和HTTP 结合使用,从而兼顾两者的优点,MQTT 用于消息的发布和订阅,HTTP 用于web文件处理和传输。

MQTT service construction

MQTT is suitable for scenarios that require fast response and high real-time requirements, such as the Internet of Vehicles.

Mainly used for message publishing and subscription.

MQTT Broker setup

 sudo apt-get update
 sudo apt-get install mosquitto
 pip install paho-mqtt

View MQTT services
systemctl status mosquitto.service 

Insert image description here

View MQTT service details
mosquitto -v

Insert image description here
Insert image description here

Communication test

Mosquitto-clients needs to be installed on the client or local machine

sudo apt-get install mosquitto-clients
mosquitto_pub -h 30.178.38.62 -t "wp/test" -m "l want the world\n" -u ellison -P 1
mosquitto_sub -h 30.178.38.62 -t "wp/test" -u ellison -P 1

Insert image description here

HTTP service establishment

Open source code: https://github.com/weipengyiyu/OTA-Docker

  • The OTA-Client of this machine receives the update file sent by the server, detects whether the file exists on the current machine, deletes the local file if it exists, and then updates the new file; if it does not exist, updates the new file in the meantime;
  • Upload file compression package files or file directories and files on OTA-Server;
  • Start an OTA-Client on each terminal, start the OTA-Server on the PC, and use the distributed-publish-subscribe method to upgrade.

Server

IP, username and password need to be configured for client connection

"mosquitto_pub -h 30.178.38.62 -t 'wp/test' -m 'update file' -u ellison -P 1"

Perform the following operations to enter the HTTP server

python3 ota_server.py

Enter the local browser on the server: http://127.0.1.1:8000/
Insert image description here
where dirtree.txt is as follows

directory tree
|____ota_client.py 3.25K
|____README.md 56B
|____dirtree.txt 325B
|____ota_server.py 22.46K

File Path  File Size  File Modify Time
README.md  56B  2023-05-13 19:19:42  
dirtree.txt  325B  2023-05-14 13:21:44  
ota_client.py  3.25K  2023-05-13 19:19:42  
ota_server.py  22.46K  2023-05-13 19:19:42  

Select the application to be updated and upload the application as a *.tar.gz compressed file. When the server accepts the completed file, it will publish update messages to the Docker clients that subscribe to the relevant updates and need to apply the update. The corresponding client will apply to the server to download the file and update the local Docker application.

client

Before executing the client, you need to configure the client such as the compressed file name, IP, user name, password, subscribed topic_name, etc. that need to be updated.

Execute on another machine or this machine

python3 ota_client.py

Container file mapping

"docker run -it -v /home/wp/ota-client:/root -d weipengyiyu/ubuntu:18.04"

In this way, after the container is started, the /root directory will be automatically created in the container. In this way, we can make it clear that in the -v parameter, the directory before the colon ":" is the host directory /home/wp/ota-client, and the directory after it is the directory inside the container.

Note: The container directory cannot be a relative path; if the host directory does not exist, it will be automatically generated.

In this way, files hanging in the host directory can be operated within the container.

Guess you like

Origin blog.csdn.net/yiyu20180729/article/details/130661475