AMQP 1.0 消息发送困难

Difficulty in Sending AMQP 1.0 Message

我正在使用 Eclipse Hono 作为 IoT 平台,它 运行 在我的私有 Kubernetes 集群本地。我已经注册了一个设备来通过 MQTT 接收命令。 在 Hono 中,必须通过使用 AMQP 1.0 协议发送消息的 Qpid 网络发送命令。

我有一个 Python 代码,用于向我的注册设备发送消息,该设备已订阅指定主题以接收命令(主题是“command/+/+/req/#” ):

代码如下:

import Tkinter as tkinter
import proton
from cli_proton_python import sender
import ssl
import uuid
import json
from proton._reactor import _generate_uuid
#imports end


# mosquitto_sub -v -h 192.168.1.154 -u rover10@rover -P roversecret -t control/+/+/req/#



#const
scheme = "amqp://"
username = "consumer@HONO"
password = "verysecret"
hostAndPort = "192.168.1.150:15672"
tenant = "rover"
device = "rover1"
topic_to_publish = "command/"+tenant #+"/"+device
speed = 380


def sendControlMsg( msg ):
    print ("sending control msg " + str(msg))
    parser = sender.options.SenderOptions()
    opts, _= parser.parse_args()
        opts.broker_url = scheme + username + ":" + password +"@"+ hostAndPort +"/"+ topic_to_publish
    opts.msg_id = "0" # will be generated automatically
    opts.msg_subject = "RoverDriving"
        opts.msg_content = str(msg)
    opts.msg_reply_to = "command_response/"+tenant+"/"+str(_generate_uuid())
    opts.msg_correlation_id = "null"
    opts.msg_content_type = "application/json"
    opts.log_lib = "TRANSPORT_DRV"
#   opts.log_msgs = 'dict'
    opts.msg_address = "command/"+tenant+"/"+device    

    container = proton.reactor.Container(sender.Send(opts))
    container.run()

#funcs

def turn_right():
    sendControlMsg( "{\"command\":\"E\",\"speed\": "+ str(speed_var.get() + 360) +" }" )
    print("turn right clicked")

def turn_right_back():
    sendControlMsg( "{\"command\":\"D\",\"speed\": "+ str(speed_var.get() + 360) +" }" )
    print("turn right back clicked")

def turn_left():
    sendControlMsg("{\"command\":\"Q\",\"speed\": "+ str(speed_var.get() + 360) +" }")
    print("turn left clicked")

def turn_left_back():
    sendControlMsg("{\"command\":\"A\",\"speed\": "+ str(speed_var.get() + 360) +" }")
    print("turn left back clicked")

def move_forward():
    sendControlMsg("{\"command\":\"W\",\"speed\": "+ str(speed_var.get() + 360) +" }")
    print("move forward clicked")

def move_back():
    sendControlMsg("{\"command\":\"S\",\"speed\": "+ str(speed_var.get() + 360) +" }")
    print("move back clicked")

def spot_right():
    sendControlMsg("{\"command\":\"K\",\"speed\": "+ str(speed_var.get() + 360) +" }")
    print("move spot right clicked")

def spot_left():
    sendControlMsg("{\"command\":\"J\",\"speed\": "+ str(speed_var.get() + 360) +" }")
    print("move spot left clicked")

def stop_move():
    sendControlMsg("{\"command\":\"F\",\"speed\": "+ str(speed_var.get() + 360) +" }")
    print("Stop clicked")

def set_speed(event):
    speed = speed_var.get() + 360
    print("set speed to", speed)
     
def showPosEvent(event):
    print("posevt")
     
def onArrowUp(event):
    move_forward() 
    print("Arrow up")

def onArrowDown(event):
    move_back()
    print("Arrow down")

def onArrowLeft(event):
    turn_left()
    print("Arrow left")

def onArrowRight(event):
    turn_right()
    print("Arrow right")

def onSpaceBar(event):
    stop_move()
    print("Spacebar pressed")


if __name__=="__main__":
    
    tkroot = tkinter.Tk()
    tkroot.title("Rover Controller")
    speed_var = tkinter.DoubleVar() #tkinter.DoubleVar()
    labelfont = ('arial', 15, 'bold')     

    #Keyboard Input           
    tkroot.bind('<Up>',onArrowUp)
    tkroot.bind('<Down>',onArrowDown)
    tkroot.bind('<Left>',onArrowLeft)
    tkroot.bind('<Right>',onArrowRight)
    tkroot.bind('<space>',onSpaceBar)
    tkroot.focus()
    #Keyboard Input End

    #Buttons
    B_right = tkinter.Button(tkroot, text =">" , command= turn_right)
    B_right.place(x = 65, y= 25, height=25, width=25)
   
    B_right_back = tkinter.Button(tkroot, text =">" , command= turn_right_back)
    B_right_back.place(x = 65, y= 55, height=25, width=25)

    B_left  = tkinter.Button(tkroot, text ="<", command= turn_left)
    B_left.place(x= 15,y = 25, height=25, width=25)

    B_left_back  = tkinter.Button(tkroot, text ="<", command= turn_left_back)
    B_left_back.place(x= 15,y = 55, height=25, width=25)

    B_up = tkinter.Button(tkroot, text ="^", command= move_forward)
    B_up.place(x= 40, y = 10 ,  height=25, width=25)

    B_down = tkinter.Button(tkroot, text ="v", command= move_back)
    B_down.place(x= 40, y = 70, height=25, width=25)

    B_stop = tkinter.Button(tkroot, text ="Stop", command= stop_move)
    B_stop.place(x = 10,y = 160 , height = 30, width=85)

    B_spot_left = tkinter.Button(tkroot, text ="<)", command= spot_left)
    B_spot_left.place(x = 100,y = 160 , height = 30, width=35)

    B_spot_right = tkinter.Button(tkroot, text =">(", command= spot_right)
    B_spot_right.place(x = 140,y = 160 , height = 30, width=35)

    speed_label = tkinter.Label(tkroot , text = "Speed")
    speed_label.place(x= 10, y = 105, height=50, width=50)

    speed_scale = tkinter.Scale(tkroot, variable = speed_var,orient = tkinter.HORIZONTAL)
    speed_scale.bind("<ButtonRelease-1>", set_speed)
    speed_scale.place(x= 60, y = 105, height=50, width=85)
    #Buttons end

    tkroot.mainloop()

当我尝试发送命令时,另一端没有任何反应,也没有传送消息! 然后我尝试嗅探数据包,结果发现我的消息中有一个错误,代理以 'Bad Request' 错误响应。

Sniffed packets stream

我想知道邮件有什么问题?是类型还是漏了什么!?

以下 Python 使用 Qpid Proton 库的代码适用于 Hono 沙盒。

from __future__ import print_function, unicode_literals
from proton import Message
from proton.handlers import MessagingHandler
from proton.reactor import Container

class HelloWorld(MessagingHandler):
    def __init__(self, server, address):
        super(HelloWorld, self).__init__()
        self.server = server
        self.address = address

    def on_start(self, event):
        print("connecting ...")
        conn = event.container.connect(self.server, sasl_enabled=True, allowed_mechs="PLAIN", allow_insecure_mechs=True, user="consumer@HONO", password="verysecret")
        event.container.create_sender(conn, self.address)

    def on_sendable(self, event):
        print("sending command")
        event.sender.send(Message(body="Hello World!", address="command/DEFAULT_TENANT/4711", content_type="text/plain", subject="call"))
        event.sender.close()
        event.connection.close()

Container(HelloWorld("amqp://hono.eclipseprojects.io:15672", "command/DEFAULT_TENANT")).run()

我想在您的代码中您主要缺少设置 SASL 属性。我还认为用户名和密码最好明确设置,而不是作为 URL 的一部分,因为用户名包含一个 @ 字符,它似乎也在 URL 中用作分隔符。