Search in sources :

Example 1 with SwitchItem

use of org.eclipse.smarthome.core.library.items.SwitchItem in project smarthome by eclipse.

the class ItemResource method postItemCommand.

@POST
@RolesAllowed({ Role.USER, Role.ADMIN })
@Path("/{itemname: [a-zA-Z_0-9]*}")
@Consumes(MediaType.TEXT_PLAIN)
@ApiOperation(value = "Sends a command to an item.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 404, message = "Item not found"), @ApiResponse(code = 400, message = "Item command null") })
public Response postItemCommand(@PathParam("itemname") @ApiParam(value = "item name", required = true) String itemname, @ApiParam(value = "valid item command (e.g. ON, OFF, UP, DOWN, REFRESH)", required = true) String value) {
    Item item = getItem(itemname);
    Command command = null;
    if (item != null) {
        if ("toggle".equalsIgnoreCase(value) && (item instanceof SwitchItem || item instanceof RollershutterItem)) {
            if (OnOffType.ON.equals(item.getStateAs(OnOffType.class))) {
                command = OnOffType.OFF;
            }
            if (OnOffType.OFF.equals(item.getStateAs(OnOffType.class))) {
                command = OnOffType.ON;
            }
            if (UpDownType.UP.equals(item.getStateAs(UpDownType.class))) {
                command = UpDownType.DOWN;
            }
            if (UpDownType.DOWN.equals(item.getStateAs(UpDownType.class))) {
                command = UpDownType.UP;
            }
        } else {
            command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), value);
        }
        if (command != null) {
            logger.debug("Received HTTP POST request at '{}' with value '{}'.", uriInfo.getPath(), value);
            eventPublisher.post(ItemEventFactory.createCommandEvent(itemname, command));
            ResponseBuilder resbuilder = Response.ok();
            resbuilder.type(MediaType.TEXT_PLAIN);
            return resbuilder.build();
        } else {
            logger.warn("Received HTTP POST request at '{}' with an invalid status value '{}'.", uriInfo.getPath(), value);
            return Response.status(Status.BAD_REQUEST).build();
        }
    } else {
        logger.info("Received HTTP POST request at '{}' for the unknown item '{}'.", uriInfo.getPath(), itemname);
        throw new WebApplicationException(404);
    }
}
Also used : ActiveItem(org.eclipse.smarthome.core.items.ActiveItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) WebApplicationException(javax.ws.rs.WebApplicationException) Command(org.eclipse.smarthome.core.types.Command) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) UpDownType(org.eclipse.smarthome.core.library.types.UpDownType) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with SwitchItem

use of org.eclipse.smarthome.core.library.items.SwitchItem in project smarthome by eclipse.

the class RunRuleModuleTest method before.

@Before
public void before() {
    registerService(new ItemProvider() {

        @Override
        public void addProviderChangeListener(final ProviderChangeListener<Item> listener) {
        }

        @Override
        public void removeProviderChangeListener(final ProviderChangeListener<Item> listener) {
        }

        @Override
        public Collection<Item> getAll() {
            return Arrays.asList(new Item[] { new SwitchItem("switch1"), new SwitchItem("switch2"), new SwitchItem("switch3"), new SwitchItem("ruleTrigger") });
        }
    });
    registerService(volatileStorageService);
}
Also used : ItemProvider(org.eclipse.smarthome.core.items.ItemProvider) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Item(org.eclipse.smarthome.core.items.Item) Collection(java.util.Collection) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Before(org.junit.Before)

Example 3 with SwitchItem

use of org.eclipse.smarthome.core.library.items.SwitchItem in project smarthome by eclipse.

the class RuntimeRuleTest method before.

@Before
public void before() {
    registerService(new ItemProvider() {

        @Override
        public void addProviderChangeListener(final ProviderChangeListener<Item> listener) {
        }

        @Override
        public void removeProviderChangeListener(final ProviderChangeListener<Item> listener) {
        }

        @Override
        public Collection<Item> getAll() {
            return Arrays.asList(new Item[] { new SwitchItem("myMotionItem"), new SwitchItem("myPresenceItem"), new SwitchItem("myLampItem"), new SwitchItem("myMotionItem2"), new SwitchItem("myPresenceItem2"), new SwitchItem("myLampItem2"), new SwitchItem("myMotionItem3"), new SwitchItem("myPresenceItem3"), new SwitchItem("myLampItem3"), new SwitchItem("myMotionItem4"), new SwitchItem("myPresenceItem4"), new SwitchItem("myLampItem4") });
        }
    });
    registerService(volatileStorageService);
}
Also used : ItemProvider(org.eclipse.smarthome.core.items.ItemProvider) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Item(org.eclipse.smarthome.core.items.Item) Collection(java.util.Collection) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Before(org.junit.Before)

Example 4 with SwitchItem

use of org.eclipse.smarthome.core.library.items.SwitchItem in project smarthome by eclipse.

the class CmdServlet method service.

@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    for (Object key : req.getParameterMap().keySet()) {
        String itemName = key.toString();
        if (!itemName.startsWith("__")) {
            // all additional webapp params start with "__" and should be ignored
            String commandName = req.getParameter(itemName);
            try {
                Item item = itemRegistry.getItem(itemName);
                // into real commands by the webapp.
                if ((item instanceof SwitchItem || item instanceof GroupItem) && commandName.equals("TOGGLE")) {
                    commandName = OnOffType.ON.equals(item.getStateAs(OnOffType.class)) ? "OFF" : "ON";
                }
                Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandName);
                if (command != null) {
                    eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, command));
                } else {
                    logger.warn("Received unknown command '{}' for item '{}'", commandName, itemName);
                }
            } catch (ItemNotFoundException e) {
                logger.warn("Received command '{}' for item '{}', but the item does not exist in the registry", commandName, itemName);
            }
        }
    }
}
Also used : SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) Command(org.eclipse.smarthome.core.types.Command) GroupItem(org.eclipse.smarthome.core.items.GroupItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 5 with SwitchItem

use of org.eclipse.smarthome.core.library.items.SwitchItem in project smarthome by eclipse.

the class CmdServlet method service.

@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    res.setContentType("text/plain");
    for (Object key : req.getParameterMap().keySet()) {
        String itemName = key.toString();
        if (!itemName.startsWith("__")) {
            // all additional webapp params start with "__" and should be ignored
            String commandName = req.getParameter(itemName);
            try {
                Item item = itemRegistry.getItem(itemName);
                // into real commands by the webapp.
                if ((item instanceof SwitchItem || item instanceof GroupItem) && commandName.equals("TOGGLE")) {
                    commandName = OnOffType.ON.equals(item.getStateAs(OnOffType.class)) ? "OFF" : "ON";
                }
                Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandName);
                if (command != null) {
                    eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, command));
                } else {
                    logger.warn("Received unknown command '{}' for item '{}'", commandName, itemName);
                }
            } catch (ItemNotFoundException e) {
                logger.warn("Received command '{}' for item '{}', but the item does not exist in the registry", commandName, itemName);
            }
        }
    }
}
Also used : SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) Command(org.eclipse.smarthome.core.types.Command) GroupItem(org.eclipse.smarthome.core.items.GroupItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Aggregations

SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)24 Test (org.junit.Test)17 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)16 ArithmeticGroupFunction (org.eclipse.smarthome.core.library.types.ArithmeticGroupFunction)11 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)9 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)9 Before (org.junit.Before)9 GroupItem (org.eclipse.smarthome.core.items.GroupItem)7 DimmerItem (org.eclipse.smarthome.core.library.items.DimmerItem)7 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)7 State (org.eclipse.smarthome.core.types.State)7 Item (org.eclipse.smarthome.core.items.Item)6 StringType (org.eclipse.smarthome.core.library.types.StringType)6 Temperature (javax.measure.quantity.Temperature)5 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)5 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)5 Collection (java.util.Collection)4 Collections (java.util.Collections)4 HashSet (java.util.HashSet)4 LinkedList (java.util.LinkedList)4