4

我正在尝试使用 onConsoleMessage 事件上的回调在 python 中打印来自 Web 控制台的消息。Pepper(编辑:1.6 版)正在运行 naoqi 2.5.5.5。我已将executeJS示例修改为测试。问题是我在回调中的消息一直为空。是不是已经在更新版本的 naoqi 中修复了一个错误?我查看了发行说明,但没有找到任何东西。

这是我正在使用的代码:

#! /usr/bin/env python
# -*- encoding: UTF-8 -*-

"""Example: Use executeJS Method"""

import qi
import argparse
import sys
import time
import signal

def signal_handler(signal, frame):
        print('Bye!')
        sys.exit(0)

def main(session):
    """
    This example uses the executeJS method.
    To Test ALTabletService, you need to run the script ON the robot.
    """
    # Get the service ALTabletService.

    try:
        tabletService = session.service("ALTabletService")

        # Display a local web page located in boot-config/html folder
        # The ip of the robot from the tablet is 198.18.0.1
        tabletService.showWebview("http://198.18.0.1/apps/boot-config/preloading_dialog.html")

        time.sleep(3)

        # Javascript script for displaying a prompt
        # ALTabletBinding is a javascript binding inject in the web page displayed on the tablet
        script = """
            console.log('A test message');
        """

        # Don't forget to disconnect the signal at the end
        signalID = 0

        # function called when the signal onJSEvent is triggered
        # by the javascript function ALTabletBinding.raiseEvent(name)
        def callback(message):
            print "[callback] received : ", message

        # attach the callback function to onJSEvent signal
        signalID = tabletService.onConsoleMessage.connect(callback)

        # inject and execute the javascript in the current web page displayed
        tabletService.executeJS(script)

        print("Waiting for Ctrl+C to disconnect")
        signal.pause()

    except Exception, e:
        print "Error was: ", e


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--ip", type=str, default="127.0.0.1",
                        help="Robot IP address. On robot or Local Naoqi: use '127.0.0.1'.")
    parser.add_argument("--port", type=int, default=9559,
                        help="Naoqi port number")

    args = parser.parse_args()
    session = qi.Session()
    try:
        session.connect("tcp://" + args.ip + ":" + str(args.port))
    except RuntimeError:
        print ("Can't connect to Naoqi at ip \"" + args.ip + "\" on port " + str(args.port) +".\n"
               "Please check your script arguments. Run with -h option for help.")
        sys.exit(1)
    main(session)

输出:

python onConsoleMessage.py --ip=192.168.1.20
[W] 1515665783.618190 30615 qi.path.sdklayout: No Application was created, trying to deduce paths
Waiting for Ctrl+C to disconnect
[callback] received :  null

有人面临同样的问题吗?

谢谢

4

1 回答 1

2

我有同样的问题。您可以通过在机器人上打开两个 ssh 控制台并在第一个执行

qicli watch ALTabletService.onConsoleMessage

第二个

qicli call ALTabletService.showWebview
qicli call ALTabletService.executeJS "console.log('hello')"

...而不是“你好”,您将看到“null”出现在您的第一个控制台中。

但是-如果您的目标是有效地测试您的网页,我通常做的只是在我的计算机上打开页面并使用 chrome 控制台(您可以将 chrome 设置为就像页面是合适尺寸的平板电脑一样,1280x800 ); 您可以在将页面连接到 Pepper 的同时执行此操作,就像它在她的平板电脑上一样,使用此处描述的方法。这对于 99% 的情况来说已经足够了;剩下的 1% 是 Pepper 的平板电脑实际上与 Chrome 不同的地方。

于 2018-01-12T10:46:42.470 回答