简体   繁体   English

谷歌助手输入到 Python Output

[英]Google Assistant Input to Python Output

the question is pretty straight forward.这个问题很简单。 I would like to control a drone (Bitcraze Crazyflie), using a Google Home.我想使用 Google Home 控制无人机 (Bitcraze Crazyflie)。 The Input is: "Drone fly to x3 y4", processed as usual by Firebase etc. Resulting in the Google Assistant Output: "Flying to x3 y4", but also an Ouput in eg JSON format, to navigate the drone. The Input is: "Drone fly to x3 y4", processed as usual by Firebase etc. Resulting in the Google Assistant Output: "Flying to x3 y4", but also an Ouput in eg JSON format, to navigate the drone. Because the drone works with Python, this is the preferable Output language.因为无人机使用 Python,所以这是首选的 Output 语言。

EDIT Added more Context编辑添加了更多上下文

Currently I'm using an node server running this code:目前我正在使用运行此代码的节点服务器:

        'use strict';

    // Import the Dialogflow module from the Actions on Google client library.
    const {dialogflow} = require('actions-on-google');

    // Import the firebase-functions package for deployment.
    const functions = require('firebase-functions');

    // Instantiate the Dialogflow client.
    const app = dialogflow({debug: true});

    // Handle the Dialogflow intent named 'fly'.
    // The intent collects parameters named 'xaxis, yaxis'.
    app.intent('fly', (conv, {xaxis,yaxis}) => {
        const xAxis = xaxis;
        const yAxis = yaxis;
        // Respond with the user's coordinates and end the conversation.
        conv.close('Roger that, flying to ' + xAxis + ", " + yAxis);

    });

    // Set the DialogflowApp object to handle the HTTPS POST request.
    exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);

Now I would like to get the const xAxis and yAxis and use them in a Python program.现在我想获得 const xAxis 和 yAxis 并在 Python 程序中使用它们。 I've tried using我试过使用

process.stdout.write(xAxis + yAxis);

Listening in Python with something like听 Python 之类的东西

out = sensor.stdout.read(1)

but the code will be run on the Google Server, so local port listening does not work.但是代码会在谷歌服务器上运行,所以本地端口监听不起作用。

Thanks for your help.谢谢你的帮助。

The best approach is having another machine on GCP rather than communicate with your home PC.最好的方法是在 GCP 上安装另一台机器,而不是与您的家用 PC 通信。 You'll learn more and have an easier time, in the long run, building solutions.从长远来看,您将学到更多知识并更轻松地构建解决方案。 As I'm more familiar with AWS rather than GCP, I can't cite the network/security components you need to configure but the docs say you don't have to.由于我更熟悉 AWS 而不是 GCP,因此我无法引用您需要配置的网络/安全组件,但文档说您不必这样做。 So, in theory, it should be just about spinning up another compute machine with your Python code running on it.因此,理论上,它应该只是启动另一台计算机,并在其上运行您的 Python 代码。

Were you to decide on speaking to your home PC, you'll need to forward ports on your router.如果您决定与您的家用 PC 通话,您需要转发路由器上的端口。 It is, currently, acting as a firewall for your LAN devices and doesn't permit outside machines initiating connections to your internal addresses.目前,它充当您的 LAN 设备的防火墙,并且不允许外部计算机启动与您的内部地址的连接。 eg your GCP machine initiating a connection to your home PC.例如,您的 GCP 机器开始连接到您的家用 PC。 The other way around is permitted, by default.默认情况下,允许使用其他方式。 If you think about it, your router has one WAN IP address but your LAN can have multiple devices (multiple LAN IPs).如果您考虑一下,您的路由器有一个 WAN IP 地址,但您的 LAN 可以有多个设备(多个 LAN IP)。 If your GCP machine connects to your router WAN IP at port 8080, to which LAN IP should it connect?如果您的 GCP 机器在端口 8080 处连接到路由器 WAN IP,它应该连接到哪个 LAN IP? You have to help your router and explicitly tell it.您必须帮助您的路由器并明确告诉它。

Once you have a networking solution in place, you can debug the connectivity itself (server can talk to client) by using netcat (nc/ncat, depending on Linux distro).一旦有了网络解决方案,您就可以使用 netcat(nc/ncat,取决于 Linux 发行版)来调试连接本身(服务器可以与客户端通信)。 Netcat is a versatile networking tool with which you can purely open connections (before you add in your program to the debugging stack) and assure the networking part of your solution is working as intended. Netcat 是一个多功能的网络工具,您可以使用它纯粹打开连接(在将程序添加到调试堆栈之前)并确保解决方案的网络部分按预期工作。

nc -v <destination_ip> <port>

Simple.简单的。

This should get you to where you want to be.这应该让你到达你想去的地方。 A working connection between your GCP drone controller and the Python processor machine.您的 GCP 无人机 controller 和 Python 处理器机器之间的工作连接。

Bonus - If you want a quick way to have your machine (PC or otherwise) listen on a port, you can use Python's built-in HTTP file server module with奖励 - 如果您想要一种让您的机器(PC 或其他)在端口上侦听的快速方法,您可以使用 Python 的内置 HTTP 文件服务器模块

python -m http.server 8080

This will serve files from the directory you ran this command.这将从您运行此命令的目录中提供文件。 So, keep that in mind if you're open to the world.所以,如果你对世界开放,请记住这一点。
Or, a simple "echo server", using netcat.或者,一个简单的“回声服务器”,使用 netcat。

nc -v -l 8080

Lastly, for a solid Python HTTP API framework, I highly recommend FastAPI .最后,对于一个可靠的 Python HTTP API 框架,我强烈推荐FastAPI It allows quickly writing a HTTP API server with, for example, a POST method that your GCP drone controller can call.它允许快速编写 HTTP API 服务器,例如,您的 GCP 无人机 controller 可以调用的 POST 方法。 It has the great bonus of generating both interactive OpenAPI docs, example , for your code and, using 3rd party tools from Swagger (that you can see in the example linked), generate server/client/testing "boiler plate" code.它具有为您的代码生成交互式OpenAPI 文档的巨大好处,例如,使用来自 Swagger 的第 3 方工具(您可以在链接的示例中看到),生成服务器/客户端/测试“样板”代码。 Did I also mention their docs are great?我是否还提到他们的文档很棒?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM