从Alexa触发器执行脚本

时间:2017-03-14 22:19:35

标签: python alexa

我一直在使用https://github.com/toddmedema/echo中的example-minimal.py脚本,需要对其进行更改,以便不再将状态更改打印到终端,而是执行另一个脚本。

我是业余爱好者,但渴望学习,甚至更渴望完成这个项目。

提前感谢您提供的任何帮助!!

""" fauxmo_minimal.py - Fabricate.IO

    This is a demo python file showing what can be done with the debounce_handler.
    The handler prints True when you say "Alexa, device on" and False when you say
    "Alexa, device off".

    If you have two or more Echos, it only handles the one that hears you more clearly.
    You can have an Echo per room and not worry about your handlers triggering for
    those other rooms.

    The IP of the triggering Echo is also passed into the act() function, so you can
    do different things based on which Echo triggered the handler.
"""

import fauxmo
import logging
import time

from debounce_handler import debounce_handler

logging.basicConfig(level=logging.DEBUG)

class device_handler(debounce_handler):
    """Publishes the on/off state requested,
       and the IP address of the Echo making the request.
    """
    TRIGGERS = {"device": 52000}

    def act(self, client_address, state, name):
        print "State", state, "on ", name, "from client @", client_address
        return True

if __name__ == "__main__":
    # Startup the fauxmo server
    fauxmo.DEBUG = True
    p = fauxmo.poller()
    u = fauxmo.upnp_broadcast_responder()
    u.init_socket()
    p.add(u)

    # Register the device callback as a fauxmo handler
    d = device_handler()
    for trig, port in d.TRIGGERS.items():
        fauxmo.fauxmo(trig, u, p, None, port, d)

    # Loop and poll for incoming Echo requests
    logging.debug("Entering fauxmo polling loop")
    while True:
        try:
            # Allow time for a ctrl-c to stop the process
            p.poll(100)
            time.sleep(0.1)
        except Exception, e:
            logging.critical("Critical exception: " + str(e))
            break

2 个答案:

答案 0 :(得分:0)

我将通过阅读该脚本并解释每个位的功能来尝试并提供帮助。这应该有助于您了解它正在做什么,因此您需要做些什么来让它运行其他东西:

import fauxmo

这个is a library允许运行脚本的任何设备伪装成Belkin WeMo;一个可以被Echo触发的设备。

import logging
import time
from debounce_handler import debounce_handler

这是导入脚本需要的更多库。 Logging将用于记录事物,这对于调试很有用,time将用于使脚本暂停,以便您可以通过键入ctrl-c和{{3}来退出它}库将用于阻止多个Echos对同一个语音命令做出反应(这将导致debounce_handler)。

logging.basicConfig(level=logging.DEBUG)

配置一个记录器,允许记录事件以协助调试。

class device_handler(debounce_handler):
    """Publishes the on/off state requested,
       and the IP address of the Echo making the request.
    """

    TRIGGERS = {"device": 52000}

    def act(self, client_address, state, name):
        print "State", state, "on ", name, "from client @", client_address
        return True

我们创建了一个名为 device_handler software bounce,其中包含一个名为 TRIGGERS class和一个名为<{3}}的<{3}} EM>行为

act 将许多变量作为输入; self (类中的任何数据结构,例如 TRIGGERS 字典), client_address state 和< EM>名称的。我们还不知道这些是什么,但这些名称是非常自我解释的,所以我们可以猜测 client_address 可能是Echo,* state&#34;的IP地址。它在,而名称将是它的名字。 这是您想要编辑的功能,因为它是Echo触发的最终功能。您可以在打印声明之后粘贴您想要的任何功能。 act 函数在调用时返回True。

if __name__ == "__main__":

如果您直接运行脚本,这将执行在其下方缩进的所有内容。如果您需要,请详细了解dictionary

    # Startup the fauxmo server
    fauxmo.DEBUG = True
    p = fauxmo.poller()
    u = fauxmo.upnp_broadcast_responder()
    u.init_socket()
    p.add(u)

正如评论所暗示的,这会启动假的WeMo服务器。我们启用调试,只需function到命令行,创建here p ,它可以处理传入的消息,并创建prints any debug messages u ,可以处理poller设备注册。然后我们告诉upnp broadcast responder,在网络上设置自己监听UPnP设备,然后UPnP u p 以便我们可以在收到广播时做出回应。

    # Register the device callback as a fauxmo handler
    d = device_handler()
    for trig, port in d.TRIGGERS.items():
        fauxmo.fauxmo(trig, u, p, None, port, d)

正如评论所说,这设置了我们之前制作的设备处理程序类的initialise a socket。现在我们add通过设备处理程序 d 中的 TRIGGERS 字典中的项目,并使用它在字典中找到的信息调用instance。如果我们查看类定义中的字典定义,我们可以看到端口 52000 上只有一个条目,一个trig 设备。这基本上完成了大部分工作,使实际的假WeMo设备与Echo对话。如果我们查看fauxmo.fauxmo函数,我们会看到,当它收到一个合适的触发器时,它调用我们之前定义的 device_handler 类中的 act 函数。

    # Loop and poll for incoming Echo requests
    logging.debug("Entering fauxmo polling loop")
    while True:
        try:
            # Allow time for a ctrl-c to stop the process
            p.poll(100)
            time.sleep(0.1)
        except Exception, e:
            logging.critical("Critical exception: " + str(e))
            break

在这里我们进入fauxmo轮询循环。通过以下代码for-loop,检查我们是否收到了消息。它下面的代码fauxmo.fauxmo用于轮询消息,查看它是否收到任何内容,然后等待一点,然后再次轮询。除非,如果由于某种原因无法执行此操作,则脚本将indefinitely loops并且将记录错误,以便您可以看到出错的地方。

答案 1 :(得分:-1)

只是澄清一下;如果Fauxmo循环正在运行,则脚本很好,对吗?

我认为TO在Echo和WeMo假设备之间没有任何连接。如果您先安装WeMo技能,则可以提供帮助。不过,您可能最初需要使用原始的WeMo设备。

我知道这些是旧线程,但它可能仍对某人有所帮助。

相关问题