Merge pull request #97 from charithag/master

Bump event processing version to comply with analytics
application-manager-new
Charitha Goonetilleke 9 years ago
commit 77a2419bc4

@ -738,7 +738,7 @@
org.wso2.carbon.analytics-common:org.wso2.carbon.event.publisher.feature:${carbon.analytics.common.version}
</featureArtifactDef>
<featureArtifactDef>
org.wso2.carbon.event-processing:org.wso2.carbon.event.processor.feature:${carbon.event-processing.version}
org.wso2.carbon.event-processing:org.wso2.carbon.event.processor.server.feature:${carbon.event-processing.version}
</featureArtifactDef>
<featureArtifactDef>
org.wso2.carbon.analytics-common:org.wso2.carbon.event.tracer.feature:${carbon.analytics.common.version}
@ -1617,7 +1617,7 @@
<version>${carbon.analytics.common.version}</version>
</feature>
<feature>
<id>org.wso2.carbon.event.processor.feature.group</id>
<id>org.wso2.carbon.event.processor.server.feature.group</id>
<version>${carbon.event-processing.version}</version>
</feature>
<feature>

@ -147,11 +147,13 @@ public class DoorManagerControllerService {
@FormParam("policy") String policy,
@FormParam("cardNumber") String cardNumber,
@FormParam("userName") String userName,
@FormParam("emailAddress") String emailAddress,
@Context HttpServletResponse response) {
try {
if (userName != null && cardNumber != null && deviceId != null) {
try {
UserStoreManager userStoreManager = this.getUserStoreManager();
DoorLockSafe doorLockSafe = new DoorLockSafe();
if (userStoreManager.isExistingUser(userName)) {
TokenClient accessTokenClient = new TokenClient(DoorManagerConstants.DEVICE_TYPE);
AccessTokenInfo accessTokenInfo = accessTokenClient.getAccessToken(deviceId, userName);
@ -165,7 +167,35 @@ public class DoorManagerControllerService {
claims.put("http://wso2.org/claims/lock/refreshtoken", accessTokenInfo.getRefresh_token());
claims.put("http://wso2.org/claims/lock/cardnumber", cardNumber);
userStoreManager.setUserClaimValues(userName, claims, null);
//TODO: Add content to dto
doorLockSafe.setAccessToken(accessTokenInfo.getAccess_token());
doorLockSafe.setRefreshToken(accessTokenInfo.getRefresh_token());
doorLockSafe.setDeviceId(deviceId);
doorLockSafe.setOwner(owner);
doorLockSafe.setEmailAddress(emailAddress);
doorLockSafe.setUIDofUser(cardNumber);
doorLockSafe.setPolicy(policy);
doorLockSafe.setSerialNumber(deviceId);
boolean status;
try {
DoorManagerDAO.beginTransaction();
status = DOOR_MANAGER_DAO.getAutomaticDoorLockerDeviceDAO().registerDoorLockSafe(doorLockSafe);
DoorManagerDAO.commitTransaction();
if (status) {
response.setStatus(Response.Status.OK.getStatusCode());
} else {
response.setStatus(Response.Status.FORBIDDEN.getStatusCode());
}
} catch (DoorManagerDeviceMgtPluginException e) {
try {
DoorManagerDAO.rollbackTransaction();
} catch (DoorManagerDeviceMgtPluginException e1) {
String msg = "Error while updating the enrollment of the Door Manager Locker device : "
+ doorLockSafe.getOwner();
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
log.error(msg, e);
}
}
response.setStatus(Response.Status.OK.getStatusCode());
} else {
response.setStatus(Response.Status.NOT_FOUND.getStatusCode());
@ -184,7 +214,7 @@ public class DoorManagerControllerService {
}
}
@Path("controller/registerNewUser")
/* @Path("controller/registerNewUser")
@POST
@Feature(code = "registerNewUser", name = "Assign to new user", type = "operation",
description = "Assign to new user")
@ -241,7 +271,7 @@ public class DoorManagerControllerService {
response.setStatus(Response.Status.BAD_REQUEST.getStatusCode());
log.error(e);
}
}
}*/
/*@Path("controller/registerNewUser")
@POST

@ -12,6 +12,7 @@ import org.wso2.carbon.device.mgt.iot.sensormgt.SensorDataManager;
import org.wso2.carbon.device.mgt.iot.transport.TransportHandlerException;
import org.wso2.carbon.device.mgt.iot.transport.mqtt.MQTTTransportHandler;
import javax.ws.rs.core.Response;
import java.io.File;
import java.util.Calendar;
import java.util.UUID;
@ -80,12 +81,16 @@ public class DoorManagerMQTTConnector extends MQTTTransportHandler {
}
if (messageData.length == 2) {
log.warn("-------------------------------------------");
log.warn(messageData[2]);
log.warn(messageData[0]);
log.warn(messageData[1]);
String lockerCurrentState = messageData[1];
try {
SensorDataManager.getInstance().setSensorRecord(deviceId, "door_locker_state",
String.valueOf(1), Calendar.getInstance().getTimeInMillis());
if (!DoorManagerServiceUtils.publishToDASCurrent(owner, deviceId, 1)) {
log.warn("An error occured whilst trying to publish with ID [" + deviceId +
"] of owner [" + owner + "]");
}
} catch(Exception e){
log.error(e);
}
@ -110,6 +115,21 @@ public class DoorManagerMQTTConnector extends MQTTTransportHandler {
String payload = operation + param;
try {
publishToAutomaticDoorLocker(topic, payload, 2, false);
if(param.equals("LOCK")){
if (!DoorManagerServiceUtils.publishToDASCurrent(deviceOwner, deviceId, 0)) {
log.warn("An error occured whilst trying to publish with ID [" + deviceId +
"] of owner [" + deviceOwner + "]");
}
}else{
if (!DoorManagerServiceUtils.publishToDASCurrent(deviceOwner, deviceId, 1)) {
log.warn("An error occured whilst trying to publish with ID [" + deviceId +
"] of owner [" + deviceOwner + "]");
}
}
if (!DoorManagerServiceUtils.publishToDASCurrent(deviceOwner, deviceId, 1)) {
log.warn("An error occured whilst trying to publish with ID [" + deviceId +
"] of owner [" + deviceOwner + "]");
}
} catch (TransportHandlerException e) {
String errorMessage = "Error publishing data to device with ID " + deviceId;
throw new DoorManagerException(errorMessage, e);

@ -0,0 +1,54 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.homeautomation.doormanager.controller.api.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.homeautomation.doormanager.plugin.constants.DoorManagerConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.analytics.exception.DataPublisherConfigurationException;
import org.wso2.carbon.device.mgt.analytics.service.DeviceAnalyticsService;
public class DoorManagerServiceUtils {
private static final Log log = LogFactory.getLog(DoorManagerServiceUtils.class);
//TODO; replace this tenant domain
private static final String SUPER_TENANT = "carbon.super";
private static final String CURRENT_STREAM_DEFINITION = "org.wso2.iot.devices.sensor";
public static boolean publishToDASCurrent(String owner, String deviceId, float current) {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
ctx.setTenantDomain(SUPER_TENANT, true);
DeviceAnalyticsService deviceAnalyticsService = (DeviceAnalyticsService) ctx.getOSGiService(
DeviceAnalyticsService.class, null);
Object metdaData[] = {owner, DoorManagerConstants.DEVICE_TYPE, deviceId, System.currentTimeMillis()};
Object payloadCurrent[] = {current};
try {
deviceAnalyticsService.publishEvent(CURRENT_STREAM_DEFINITION, "1.0.0", metdaData, new Object[0], payloadCurrent);
} catch (DataPublisherConfigurationException e) {
return false;
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
return true;
}
}

@ -228,5 +228,10 @@
<artifactId>json-simple</artifactId>
<version>1.1.wso2v1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
</dependencies>
</project>

@ -18,9 +18,16 @@
package org.homeautomation.doormanager.manager.api;
import com.google.gson.JsonObject;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.geronimo.mail.util.Base64;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.SystemDefaultHttpClient;
import org.homeautomation.doormanager.manager.api.dto.UserInfo;
import org.homeautomation.doormanager.plugin.constants.DoorManagerConstants;
import org.homeautomation.doormanager.plugin.exception.DoorManagerDeviceMgtPluginException;
@ -54,6 +61,7 @@ import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.*;
@ -265,18 +273,22 @@ public class DoorManagerManagerService {
log.warn(userInfo.userName);
log.warn(userInfo.cardNumber);
log.warn(userInfo.deviceId);
response.addHeader("REMOTE_USER", "ppppppppppppppppppppp");
//response.addDateHeader("Authentication","55555555555555555555");
if (userInfo.userName != null && userInfo.cardNumber != null && userInfo.deviceId != null) {
try {
UserStoreManager userStoreManager = this.getUserStoreManager();
if (userStoreManager.isExistingUser(userInfo.userName)) {
String accessToken = userStoreManager.getUserClaimValue(userInfo.userName, "http://wso2.org/claims/lock/accesstoken", null);
String cardNumber = userStoreManager.getUserClaimValue(userInfo.userName, "http://wso2.org/claims/lock/cardnumber", null);
log.warn(accessToken);
log.warn(cardNumber);
if(cardNumber.equals(userInfo.cardNumber)){
if(accessToken != null){
if(accessToken != null && doorManagerDAO.getAutomaticDoorLockerDeviceDAO().
checkCardDoorAssociation(cardNumber, userInfo.deviceId)){
JSONObject credentials = new JSONObject();
credentials.put(DoorManagerConstants.DEVICE_PLUGIN_PROPERTY_ACCESS_TOKEN, accessToken);
credentials.put(DoorManagerConstants.DEVICE_PLUGIN_PROPERTY_ACCESS_TOKEN, accessToken);
sendCEPEvent(userInfo.deviceId, cardNumber, true);
log.warn(doorManagerDAO.getAutomaticDoorLockerDeviceDAO().getUserEmailAddress(cardNumber));
return Response.ok(credentials, MediaType.APPLICATION_JSON_TYPE).build();
}
}
@ -299,6 +311,39 @@ public class DoorManagerManagerService {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
private void sendCEPEvent(String deviceId, String cardId, boolean accessStatus){
String cepEventReciever = "http://localhost:9768/endpoints/LockEventReciever";
HttpClient httpClient = new SystemDefaultHttpClient();
HttpPost method = new HttpPost(cepEventReciever);
JsonObject event = new JsonObject();
JsonObject metaData = new JsonObject();
metaData.addProperty("deviceID", deviceId);
metaData.addProperty("cardID", cardId);
event.add("metaData", metaData);
String eventString = "{\"event\": " + event + "}";
try {
StringEntity entity = new StringEntity(eventString);
method.setEntity(entity);
if (cepEventReciever.startsWith("https")) {
method.setHeader("Authorization", "Basic " + Base64.encode(("admin" + ":" + "admin").getBytes()));
}
httpClient.execute(method).getEntity().getContent().close();
} catch (UnsupportedEncodingException e) {
log.error("Error while constituting CEP event"+ e.getMessage());
} catch (ClientProtocolException e) {
log.error("Error while sending message to CEP "+ e.getMessage());
} catch (IOException e) {
log.error("Error while sending message to CEP "+ e.getMessage());
}
}
@POST
@Path("manager/get_user_info")
@Produces(MediaType.APPLICATION_JSON)

@ -261,6 +261,56 @@ public class DoorManagerDAOImpl {
return status;
}
public boolean checkCardDoorAssociation(String cardNum, String deviceID) throws DoorManagerDeviceMgtPluginException {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
try {
conn = DoorManagerDAO.getConnection();
String selectDBQuery = "SELECT * FROM REGISTERED_DOORLOCK_SAFE WHERE UID_of_USER = ? AND doormanager_DEVICE_ID = ?";
stmt = conn.prepareStatement(selectDBQuery);
stmt.setString(1, cardNum);
stmt.setString(2, deviceID);
resultSet = stmt.executeQuery();
String result;
if(resultSet.next()){
return true;
}else{
return false;
}
} catch (SQLException e) {
String msg = "No associations were found between lock : "+ deviceID +" and card : "+ cardNum;
throw new DoorManagerDeviceMgtPluginException(msg, e);
} finally {
DoorManagerUtils.cleanupResources(stmt, null);
}
}
public String getUserEmailAddress(String cardNum) throws DoorManagerDeviceMgtPluginException {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
String email;
try {
conn = DoorManagerDAO.getConnection();
String selectDBQuery = "SELECT EMAIL_ADDRESS FROM REGISTERED_DOORLOCK_SAFE WHERE UID_of_USER = ?";
stmt = conn.prepareStatement(selectDBQuery);
stmt.setString(1, cardNum);
resultSet = stmt.executeQuery();
if(resultSet.next()){
email = resultSet.getString("EMAIL_ADDRESS");
log.warn(email);
return email;
}
return null;
} catch (SQLException e) {
String msg = "No email found for the and card : "+ cardNum;
throw new DoorManagerDeviceMgtPluginException(msg, e);
} finally {
DoorManagerUtils.cleanupResources(stmt, null);
}
}
public List<String> getUserCredentials(String deviceId, String UIDofUser) throws DoorManagerDeviceMgtPluginException {
Connection conn = null;

@ -25,7 +25,7 @@
{{/zone}}
{{#zone "device-thumbnail"}}
<img src="{{@unit.publicUri}}/images/current-sensor.png"/>
<img src="{{@unit.publicUri}}/images/doormanager-icon.png"/>
{{/zone}}
{{#zone "operation-status"}}

@ -4,7 +4,7 @@
</div>
<div class="col-xs-12 col-sm-4 col-md-4 col-lg-4 padding-top">
<img src="{{@unit.publicUri}}/images/doorManager-icon.png" class="img-responsive">
<img src="{{@unit.publicUri}}/images/doormanager-icon.png" class="img-responsive">
</div>
<div class="col-xs-12 col-sm-8 col-md-8 col-lg-8 padding-top">

@ -140,6 +140,11 @@
<version>4.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
</dependencies>
</dependencyManagement>
<pluginRepositories>
@ -254,4 +259,4 @@
<maven-antrun-plugin.version>1.8</maven-antrun-plugin.version>
<maven-scr-plugin.version>1.7.2</maven-scr-plugin.version>
</properties>
</project>
</project>

@ -1079,7 +1079,7 @@
<cipher.tool.version>1.0.0-wso2v3</cipher.tool.version>
<carbon.dashboard.version>1.0.9</carbon.dashboard.version>
<shindig.version>1.0.0</shindig.version>
<carbon.event-processing.version>2.0.11</carbon.event-processing.version>
<carbon.event-processing.version>2.0.12</carbon.event-processing.version>
<carbon.data.version>4.3.5-SNAPSHOT</carbon.data.version>
<orbit.version.commons-httpclient>3.1.0.wso2v2</orbit.version.commons-httpclient>

Loading…
Cancel
Save