Hono: Java-based example for sending Command & Control messages from application to device

Created on 31 Oct 2018  路  9Comments  路  Source: eclipse/hono

First of all: big thanks for your work on Hono so far, especially for the introduced C&C functionality.

However, I wonder if there is a Java-based example available, which demonstrate the dispatchment of C&C messages from a business application to a authenticated device via the MQTT adapter?

The reason I'm asking is that I'm not that familiar with AMQP so far and currently struggeling to send such C&C messages to devices.
Based on Qpid JMS, I want to send messages via the queue/topic "control/TenantXY/DeviceXY" to an authenticated device (DeviceXY of TenantXY) subscribed to "control/+/+/req/#", but this ends up in a "javax.jms.JMSException: Transport connection remotely closed.". Probably this is because I provided the wrong queue/topic.

C&C question

All 9 comments

Thank you for your interest :-)

There is no example for a device using MQTT. However, the example application in hono-example illustrates how an application (the HonoConsumer) can send commands to an HTTP based device using the HonoClient. From the application's perspective it should make no difference if the device is connected via HTTP or MQTT. Hope this helps.

@phil-hei: In my opinion, take a look at the code in Commander.java from hono-cli and HonoConsumerBase.java from hono-example , which gives you insights on how to send C&C messages from a business application. Also take a look at the getting started guide on sending commands using CLI.

Big thanks for the hints. The example in HonoConsumerBase (I thought at first, that the name indicates an example, where telemetry data are just consumed) was indeed really helpful and sending out commands works now like a charm.

We further have a node.js application, which was able to to publish and subcribe to MQTT topics redirected by Hono. This worked in Hono 0.5, but not in the 0.7 version anymore. I assume that the Hono Api is not available in JS? Is there any recommended JavaScript AMQP client for realizing C&C and the consumption of telemetry data?

We further have a node.js application, which was able to to publish and subcribe to MQTT topics redirected by Hono. This worked in Hono 0.5, but not in the 0.7 version anymore.

Can you be a little more specific as to which topics you want to publish to/subscribe to? In Hono 0.5 you should have been able to publish to the telemetry and event topics but there was no C&C topic to subscribe to yet.

I just want to send telmetry data to the node.js applicationd and send back comand to the device. With Hono 0.5, the MQTT adapter supported, as I remember right, the publishing to specific topics, e.g. telemtry/device1, and the subscription to specific topics, e.g. driving/device1/control, for receiving "commands".

However, with Hono 0.7, we've to connect to the AMQP network to do that. We are currently unsure which AMQP client for JS will work. Proton seems not to be available for JS. Maybe there is a recommendation which client to use, at least it should support AMQP 1.0 if I'm right?

Hi,
a couple of months ago I have written a simple IoT Hub client in NodeJS. The client uses the "amqp10" node module. This module I had to patch as it contains a small bug. A detailed description of what the bug is about, please see the following bug, which I have raised for the amqp10 project: https://github.com/noodlefrenzy/node-amqp10/issues/363

The code for the simple client can be seen in the following:

`var AMQPClient = require('amqp10').Client,
Policy = require('amqp10').Policy;
var Promise = require('bluebird');

var tenant_id = ;
var messaging_endpoint = ;
var port = ;
var user = ;
var password = ;

var uri = "amqps://" + user + ":" + password + "@" + messaging_endpoint + ":" + port;

/*

  • setup AMQP10 client policy to include certificate for the HUB on AWS
  • assumption is that the certificate file e.g. iothub.pem is located
  • in root of this client
    */

var client = new AMQPClient(Policy.merge(Policy.QpidJava,{
connect: {
options:{
sslOptions: {
caFile: 'iothub.pem'
}
}
}
}));

/*

  • Establish connection by creating promises for the receiver
  • Please note that event-receiver has not been inclued in this client
    */

client.connect(uri)
.then(function() {
return Promise.all([
client.createReceiver('telemetry/' + tenant_id),
]);
}).spread(function(receiver, sender) {
receiver.on('errorReceived', function(err) {
console.log('errors: ', err);
});
receiver.on('message', function(message) {
console.log('Rx message: ', JSON.parse(message.body));
});
}).error(function(err) {
console.log("error: ", err);
});
The patch for the amqp10 module is described in the linked issue (above), however the patch is simple by just adapting the policy.js file within the amqp10/lib folder within your referenced modules. Within the policy.js file you need to touch the Policy.prototype.parseAddress function and replace var auth = matchAuth.exec(address)[1];withvar auth = parsedAddress.auth;`

Thats it.

Hope this helps a bit.

The example in HonoConsumerBase (I thought at first, that the name indicates an example, where telemetry data are just consumed)

Thanks for indicating your confusion with the name of the class - it was originally only for consumption, and now is more than that. I will think of a better name for the class, so others will not run into the same confusion.

In Hono 0.8 the class HonoConsumerBase was renamed to HonoExampleApplicationBase (as announced).

@phil-hei : is there anything open with this issue?
If not, could you consider to close it?
Thanks!

Was this page helpful?
0 / 5 - 0 ratings