use of org.openhab.io.hueemulation.internal.dto.response.HueResponse in project openhab-addons by openhab.
the class ConfigurationAccess method catchAll.
@Path("{username}/{var:.+}")
@Produces(MediaType.APPLICATION_JSON)
public Response catchAll(@Context UriInfo uri) {
HueResponse e = new HueResponse(new HueErrorMessage(HueResponse.INVALID_JSON, uri.getPath().replace("/api", ""), "Invalid request: "));
String str = cs.gson.toJson(Collections.singleton(e), new TypeToken<List<?>>() {
}.getType());
return Response.status(404).entity(str).build();
}
use of org.openhab.io.hueemulation.internal.dto.response.HueResponse in project openhab-addons by openhab.
the class LightsAndGroups method setLightStateApi.
@SuppressWarnings({ "null", "unused" })
@PUT
@Path("{username}/lights/{id}/state")
@Operation(summary = "Set light state", responses = { @ApiResponse(responseCode = "200", description = "OK") })
public //
Response setLightStateApi(//
@Context UriInfo uri, @PathParam("username") @Parameter(description = "username") String username, @PathParam("id") @Parameter(description = "light id") String id, String body) {
if (!userManagement.authorizeUser(username)) {
return NetworkUtils.singleError(cs.gson, uri, HueResponse.UNAUTHORIZED, "Not Authorized");
}
HueLightEntry hueDevice = cs.ds.lights.get(id);
if (hueDevice == null) {
return NetworkUtils.singleError(cs.gson, uri, HueResponse.NOT_AVAILABLE, "Light not existing");
}
HueStateChange newState = cs.gson.fromJson(body, HueStateChange.class);
if (newState == null) {
return NetworkUtils.singleError(cs.gson, uri, HueResponse.INVALID_JSON, "Invalid request: No state change data received!");
}
hueDevice.state = StateUtils.colorStateFromItemState(hueDevice.item.getState(), hueDevice.deviceType);
String itemUID = hueDevice.item.getUID();
List<HueResponse> responses = new ArrayList<>();
Command command = StateUtils.computeCommandByState(responses, "/lights/" + id + "/state", hueDevice.state, newState);
// If a command could be created, post it to the framework now
if (command != null) {
EventPublisher localEventPublisher = eventPublisher;
if (localEventPublisher != null) {
logger.debug("sending {} to {}", command, itemUID);
localEventPublisher.post(ItemEventFactory.createCommandEvent(itemUID, command, "hueemulation"));
} else {
logger.warn("No event publisher. Cannot post item '{}' command!", itemUID);
}
hueDevice.lastCommand = command;
hueDevice.lastHueChange = newState;
}
return Response.ok(cs.gson.toJson(responses, new TypeToken<List<?>>() {
}.getType())).build();
}
use of org.openhab.io.hueemulation.internal.dto.response.HueResponse in project openhab-addons by openhab.
the class UsersAndConfigTests method addUser.
@Test
public void addUser() {
// GET should fail
assertEquals(405, commonSetup.client.target(commonSetup.basePath).request().get().getStatus());
String body = "{'username':'testuser','devicetype':'app#device'}";
Response response;
HueResponse[] r;
// Post should create a user, except: if linkbutton not enabled
response = commonSetup.client.target(commonSetup.basePath).request().post(Entity.json(body));
assertThat(response.getStatus(), is(200));
r = commonSetup.cs.gson.fromJson(response.readEntity(String.class), HueResponse[].class);
assertNotNull(r[0].error);
// Post should create a user
commonSetup.cs.ds.config.linkbutton = true;
response = commonSetup.client.target(commonSetup.basePath).request().post(Entity.json(body));
assertThat(response.getStatus(), is(200));
JsonElement e = JsonParser.parseString(response.readEntity(String.class)).getAsJsonArray().get(0);
e = e.getAsJsonObject().get("success");
HueSuccessResponseCreateUser rc = commonSetup.cs.gson.fromJson(e, HueSuccessResponseCreateUser.class);
assertNotNull(rc);
assertThat(commonSetup.cs.ds.config.whitelist.get(rc.username).name, is("app#device"));
}
Aggregations