sane-city / wot-servient

W3C Web of Things implementation for Java
MIT License
30 stars 5 forks source link

Unable to invoke action with parameters #11

Closed LeebPhil closed 3 years ago

LeebPhil commented 3 years ago

Good evening,

I'm trying to invoke an action with parameters/UriVars and it does not work. I tried it with a simple version of your example (CounterUriVariables) but it only produces the following exception:

[ForkJoinPool.commonPool-worker-5] DEBUG org.apache.http.wire - http-outgoing-1 << "city.sane.wot.content.ContentCodecException: Failed to decode application/json: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token[\n]"
17:57:43.514 [ForkJoinPool.commonPool-worker-5] DEBUG org.apache.http.wire - http-outgoing-1 << " at [Source: (byte[])"{"step":1}"; line: 1, column: 1]"

Which leads to a 503 null response from exposedThing.

Two code snippets are:

Object invoke = consumedCs.getAction("increment") .invoke(Map.of("step", 1)).join();
System.out.println(invoke);

and

        exposedThing.addAction("increment",
                new ThingAction.Builder()
                        .setDescription("Incrementing counter value with optional step value as uriVariable")
                        .setUriVariables(Map.of(
                                "step", Map.of(
                                        "type", "integer",
                                        "minimum", 1,
                                        "maximum", 250
                                )
                        ))
                        .build(),
                (input, options) -> {
                    System.out.println("CounterUriVariables: Incrementing, input= " + input + ", options= " + options);
                    exposedThing.getProperty("myProperty").read().thenApply(value -> {
                        int step;
                        if (input != null && ((Map) input).containsKey("step")) {
                            step = (Integer) ((Map) input).get("step");
                        }
                        else if (options.containsKey("uriVariables") && options.get("uriVariables").containsKey("step")) {
                            step = (int) options.get("uriVariables").get("step");
                        }
                        else {
                            step = 1;
                        }

                        int newValue = ((Integer) value) + step;
                        exposedThing.getProperty("myProperty").write(newValue);
                        return newValue;
                    });
                }
        );

current used of city.sane.wot-servient: 1.13

HeikoBornholdt commented 3 years ago

Hey,

thank you for the issue. There is an error in the example. You have to add a .setInput(new ObjectSchema()) to the ThingAction.Builder object.