Python Daemon Script for Device Control with MQTT and Modbus
This is a Python script that runs as a daemon and handles the control of devices through various protocols such as MQTT and Modbus. The script loads the configuration from a config file, creates a controller object using the loaded configuration, and starts threads to handle the control of devices connected to the controller.
The script first loads the configuration from a config file using the 'configparser' module. It then extracts the configuration parameters for the Redis, MQTT, and MariaDB databases, as well as the IP addresses of the controllers. It then creates a 'smartController' object, which is responsible for handling the control of devices connected to the controller.
The script then creates threads to handle the control of devices. It first loads the device configuration using the 'loadDeviceConf' method of the 'smartController' object. It then iterates over the controllers and checks if they are of the type that requires a 485 connection. If they are, it creates a thread to handle the control of devices connected to that controller. The threads are started using the 'start' method of the 'threading.Thread' class.
Finally, the script enters an infinite loop, which checks the status of the threads handling the control of devices. It first gets a list of all threads using the 'threading.enumerate' method. It then creates a list of thread names and a list of controller thread names. It then iterates over the controllers and checks if the controller is of the type that requires a 485 connection. If it is, it checks if the thread handling the control of devices for that controller is still running. If it is not, it creates a new thread to handle the control of devices connected to that controller.
The script then sleeps for 5 seconds before checking the status of the threads again.
原文地址: https://www.cveoy.top/t/topic/oBmB 著作权归作者所有。请勿转载和采集!