eclipse-velocitas / vehicle-app-python-template

Vehicle App template for Python
Apache License 2.0
17 stars 24 forks source link

[Bug]: APP can not connect mqtt as native, not Dapr #230

Open ley901017 opened 6 months ago

ley901017 commented 6 months ago

Severity

Critical

What release version, tag or commit-hash did you use?

v0.1.1

Current Behavior

When I run velocita app directly without dapr, the below error message has been appeared.

Exception ignored in: <function Client.__del__ at 0x7f2f7c87c670>
Traceback (most recent call last):
  File "/home/vscode/.local/lib/python3.10/site-packages/paho/mqtt/client.py", line 874, in __del__
    self._reset_sockets()
  File "/home/vscode/.local/lib/python3.10/site-packages/paho/mqtt/client.py", line 1133, in _reset_sockets
    self._sock_close()
  File "/home/vscode/.local/lib/python3.10/site-packages/paho/mqtt/client.py", line 1119, in _sock_close
    if not self._sock:
AttributeError: 'Client' object has no attribute '_sock'
Traceback (most recent call last):
  File "/workspaces/vehicle-app-python-template/app/src/main.py", line 25, in <module>
    from vehicle import Vehicle, vehicle  # type: ignore
  File "/home/vscode/.local/lib/python3.10/site-packages/vehicle/__init__.py", line 8, in <module>
    from velocitas_sdk.model import (
  File "/home/vscode/.local/lib/python3.10/site-packages/velocitas_sdk/model.py", line 24, in <module>
    from velocitas_sdk import config
  File "/home/vscode/.local/lib/python3.10/site-packages/velocitas_sdk/config.py", line 49, in <module>
    _config = Config(__middleware_type)
  File "/home/vscode/.local/lib/python3.10/site-packages/velocitas_sdk/config.py", line 36, in __init__
    self.middleware: Middleware = self.__create_middleware(__middleware)
  File "/home/vscode/.local/lib/python3.10/site-packages/velocitas_sdk/config.py", line 41, in __create_middleware
    _middleware = NativeMiddleware()
  File "/home/vscode/.local/lib/python3.10/site-packages/velocitas_sdk/native/middleware.py", line 34, in __init__
    self.pubsub_client = MqttClient(_port, _hostname)
  File "/home/vscode/.local/lib/python3.10/site-packages/velocitas_sdk/native/mqtt.py", line 42, in __init__
    self._pub_client = mqtt.Client()
TypeError: Client.__init__() missing 1 required positional argument: 'callback_api_version'

Steps to Reproduce

  1. In Dev Container, run task Local Runtime - Up
  2. execute python3 /workspaces/vehicle-app-python-template/app/src/main.py in terminal
  3. then you will see the error message

Expected Behavior

The app can run normally without dapr.

Possible Solution

No response

Additional Information

In velocitas_sdk, the below code may have some bugs, the path is velocitas_sdk/native/mqtt.py the function init of MqttClient, I cannot see any parameters on line 42 when call mqtt,Client()

lass MqttClient(PubSubClient):
    """This class is a wrapper for the on_message callback of the MQTT broker."""

    def __init__(self, port: Optional[int] = None, hostname: Optional[str] = None):
        self._port = port
        self._hostname = hostname
        self._topics_to_subscribe: list[MqttTopicSubscription] = []

        self._pub_client = mqtt.Client()
        self._sub_client = mqtt.Client()
        self._sub_client.on_connect = self.on_connect
        self._sub_client.on_disconnect = self.on_disconnect

        self._sub_client.connect(self._hostname, self._port)
        self._pub_client.connect(self._hostname, self._port)

Code of Conduct

dennismeister93 commented 5 months ago

Hi @ley901017

  1. Option: If you want to stay at tag v0.1.1:

git checkout v0.1.1

velocitas.json:

{
    "packages": [
        {
            "name": "devenv-runtimes",
            "version": "v2.2.6"
        },
        {
            "name": "devenv-github-workflows",
            "version": "v4.1.3"
        },
        {
            "name": "devenv-github-templates",
            "version": "v1.0.3"
        },
        {
            "name": "devenv-devcontainer-setup",
            "version": "v1.4.7"
        }
    ],
    "variables": {
        "language": "python",
        "repoType": "app",
        "appManifestPath": "app/AppManifest.json",
        "githubRepoId": "eclipse-velocitas/vehicle-app-python-template",
        "generatedModelPath": "./gen/vehicle_model"
    },
    "cliVersion": "v0.6.3"
}
velocitas --version 
velocitas-cli/0.6.3 linux-arm64 node-v18.5.0

Steps:

start dev container -> wait for installation / setup

export SDV_MIDDLEWARE_TYPE=native
export SDV_MQTT_ADDRESS=mqtt://localhost:1883
export SDV_VEHICLEDATABROKER_ADDRESS=grpc://localhost:55555

Change app/requirements-velocitas.txt content to (We fixed the bug there where your problem originates from https://github.com/eclipse-velocitas/vehicle-app-python-sdk/pull/119 fixed with https://github.com/eclipse-velocitas/vehicle-app-python-sdk/releases/tag/v0.13.1):

velocitas-sdk==0.13.1

velocitas exec sdk-installer install-deps
velocitas exec sdk-installer run

Press F1 and run Task: Local Runtime - Up

Afterwards: python3 /workspaces/vehicle-app-python-template/app/src/main.py

  1. Option (preferred one because that is using all the latest changes out of the box):

git checkout main

velocitas.json:

{
    "packages": [
        {
            "name": "devenv-runtimes",
            "version": "v2.2.6"
        },
        {
            "name": "devenv-github-workflows",
            "version": "v4.1.4"
        },
        {
            "name": "devenv-github-templates",
            "version": "v1.0.3"
        },
        {
            "name": "devenv-devcontainer-setup",
            "version": "v1.5.1"
        }
    ],
    "variables": {
        "language": "python",
        "repoType": "app",
        "appManifestPath": "app/AppManifest.json",
        "githubRepoId": "eclipse-velocitas/vehicle-app-python-template",
        "generatedModelPath": "./gen/vehicle_model"
    },
    "cliVersion": "v0.7.0"
}
velocitas --version
velocitas-cli/0.7.0 linux-arm64 node-v18.5.0

start dev container -> wait for installation / setup

export SDV_MIDDLEWARE_TYPE=native
export SDV_MQTT_ADDRESS=mqtt://localhost:1883
export SDV_VEHICLEDATABROKER_ADDRESS=grpc://localhost:55555

Press F1 and run Task: Local Runtime - Up

Afterwards: python3 /workspaces/vehicle-app-python-template/app/src/main.py