| 
 | 1 | +// Copyright 2024 Contributors to the Eclipse Foundation. All rights reserved.  | 
 | 2 | +// Use of this source code is governed by a BSD-style  | 
 | 3 | +// license that can be found in the LICENSE file.  | 
 | 4 | +//  | 
 | 5 | +// SPDX-License-Identifier: BSD-3-Clause  | 
 | 6 | + | 
 | 7 | +import "dart:convert";  | 
 | 8 | +import "dart:io";  | 
 | 9 | + | 
 | 10 | +import "package:args/args.dart";  | 
 | 11 | +import "package:dart_wot/binding_coap.dart";  | 
 | 12 | +import "package:dart_wot/binding_http.dart";  | 
 | 13 | +import "package:dart_wot/binding_mqtt.dart";  | 
 | 14 | +import "package:dart_wot/core.dart";  | 
 | 15 | + | 
 | 16 | +const success = 0;  | 
 | 17 | + | 
 | 18 | +Future<void> main(List<String> args) async {  | 
 | 19 | +  exitCode = success;  | 
 | 20 | + | 
 | 21 | +  final servient = Servient.create(  | 
 | 22 | +    clientFactories: [  | 
 | 23 | +      CoapClientFactory(),  | 
 | 24 | +      HttpClientFactory(),  | 
 | 25 | +      MqttClientFactory(),  | 
 | 26 | +    ],  | 
 | 27 | +  );  | 
 | 28 | + | 
 | 29 | +  final wot = await servient.start();  | 
 | 30 | + | 
 | 31 | +  final argParser = ArgParser()  | 
 | 32 | +    ..addCommand("read-property")  | 
 | 33 | +    ..addCommand("request-td");  | 
 | 34 | + | 
 | 35 | +  final argResults = argParser.parse(args);  | 
 | 36 | + | 
 | 37 | +  final command = argResults.command;  | 
 | 38 | + | 
 | 39 | +  switch (command?.name) {  | 
 | 40 | +    case "read-property":  | 
 | 41 | +      final uri = Uri.parse(command?.arguments.first ?? "");  | 
 | 42 | +      final thingDescription = await wot.requestThingDescription(uri);  | 
 | 43 | + | 
 | 44 | +      final consumedThing = await wot.consume(thingDescription);  | 
 | 45 | +      final propertyKey = command?.arguments.elementAtOrNull(1) ?? "";  | 
 | 46 | + | 
 | 47 | +      final interactionOutput = await consumedThing.readProperty(propertyKey);  | 
 | 48 | +      final value = await interactionOutput.value();  | 
 | 49 | + | 
 | 50 | +      stdout.write(value);  | 
 | 51 | +    case "request-td":  | 
 | 52 | +      final uri = Uri.parse(command?.arguments.first ?? "");  | 
 | 53 | +      final thingDescription = await wot.requestThingDescription(uri);  | 
 | 54 | +      writeThingDescription(thingDescription);  | 
 | 55 | +  }  | 
 | 56 | +}  | 
 | 57 | + | 
 | 58 | +void writeThingDescription(ThingDescription thingDescription) {  | 
 | 59 | +  // TODO: Also support other serialization formats (especially CBOR)  | 
 | 60 | +  final thingDescriptionJson = jsonEncode(thingDescription.toJson());  | 
 | 61 | +  stdout.write(thingDescriptionJson);  | 
 | 62 | +}  | 
0 commit comments