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);
}
}
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);
}
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);
}
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);
}
}
}
}
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);
}
}
}
}
Aggregations