io.github.twinklekhj.ros.op.RosResponse Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rosbridge Show documentation
Show all versions of rosbridge Show documentation
RosBridge library for Java implementing ros bridge protocol
package io.github.twinklekhj.ros.op;
import io.vertx.core.json.JsonObject;
import lombok.*;
import java.util.Collections;
import java.util.Map;
@Builder
@AllArgsConstructor
@RequiredArgsConstructor
@ToString
public class RosResponse implements RosOperation {
private final Type op = Type.CALL_SERVICE;
@NonNull
private final String service;
@NonNull
private final boolean result;
private String id;
private Map values;
private static RosResponseBuilder builder() {
return new RosResponseBuilder();
}
public static RosResponseBuilder builder(String service, boolean result) {
return builder().service(service).result(result);
}
public static RosResponse fromString(String str) {
JsonObject node = new JsonObject(str);
return fromJsonObject(node);
}
public static RosResponse fromJsonObject(JsonObject node) {
Object valuesObj = node.getMap().get("values");
String id = node.getString("id");
Map values = Collections.emptyMap();
if (valuesObj instanceof Map) {
values = (Map) valuesObj;
} else if (valuesObj instanceof JsonObject) {
values = ((JsonObject) valuesObj).getMap();
}
boolean result = node.getBoolean("result");
String service = node.getString("service");
return new RosResponse(service, result, id, values);
}
public String getService() {
return service;
}
public boolean getResult() {
return result;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Map getValues() {
return values;
}
public void setValues(Map values) {
this.values = values;
}
@Override
public String toJson() {
return new JsonObject()
.put("op", this.op.code)
.put("service", this.service)
.put("values", this.values)
.put("result", this.result)
.put("id", this.id).toString();
}
@Override
public Type getOperation() {
return this.op;
}
}