Search in sources :

Example 1 with HueGroupEntry

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;
    }
}
Also used : HueCommand(org.openhab.io.hueemulation.internal.dto.changerequest.HueCommand) HueLightEntry(org.openhab.io.hueemulation.internal.dto.HueLightEntry) HueGroupEntry(org.openhab.io.hueemulation.internal.dto.HueGroupEntry) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 2 with HueGroupEntry

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();
    }
}
Also used : DeviceType(org.openhab.io.hueemulation.internal.DeviceType) GenericItem(org.openhab.core.items.GenericItem) GroupItem(org.openhab.core.items.GroupItem) Item(org.openhab.core.items.Item) HueLightEntry(org.openhab.io.hueemulation.internal.dto.HueLightEntry) GenericItem(org.openhab.core.items.GenericItem) GroupItem(org.openhab.core.items.GroupItem) HueGroupEntry(org.openhab.io.hueemulation.internal.dto.HueGroupEntry)

Example 3 with HueGroupEntry

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();
}
Also used : 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) GroupItem(org.openhab.core.items.GroupItem) HueGroupEntry(org.openhab.io.hueemulation.internal.dto.HueGroupEntry) 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 4 with HueGroupEntry

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));
}
Also used : GroupItem(org.openhab.core.items.GroupItem) HueGroupEntry(org.openhab.io.hueemulation.internal.dto.HueGroupEntry) Test(org.junit.jupiter.api.Test)

Example 5 with HueGroupEntry

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));
}
Also used : HueLightEntry(org.openhab.io.hueemulation.internal.dto.HueLightEntry) DummyItemRegistry(org.openhab.io.hueemulation.internal.rest.mocks.DummyItemRegistry) ColorItem(org.openhab.core.library.items.ColorItem) GroupItem(org.openhab.core.items.GroupItem) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) HueGroupEntry(org.openhab.io.hueemulation.internal.dto.HueGroupEntry) SwitchItem(org.openhab.core.library.items.SwitchItem) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

HueGroupEntry (org.openhab.io.hueemulation.internal.dto.HueGroupEntry)9 GroupItem (org.openhab.core.items.GroupItem)8 HueLightEntry (org.openhab.io.hueemulation.internal.dto.HueLightEntry)5 GenericItem (org.openhab.core.items.GenericItem)3 SwitchItem (org.openhab.core.library.items.SwitchItem)3 Operation (io.swagger.v3.oas.annotations.Operation)2 ArrayList (java.util.ArrayList)2 Path (javax.ws.rs.Path)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Test (org.junit.jupiter.api.Test)2 Item (org.openhab.core.items.Item)2 DeviceType (org.openhab.io.hueemulation.internal.DeviceType)2 TypeToken (com.google.gson.reflect.TypeToken)1 POST (javax.ws.rs.POST)1 PUT (javax.ws.rs.PUT)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)1 EventPublisher (org.openhab.core.events.EventPublisher)1 ColorItem (org.openhab.core.library.items.ColorItem)1 ContactItem (org.openhab.core.library.items.ContactItem)1