我正在尝试使用onConsoleMessage事件的回调,用python打印来自web控制台的消息。Pepper (编辑:1.6版)运行的是naoqi 2.5.5.5。作为测试,我已经修改了executeJS示例。问题是我在回调中一直得到空的消息。是不是在新版本的naoqi中已经修复的bug?我已经看过发行说明了,但我什么也没找到。
下面是我使用的代码:
#! /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是否有人也面临同样的问题?
谢谢
发布于 2018-01-12 18:46:42
我也有同样的问题。您可以通过在机器人上打开两个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的地方。
https://stackoverflow.com/questions/48204925
复制相似问题