use of org.openhab.io.hueemulation.internal.dto.HueGroupEntry in project openhab-addons by openhab.
the class RuleUtils method httpActionToHueCommand.
@SuppressWarnings({ "unused", "null" })
@Nullable
public static HueCommand httpActionToHueCommand(HueDataStore ds, Action a, @Nullable String ruleName) {
ConfigHttpAction config = a.getConfiguration().as(ConfigHttpAction.class);
// Example: "/api/<username>/groups/1/action" or "/api/<username>/lights/1/state"
String[] validation = config.url.split("/");
if (validation.length < 6 || !validation[0].isEmpty() || !"api".equals(validation[1])) {
LOGGER.warn("Hue Rule '{}': Given address in action {} invalid!", ruleName, a.getLabel());
return null;
}
if ("groups".equals(validation[3]) && "action".equals(validation[5])) {
HueGroupEntry gentry = ds.groups.get(validation[4]);
if (gentry == null) {
LOGGER.warn("Hue Rule '{}': Given address in action {} invalid. Group does not exist: {}", ruleName, a.getLabel(), validation[4]);
return null;
}
return new HueCommand(config.url, config.method, config.body);
} else if ("lights".equals(validation[3]) && "state".equals(validation[5])) {
HueLightEntry lentry = ds.lights.get(validation[4]);
if (lentry == null) {
LOGGER.warn("Hue Rule '{}': Given address in action {} invalid. Light does not exist: {}", ruleName, a.getLabel(), validation[4]);
return null;
}
return new HueCommand(config.url, config.method, config.body);
} else {
LOGGER.warn("Hue Rule '{}': Given address in action {} invalid. Can only handle lights and groups, not {}", ruleName, a.getLabel(), validation[3]);
return null;
}
}
use of org.openhab.io.hueemulation.internal.dto.HueGroupEntry in project openhab-addons by openhab.
the class LightsAndGroups method added.
@Override
public synchronized void added(Item newElement) {
if (!(newElement instanceof GenericItem)) {
return;
}
GenericItem element = (GenericItem) newElement;
if (!(element instanceof GroupItem) && !ALLOWED_ITEM_TYPES.contains(element.getType())) {
return;
}
DeviceType deviceType = StateUtils.determineTargetType(cs, element);
if (deviceType == null) {
return;
}
String hueID = cs.mapItemUIDtoHueID(element);
if (element instanceof GroupItem && !element.hasTag(EXPOSE_AS_DEVICE_TAG)) {
GroupItem g = (GroupItem) element;
HueGroupEntry group = new HueGroupEntry(g.getName(), g, deviceType);
// Restore group type and room class from tags
for (String tag : g.getTags()) {
if (tag.startsWith("huetype_")) {
group.type = tag.split("huetype_")[1];
} else if (tag.startsWith("hueroom_")) {
group.roomclass = tag.split("hueroom_")[1];
}
}
// Add group members
group.lights = new ArrayList<>();
for (Item item : g.getMembers()) {
group.lights.add(cs.mapItemUIDtoHueID(item));
}
cs.ds.groups.put(hueID, group);
} else {
HueLightEntry device = new HueLightEntry(element, cs.getHueUniqueId(hueID), deviceType);
device.item = element;
cs.ds.lights.put(hueID, device);
updateGroup0();
}
}
use of org.openhab.io.hueemulation.internal.dto.HueGroupEntry in project openhab-addons by openhab.
the class LightsAndGroups method setGroupActionApi.
@SuppressWarnings({ "null", "unused" })
@PUT
@Path("{username}/groups/{id}/action")
@Operation(summary = "Initiate group action", responses = { @ApiResponse(responseCode = "200", description = "OK") })
public //
Response setGroupActionApi(//
@Context UriInfo uri, @PathParam("username") @Parameter(description = "username") String username, @PathParam("id") @Parameter(description = "group id") String id, String body) {
if (!userManagement.authorizeUser(username)) {
return NetworkUtils.singleError(cs.gson, uri, HueResponse.UNAUTHORIZED, "Not Authorized");
}
HueGroupEntry hueDevice = cs.ds.groups.get(id);
GroupItem groupItem = hueDevice.groupItem;
if (hueDevice == null || groupItem == null) {
return NetworkUtils.singleError(cs.gson, uri, HueResponse.NOT_AVAILABLE, "Group not existing");
}
HueStateChange state = cs.gson.fromJson(body, HueStateChange.class);
if (state == null) {
return NetworkUtils.singleError(cs.gson, uri, HueResponse.INVALID_JSON, "Invalid request: No state change data received!");
}
// First synchronize the internal state information with the framework
hueDevice.action = StateUtils.colorStateFromItemState(groupItem.getState(), hueDevice.deviceType);
List<HueResponse> responses = new ArrayList<>();
Command command = StateUtils.computeCommandByState(responses, "/groups/" + id + "/state/", hueDevice.action, state);
// If a command could be created, post it to the framework now
if (command != null) {
logger.debug("sending {} to {}", command, id);
EventPublisher localEventPublisher = eventPublisher;
if (localEventPublisher != null) {
localEventPublisher.post(ItemEventFactory.createCommandEvent(groupItem.getUID(), command, "hueemulation"));
} else {
logger.warn("No event publisher. Cannot post item '{}' command!", groupItem.getUID());
}
}
return Response.ok(cs.gson.toJson(responses, new TypeToken<List<?>>() {
}.getType())).build();
}
use of org.openhab.io.hueemulation.internal.dto.HueGroupEntry in project openhab-addons by openhab.
the class LightsAndGroupsTests method addGroupWithoutTypeByTag.
@Test
public void addGroupWithoutTypeByTag() {
GroupItem item = new GroupItem("group1", null);
item.addTag("Switchable");
itemRegistry.add(item);
HueGroupEntry device = cs.ds.groups.get(cs.mapItemUIDtoHueID(item));
assertThat(device.groupItem, is(item));
assertThat(device.action, is(instanceOf(HueStatePlug.class)));
assertThat(cs.ds.groups.get(cs.mapItemUIDtoHueID(item)).groupItem, is(item));
}
use of org.openhab.io.hueemulation.internal.dto.HueGroupEntry in project openhab-addons by openhab.
the class LightsAndGroupsTests method setUp.
@BeforeEach
public void setUp() throws IOException {
commonSetup = new CommonSetup(false);
itemRegistry = new DummyItemRegistry();
this.cs = commonSetup.cs;
subject.cs = cs;
subject.eventPublisher = commonSetup.eventPublisher;
subject.userManagement = commonSetup.userManagement;
subject.itemRegistry = itemRegistry;
subject.activate();
// Add simulated lights
cs.ds.lights.put("1", new HueLightEntry(new SwitchItem("switch"), "switch", DeviceType.SwitchType));
cs.ds.lights.put("2", new HueLightEntry(new ColorItem("color"), "color", DeviceType.ColorType));
cs.ds.lights.put("3", new HueLightEntry(new ColorItem("white"), "white", DeviceType.WhiteTemperatureType));
// Add group item
cs.ds.groups.put("10", new HueGroupEntry("name", new GroupItem("white", new SwitchItem("switch")), DeviceType.SwitchType));
commonSetup.start(new ResourceConfig().registerInstances(subject));
}
Aggregations