我正在尝试使用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

有人遇到过同样的问题吗?

谢谢

最佳答案

我有同样的问题。您可以通过在机器人上打开两个ssh控制台,然后在第一个执行上轻松地重现它

qicli watch ALTabletService.onConsoleMessage

在第二个
qicli call ALTabletService.showWebview
qicli call ALTabletService.executeJS "console.log('hello')"

...,而不是“hello”,您会看到“null”出现在第一个控制台中。

但是-如果您的目标是有效测试您的网页,那么我通常要做的就是打开计算机上的页面并使用chrome控制台(您可以将chrome设置为该页面是大小正确的平板电脑,1280x800 );您可以在将页面仍连接到Pepper的同时执行此操作,就像在平板电脑using the method described here上一样。这足以满足99%的情况;剩下的1%是Pepper的平板电脑实际上不同于Chrome的东西。

关于javascript - Pepper Naoqi 2.5 onConsoleMessage空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48204925/

10-09 23:51