Skip to content

Commit

Permalink
feat: add first version of MQTT binding
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Aug 22, 2022
1 parent bf83ce2 commit 181bf9a
Show file tree
Hide file tree
Showing 11 changed files with 749 additions and 5 deletions.
76 changes: 71 additions & 5 deletions example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,44 @@

import 'package:dart_wot/dart_wot.dart';

final Map<String, BasicCredentials> basicCredentials = {
'urn:test': BasicCredentials('rw', 'readwrite')
};

Future<BasicCredentials?> basicCredentialsCallback(
Uri uri,
Form? form, [
BasicCredentials? invalidCredentials,
]) async {
final id = form?.interactionAffordance.thingDescription.identifier;

return basicCredentials[id];
}

Future<void> main(List<String> args) async {
final CoapClientFactory coapClientFactory = CoapClientFactory();
final HttpClientFactory httpClientFactory = HttpClientFactory();
final servient = Servient()
final MqttClientFactory mqttClientFactory = MqttClientFactory();
final servient = Servient(
clientSecurityProvider: ClientSecurityProvider(
basicCredentialsCallback: basicCredentialsCallback,
),
)
..addClientFactory(coapClientFactory)
..addClientFactory(httpClientFactory);
..addClientFactory(httpClientFactory)
..addClientFactory(mqttClientFactory);
final wot = await servient.start();

const thingDescriptionJson = '''
{
"@context": "http://www.w3.org/ns/td",
"title": "Test Thing",
"id": "urn:test",
"base": "coap://coap.me",
"security": ["nosec_sc"],
"security": ["auto_sc"],
"securityDefinitions": {
"nosec_sc": {
"scheme": "nosec"
"auto_sc": {
"scheme": "auto"
}
},
"properties": {
Expand All @@ -34,6 +55,28 @@ Future<void> main(List<String> args) async {
"href": "/hello"
}
]
},
"status2": {
"observable": true,
"forms": [
{
"href": "mqtt://test.mosquitto.org:1884",
"mqv:filter": "test",
"op": ["readproperty", "observeproperty"],
"contentType": "text/plain"
}
]
}
},
"actions": {
"toggle": {
"forms": [
{
"href": "mqtt://test.mosquitto.org:1884",
"mqv:topic": "test",
"mqv:retain": true
}
]
}
}
}
Expand All @@ -44,6 +87,19 @@ Future<void> main(List<String> args) async {
final status = await consumedThing.readProperty('status');
final value = await status.value();
print(value);
final subscription = await consumedThing.observeProperty(
'status2',
(data) async {
final value = await data.value();
print(value);
},
);

await consumedThing.invokeAction('toggle', 'Hello World!');
await consumedThing.invokeAction('toggle', 'Hello World!');
await consumedThing.invokeAction('toggle', 'Hello World!');
await consumedThing.invokeAction('toggle', 'Hello World!');
await subscription.stop();

final thingUri = Uri.parse(
'https://raw.githubusercontent.com/w3c/wot-testing'
Expand All @@ -61,5 +117,15 @@ Future<void> main(List<String> args) async {
);
}

await consumedThing.invokeAction('toggle', 'Bye World!');
await consumedThing.readAndPrintProperty('status2');
print('Done!');
}

extension ReadAndPrintExtension on ConsumedThing {
Future<void> readAndPrintProperty(String propertyName) async {
final output = await readProperty(propertyName);
final value = await output.value();
print(value);
}
}
13 changes: 13 additions & 0 deletions lib/binding_mqtt.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2022 The NAMIB Project Developers. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// SPDX-License-Identifier: BSD-3-Clause

/// Protocol binding for the Message Queue Telemetry Transport (MQTT). Follows
/// the latest [WoT Binding Templates Specification][spec link] for MQTT.
///
/// [spec link]: https://w3c.github.io/wot-binding-templates/bindings/protocols/mqtt
export 'src/binding_mqtt/mqtt_client_factory.dart';
export 'src/binding_mqtt/mqtt_config.dart';
1 change: 1 addition & 0 deletions lib/dart_wot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ library dart_wot;

export 'binding_coap.dart';
export 'binding_http.dart';
export 'binding_mqtt.dart';
export 'core.dart';
export 'definitions.dart';
export 'scripting_api.dart';
51 changes: 51 additions & 0 deletions lib/src/binding_mqtt/constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2022 The NAMIB Project Developers. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// SPDX-License-Identifier: BSD-3-Clause

/// The URI scheme for unsecured MQTT (using TCP).
///
/// Note that this scheme is not standardized yet, as there is an ongoing debate
/// about URI schemes in the context of MQTT.
const mqttUriScheme = 'mqtt';

/// The default port number used for the [mqttUriScheme].
const defaultMqttPort = 1883;

/// The URI scheme for secure MQTT (using TCP and TLS).
///
/// Note that this scheme is not standardized yet, as there is an ongoing debate
/// about URI schemes in the context of MQTT.
const mqttSecureUriScheme = 'mqtts';

/// The default port number used for the [mqttSecureUriScheme].
const defaultMqttSecurePort = 8883;

/// URI pointing to the MQTT vocabulary.
///
/// Used for resolving MQTT-related compact URIs (CURIEs) in TDs. Note that
/// the MQTT vocabulary is not standardized yet, so this URI will change in
/// future versions of this library.
const mqttContextUri = 'http://www.example.org/mqtt-binding#';

/// The default prefix used in MQTT-related compact URIs (CURIEs) in TDs.
const defaultMqttPrefix = 'mqv';

/// Default timeout length used for reading properties and discovering TDs.
const defaultTimeout = Duration(seconds: 10);

/// Default duration MQTT connections are kept alive in seconds.
const defaultKeepAlivePeriod = 20;

/// Default content type when returning a `Content` object from the MQTT
/// binding.
///
/// Evaluates to `'application/octet-stream'.
const defaultContentType = 'application/octet-stream';

/// Content type used for the Content objects returned by discovery using MQTT.
///
/// Evaluates to `application/td+json`.
// TODO: Should probably be redefined globally
const discoveryContentType = 'application/td+json';
16 changes: 16 additions & 0 deletions lib/src/binding_mqtt/mqtt_binding_exception.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2022 The NAMIB Project Developers. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// SPDX-License-Identifier: BSD-3-Clause

/// An [Exception] that is thrown if an error occurs within the MQTT binding.
class MqttBindingException implements Exception {
/// Constructor.
MqttBindingException(this._message);

final String _message;

@override
String toString() => 'MqttBindingException: $_message';
}
Loading

0 comments on commit 181bf9a

Please sign in to comment.