|
| 1 | +/* |
| 2 | + * Copyright (c) 2023. Smart Operating Block |
| 3 | + * |
| 4 | + * Use of this source code is governed by an MIT-style |
| 5 | + * license that can be found in the LICENSE file or at |
| 6 | + * https://opensource.org/licenses/MIT. |
| 7 | + */ |
| 8 | + |
| 9 | +package infrastructure.wot; |
| 10 | + |
| 11 | +import city.sane.wot.DefaultWot; |
| 12 | +import city.sane.wot.Wot; |
| 13 | +import city.sane.wot.WotException; |
| 14 | +import city.sane.wot.thing.ConsumedThing; |
| 15 | +import okhttp3.OkHttpClient; |
| 16 | +import okhttp3.Request; |
| 17 | +import okhttp3.Response; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Objects; |
| 22 | +import java.util.Optional; |
| 23 | +import java.util.concurrent.ExecutionException; |
| 24 | +import java.util.logging.Logger; |
| 25 | + |
| 26 | +/** |
| 27 | + * Web of things client. |
| 28 | + */ |
| 29 | +public class WotClient { |
| 30 | + private static final String THING_DIRECTORY_VARIABLE = "THING_DESCRIPTION_DIRECTORY_BASE_URL"; |
| 31 | + private ConsumedThing thing; |
| 32 | + |
| 33 | + static { |
| 34 | + // Checks on existence of environmental variable |
| 35 | + Objects.requireNonNull( |
| 36 | + System.getenv(THING_DIRECTORY_VARIABLE), |
| 37 | + "Thing Description Directory service base url is required" |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Default constructor. |
| 43 | + * @param thingId the thing id to use. |
| 44 | + */ |
| 45 | + public WotClient(final String thingId) { |
| 46 | + try { |
| 47 | + final String thingUrl = System.getenv(THING_DIRECTORY_VARIABLE) + thingId; |
| 48 | + final Wot wotClient = DefaultWot.clientOnly(); |
| 49 | + final String thingDescription = this.obtainThingDescription(thingUrl); |
| 50 | + this.thing = wotClient.consume(thingDescription); |
| 51 | + } catch (final WotException e) { |
| 52 | + Logger.getLogger(WotClient.class.toString()).info("Wot TDD configuration error"); |
| 53 | + } catch (IOException e) { |
| 54 | + Logger.getLogger(WotClient.class.toString()).info(thingId + " does not have a Thing Description associated"); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Invoke an action on the thing. |
| 60 | + * @param action the name of the action. |
| 61 | + * @param inputs the inputs of the action. |
| 62 | + */ |
| 63 | + public void invoke(final String action, final Map<String, Object> inputs) { |
| 64 | + try { |
| 65 | + this.thing.getActions().get(action).invoke(inputs).get(); |
| 66 | + } catch (final InterruptedException | ExecutionException e) { |
| 67 | + Logger.getLogger(WotClient.class.toString()).info("Error in invoking '" + action + "' on " + thing.getId()); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private String obtainThingDescription(final String thingUrl) throws IOException { |
| 72 | + // We need that because sane-city wot doesn't support application/td+json schema. |
| 73 | + final OkHttpClient httpClient = new OkHttpClient(); |
| 74 | + final Request request = new Request.Builder().url(thingUrl).get().build(); |
| 75 | + try (Response response = httpClient.newCall(request).execute()) { |
| 76 | + return Optional.ofNullable(response.body()).map(responseBody -> { |
| 77 | + try { |
| 78 | + return responseBody.string(); |
| 79 | + } catch (IOException e) { |
| 80 | + return ""; |
| 81 | + } |
| 82 | + }).orElse(""); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments