@ -69,11 +69,9 @@ import java.util.HashMap;
import java.util.List ;
import java.util.Map ;
import java.util.UUID ;
import java.util.concurrent.ConcurrentHashMap ;
/ * *
* This is the API which is used to control and manage device type functionality
* This is the API which is used to control and manage device type functionality .
* /
@SuppressWarnings ( "NonJaxWsWebServices" )
@API ( name = "watertank" , version = "1.0.0" , context = "/watertank" , tags = "watertank" )
@ -83,7 +81,6 @@ public class DeviceTypeServiceImpl implements DeviceTypeService {
private static final String KEY_TYPE = "PRODUCTION" ;
private static Log log = LogFactory . getLog ( DeviceTypeService . class ) ;
private static ApiApplicationKey apiApplicationKey ;
private ConcurrentHashMap < String , DeviceJSON > deviceToIpMap = new ConcurrentHashMap < > ( ) ;
private static String shortUUID ( ) {
UUID uuid = UUID . randomUUID ( ) ;
@ -92,26 +89,28 @@ public class DeviceTypeServiceImpl implements DeviceTypeService {
}
/ * *
* @param agentInfo device owner , id
* @return true if device instance is added to map
* Validate registration information .
*
* @param agentInfo device owner , id .
* @return true if device instance is added to map .
* /
@Path ( "device/register" )
@POST
@Consumes ( MediaType . APPLICATION_JSON )
public Response registerDevice ( final DeviceJSON agentInfo ) {
String deviceId = agentInfo . deviceId ;
if ( ( agentInfo . deviceId ! = null ) & & ( agentInfo . owner ! = null ) ) {
deviceToIpMap . put ( deviceId , agentInfo ) ;
return Response . status ( Response . Status . OK ) . build ( ) ;
}
return Response . status ( Response . Status . NOT_ACCEPTABLE ) . build ( ) ;
}
/ * *
* @param deviceId unique identifier for given device type
* @param onLevel level to turn on the relay
* @param offLevel level to turn off the relay
* @param sensorHeight height to water level sensor from bottom of the tank
* Update watertank configurations .
*
* @param deviceId unique identifier for given device type .
* @param onLevel level to turn on the relay .
* @param offLevel level to turn off the relay .
* @param sensorHeight height to water level sensor from bottom of the tank .
* /
@Path ( "device/{deviceId}/change-levels" )
@POST
@ -125,7 +124,7 @@ public class DeviceTypeServiceImpl implements DeviceTypeService {
try {
if ( ! APIUtil . getDeviceAccessAuthorizationService ( )
. isUserAuthorized ( new DeviceIdentifier ( deviceId , DeviceTypeConstants . DEVICE_TYPE ) ) ) {
return Response . status ( Response . Status . UNAUTHORIZED .getStatusCode ( ) ). build ( ) ;
return Response . status ( Response . Status . UNAUTHORIZED ). build ( ) ;
}
String configs = onLevel + "," + offLevel + "," + sensorHeight ;
@ -137,19 +136,19 @@ public class DeviceTypeServiceImpl implements DeviceTypeService {
dynamicProperties , configs ) ;
return Response . ok ( ) . build ( ) ;
} catch ( DeviceAccessAuthorizationException e ) {
log . error ( e . getErrorMessage ( ) , e ) ;
return Response . status ( Response . Status . INTERNAL_SERVER_ERROR ) . build( ) ;
log . error ( "Unable to update configs" , e ) ;
return Response . status ( Response . Status . INTERNAL_SERVER_ERROR ) . entity( e . getMessage ( ) ) . build( ) ;
}
}
/ * *
* Retrieve Sensor data for the given time period
* Retrieve Sensor data for the given time period .
*
* @param deviceId unique identifier for given device type instance
* @param sensorName name of the sensor
* @param from starting time
* @param to ending time
* @return response with List < SensorRecord > object which includes sensor data which is requested
* @param deviceId unique identifier for given device type instance .
* @param sensorName name of the sensor .
* @param from starting time .
* @param to ending time .
* @return response with List < SensorRecord > object which includes sensor data which is requested .
* /
@Path ( "device/stats/{deviceId}/sensors/{sensorName}" )
@GET
@ -170,33 +169,30 @@ public class DeviceTypeServiceImpl implements DeviceTypeService {
sensorTableName = DeviceTypeConstants . WATERLEVEL_EVENT_TABLE ;
break ;
default :
return Response . status ( Response . Status . BAD_REQUEST ) . entity ( "Invalid event stream ") . build ( ) ;
return Response . status ( Response . Status . BAD_REQUEST ) . entity ( "Invalid event stream name ") . build ( ) ;
}
try {
if ( ! APIUtil . getDeviceAccessAuthorizationService ( ) . isUserAuthorized ( new DeviceIdentifier ( deviceId ,
DeviceTypeConstants . DEVICE_TYPE ) ) ) {
return Response . status ( Response . Status . UNAUTHORIZED .getStatusCode ( ) ). build ( ) ;
if ( ! APIUtil . getDeviceAccessAuthorizationService ( )
. isUserAuthorized ( new DeviceIdentifier ( deviceId , DeviceTypeConstants . DEVICE_TYPE ) ) ) {
return Response . status ( Response . Status . UNAUTHORIZED ). build ( ) ;
}
List < SortByField > sortByFields = new ArrayList < > ( ) ;
SortByField sortByField = new SortByField ( "time" , SORT . ASC , false ) ;
sortByFields . add ( sortByField ) ;
List < SensorRecord > sensorRecords = APIUtil . getAllEventsForDevice ( sensorTableName , query , sortByFields ) ;
return Response . status ( Response . Status . OK .getStatusCode ( ) ). entity ( sensorRecords ) . build ( ) ;
} catch ( AnalyticsException e ) {
return Response . status ( Response . Status . OK ). entity ( sensorRecords ) . build ( ) ;
} catch ( AnalyticsException | DeviceAccessAuthorizationException e ) {
String errorMsg = "Error on retrieving stats on table " + sensorTableName + " with query " + query ;
log . error ( errorMsg ) ;
return Response . status ( Response . Status . INTERNAL_SERVER_ERROR . getStatusCode ( ) ) . entity ( errorMsg ) . build ( ) ;
} catch ( DeviceAccessAuthorizationException e ) {
log . error ( e . getErrorMessage ( ) , e ) ;
return Response . status ( Response . Status . INTERNAL_SERVER_ERROR ) . build ( ) ;
return Response . status ( Response . Status . INTERNAL_SERVER_ERROR ) . entity ( errorMsg ) . build ( ) ;
}
}
/ * *
* Remove device type instance using device id
* Remove device type instance using device id .
*
* @param deviceId unique identifier for given device type instance
* @param deviceId unique identifier for given device type instance .
* /
@Path ( "/device/{deviceId}" )
@DELETE
@ -206,29 +202,26 @@ public class DeviceTypeServiceImpl implements DeviceTypeService {
deviceIdentifier . setId ( deviceId ) ;
deviceIdentifier . setType ( DeviceTypeConstants . DEVICE_TYPE ) ;
if ( ! APIUtil . getDeviceAccessAuthorizationService ( ) . isUserAuthorized ( deviceIdentifier ) ) {
return Response . status ( Response . Status . UNAUTHORIZED .getStatusCode ( ) ). build ( ) ;
return Response . status ( Response . Status . UNAUTHORIZED ). build ( ) ;
}
boolean removed = APIUtil . getDeviceManagementService ( ) . disenrollDevice (
deviceIdentifier ) ;
if ( removed ) {
return Response . ok ( ) . build ( ) ;
} else {
return Response . status ( Response . Status . NOT_ACCEPTABLE .getStatusCode ( ) ). build ( ) ;
return Response . status ( Response . Status . NOT_ACCEPTABLE ). build ( ) ;
}
} catch ( DeviceManagementException e ) {
log . error ( e . getErrorMessage ( ) , e ) ;
return Response . status ( Response . Status . INTERNAL_SERVER_ERROR . getStatusCode ( ) ) . build ( ) ;
} catch ( DeviceAccessAuthorizationException e ) {
log . error ( e . getErrorMessage ( ) , e ) ;
return Response . status ( Response . Status . INTERNAL_SERVER_ERROR . getStatusCode ( ) ) . build ( ) ;
} catch ( DeviceManagementException | DeviceAccessAuthorizationException e ) {
log . error ( "Unable to remove the device" , e ) ;
return Response . status ( Response . Status . INTERNAL_SERVER_ERROR ) . entity ( e . getMessage ( ) ) . build ( ) ;
}
}
/ * *
* Update device instance name
* Update device instance name .
*
* @param deviceId unique identifier for given device type instance
* @param name new name for the device type instance
* @param deviceId unique identifier for given device type instance .
* @param name new name for the device type instance .
* /
@Path ( "/device/{deviceId}" )
@PUT
@ -238,7 +231,7 @@ public class DeviceTypeServiceImpl implements DeviceTypeService {
deviceIdentifier . setId ( deviceId ) ;
deviceIdentifier . setType ( DeviceTypeConstants . DEVICE_TYPE ) ;
if ( ! APIUtil . getDeviceAccessAuthorizationService ( ) . isUserAuthorized ( deviceIdentifier ) ) {
return Response . status ( Response . Status . UNAUTHORIZED .getStatusCode ( ) ). build ( ) ;
return Response . status ( Response . Status . UNAUTHORIZED ). build ( ) ;
}
Device device = APIUtil . getDeviceManagementService ( ) . getDevice ( deviceIdentifier ) ;
device . setDeviceIdentifier ( deviceId ) ;
@ -249,22 +242,19 @@ public class DeviceTypeServiceImpl implements DeviceTypeService {
if ( updated ) {
return Response . ok ( ) . build ( ) ;
} else {
return Response . status ( Response . Status . NOT_ACCEPTABLE .getStatusCode ( ) ). build ( ) ;
return Response . status ( Response . Status . NOT_ACCEPTABLE ). build ( ) ;
}
} catch ( DeviceManagementException e ) {
log . error ( e . getErrorMessage ( ) , e ) ;
return Response . status ( Response . Status . INTERNAL_SERVER_ERROR . getStatusCode ( ) ) . build ( ) ;
} catch ( DeviceAccessAuthorizationException e ) {
log . error ( e . getErrorMessage ( ) , e ) ;
return Response . status ( Response . Status . INTERNAL_SERVER_ERROR . getStatusCode ( ) ) . build ( ) ;
} catch ( DeviceManagementException | DeviceAccessAuthorizationException e ) {
log . error ( "Unable to update the device" , e ) ;
return Response . status ( Response . Status . INTERNAL_SERVER_ERROR ) . entity ( e . getMessage ( ) ) . build ( ) ;
}
}
/ * *
* To get device information
* To get device information .
*
* @param deviceId unique identifier for given device type instance
* @return
* @param deviceId unique identifier for given device type instance .
* @return device object .
* /
@Path ( "/device/{deviceId}" )
@GET
@ -276,23 +266,20 @@ public class DeviceTypeServiceImpl implements DeviceTypeService {
deviceIdentifier . setId ( deviceId ) ;
deviceIdentifier . setType ( DeviceTypeConstants . DEVICE_TYPE ) ;
if ( ! APIUtil . getDeviceAccessAuthorizationService ( ) . isUserAuthorized ( deviceIdentifier ) ) {
return Response . status ( Response . Status . UNAUTHORIZED .getStatusCode ( ) ). build ( ) ;
return Response . status ( Response . Status . UNAUTHORIZED ). build ( ) ;
}
Device device = APIUtil . getDeviceManagementService ( ) . getDevice ( deviceIdentifier ) ;
return Response . ok ( ) . entity ( device ) . build ( ) ;
} catch ( DeviceManagementException e ) {
log . error ( e . getErrorMessage ( ) , e ) ;
return Response . status ( Response . Status . INTERNAL_SERVER_ERROR . getStatusCode ( ) ) . build ( ) ;
} catch ( DeviceAccessAuthorizationException e ) {
log . error ( e . getErrorMessage ( ) , e ) ;
return Response . status ( Response . Status . INTERNAL_SERVER_ERROR . getStatusCode ( ) ) . build ( ) ;
} catch ( DeviceManagementException | DeviceAccessAuthorizationException e ) {
log . error ( "Unable to get the device" , e ) ;
return Response . status ( Response . Status . INTERNAL_SERVER_ERROR ) . entity ( e . getMessage ( ) ) . build ( ) ;
}
}
/ * *
* Get all device type instance which belongs to user
* Get all device type instance which belongs to user .
*
* @return Array of devices which includes device ' s information
* @return Array of devices which includes device ' s information .
* /
@Path ( "/devices" )
@GET
@ -302,27 +289,27 @@ public class DeviceTypeServiceImpl implements DeviceTypeService {
try {
List < Device > userDevices =
APIUtil . getDeviceManagementService ( ) . getDevicesOfUser ( APIUtil . getAuthenticatedUser ( ) ) ;
ArrayList < Device > userDevicesforwatertank = new ArrayList < > ( ) ;
ArrayList < Device > waterTankDevices = new ArrayList < > ( ) ;
for ( Device device : userDevices ) {
if ( device . getType ( ) . equals ( DeviceTypeConstants . DEVICE_TYPE ) & &
device . getEnrolmentInfo ( ) . getStatus ( ) . equals ( EnrolmentInfo . Status . ACTIVE ) ) {
userDevicesforwatertank . add ( device ) ;
waterTankDevices . add ( device ) ;
}
}
Device [ ] devices = userDevicesforwatertank . toArray ( new Device [ ] { } ) ;
Device [ ] devices = waterTankDevices . toArray ( new Device [ ] { } ) ;
return Response . ok ( ) . entity ( devices ) . build ( ) ;
} catch ( DeviceManagementException e ) {
log . error ( e . getErrorMessage ( ) , e ) ;
return Response . status ( Response . Status . INTERNAL_SERVER_ERROR .getStatusCod e( ) ) . build ( ) ;
log . error ( "Unable to get all devices" , e ) ;
return Response . status ( Response . Status . INTERNAL_SERVER_ERROR ). entity ( e . getMessag e( ) ) . build ( ) ;
}
}
/ * *
* To download device type agent source code as zip file
* To download device type agent source code as zip file .
*
* @param deviceName name for the device type instance
* @param sketchType folder name where device type agent was installed into server
* @return Agent source code as zip file
* @param deviceName name for the device type instance .
* @param sketchType folder name where device type agent was installed into server .
* @return Agent source code as zip file .
* /
@Path ( "/device/download" )
@GET
@ -331,70 +318,65 @@ public class DeviceTypeServiceImpl implements DeviceTypeService {
@QueryParam ( "sketchType" ) String sketchType ) {
try {
ZipArchive zipFile = createDownloadFile ( APIUtil . getAuthenticatedUser ( ) , deviceName , sketchType ) ;
zipFile . getZipFile ( ) . delete ( ) ;
Response . ResponseBuilder response = Response . ok ( FileUtils . readFileToByteArray ( zipFile . getZipFile ( ) ) ) ;
response . status ( Response . Status . OK ) ;
response . type ( "application/zip" ) ;
response . header ( "Content-Disposition" , "attachment; filename=\"" + zipFile . getFileName ( ) + "\"" ) ;
Response resp = response . build ( ) ;
zipFile . getZipFile ( ) . delete ( ) ;
return resp ;
return response . build ( ) ;
} catch ( IllegalArgumentException ex ) {
return Response . status ( 400 ) . entity ( ex . getMessage ( ) ) . build ( ) ; //bad request
} catch ( DeviceManagementException ex ) {
log . error ( ex . getMessage ( ) , ex ) ;
return Response . status ( 500 ) . entity ( ex . getMessage ( ) ) . build ( ) ;
} catch ( JWTClientException ex ) {
log . error ( ex . getMessage ( ) , ex ) ;
return Response . status ( 500 ) . entity ( ex . getMessage ( ) ) . build ( ) ;
} catch ( APIManagerException ex ) {
log . error ( ex . getMessage ( ) , ex ) ;
return Response . status ( 500 ) . entity ( ex . getMessage ( ) ) . build ( ) ;
} catch ( IOException ex ) {
log . error ( ex . getMessage ( ) , ex ) ;
return Response . status ( 500 ) . entity ( ex . getMessage ( ) ) . build ( ) ;
} catch ( UserStoreException ex ) {
log . error ( ex . getMessage ( ) , ex ) ;
return Response . status ( 500 ) . entity ( ex . getMessage ( ) ) . build ( ) ;
return Response . status ( Response . Status . BAD_REQUEST ) . entity ( ex . getMessage ( ) ) . build ( ) ; //bad request
} catch ( DeviceManagementException | JWTClientException | APIManagerException | IOException
| UserStoreException ex ) {
log . error ( "Unable to download sketch" , ex ) ;
return Response . status ( Response . Status . INTERNAL_SERVER_ERROR ) . entity ( ex . getMessage ( ) ) . build ( ) ;
}
}
/ * *
* Register device into device management service
* Register device into device management service .
*
* @param deviceId unique identifier for given device type instance
* @param name name for the device type instance
* @return check whether device is installed into cdmf
* @param deviceId unique identifier for given device type instance .
* @param name name for the device type instance .
* @return whether device is installed into cdmf or not .
* /
private boolean register ( String deviceId , String name ) {
try {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier ( ) ;
deviceIdentifier . setId ( deviceId ) ;
deviceIdentifier . setType ( DeviceTypeConstants . DEVICE_TYPE ) ;
if ( APIUtil . getDeviceManagementService ( ) . isEnrolled ( deviceIdentifier ) ) {
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 ( DeviceTypeConstants . DEVICE_TYPE ) ;
enrolmentInfo . setOwner ( APIUtil . getAuthenticatedUser ( ) ) ;
device . setEnrolmentInfo ( enrolmentInfo ) ;
boolean added = APIUtil . getDeviceManagementService ( ) . enrollDevice ( device ) ;
if ( added ) {
APIUtil . registerApiAccessRoles ( APIUtil . getAuthenticatedUser ( ) ) ;
}
return added ;
} catch ( DeviceManagementException e ) {
log . error ( e . getMessage ( ) , e ) ;
private boolean register ( String deviceId , String name ) throws DeviceManagementException {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier ( ) ;
deviceIdentifier . setId ( deviceId ) ;
deviceIdentifier . setType ( DeviceTypeConstants . DEVICE_TYPE ) ;
if ( APIUtil . getDeviceManagementService ( ) . isEnrolled ( deviceIdentifier ) ) {
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 ( DeviceTypeConstants . DEVICE_TYPE ) ;
enrolmentInfo . setOwner ( APIUtil . getAuthenticatedUser ( ) ) ;
device . setEnrolmentInfo ( enrolmentInfo ) ;
boolean added = APIUtil . getDeviceManagementService ( ) . enrollDevice ( device ) ;
if ( added ) {
APIUtil . registerApiAccessRoles ( APIUtil . getAuthenticatedUser ( ) ) ;
}
return added ;
}
/ * *
* Generates zip archive with the device agent .
*
* @param owner of the device .
* @param deviceName given to the device .
* @param sketchType of the device .
* @return zip archive to download .
* @throws DeviceManagementException
* @throws JWTClientException
* @throws APIManagerException
* @throws UserStoreException
* /
private ZipArchive createDownloadFile ( String owner , String deviceName , String sketchType )
throws DeviceManagementException , JWTClientException , APIManagerException ,
UserStoreException {
@ -412,8 +394,9 @@ public class DeviceTypeServiceImpl implements DeviceTypeService {
JWTClient jwtClient = APIUtil . getJWTClientManagerService ( ) . getJWTClient ( ) ;
String scopes = "device_type_" + DeviceTypeConstants . DEVICE_TYPE + " device_" + deviceId ;
AccessTokenInfo accessTokenInfo = jwtClient . getAccessToken ( apiApplicationKey . getConsumerKey ( ) ,
apiApplicationKey . getConsumerSecret ( ) , owner + "@" + APIUtil . getAuthenticatedUserTenantDomain ( ) , scopes ) ;
apiApplicationKey . getConsumerSecret ( ) ,
owner + "@" + APIUtil . getAuthenticatedUserTenantDomain ( ) ,
scopes ) ;
//create token
String accessToken = accessTokenInfo . getAccessToken ( ) ;
String refreshToken = accessTokenInfo . getRefreshToken ( ) ;
@ -423,9 +406,8 @@ public class DeviceTypeServiceImpl implements DeviceTypeService {
throw new DeviceManagementException ( msg ) ;
}
ZipUtil ziputil = new ZipUtil ( ) ;
ZipArchive zipFile = ziputil . createZipFile ( owner , APIUtil . getTenantDomainOftheUser ( ) , sketchType ,
deviceId , deviceName , accessToken , refreshToken ) ;
return zipFile ;
return ziputil . createZipFile ( owner , APIUtil . getTenantDomainOftheUser ( ) , sketchType ,
deviceId , deviceName , accessToken , refreshToken ) ;
}
}
}