public class MqttDevice extends java.lang.Object implements IotDevice
IotDevice
connector.
The MQTT IotDevice
is an abstraction on top of
the MqttStreams
connector.
The connector doesn't presume a particular pattern for Device MQTT "event" topic and "command" topics though default patterns are provided.
Connector configuration Properties fall into two categories:
MqttConfig.fromProperties(Properties)
{mqttDevice.topic.prefix}id/{mqttDevice.id}/evt/{EVENTID}/fmt/json
The pattern must include {EVENTID} and must end with "/fmt/json".
{mqttDevice.topic.prefix}id/{mqttDevice.id}/cmd/{COMMAND}/fmt/json
The pattern must include {COMMAND} and must end with "/fmt/json".
// assuming a properties file containing at least:
// mqttDevice.id=012345678
// mqtt.serverURLs=tcp://myMqttBrokerHost:1883
String propsPath = <path to properties file>;
Properties properties = new Properties();
properties.load(Files.newBufferedReader(new File(propsPath).toPath()));
Topology t = new DirectProvider();
MqttDevice mqttDevice = new MqttDevice(t, properties);
// publish JSON "status" device event tuples every hour
TStream<JsonObject> myStatusEvents = t.poll(myGetStatusAsJson(), 1, TimeUnit.HOURS);
mqttDevice.events(myStatusEvents, "status", QoS.FIRE_AND_FORGET);
// handle a device command. In this example the payload is expected
// to be JSON and have a "value" property containing the new threshold.
mqttDevice.command("setSensorThreshold")
.sink(json -> setSensorThreshold(json.get(CMD_PAYLOAD).getAsJsonObject().get("value").getAsString());
CMD_DEVICE, CMD_FORMAT, CMD_ID, CMD_PAYLOAD, CMD_TS, RESERVED_ID_PREFIX
Constructor and Description |
---|
MqttDevice(Topology topology,
java.util.Properties properties)
Create an MqttDevice connector.
|
MqttDevice(Topology topology,
java.util.Properties properties,
MqttConfig mqttConfig)
Create an MqttDevice connector.
|
Modifier and Type | Method and Description |
---|---|
TStream<com.google.gson.JsonObject> |
commands(java.lang.String... commands)
Create a stream of device commands as JSON objects.
|
java.lang.String |
commandTopic(java.lang.String command)
Get the MQTT topic for a command.
|
TSink<com.google.gson.JsonObject> |
events(TStream<com.google.gson.JsonObject> stream,
Function<com.google.gson.JsonObject,java.lang.String> eventId,
UnaryOperator<com.google.gson.JsonObject> payload,
Function<com.google.gson.JsonObject,java.lang.Integer> qos)
Publish a stream's tuples as device events.
|
TSink<com.google.gson.JsonObject> |
events(TStream<com.google.gson.JsonObject> stream,
java.lang.String eventId,
int qos)
Publish a stream's tuples as device events.
|
java.lang.String |
eventTopic(java.lang.String eventId)
Get the MQTT topic for an device event.
|
java.lang.String |
getDeviceId()
Get the device's unique opaque device identifier.
|
java.lang.String |
getDeviceType()
Get the device's opaque device type identifier.
|
MqttConfig |
getMqttConfig()
Get the device's
MqttConfig |
Topology |
topology()
Topology this element is contained in.
|
public MqttDevice(Topology topology, java.util.Properties properties)
All configuration information comes from properties
.
topology
- topology to add the connector to.properties
- connector properties.public MqttDevice(Topology topology, java.util.Properties properties, MqttConfig mqttConfig)
Uses mattConfig
for the base MQTT connector configuration
and uses properties
only for MQTT Device properties.
topology
- topology to add the connector to.properties
- connector properties. Properties beyond those
noted in the Device properties section above are ignored.mqttConfig
- base MQTT configuration. may be null.public java.lang.String eventTopic(java.lang.String eventId)
eventId
- the event id.
if null, returns a topic filter for all of the device's events.public java.lang.String commandTopic(java.lang.String command)
command
- the command id.
if null, returns a topic filter for all of the device's commands.public MqttConfig getMqttConfig()
MqttConfig
public TSink<com.google.gson.JsonObject> events(TStream<com.google.gson.JsonObject> stream, Function<com.google.gson.JsonObject,java.lang.String> eventId, UnaryOperator<com.google.gson.JsonObject> payload, Function<com.google.gson.JsonObject,java.lang.Integer> qos)
IotDevice
Each tuple is published as a device event with the supplied functions providing the event identifier, payload and QoS. The event identifier and QoS can be generated based upon the tuple.
events
in interface IotDevice
stream
- Stream to be published.eventId
- function to supply the event identifier.payload
- function to supply the event's payload.qos
- function to supply the event's delivery Quality of Service.public TSink<com.google.gson.JsonObject> events(TStream<com.google.gson.JsonObject> stream, java.lang.String eventId, int qos)
IotDevice
Each tuple is published as a device event with fixed event identifier and QoS.
public TStream<com.google.gson.JsonObject> commands(java.lang.String... commands)
IotDevice
commands
will result in a tuple
on the stream. The JSON object has these keys:
device
- Command's opaque target device's id String.
command
- Command identifier as a Stringtsms
- Timestamp of the command in milliseconds since the 1970/1/1 epoch.format
- Format of the command as a Stringpayload
- Payload of the command
format
is json
then payload
is JSONpayload
is Stringpublic Topology topology()
TopologyElement
topology
in interface TopologyElement
public java.lang.String getDeviceType()
This connector does not support the notion of a device-type as part of its device id model. An empty string is returned.
getDeviceType
in interface IotDevice
public java.lang.String getDeviceId()
IotDevice
getDeviceId
in interface IotDevice
Copyright © 2017 The Apache Software Foundation. All Rights Reserved - 4744f56-20170226-1707