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

Question: How to use return in on_message #742

Closed
rdrigos opened this issue Aug 22, 2023 · 2 comments
Closed

Question: How to use return in on_message #742

rdrigos opened this issue Aug 22, 2023 · 2 comments
Labels
Status: Available No one has claimed responsibility for resolving this issue.

Comments

@rdrigos
Copy link

rdrigos commented Aug 22, 2023

I'm developing a function and I needed to access the values returned from paho's callback: 'on_message', as it is an automatic and recursive method when a message arrives. How could I add a return to work with the message?

Example:

def receive_payload(
     client: mqtt.Client,
     userdata: dict[str, Any] | None,
     message: mqtt.MQTTMessage,
) -> dict[str, Any]:
     payload_message: dict[str, Any] = json.loads(message.payload.decode())
     payload_topic: str = message.topic

     return {
         'payloadTopic': payload_topic,
         'payloadMessage': payload_message,
     }

client: mqtt.Client = mqtt.Client()
client.on_message = receive_payload

How do I access the return value of the 'receive_payload' function?
Because I couldn't just call it receive_payload(), because I would need to pass the arguments and it wouldn't work correctly, because it's not in the on_message callback

@github-actions github-actions bot added the Status: Available No one has claimed responsibility for resolving this issue. label Aug 22, 2023
@FabricioMoraes-Dev
Copy link

I'm developing a function and I needed to access the values returned from paho's callback: 'on_message', as it is an automatic and recursive method when a message arrives. How could I add a return to work with the message?

Example:

def receive_payload(
     client: mqtt.Client,
     userdata: dict[str, Any] | None,
     message: mqtt.MQTTMessage,
) -> dict[str, Any]:
     payload_message: dict[str, Any] = json.loads(message.payload.decode())
     payload_topic: str = message.topic

     return {
         'payloadTopic': payload_topic,
         'payloadMessage': payload_message,
     }

client: mqtt.Client = mqtt.Client()
client.on_message = receive_payload

How do I access the return value of the 'receive_payload' function? Because I couldn't just call it receive_payload(), because I would need to pass the arguments and it wouldn't work correctly, because it's not in the on_message callback

Tente usar a propriedade "content" para acessar o valor retornado da função. Segue abaixo um caso de uso. Não sei se irá funcionar no seu caso, mas é válido tentar!

def main(req: func.HttpRequest) -> func.HttpResponse:
    
    nome = req.params.get('nome')
    sobrenome = req.params.get('sobrenome')
    email = req.params.get('email')
    
    new_user =  create_user(email, nome, sobrenome)
    
    user_id = json.loads(new_user.content)[0]["id"]
    enrol_users(user_id)
    #userid = req.params.get(user_id)
    #func.HttpResponse(f'{new_user.content}',status_code=200),
    
    return func.HttpResponse(f'user_id = {user_id}',status_code=200)
  

@rdrigos
Copy link
Author

rdrigos commented Aug 22, 2023

Estou desenvolvendo uma função e precisava acessar os valores retornados do callback do paho: 'on_message', pois é um método automático e recursivo quando chega uma mensagem. Como eu poderia adicionar um retorno ao trabalho com a mensagem?
Exemplo:

def receive_payload(
     client: mqtt.Client,
     userdata: dict[str, Any] | None,
     message: mqtt.MQTTMessage,
) -> dict[str, Any]:
     payload_message: dict[str, Any] = json.loads(message.payload.decode())
     payload_topic: str = message.topic

     return {
         'payloadTopic': payload_topic,
         'payloadMessage': payload_message,
     }

client: mqtt.Client = mqtt.Client()
client.on_message = receive_payload

Como faço para acessar o valor de retorno da função 'receive_payload'? Pois não poderia simplesmente chamar de receive_payload(), pois precisaria passar os argumentos e não funcionaria corretamente, pois não está no callback on_message

Tente usar a propriedade "content" para acessar o valor retornado da função. Segue abaixo um caso de uso. Não sei se irá funcionar no seu caso, mas é válido tentar!

def main(req: func.HttpRequest) -> func.HttpResponse:
    
    nome = req.params.get('nome')
    sobrenome = req.params.get('sobrenome')
    email = req.params.get('email')
    
    new_user =  create_user(email, nome, sobrenome)
    
    user_id = json.loads(new_user.content)[0]["id"]
    enrol_users(user_id)
    #userid = req.params.get(user_id)
    #func.HttpResponse(f'{new_user.content}',status_code=200),
    
    return func.HttpResponse(f'user_id = {user_id}',status_code=200)
  

Obrigado, vou tentar executar esse solução!

@rdrigos rdrigos closed this as completed Aug 24, 2023
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

2 participants