Search in sources :

Example 6 with HueResponse

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();
}
Also used : HueErrorMessage(org.openhab.io.hueemulation.internal.dto.response.HueResponse.HueErrorMessage) TypeToken(com.google.gson.reflect.TypeToken) HueResponse(org.openhab.io.hueemulation.internal.dto.response.HueResponse) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces)

Example 7 with HueResponse

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();
}
Also used : HueLightEntry(org.openhab.io.hueemulation.internal.dto.HueLightEntry) HueStateChange(org.openhab.io.hueemulation.internal.dto.changerequest.HueStateChange) EventPublisher(org.openhab.core.events.EventPublisher) Command(org.openhab.core.types.Command) TypeToken(com.google.gson.reflect.TypeToken) ArrayList(java.util.ArrayList) HueResponse(org.openhab.io.hueemulation.internal.dto.response.HueResponse) Path(javax.ws.rs.Path) Operation(io.swagger.v3.oas.annotations.Operation) PUT(javax.ws.rs.PUT)

Example 8 with HueResponse

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"));
}
Also used : HueResponse(org.openhab.io.hueemulation.internal.dto.response.HueResponse) Response(javax.ws.rs.core.Response) HueSuccessResponseCreateUser(org.openhab.io.hueemulation.internal.dto.response.HueSuccessResponseCreateUser) JsonElement(com.google.gson.JsonElement) HueResponse(org.openhab.io.hueemulation.internal.dto.response.HueResponse) Test(org.junit.jupiter.api.Test)

Aggregations

HueResponse (org.openhab.io.hueemulation.internal.dto.response.HueResponse)8 TypeToken (com.google.gson.reflect.TypeToken)6 ArrayList (java.util.ArrayList)4 Operation (io.swagger.v3.oas.annotations.Operation)3 Path (javax.ws.rs.Path)3 Command (org.openhab.core.types.Command)3 HueErrorMessage (org.openhab.io.hueemulation.internal.dto.response.HueResponse.HueErrorMessage)3 PUT (javax.ws.rs.PUT)2 EventPublisher (org.openhab.core.events.EventPublisher)2 HueStateChange (org.openhab.io.hueemulation.internal.dto.changerequest.HueStateChange)2 HueSuccessResponseCreateUser (org.openhab.io.hueemulation.internal.dto.response.HueSuccessResponseCreateUser)2 JsonElement (com.google.gson.JsonElement)1 TreeMap (java.util.TreeMap)1 POST (javax.ws.rs.POST)1 Produces (javax.ws.rs.Produces)1 Response (javax.ws.rs.core.Response)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 Test (org.junit.jupiter.api.Test)1 GroupItem (org.openhab.core.items.GroupItem)1 PercentType (org.openhab.core.library.types.PercentType)1