@POST @Timed public Scene createNewScene( @PathParam("groupId") long groupId, @QueryParam("name") Optional<String> name) { Group group = groups.getGroupById(groupId); if (group == null) { throw new WebApplicationException(404); } Scenes scenes = group.getScenes(); Scene newScene = scenes.createNewScene(); if (name.isPresent()) { newScene.setName(name.get()); } List<Long> lightIds = group.getLights(); for (Long lightId : lightIds) { long prevStateLastUpdated = newScene.getStateLastUpdated(); Light light = lights.getLightById(lightId); serialComm.writeCommand( "storeScene -s " + light.getShortNwkAddress() + " " + light.getEndpointId() + " " + group.getGroupAddress() + " " + newScene.getId()); long timeout = System.currentTimeMillis() + Config.WAIT_ATTR_TIMEOUT_MS; while (newScene.getStateLastUpdated() <= prevStateLastUpdated) { if (System.currentTimeMillis() >= timeout) break; } } return newScene; }
@DELETE @Timed @Path("{sceneId}") public Group deleteScene(@PathParam("groupId") long groupId, @PathParam("sceneId") long sceneId) { Group group = groups.getGroupById(groupId); if (group == null) { throw new WebApplicationException(404); } Scenes scenes = group.getScenes(); Scene scene = scenes.getSceneById(sceneId); if (scene == null) { throw new WebApplicationException(404); } List<Long> lightIds = group.getLights(); for (Long lightId : lightIds) { long prevScenesLastDeleted = scenes.getScenesLastDeleted(); Light light = lights.getLightById(lightId); serialComm.writeCommand( "removeScene -s " + light.getShortNwkAddress() + " " + light.getEndpointId() + " " + group.getGroupAddress() + " " + scene.getId()); long timeout = System.currentTimeMillis() + Config.WAIT_ATTR_TIMEOUT_MS; while (scenes.getScenesLastDeleted() <= prevScenesLastDeleted) { if (System.currentTimeMillis() >= timeout) break; } } return group; }