diff --git a/modules/samples/connectedcup/component/controller/src/main/java/org/coffeeking/controller/service/ConnectedCupControllerService.java b/modules/samples/connectedcup/component/controller/src/main/java/org/coffeeking/controller/service/ConnectedCupControllerService.java index 525dbbd4..161cc211 100644 --- a/modules/samples/connectedcup/component/controller/src/main/java/org/coffeeking/controller/service/ConnectedCupControllerService.java +++ b/modules/samples/connectedcup/component/controller/src/main/java/org/coffeeking/controller/service/ConnectedCupControllerService.java @@ -58,9 +58,6 @@ public class ConnectedCupControllerService { private static final String SUPER_TENANT = "carbon.super"; private static ConnectedCupMQTTConnector connectedCupMQTTConnector; - @Context - HttpServletResponse response; - public ConnectedCupMQTTConnector connectedCupMQTTConnector() { return ConnectedCupControllerService.connectedCupMQTTConnector; @@ -81,8 +78,11 @@ public class ConnectedCupControllerService { * @param deviceId * @param owner */ - @Path("controller/cup/coffeelevel") + @Path("controller/coffeelevel") @GET + @Produces(MediaType.APPLICATION_JSON) + @Feature(code = "coffeelevel", name="Coffeelevel", description = "read coffeelevel from this device", type = + "monitor") public SensorRecord readCoffeeLevel(@HeaderParam("owner") String owner, @HeaderParam("deviceId") String deviceId, @Context HttpServletResponse response) { @@ -118,8 +118,11 @@ public class ConnectedCupControllerService { - @Path("controller/cup/temperature") + @Path("controller/temperature") @GET + @Produces(MediaType.APPLICATION_JSON) + @Feature(code = "temperature", name="Temperature", description = "read temperature from this device", type = + "monitor") public SensorRecord readTemperature(@HeaderParam("owner") String owner, @HeaderParam("deviceId") String deviceId, @Context HttpServletResponse response) { @@ -156,9 +159,9 @@ public class ConnectedCupControllerService { @Path("controller/ordercoffee") @POST - public HttpServletResponse orderCoffee(@QueryParam("deviceId") String deviceId, @QueryParam("deviceOwner") String + public void orderCoffee(@QueryParam("deviceId") String deviceId, @QueryParam("deviceOwner") String deviceOwner, @Context HttpServletResponse response){ - SensorRecord sensorRecord = null; + DeviceValidator deviceValidator = new DeviceValidator(); try { if (!deviceValidator.isExist(deviceOwner, SUPER_TENANT, new DeviceIdentifier( @@ -175,6 +178,6 @@ public class ConnectedCupControllerService { if (log.isDebugEnabled()) { log.debug("Sending request to read liquid level value of device [" + deviceId + "] via MQTT"); } - return response; +// return response; } } \ No newline at end of file diff --git a/modules/samples/connectedcup/component/controller/src/main/java/org/coffeeking/controller/service/transport/ConnectedCupMQTTConnector.java b/modules/samples/connectedcup/component/controller/src/main/java/org/coffeeking/controller/service/transport/ConnectedCupMQTTConnector.java index 293c1b5e..00c6f569 100644 --- a/modules/samples/connectedcup/component/controller/src/main/java/org/coffeeking/controller/service/transport/ConnectedCupMQTTConnector.java +++ b/modules/samples/connectedcup/component/controller/src/main/java/org/coffeeking/controller/service/transport/ConnectedCupMQTTConnector.java @@ -84,7 +84,7 @@ public class ConnectedCupMQTTConnector extends MQTTTransportHandler { public void publishDeviceData(String... publishData) throws TransportHandlerException { if (publishData.length != 4) { String errorMsg = "Incorrect number of arguments received to SEND-MQTT Message. " + - "Need to be [owner, deviceId, resource{BULB/TEMP}, state{ON/OFF or null}]"; + "Need to be [owner, deviceId, resource{BULB/TEMP}, state{ON/OFF or null}]"; log.error(errorMsg); throw new TransportHandlerException(errorMsg); } @@ -96,8 +96,8 @@ public class ConnectedCupMQTTConnector extends MQTTTransportHandler { MqttMessage pushMessage = new MqttMessage(); String publishTopic = - serverName + File.separator + deviceOwner + File.separator + - ConnectedCupConstants.DEVICE_TYPE + File.separator + deviceId; + "wso2" + File.separator + deviceOwner + File.separator + + ConnectedCupConstants.DEVICE_TYPE + File.separator + deviceId; try { @@ -110,8 +110,8 @@ public class ConnectedCupMQTTConnector extends MQTTTransportHandler { publishToQueue(publishTopic, pushMessage); } catch (Exception e) { - String errorMsg = "Preparing Secure payload failed for device - [" + deviceId + "] of owner - " + - "[" + deviceOwner + "]."; + String errorMsg = "Preparing payload failed for device - [" + deviceId + "] of owner - " + + "[" + deviceOwner + "]."; log.error(errorMsg); throw new TransportHandlerException(errorMsg, e); } @@ -122,33 +122,55 @@ public class ConnectedCupMQTTConnector extends MQTTTransportHandler { public void processIncomingMessage(MqttMessage mqttMessage, String... strings) throws TransportHandlerException { String topic = strings[0]; - String ownerAndId = topic.replace("wso2" + File.separator + "iot" + File.separator, ""); + String ownerAndId = topic.replace("wso2" + File.separator, ""); ownerAndId = ownerAndId.replace(File.separator + ConnectedCupConstants.DEVICE_TYPE + File.separator, ":"); - ownerAndId = ownerAndId.replace(File.separator + "connectedcup_publisher", ""); + ownerAndId = ownerAndId.replace(File.separator + "connected_publisher", ""); String owner = ownerAndId.split(":")[0]; String deviceId = ownerAndId.split(":")[1]; +// String actualMessage = mqttMessage.toString(); String[] messageData = mqttMessage.toString().split(":"); Float value = Float.valueOf(messageData[1]); - switch (messageData[0]){ - case "temperature": - SensorDataManager.getInstance().setSensorRecord(deviceId, ConnectedCupConstants.SENSOR_TEMPERATURE, - String.valueOf(messageData[1]), - Calendar.getInstance().getTimeInMillis()); - ConnectedCupServiceUtils.publishToDAS(owner, deviceId, value); + +// if (actualMessage.contains("PUBLISHER")) { +// float temperature = Float.parseFloat(actualMessage.split(":")[2]); +// +// if (!ConnectedCupServiceUtils.publishToDAS(owner, deviceId, messageData[0], value)) { +// log.error("MQTT Subscriber: Publishing data to DAS failed."); +// } +// +// if (log.isDebugEnabled()) { +// log.debug("MQTT Subscriber: Published data to DAS successfully."); +// } +// +// } else if (actualMessage.contains("TEMPERATURE")) { +// String temperatureValue = actualMessage.split(":")[1]; +// SensorDataManager.getInstance().setSensorRecord(deviceId, ConnectedCupConstants.SENSOR_TEMPERATURE, +// temperatureValue, +// Calendar.getInstance().getTimeInMillis()); +// } +// +// + + switch(messageData[0]) { + case "temperature": SensorDataManager.getInstance().setSensorRecord(deviceId, ConnectedCupConstants.SENSOR_TEMPERATURE, + String.valueOf(messageData[1]), + Calendar.getInstance().getTimeInMillis()); + break; + case "coffeelevel": SensorDataManager.getInstance().setSensorRecord(deviceId, ConnectedCupConstants.SENSOR_LEVEL, + String.valueOf(messageData[1]), + Calendar.getInstance().getTimeInMillis()); break; - case "coffeelevel": - SensorDataManager.getInstance().setSensorRecord(deviceId, ConnectedCupConstants.SENSOR_TEMPERATURE, - String.valueOf(messageData[1]), - Calendar.getInstance().getTimeInMillis()); - ConnectedCupServiceUtils.publishToDAS(owner, deviceId, value); } - log.info("Received MQTT message for OWNER: " + owner + " DEVICE.ID: " + deviceId + " | Command: " + - messageData[0] +" " + messageData[1] ); + ConnectedCupServiceUtils.publishToDAS(owner, deviceId, messageData[0], value); + if (log.isDebugEnabled()) { + log.debug("Received MQTT message for OWNER: " + owner + " DEVICE.ID: " + deviceId + " | Command: " + + messageData[0] +" " + messageData[1] ); + } } @@ -162,14 +184,14 @@ public class ConnectedCupMQTTConnector extends MQTTTransportHandler { } catch (MqttException e) { if (log.isDebugEnabled()) { log.warn("Unable to 'STOP' MQTT connection at broker at: " + mqttBrokerEndPoint - + " for device-type - " + ConnectedCupConstants.DEVICE_TYPE, e); + + " for device-type - " + ConnectedCupConstants.DEVICE_TYPE, e); } try { Thread.sleep(timeoutInterval); } catch (InterruptedException e1) { log.error("MQTT-Terminator: Thread Sleep Interrupt Exception at device-type - " + - ConnectedCupConstants.DEVICE_TYPE, e1); + ConnectedCupConstants.DEVICE_TYPE, e1); } } } diff --git a/modules/samples/connectedcup/component/controller/src/main/webapp/WEB-INF/cxf-servlet.xml b/modules/samples/connectedcup/component/controller/src/main/webapp/WEB-INF/cxf-servlet.xml index dfbbf91a..ab9e21dd 100644 --- a/modules/samples/connectedcup/component/controller/src/main/webapp/WEB-INF/cxf-servlet.xml +++ b/modules/samples/connectedcup/component/controller/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -25,7 +25,7 @@ http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> - + diff --git a/modules/samples/connectedcup/component/manager/src/main/java/org/coffeeking/manager/service/ConnectedCupManagerService.java b/modules/samples/connectedcup/component/manager/src/main/java/org/coffeeking/manager/service/ConnectedCupManagerService.java index 3e722ffa..932a0758 100644 --- a/modules/samples/connectedcup/component/manager/src/main/java/org/coffeeking/manager/service/ConnectedCupManagerService.java +++ b/modules/samples/connectedcup/component/manager/src/main/java/org/coffeeking/manager/service/ConnectedCupManagerService.java @@ -53,7 +53,7 @@ import java.util.List; import java.util.UUID; @API( name="connectedcup_mgt", version="1.0.0", context="/connectedcup_mgt") -public class ConnectedCupManagerService { +public class ConnectedCupManagerService { private static Log log = LogFactory.getLog(ConnectedCupManagerService.class); private static final String SUPER_TENANT = "carbon.super"; @@ -69,175 +69,175 @@ public class ConnectedCupManagerService { public boolean register(@QueryParam("name") String name, @QueryParam("owner") String owner) { - DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); - String deviceId = shortUUID(); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(deviceId); - deviceIdentifier.setType(ConnectedCupConstants.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(ConnectedCupConstants.DEVICE_TYPE); - enrolmentInfo.setOwner(owner); - device.setEnrolmentInfo(enrolmentInfo); - - KeyGenerationUtil.createApplicationKeys(ConnectedCupConstants.DEVICE_TYPE); - - TokenClient accessTokenClient = new TokenClient(ConnectedCupConstants.DEVICE_TYPE); - AccessTokenInfo accessTokenInfo = accessTokenClient.getAccessToken(owner, deviceId); - - //create token - String accessToken = accessTokenInfo.getAccess_token(); - String refreshToken = accessTokenInfo.getRefresh_token(); - List properties = new ArrayList<>(); - - Device.Property accessTokenProperty = new Device.Property(); - accessTokenProperty.setName("accessToken"); - accessTokenProperty.setValue(accessToken); - - Device.Property refreshTokenProperty = new Device.Property(); - refreshTokenProperty.setName("refreshToken"); - refreshTokenProperty.setValue(refreshToken); - - properties.add(accessTokenProperty); - properties.add(refreshTokenProperty); - device.setProperties(properties); - - 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()); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); + String deviceId = shortUUID(); + + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(deviceId); + deviceIdentifier.setType(ConnectedCupConstants.DEVICE_TYPE); + + try { + if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) { + response.setStatus(Response.Status.CONFLICT.getStatusCode()); return false; - } catch (AccessTokenException e) { - e.printStackTrace(); - } finally { - deviceManagement.endTenantFlow(); } - return true; + 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(ConnectedCupConstants.DEVICE_TYPE); + enrolmentInfo.setOwner(owner); + device.setEnrolmentInfo(enrolmentInfo); + + KeyGenerationUtil.createApplicationKeys(ConnectedCupConstants.DEVICE_TYPE); + + TokenClient accessTokenClient = new TokenClient(ConnectedCupConstants.DEVICE_TYPE); + AccessTokenInfo accessTokenInfo = accessTokenClient.getAccessToken(owner, deviceId); + + //create token + String accessToken = accessTokenInfo.getAccess_token(); + String refreshToken = accessTokenInfo.getRefresh_token(); + List properties = new ArrayList<>(); + + Device.Property accessTokenProperty = new Device.Property(); + accessTokenProperty.setName("accessToken"); + accessTokenProperty.setValue(accessToken); + + Device.Property refreshTokenProperty = new Device.Property(); + refreshTokenProperty.setName("refreshToken"); + refreshTokenProperty.setValue(refreshToken); + + properties.add(accessTokenProperty); + properties.add(refreshTokenProperty); + device.setProperties(properties); + + 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; + } catch (AccessTokenException e) { + e.printStackTrace(); + } finally { + deviceManagement.endTenantFlow(); } + return true; - @Path("/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(ConnectedCupConstants.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(); - } + } + + @Path("/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(ConnectedCupConstants.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(); } - @Path("/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(ConnectedCupConstants.DEVICE_TYPE); + @Path("/device/update/{device_id}") + @POST + public boolean updateDevice(@PathParam("device_id") String deviceId, + @QueryParam("name") String name, + @Context HttpServletResponse response) { - try { - Device device = deviceManagement.getDeviceManagementService().getDevice( - deviceIdentifier); - device.setDeviceIdentifier(deviceId); + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); - // device.setDeviceTypeId(deviceTypeId); - device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(deviceId); + deviceIdentifier.setType(ConnectedCupConstants.DEVICE_TYPE); - device.setName(name); - device.setType(ConnectedCupConstants.DEVICE_TYPE); + try { + Device device = deviceManagement.getDeviceManagementService().getDevice( + deviceIdentifier); + device.setDeviceIdentifier(deviceId); - boolean updated = deviceManagement.getDeviceManagementService().modifyEnrollment( - device); + // device.setDeviceTypeId(deviceTypeId); + device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); + device.setName(name); + device.setType(ConnectedCupConstants.DEVICE_TYPE); - if (updated) { - response.setStatus(Response.Status.OK.getStatusCode()); + boolean updated = deviceManagement.getDeviceManagementService().modifyEnrollment( + device); - } 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(); - } + if (updated) { + response.setStatus(Response.Status.OK.getStatusCode()); - } + } else { + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); - @Path("/device/{device_id}") - @GET - @Consumes("application/json") - @Produces("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(ConnectedCupConstants.DEVICE_TYPE); - - try { - Device device = deviceManagement.getDeviceManagementService().getDevice( - deviceIdentifier); - - return device; - } catch (DeviceManagementException e) { - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - return null; - } finally { - deviceManagement.endTenantFlow(); } - + return updated; + } catch (DeviceManagementException e) { + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); + return false; + } finally { + deviceManagement.endTenantFlow(); } - 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); + } + + @Path("/device/{device_id}") + @GET + @Consumes("application/json") + @Produces("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(ConnectedCupConstants.DEVICE_TYPE); + + try { + Device device = deviceManagement.getDeviceManagementService().getDevice( + deviceIdentifier); + + return device; + } catch (DeviceManagementException e) { + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); + return null; + } finally { + deviceManagement.endTenantFlow(); } + } + + 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); + } + } \ No newline at end of file diff --git a/modules/samples/connectedcup/component/pom.xml b/modules/samples/connectedcup/component/pom.xml index 95fc9f7d..45869d0a 100644 --- a/modules/samples/connectedcup/component/pom.xml +++ b/modules/samples/connectedcup/component/pom.xml @@ -40,7 +40,7 @@ plugin controller manager - connected-cup-agent + diff --git a/modules/samples/connectedcup/feature/analytics/CoffeeLevel_Sensor/Eventreceiver_coffeelevel_1.0.0/artifact.xml b/modules/samples/connectedcup/feature/analytics/CoffeeLevel_Sensor/Eventreceiver_coffeelevel_1.0.0/artifact.xml index 3ba63806..68eb6cbf 100644 --- a/modules/samples/connectedcup/feature/analytics/CoffeeLevel_Sensor/Eventreceiver_coffeelevel_1.0.0/artifact.xml +++ b/modules/samples/connectedcup/feature/analytics/CoffeeLevel_Sensor/Eventreceiver_coffeelevel_1.0.0/artifact.xml @@ -1,4 +1,4 @@ - Eventreceiver_coffeelevel.xml + EventReceiver_coffeelevel.xml diff --git a/modules/samples/connectedcup/feature/analytics/CoffeeLevel_Sensor/Sparkscripts_1.0.0/CoffeeLevel_Sensor_Script.xml b/modules/samples/connectedcup/feature/analytics/CoffeeLevel_Sensor/Sparkscripts_1.0.0/CoffeeLevel_Sensor_Script.xml index cb22dc0a..992d0b6d 100644 --- a/modules/samples/connectedcup/feature/analytics/CoffeeLevel_Sensor/Sparkscripts_1.0.0/CoffeeLevel_Sensor_Script.xml +++ b/modules/samples/connectedcup/feature/analytics/CoffeeLevel_Sensor/Sparkscripts_1.0.0/CoffeeLevel_Sensor_Script.xml @@ -1,12 +1,12 @@ - IoTServer_Sensor_Script + CoffeeLevel_Sensor_Script