Post message from Vernemq plugin

I want to convert an HTTP request to an MQTT request. Therefore, for this I received an HTTP request, which consists of information similar to the topic and the message for publication. I have to post the provided message in the provided topic.

I can post a message, but the problem is that I can provide the theme and payload of the function I used. Here is a snippet of code that I write to post:

Data = mochiweb_request:parse_post(Req),
{RegisterFun, PublishFun, SubscribeFun} = vmq_reg:direct_plugin_exports(http_to_mqtt),
Topic    = get_value("topic", Data),
List_of_topics = string:tokens(Topic, "/"),
Lot = lists:map(fun(X) -> list_to_binary(X) end, List_of_topics),
Payload  = list_to_binary(get_value("message", Data)),
error_logger:info_msg("Topics: ~p~nPayload: ~p",[Lot, Payload]),
PublishFun(Lot,Payload),
Req:ok({"text/html", [], "<p>Thank you. <p>"})

Here, PublishFun, which I get from vmq_reg, can only allow the subject and message. Is there any other way to post a message giving the meaning of Qos, Retain and Dup.

I create a server using mochiweb and use it as a plugin in vernemq.

+3
source share
2

VerneMQ, .

:

Data = mochiweb_request:parse_post(Req),
{RegisterFun,PublishFun,SubscribeFun} = vmq_reg:direct_plugin_exports(http_to_mqtt),
Topic    = get_value("topic", Data),
List_of_topics = string:tokens(Topic, "/"),
Lot = lists:map(fun(X) -> list_to_binary(X) end, List_of_topics),
Payload  = list_to_binary(get_value("message", Data)),
Qos = erlang:list_to_integer(get_value("qos",Data)),
Retain = erlang:list_to_integer(get_value("retain",Data)),
error_logger:info_msg("Topics: ~p~nPayload: ~p~nQOS: ~p~nRetain: ~p",[Lot, Payload,Qos,Retain]),
PublishFun(Lot,Payload,#{qos => Qos, retain => Retain}),
Req:ok({"text/html", [], "<p>Thank you. <p>"})
+1

, .

0

All Articles