Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pahp mqtt遇到的一个警告 #847

Closed
WKTYesq opened this issue Jun 4, 2024 · 3 comments
Closed

pahp mqtt遇到的一个警告 #847

WKTYesq opened this issue Jun 4, 2024 · 3 comments
Labels
Status: Available No one has claimed responsibility for resolving this issue.

Comments

@WKTYesq
Copy link

WKTYesq commented Jun 4, 2024

我使用pahp-mqtt遇到一个警告,如下所示:

import paho.mqtt.client as mqtt


MQTT_SERVER = 'mqtt.lfzk.com'
MQTT_PORT = 1883
MQTT_TOPIC = 'ws'
MQTT_USERNAME = 'remote'
MQTT_PASSWORD = 'lfzk@1020@re'
MQTT_CLIENT_ID = 'mqttx_0ea89a05'


# 当客户端收到连接时的回调
def on_connect(client, userdata, flags, rc):
    if rc == 0:
        print("已连接到MQTT代理!")
        # 订阅主题
        client.subscribe(MQTT_TOPIC)
    else:
        print(f"连接失败,返回码 {rc}")


def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload.decode("utf-8")))


if __name__ == '__main__':
    client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1)

    client.on_connect = on_connect
    client.message_callback_add(MQTT_TOPIC, on_message)
    # client.on_message = on_message
    client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)
    client.connect(MQTT_SERVER, MQTT_PORT, 60)

    client.loop_forever()

控制台:

D:\PythonWorkSpace\IPC\mqtt_client.py:28: DeprecationWarning: Callback API version 1 is deprecated, update to latest version
  client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1)
已连接到MQTT代理!
ws {
    "code": "200",
    "data": {
        "msgId": "767",
        "sn": "211323091015",
        "time": 1717468404887,
        "value": {
            "production": "6224",
            "production1": "7174",
            "production2": "6592",
            "runtime": "288",
            "runtime1": "277",
            "runtime2": "302",
            "status": "0",
            "status1": "1",
            "status2": "0"
        }
    }
}

这个警告我不知道能不能处理 DeprecationWarning: Callback API version 1 is deprecated, update to latest version client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1)

我将client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1)修改为client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)后运行:

D:\PythonWorkSpace\IPC\.venv\Scripts\python.exe D:\PythonWorkSpace\IPC\mqtt_client.py 
Traceback (most recent call last):
  File "D:\PythonWorkSpace\IPC\mqtt_client.py", line 36, in <module>
    client.loop_forever()
  File "D:\PythonWorkSpace\IPC\.venv\Lib\site-packages\paho\mqtt\client.py", line 2297, in loop_forever
    rc = self._loop(timeout)
         ^^^^^^^^^^^^^^^^^^^
  File "D:\PythonWorkSpace\IPC\.venv\Lib\site-packages\paho\mqtt\client.py", line 1686, in _loop
    rc = self.loop_read()
         ^^^^^^^^^^^^^^^^
  File "D:\PythonWorkSpace\IPC\.venv\Lib\site-packages\paho\mqtt\client.py", line 2100, in loop_read
    rc = self._packet_read()
         ^^^^^^^^^^^^^^^^^^^
  File "D:\PythonWorkSpace\IPC\.venv\Lib\site-packages\paho\mqtt\client.py", line 3142, in _packet_read
    rc = self._packet_handle()
         ^^^^^^^^^^^^^^^^^^^^^
  File "D:\PythonWorkSpace\IPC\.venv\Lib\site-packages\paho\mqtt\client.py", line 3814, in _packet_handle
    return self._handle_connack()
           ^^^^^^^^^^^^^^^^^^^^^^
  File "D:\PythonWorkSpace\IPC\.venv\Lib\site-packages\paho\mqtt\client.py", line 3934, in _handle_connack
    on_connect(
TypeError: on_connect() takes 4 positional arguments but 5 were given

Process finished with exit code 1

求大佬帮忙解决,谢谢

@github-actions github-actions bot added the Status: Available No one has claimed responsibility for resolving this issue. label Jun 4, 2024
@MattBrittan
Copy link
Contributor

See the migrations doc for info on this. But, in summary:

Expected signature for callback API version 2: connect_callback(client, userdata, connect_flags, reason_code, properties)
Expected signature for callback API version 1 change with MQTT protocol version:
* For MQTT v3.1 and v3.1.1 it's: connect_callback(client, userdata, flags, rc)
* For MQTT v5.0 it's: connect_callback(client, userdata, flags, reason_code, properties)

So you need to update your callbacks when changing the API version.

Note: I only speak English (and have not seen any Chinese responses in this repo).

@etimes2008
Copy link

这个版本是警告,能照样运行,估计后面版本会默认是api2了

@MattBrittan
Copy link
Contributor

Closing this as it looks like a response has been provided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Available No one has claimed responsibility for resolving this issue.
Projects
None yet
Development

No branches or pull requests

3 participants