Skip to content

Package: DeviceManagementCommands

DeviceManagementCommands

nameinstructionbranchcomplexitylinemethod
DeviceManagementCommands()
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
sendCommand(ScopeId, EntityId, Long, DeviceCommandInput)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.app.api.resources.v1.resources;
14:
15: import javax.ws.rs.Consumes;
16: import javax.ws.rs.DefaultValue;
17: import javax.ws.rs.POST;
18: import javax.ws.rs.Path;
19: import javax.ws.rs.PathParam;
20: import javax.ws.rs.Produces;
21: import javax.ws.rs.QueryParam;
22: import javax.ws.rs.core.MediaType;
23:
24: import org.eclipse.kapua.KapuaException;
25: import org.eclipse.kapua.app.api.core.resources.AbstractKapuaResource;
26: import org.eclipse.kapua.app.api.core.model.EntityId;
27: import org.eclipse.kapua.app.api.core.model.ScopeId;
28: import org.eclipse.kapua.locator.KapuaLocator;
29: import org.eclipse.kapua.service.KapuaService;
30: import org.eclipse.kapua.service.device.management.command.DeviceCommandInput;
31: import org.eclipse.kapua.service.device.management.command.DeviceCommandManagementService;
32: import org.eclipse.kapua.service.device.management.command.DeviceCommandOutput;
33: import org.eclipse.kapua.service.device.registry.Device;
34:
35: @Path("{scopeId}/devices/{deviceId}/commands")
36: public class DeviceManagementCommands extends AbstractKapuaResource {
37:
38: private final KapuaLocator locator = KapuaLocator.getInstance();
39: private final DeviceCommandManagementService commandService = locator.getService(DeviceCommandManagementService.class);
40:
41: /**
42: * Executes a remote command on a device and return the command output.
43: * <p>
44: * <p>
45: * Example to list all files in the current working directory:
46: * <p>
47: *
48: * <pre>
49: * Client client = client();
50: * WebResource apisWeb = client.resource(APIS_TEST_URL);
51: * WebResource.Builder deviceCommandWebXml = apisWeb.path("devices")
52: * .path(s_clientId)
53: * .path("command")
54: * .accept(MediaType.APPLICATION_XML)
55: * .type(MediaType.APPLICATION_XML);
56: *
57: * DeviceCommandInput commandInput = new DeviceCommandInput();
58: * commandInput.setCommand("ls");
59: * commandInput.setArguments(new String[] { "-l", "-a" });
60: *
61: * DeviceCommandOutput commandOutput = deviceCommandWebXml.post(DeviceCommandOutput.class, commandInput);
62: * </pre>
63: *
64: * @param scopeId
65: * The {@link ScopeId} of the {@link Device}.
66: * @param deviceId
67: * The {@link Device} ID.
68: * @param timeout
69: * The timeout of the command execution
70: * @param commandInput
71: * The input command
72: * @return The command output.
73: * @throws KapuaException
74: * Whenever something bad happens. See specific {@link KapuaService} exceptions.
75: * @since 1.0.0
76: */
77: @POST
78: @Path("_execute")
79: @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
80: @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
81: public DeviceCommandOutput sendCommand(
82: @PathParam("scopeId") ScopeId scopeId,
83: @PathParam("deviceId") EntityId deviceId,
84: @QueryParam("timeout") @DefaultValue("30000") Long timeout,
85: DeviceCommandInput commandInput) throws KapuaException {
86: return commandService.exec(scopeId, deviceId, commandInput, timeout);
87: }
88: }