dart_wot 0.5.0 copy "dart_wot: ^0.5.0" to clipboard
dart_wot: ^0.5.0 copied to clipboard

outdated

A W3C Web of Things implementation written in Dart. Supports interacting with Things using CoAP and HTTP(S).

example/dart_wot_example.dart

// Copyright 2021 The NAMIB Project Developers
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: MIT OR Apache-2.0

import 'dart:io';

import 'package:dart_wot/dart_wot.dart';

final thingDescriptionJson = '''
{
  "@context": ["http://www.w3.org/ns/td", {"@language": "de"}],
  "title": "Test Thing",
  "base": "coap://coap.me",
  "securityDefinitions": {
    "nosec_sc": {
      "scheme": "nosec",
      "descriptions": {
        "de": "Keine Sicherheit",
        "en": "No Security"
      }
    }
  },
  "properties": {
    "status": {
      "observable": true,
      "forms": [
        {
          "href": "/.well-known/core"
        },
        {
          "href": "/hello",
          "op": ["observeproperty", "unobserveproperty"]
        }
      ]
    },
    "differentStatus": {
      "forms": [
        {
          "href": "coap://coap.me",
          "cov:methodName": "PUT"
        }
      ]
    }
  },
  "actions": {
    "toggle": {
      "forms": [
        {
          "href": "coap://coap.me"
        }
      ]
    }
  },
  "events": {
    "overheating": {
      "forms": [
        {
          "href": "coap://coap.me"
        }
      ]
    }
  }
}
''';

Future<void> main() async {
  // TODO(JKRhb): Add a proper example
  final coapConfig = CoapConfig(blocksize: 64);
  final CoapClientFactory coapClientFactory = CoapClientFactory(coapConfig);
  final HttpClientFactory httpClientFactory = HttpClientFactory();
  final servient = Servient()
    ..addClientFactory(coapClientFactory)
    ..addClientFactory(httpClientFactory);
  final wot = await servient.start();

  final thingDescription = ThingDescription(thingDescriptionJson);
  final consumedThing = await wot.consume(thingDescription);
  final status = await consumedThing.readProperty("status", null);
  final value1 = await status.value();
  print(value1);
  await consumedThing.invokeAction("toggle", null, null);
  final status2 = await consumedThing.readProperty("status", null);
  final value2 = await status2.value();
  print(value2);

  Subscription? subscription;

  // TODO(JKRhb): Turn into a "real" observation example.
  subscription = await consumedThing.observeProperty("status", (data) async {
    final value = await data.value();
    print(value);
    await subscription?.stop();
  });

  Future<void> handleThingInteraction(ThingDescription thingDescription) async {
    final consumedThing = await wot.consume(thingDescription);
    print("The title of the fetched TD is ${consumedThing.title}.");
    print("Done!");

    exit(0);
  }

  wot.discover(
      handleThingInteraction,
      ThingFilter(
          "https://raw.githubusercontent.com/w3c/wot-testing"
          "/b07fa6124bca7796e6ca752a3640fac264d3bcbc/events/2021.03.Online/TDs"
          "/Oracle/oracle-Festo_Shared.td.jsonld",
          DiscoveryMethod.direct));
}
2
likes
0
pub points
20%
popularity

Publisher

verified publisherthingweb.io

A W3C Web of Things implementation written in Dart. Supports interacting with Things using CoAP and HTTP(S).

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

cbor, coap, http, http_parser, typed_data, uuid

More

Packages that depend on dart_wot