Geesara's changes

application-manager-new
charithag 9 years ago
parent a503433572
commit c556a9a7b0

@ -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;
}
}

@ -273,14 +273,14 @@ 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 && doorManagerDAO.getAutomaticDoorLockerDeviceDAO().
checkCardDoorAssociation(cardNumber, userInfo.deviceId)){
@ -288,6 +288,7 @@ public class DoorManagerManagerService {
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();
}
}
@ -311,22 +312,17 @@ public class DoorManagerManagerService {
}
private void sendCEPEvent(String deviceId, String cardId, boolean accessStatus){
String cepEventReciever = " http://localhost:9763/endpoints/lockAuthorizedEvent";
String cepEventReciever = "http://localhost:9768/endpoints/LockEventReciever";
HttpClient httpClient = new SystemDefaultHttpClient();
HttpPost method = new HttpPost(cepEventReciever);
JsonObject event = new JsonObject();
JsonObject metaData = new JsonObject();
JsonObject payLoadData = new JsonObject();
metaData.addProperty("timestamp", System.currentTimeMillis());
metaData.addProperty("sensorName", "deviceLock");
payLoadData.addProperty("deviceID", deviceId);
payLoadData.addProperty("cardID", cardId);
metaData.addProperty("deviceID", deviceId);
metaData.addProperty("cardID", cardId);
event.add("metaData", metaData);
event.add("payloadData", payLoadData);
String eventString = "{\"event\": " + event + "}";

@ -262,33 +262,55 @@ public class DoorManagerDAOImpl {
}
public boolean checkCardDoorAssociation(String cardNum, String deviceID) throws DoorManagerDeviceMgtPluginException {
boolean status = false;
Connection conn = null;
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
try {
conn = DoorManagerDAO.getConnection();
String createDBQuery = "SELECT * FROM SHARED_DOORLOCK_SAFE WHERE" +
" DOORMANAGER_DEVICE_ID = ? AND SERIAL_NUMBER = ?";
stmt = conn.prepareStatement(createDBQuery);
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);
int rows = stmt.executeUpdate();
if (rows > 0) {
status = true;
if (log.isDebugEnabled()) {
log.debug("Lock : "+ deviceID + " is associated with card no : "+cardNum);
}
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;
log.error(msg, e);
throw new DoorManagerDeviceMgtPluginException(msg, e);
} finally {
DoorManagerUtils.cleanupResources(stmt, null);
}
return status;
}
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>

Loading…
Cancel
Save