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

when i use offical Respond to server RPC call example parameter #34

Open
alphijiang opened this issue Nov 8, 2022 · 4 comments
Open

Comments

@alphijiang
Copy link

the official example as below:

from psutil import cpu_percent, virtual_memory
from time import sleep
from tb_device_mqtt import TBDeviceMqttClient


# dependently of request method we send different data back
def on_server_side_rpc_request(client, request_id, request_body):
    print(request_id, request_body)
    if request_body["method"] == "getCPULoad":
        client.send_rpc_reply(request_id, {"CPU percent": cpu_percent()})
    elif request_body["method"] == "getMemoryUsage":
        client.send_rpc_reply(request_id, {"Memory": virtual_memory().percent})

client = TBDeviceMqttClient("127.0.0.1", "A1_TEST_TOKEN")
client.set_server_side_rpc_request_handler(on_server_side_rpc_request)
client.connect()


while True:
    sleep(1)

but the SDK does not include client parameters. so I modified the local SDK to fit your official example and use client seems likes to be better for using global variables.
can you add this parameter back?

@palexandri
Copy link

palexandri commented Dec 6, 2022

I can verify this behavior. self is missing in the tb_device_mqtt.py callback in line 294.

@tatuiot
Copy link

tatuiot commented Dec 20, 2022

Hi , I am facing same issue, but I cannot find the error, where should be placed the "self" . It looks to me like it is already there. thanks

self.__device_on_server_side_rpc_response(request_id, content)

But I still get the error when receiving RPC from server

File "/home/tatu/.virtualenvs/venv/lib/python3.9/site-packages/tb_device_mqtt.py", line 294, in _on_decoded_message
self.__device_on_server_side_rpc_response(request_id, content)
TypeError: on_server_side_rpc_request() missing 1 required positional argument: 'request_body'

Thanks a lot

@alphijiang
Copy link
Author

Hi , I am facing same issue, but I cannot find the error, where should be placed the "self" . It looks to me like it is already there. thanks

self.__device_on_server_side_rpc_response(request_id, content)

But I still get the error when receiving RPC from server

File "/home/tatu/.virtualenvs/venv/lib/python3.9/site-packages/tb_device_mqtt.py", line 294, in _on_decoded_message self.__device_on_server_side_rpc_response(request_id, content) TypeError: on_server_side_rpc_request() missing 1 required positional argument: 'request_body'

Thanks a lot

def on_server_side_rpc_request(client, request_id, request_body): in current SDK not include client parameter, you can use global client , such as below
def on_server_side_rpc_request(crequest_id, request_body):
global client
...

@tatuiot
Copy link

tatuiot commented Dec 23, 2022

Thanks a lot. After reading and reading, and testing and testing, and more testing, I finally realized whay you meant. heheheh Great , thanks a lot

def on_server_side_rpc_request(request_id, request_body):
global client
print(request_id, request_body)
if request_body["method"] == "getCPULoad":
client.send_rpc_reply(request_id, {"CPU percent": cpu_percent()})
elif request_body["method"] == "getMemoryUsage":
client.send_rpc_reply(request_id, {"Memory": virtual_memory().percent})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants