删除远程mqtt代码
This commit is contained in:
+2
-49
@@ -39,7 +39,6 @@ class MQTTClient(QObject):
|
||||
super().__init__()
|
||||
self.device_id = device_id
|
||||
self.local_client = mqtt.Client(client_id = 'controller_' + self.random_string(6)) # 创建本地客户端
|
||||
self.remote_client = mqtt.Client(client_id=device_id) # 创建远程客户端
|
||||
self.device_client = mqtt.Client(client_id = 'controller_dev_' + self.random_string(6)) # 创建设备客户端
|
||||
self.m_clienttimestamp = 0
|
||||
self.cur_product_id = "C8111D73-F387-981D-18D5-849816E1793C"
|
||||
@@ -47,14 +46,10 @@ class MQTTClient(QObject):
|
||||
self.local_client.on_connect = self.on_connect
|
||||
self.local_client.on_message = self.on_message
|
||||
self.local_client.on_disconnect = self.on_disconnect
|
||||
self.remote_client.on_connect = self.on_connect
|
||||
self.remote_client.on_message = self.on_message
|
||||
self.remote_client.on_disconnect = self.on_disconnect
|
||||
self.device_client.on_connect = self.on_dev_connect
|
||||
self.device_client.on_message = self.on_dev_message
|
||||
self.device_client.on_disconnect = self.on_disconnect
|
||||
self.local_client.username_pw_set(USER, PWD)
|
||||
self.remote_client.username_pw_set("cpy_machine", "tgzz2025") # 设置用户名和密码
|
||||
self.device_client.username_pw_set(USER, PWD) # 设置用户名和密码
|
||||
self.sportModes = ["fast", "normal", "slow"]
|
||||
self.sportIndex = 0
|
||||
@@ -101,35 +96,10 @@ class MQTTClient(QObject):
|
||||
print(f"检查EMQX状态失败: {e}")
|
||||
return False
|
||||
|
||||
def checkRemoteServer(self, host = '127.0.0.1', port = 1883, timeout=1):
|
||||
"""
|
||||
Check if the TCP server is available
|
||||
:param host: Server IP address or domain name
|
||||
:param port: Server port number
|
||||
:param timeout: Connection timeout in seconds
|
||||
:return: True if the server is available, False otherwise
|
||||
"""
|
||||
print(f"Starting to check if {host}:{port} is available")
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.settimeout(timeout)
|
||||
try:
|
||||
result = sock.connect_ex((host, port))
|
||||
if result == 0:
|
||||
print(f"Successfully connected to {host}:{port}")
|
||||
return True
|
||||
else:
|
||||
print(f"Failed to connect to {host}:{port}, error code: {result}")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"An exception occurred while connecting to {host}:{port}: {e}")
|
||||
return False
|
||||
finally:
|
||||
sock.close()
|
||||
|
||||
def connTimerHandler(self):
|
||||
print("check broker")
|
||||
self.conn_timer.stop()
|
||||
if self.get_emqx_status() and self.checkRemoteServer(host=LOCALBORKER,port=LOCALPORT):
|
||||
if self.get_emqx_status():
|
||||
print("mqtt brokeris ready")
|
||||
self.connectToBroker()
|
||||
else:
|
||||
@@ -142,14 +112,10 @@ class MQTTClient(QObject):
|
||||
print("connect to broker local ")
|
||||
self.local_client.connect(host=LOCALBORKER,port=LOCALPORT) # 连接本地 MQTT 服务器
|
||||
time.sleep(0.5)
|
||||
# print("connect to broker remote")
|
||||
# self.remote_client.connect(host=BORKER,port=PORT) # 连接远程 MQTT 服务器
|
||||
time.sleep(0.5)
|
||||
print("connect to broker local dev")
|
||||
self.device_client.connect(host=DEVICEBORKER,port=DEVICEPORT) # 连接设备 MQTT 服务器
|
||||
time.sleep(0.5)
|
||||
self.local_client.loop_start() # 开始循环以处理网络流量
|
||||
# self.remote_client.loop_start() # 开始循环以处理网络流量
|
||||
self.device_client.loop_start() # 开始循环以处理网络流量
|
||||
except Exception as e:
|
||||
print(e)
|
||||
@@ -522,33 +488,21 @@ class MQTTClient(QObject):
|
||||
def publish_cpy_server(self, topicname, data, qos=0):
|
||||
|
||||
self.publish_local(topicname, data, qos) # 发布数据到本地
|
||||
# if self.client_alive(): # 检查客户端是否存活
|
||||
# self.publish_remote(topicname, data, qos) # 如果存活,发布数据到远程
|
||||
|
||||
def publish_cpy(self, topicname, data, qos=0):
|
||||
topicname = TOPICFIRST + self.device_id + topicname # 构建完整的主题名称
|
||||
self.publish_local(topicname, data, qos) # 发布数据到本地
|
||||
# if self.client_alive(): # 检查客户端是否存活
|
||||
# self.publish_remote(topicname, data, qos) # 如果存活,发布数据到远程
|
||||
|
||||
def publish_rev_cpy(self, cid, data, qos = 1):
|
||||
topicname = TOPICFIRST + cid + TOPICRECEIVE # 构建完整的主题名称
|
||||
# print("topicname:",topicname)
|
||||
self.publish_local(topicname, data, qos) # 发布数据到本地
|
||||
# if self.client_alive(): # 检查客户端是否存活
|
||||
# self.publish_remote(topicname, data, qos) # 如果存活,发布数据到远程
|
||||
# print("json_data:",data)
|
||||
|
||||
def publish_local(self, topic, data, qos = 0):
|
||||
json_data = json.dumps(data) # 将数据转换为 JSON 字符串
|
||||
self.local_client.publish(topic, json_data, qos) # 发布数据到本地
|
||||
# print(f"Published to local: {topic} -> {json_data}")
|
||||
|
||||
def publish_remote(self, topic, data, qos = 0):
|
||||
pass
|
||||
# json_data = json.dumps(data) # 将数据转换为 JSON 字符串
|
||||
# self.remote_client.publish(topic, json_data, qos) # 发布数据到远程
|
||||
|
||||
def send_collect_data(self, data):
|
||||
self.publish_cpy(TOPICCOLLECT, data, 1) # 发布
|
||||
|
||||
@@ -598,8 +552,7 @@ class MQTTClient(QObject):
|
||||
print("disconnect_broker")
|
||||
self.local_client.disconnect()
|
||||
self.local_client.loop_stop()
|
||||
self.remote_client.disconnect()
|
||||
self.remote_client.loop_stop()
|
||||
|
||||
self.device_client.disconnect()
|
||||
self.device_client.loop_stop()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user