|
|
|
@ -46,16 +46,7 @@ import org.wso2.carbon.device.mgt.iot.util.ZipUtil;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import javax.ws.rs.Consumes;
|
|
|
|
|
import javax.ws.rs.DELETE;
|
|
|
|
|
import javax.ws.rs.GET;
|
|
|
|
|
import javax.ws.rs.HeaderParam;
|
|
|
|
|
import javax.ws.rs.POST;
|
|
|
|
|
import javax.ws.rs.PUT;
|
|
|
|
|
import javax.ws.rs.Path;
|
|
|
|
|
import javax.ws.rs.PathParam;
|
|
|
|
|
import javax.ws.rs.Produces;
|
|
|
|
|
import javax.ws.rs.QueryParam;
|
|
|
|
|
import javax.ws.rs.*;
|
|
|
|
|
import javax.ws.rs.core.Context;
|
|
|
|
|
import javax.ws.rs.core.MediaType;
|
|
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
@ -139,307 +130,6 @@ public class ArduinoService {
|
|
|
|
|
return internalControlsQueue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------
|
|
|
|
|
Device management specific APIs
|
|
|
|
|
--------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param deviceId
|
|
|
|
|
* @param name
|
|
|
|
|
* @param owner
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
@Path("manager/device/register")
|
|
|
|
|
@PUT
|
|
|
|
|
public boolean register(@QueryParam("deviceId") String deviceId,
|
|
|
|
|
@QueryParam("name") String name, @QueryParam("owner") String owner) {
|
|
|
|
|
|
|
|
|
|
DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
|
|
|
|
|
|
|
|
|
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
|
|
|
|
deviceIdentifier.setId(deviceId);
|
|
|
|
|
deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE);
|
|
|
|
|
try {
|
|
|
|
|
if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) {
|
|
|
|
|
response.setStatus(Response.Status.CONFLICT.getStatusCode());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
Device device = new Device();
|
|
|
|
|
device.setDeviceIdentifier(deviceId);
|
|
|
|
|
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
|
|
|
|
|
enrolmentInfo.setDateOfEnrolment(new Date().getTime());
|
|
|
|
|
enrolmentInfo.setDateOfLastUpdate(new Date().getTime());
|
|
|
|
|
enrolmentInfo.setStatus(EnrolmentInfo.Status.ACTIVE);
|
|
|
|
|
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD);
|
|
|
|
|
device.setName(name);
|
|
|
|
|
device.setType(ArduinoConstants.DEVICE_TYPE);
|
|
|
|
|
enrolmentInfo.setOwner(owner);
|
|
|
|
|
device.setEnrolmentInfo(enrolmentInfo);
|
|
|
|
|
|
|
|
|
|
boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device);
|
|
|
|
|
if (added) {
|
|
|
|
|
response.setStatus(Response.Status.OK.getStatusCode());
|
|
|
|
|
} else {
|
|
|
|
|
response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return added;
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
|
return false;
|
|
|
|
|
} finally {
|
|
|
|
|
deviceManagement.endTenantFlow();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param deviceId
|
|
|
|
|
* @param response
|
|
|
|
|
*/
|
|
|
|
|
@Path("manager/device/remove/{device_id}")
|
|
|
|
|
@DELETE
|
|
|
|
|
public void removeDevice(@PathParam("device_id") String deviceId, @Context HttpServletResponse response) {
|
|
|
|
|
|
|
|
|
|
DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
|
|
|
|
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
|
|
|
|
deviceIdentifier.setId(deviceId);
|
|
|
|
|
deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE);
|
|
|
|
|
try {
|
|
|
|
|
boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(
|
|
|
|
|
deviceIdentifier);
|
|
|
|
|
if (removed) {
|
|
|
|
|
response.setStatus(Response.Status.OK.getStatusCode());
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
|
} finally {
|
|
|
|
|
deviceManagement.endTenantFlow();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param deviceId
|
|
|
|
|
* @param name
|
|
|
|
|
* @param response
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
@Path("manager/device/update/{device_id}")
|
|
|
|
|
@POST
|
|
|
|
|
public boolean updateDevice(@PathParam("device_id") String deviceId,
|
|
|
|
|
@QueryParam("name") String name,
|
|
|
|
|
@Context HttpServletResponse response) {
|
|
|
|
|
|
|
|
|
|
DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
|
|
|
|
|
|
|
|
|
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
|
|
|
|
deviceIdentifier.setId(deviceId);
|
|
|
|
|
deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE);
|
|
|
|
|
try {
|
|
|
|
|
Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier);
|
|
|
|
|
device.setDeviceIdentifier(deviceId);
|
|
|
|
|
|
|
|
|
|
// device.setDeviceTypeId(deviceTypeId);
|
|
|
|
|
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
|
|
|
|
|
|
|
|
|
|
device.setName(name);
|
|
|
|
|
device.setType(ArduinoConstants.DEVICE_TYPE);
|
|
|
|
|
|
|
|
|
|
boolean updated = deviceManagement.getDeviceManagementService().modifyEnrollment(device);
|
|
|
|
|
|
|
|
|
|
if (updated) {
|
|
|
|
|
response.setStatus(Response.Status.OK.getStatusCode());
|
|
|
|
|
} else {
|
|
|
|
|
response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return updated;
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
|
return false;
|
|
|
|
|
} finally {
|
|
|
|
|
deviceManagement.endTenantFlow();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param deviceId
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
@Path("manager/device/{device_id}")
|
|
|
|
|
@GET
|
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
|
public Device getDevice(@PathParam("device_id") String deviceId) {
|
|
|
|
|
|
|
|
|
|
DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
|
|
|
|
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
|
|
|
|
deviceIdentifier.setId(deviceId);
|
|
|
|
|
deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
return deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier);
|
|
|
|
|
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
|
return null;
|
|
|
|
|
} finally {
|
|
|
|
|
deviceManagement.endTenantFlow();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param username
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
@Path("manager/devices/{username}")
|
|
|
|
|
@GET
|
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
|
public Device[] getArduinoDevices(@PathParam("username") String username) {
|
|
|
|
|
|
|
|
|
|
DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
List<Device> userDevices =
|
|
|
|
|
deviceManagement.getDeviceManagementService().getDevicesOfUser(username);
|
|
|
|
|
ArrayList<Device> userDevicesforArduino = new ArrayList<>();
|
|
|
|
|
for (Device device : userDevices) {
|
|
|
|
|
if (device.getType().equals(ArduinoConstants.DEVICE_TYPE) &&
|
|
|
|
|
device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.ACTIVE)) {
|
|
|
|
|
userDevicesforArduino.add(device);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return userDevicesforArduino.toArray(new Device[]{});
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
|
return null;
|
|
|
|
|
} finally {
|
|
|
|
|
deviceManagement.endTenantFlow();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param owner
|
|
|
|
|
* @param customDeviceName
|
|
|
|
|
* @param sketchType
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
@Path("manager/device/{sketch_type}/download")
|
|
|
|
|
@GET
|
|
|
|
|
@Produces("application/octet-stream")
|
|
|
|
|
public Response downloadSketch(@QueryParam("owner") String owner,
|
|
|
|
|
@QueryParam("deviceName") String customDeviceName,
|
|
|
|
|
@PathParam("sketch_type") String sketchType) {
|
|
|
|
|
//TODO:: null check customDeviceName at UI level
|
|
|
|
|
try {
|
|
|
|
|
ZipArchive zipFile = createDownloadFile(owner, customDeviceName, sketchType);
|
|
|
|
|
Response.ResponseBuilder rb = Response.ok(zipFile.getZipFile());
|
|
|
|
|
rb.header("Content-Disposition",
|
|
|
|
|
"attachment; filename=\"" + zipFile.getFileName() + "\"");
|
|
|
|
|
return rb.build();
|
|
|
|
|
} catch (IllegalArgumentException ex) {
|
|
|
|
|
return Response.status(400).entity(ex.getMessage()).build();//bad request
|
|
|
|
|
} catch (DeviceManagementException ex) {
|
|
|
|
|
return Response.status(500).entity(ex.getMessage()).build();
|
|
|
|
|
} catch (AccessTokenException ex) {
|
|
|
|
|
return Response.status(500).entity(ex.getMessage()).build();
|
|
|
|
|
} catch (DeviceControllerException ex) {
|
|
|
|
|
return Response.status(500).entity(ex.getMessage()).build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param owner
|
|
|
|
|
* @param customDeviceName
|
|
|
|
|
* @param sketchType
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
@Path("manager/device/{sketch_type}/generate_link")
|
|
|
|
|
@GET
|
|
|
|
|
public Response generateSketchLink(@QueryParam("owner") String owner,
|
|
|
|
|
@QueryParam("deviceName") String customDeviceName,
|
|
|
|
|
@PathParam("sketch_type") String sketchType) {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
ZipArchive zipFile = createDownloadFile(owner, customDeviceName, sketchType);
|
|
|
|
|
Response.ResponseBuilder rb = Response.ok(zipFile.getDeviceId());
|
|
|
|
|
return rb.build();
|
|
|
|
|
} catch (IllegalArgumentException ex) {
|
|
|
|
|
return Response.status(400).entity(ex.getMessage()).build();//bad request
|
|
|
|
|
} catch (DeviceManagementException ex) {
|
|
|
|
|
return Response.status(500).entity(ex.getMessage()).build();
|
|
|
|
|
} catch (AccessTokenException ex) {
|
|
|
|
|
return Response.status(500).entity(ex.getMessage()).build();
|
|
|
|
|
} catch (DeviceControllerException ex) {
|
|
|
|
|
return Response.status(500).entity(ex.getMessage()).build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param owner
|
|
|
|
|
* @param customDeviceName
|
|
|
|
|
* @param sketchType
|
|
|
|
|
* @return
|
|
|
|
|
* @throws DeviceManagementException
|
|
|
|
|
* @throws AccessTokenException
|
|
|
|
|
* @throws DeviceControllerException
|
|
|
|
|
*/
|
|
|
|
|
private ZipArchive createDownloadFile(String owner, String customDeviceName, String sketchType)
|
|
|
|
|
throws DeviceManagementException, AccessTokenException, DeviceControllerException {
|
|
|
|
|
if (owner == null) {
|
|
|
|
|
throw new IllegalArgumentException("Error on createDownloadFile() Owner is null!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//create new device id
|
|
|
|
|
String deviceId = shortUUID();
|
|
|
|
|
|
|
|
|
|
KeyGenerationUtil.createApplicationKeys("arduino");
|
|
|
|
|
|
|
|
|
|
TokenClient accessTokenClient = new TokenClient(ArduinoConstants.DEVICE_TYPE);
|
|
|
|
|
AccessTokenInfo accessTokenInfo = accessTokenClient.getAccessToken(owner, deviceId);
|
|
|
|
|
|
|
|
|
|
//create token
|
|
|
|
|
String accessToken = accessTokenInfo.getAccess_token();
|
|
|
|
|
String refreshToken = accessTokenInfo.getRefresh_token();
|
|
|
|
|
|
|
|
|
|
//Register the device with CDMF
|
|
|
|
|
String deviceName = customDeviceName + "_" + deviceId;
|
|
|
|
|
boolean status = register(deviceId, deviceName, owner);
|
|
|
|
|
|
|
|
|
|
if (!status) {
|
|
|
|
|
String msg = "Error occurred while registering the device with " + "id: " + deviceId + " owner:" + owner;
|
|
|
|
|
throw new DeviceManagementException(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ZipUtil ziputil = new ZipUtil();
|
|
|
|
|
ZipArchive zipFile = ziputil.createZipFile(owner, SUPER_TENANT, sketchType, deviceId, deviceName, accessToken,
|
|
|
|
|
refreshToken);
|
|
|
|
|
zipFile.setDeviceId(deviceId);
|
|
|
|
|
return zipFile;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private static String shortUUID() {
|
|
|
|
|
UUID uuid = UUID.randomUUID();
|
|
|
|
|
long l = ByteBuffer.wrap(uuid.toString().getBytes(StandardCharsets.UTF_8)).getLong();
|
|
|
|
|
return Long.toString(l, Character.MAX_RADIX);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------
|
|
|
|
|
Device specific APIs - Control APIs + Data-Publishing APIs
|
|
|
|
|
--------------------------------------------------------------------------------------- */
|
|
|
|
@ -496,7 +186,7 @@ public class ArduinoService {
|
|
|
|
|
public void switchBulb(@HeaderParam("owner") String owner,
|
|
|
|
|
@HeaderParam("deviceId") String deviceId,
|
|
|
|
|
@HeaderParam("protocol") String protocol,
|
|
|
|
|
@PathParam("state") String state,
|
|
|
|
|
@FormParam("state") String state,
|
|
|
|
|
@Context HttpServletResponse response) {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|