Skip to content

Commit

Permalink
Device Subscription에 따른 로직 변경 iotoasis#291
Browse files Browse the repository at this point in the history
  • Loading branch information
pahnjy01 committed Dec 8, 2016
1 parent 4875552 commit e20673a
Show file tree
Hide file tree
Showing 13 changed files with 229 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ public class DeviceStatusData {
*/
private String _uri;

/**
* Device를 제어 했을시 제어 항목
*/
private String _command;

/**
* Device의 상태 값
*/
Expand All @@ -33,10 +28,9 @@ public class DeviceStatusData {
public DeviceStatusData() {
}

public DeviceStatusData(String _commandId, String _uri, String _command, String deviceStatus, String timestamp) {
public DeviceStatusData(String _commandId, String _uri, String deviceStatus, String timestamp) {
this._commandId = _commandId;
this._uri = _uri;
this._command = _command;
this.deviceStatus = deviceStatus;
this.timestamp = timestamp;
}
Expand All @@ -57,13 +51,6 @@ public void set_uri(String _uri) {
this._uri = _uri;
}

public String get_command() {
return _command;
}

public void set_command(String _command) {
this._command = _command;
}

public String getDeviceStatus() {
return deviceStatus;
Expand Down Expand Up @@ -91,7 +78,6 @@ public String toString() {
return "DeviceStatusData{" +
"_commandId='" + _commandId + '\'' +
", _uri='" + _uri + '\'' +
", _command='" + _command + '\'' +
", deviceStatus='" + deviceStatus + '\'' +
", timestamp='" + timestamp + '\'' +
'}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
public class DeviceSubscriptionData {
//
private String _uri;
private String _notificationUri;
private String _commandId;
private String _notificationUri;

public DeviceSubscriptionData() {
}

public DeviceSubscriptionData(String _uri, String _notificationUri) {
public DeviceSubscriptionData(String _uri, String _commandId, String _notificationUri) {
this._uri = _uri;
this._commandId = _commandId;
this._notificationUri = _notificationUri;
}

Expand All @@ -21,6 +23,14 @@ public void set_uri(String _uri) {
this._uri = _uri;
}

public String get_commandId() {
return _commandId;
}

public void set_commandId(String _commandId) {
this._commandId = _commandId;
}

public String get_notificationUri() {
return _notificationUri;
}
Expand All @@ -33,6 +43,7 @@ public void set_notificationUri(String _notificationUri) {
public String toString() {
return "DeviceSubscriptionData{" +
"_uri='" + _uri + '\'' +
", _commandId='" + _commandId + '\'' +
", _notificationUri='" + _notificationUri + '\'' +
'}';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public interface DeviceManager {
String searchOperation(String de, String deviceService);
List<Device> searchDeviceList();
void deviceUpdate(DeviceStatusData deviceStatusData);
String deviceSubscription(String uri);
String deviceSubscription(String uri, String commandId);
String deviceSubscriptionRelease(String uri);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.pineone.icbms.so.device.proxy.DeviceICollectionProxy;
import com.pineone.icbms.so.device.store.DeviceResultStore;
import com.pineone.icbms.so.device.store.DeviceStore;
import com.pineone.icbms.so.device.store.DeviceSubscriptionStore;
import com.pineone.icbms.so.device.store.mongo.DeviceSubscriptionObject;
import com.pineone.icbms.so.device.util.ClientProfile;
import com.pineone.icbms.so.util.address.AddressStore;
import com.pineone.icbms.so.util.address.ContextAddress;
Expand All @@ -21,7 +23,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class DeviceManagerLogic implements DeviceManager {
Expand All @@ -46,6 +47,9 @@ public class DeviceManagerLogic implements DeviceManager {
@Autowired
private DeviceControlProxy deviceControlProxy;

@Autowired
private DeviceSubscriptionStore deviceSubscriptionStore;

@Autowired
ContextAddress contextAddress;

Expand Down Expand Up @@ -133,13 +137,25 @@ public String deviceExecute(String deviceId,String deviceCommand, String session
* Device 제어 후 제어 결과가 Success면 Device Subscription 요청
*/
if(resultMessage.getCode().equals(ResultMessage.RESPONSE_SUCCESS_ONEM2MCODE)) {
String response = deviceSubscription(device.getDeviceUri());
String subscriptionUri = device.getDeviceUri() + (ClientProfile.actionDeviceCommand(device.getDeviceUri()) ? ClientProfile.SI_CONTAINER_ACTION : ClientProfile.SI_CONTAINER_POWER) + ClientProfile.SI_CONTAINER_STATUS;
String response = deviceSubscription(subscriptionUri, deviceControlMessage.get_commandId());
logger.debug(LogPrint.LogMethodNamePrint() + " | Device Subscription : " + " , Device Uri = " + device.getDeviceUri() + " , Result : " + response + " , Session ID = " + sessionId);

/**
* Device Subscription 데이터 저장
*/
if(response.equals(ResultMessage.RESPONSE_SUCCESS_ONEM2MCODE)){
saveDeviceSubscriptionData(deviceControlMessage.get_commandId(), deviceControlMessage.getCon());
}

/*
디바이스 해제는 Controller에서 상태 업데이트 후 해제.
if(response.equals(ResultMessage.RESPONSE_SUCCESS_ONEM2MCODE)){
/**
*//**
* Device Subscription이 성공이면 30초 후 Subscription 해지 요청.
* 시간은 정책으로 수정 가능.
*/
*//*
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
Expand All @@ -156,6 +172,8 @@ public void run() {
});
thread.start();
}
*/

}


Expand Down Expand Up @@ -229,18 +247,27 @@ public List<Device> searchDeviceList() {
public void deviceUpdate(DeviceStatusData deviceStatusData) {
//
if(!deviceStatusData.get_uri().isEmpty()){
Device device = deviceSearchById(deviceStatusData.get_uri());
if(!deviceStatusData.getDeviceStatus().isEmpty() && !deviceStatusData.checkDeviceStatus(device.getDeviceStatus())) {
String deviceUri = getOnem2mDeviceUri(deviceStatusData.get_uri());
Device device = deviceSearchById(deviceUri);
DeviceSubscriptionObject deviceSubscriptionObject = deviceSubscriptionStore.retrieve(deviceStatusData.get_commandId());

if(deviceSubscriptionObject != null && deviceSubscriptionObject.get_commandId().equals(deviceStatusData.get_commandId()) && deviceSubscriptionObject.getDeviceStatus().equals(deviceStatusData.getDeviceStatus())){
device.setDeviceStatus(deviceStatusData.getDeviceStatus());
deviceStore.update(device);
deviceSubscriptionRelease(deviceUri + (ClientProfile.actionDeviceCommand(device.getDeviceUri()) ? ClientProfile.SI_CONTAINER_ACTION : ClientProfile.SI_CONTAINER_POWER) + ClientProfile.SI_CONTAINER_STATUS);
deviceSubscriptionStore.delete(deviceSubscriptionObject.get_id());
}

if(!deviceStatusData.getDeviceStatus().isEmpty() && !deviceStatusData.checkDeviceStatus(device.getDeviceStatus())) {

}
}
}

@Override
public String deviceSubscription(String uri) {
public String deviceSubscription(String uri, String commandId) {
//
return deviceControlProxy.deviceSubscriptionRequest(uri);
return deviceControlProxy.deviceSubscriptionRequest(uri, commandId);
}

@Override
Expand All @@ -249,13 +276,12 @@ public String deviceSubscriptionRelease(String uri) {
return deviceControlProxy.deviceSubscriptionReleaseRequest(uri);
}


private DeviceControlMessage deviceDataConversion(String deviceId, String commandId, String deviceCommand){
DeviceControlMessage deviceControlMessage = new DeviceControlMessage();

deviceControlMessage.set_uri(deviceId);
deviceControlMessage.set_commandId(commandId);
if(deviceId != null && (deviceId.contains("Blind") || deviceId.contains("BeamScreen"))){
if(deviceId != null && ClientProfile.actionDeviceCommand(deviceId)){
deviceControlMessage.set_command(ClientProfile.SI_CONTROL_ACTION);
}else {
deviceControlMessage.set_command(ClientProfile.SI_CONTROL_POWER);
Expand Down Expand Up @@ -322,4 +348,19 @@ private void sessionDataUpdate(SessionStore sessionStore, Session session, Strin
sessionStore.updateSession(session);
}

public String getOnem2mDeviceUri(String uri){
//
int stringlength = 3;
String[] strings = uri.split("/");

for(int i =1; i<4 ; i++){
stringlength += strings[i].length();
}
return uri.substring(0, stringlength);
}

public void saveDeviceSubscriptionData(String deviceUri, String commandId){
deviceSubscriptionStore.create(new DeviceSubscriptionObject(deviceUri, commandId));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public interface DeviceControlProxy {
* @param deviceUri
* @return
*/
String deviceSubscriptionRequest(String deviceUri);
String deviceSubscriptionRequest(String deviceUri, String commandId);

/**
* Device 구독 해지 요청
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,17 @@ public ResultMessage deviceControlRequest(String requestUrl, DeviceControlMessag
}

@Override
public String deviceSubscriptionRequest(String deviceUri) {
public String deviceSubscriptionRequest(String deviceUri, String commandId) {
//
logger.info("<================ Device Subscription Request Start ================>");
logger.debug(LogPrint.LogMethodNamePrint() + " | DeviceUri = " + deviceUri);

DeviceSubscriptionData deviceSubscriptionData = new DeviceSubscriptionData();
deviceSubscriptionData.set_notificationUri(contextAddress.getServerAddress(ContextAddress.SO_SERVER) + AddressStore.SO_DEVICE_STATUS);
deviceSubscriptionData.set_uri(deviceUri);
deviceSubscriptionData.set_commandId(commandId);

String requestBody = new Gson().toJson(deviceSubscriptionData);
// DeviceLogic에 생성되거나 제어되면 서브스크립트 걸어야 겠군.
// 현제 정책이 안되어 있어서. 등록되면 걸지.. 제어시 걸지는 고려 필요.

String responseData = clientService.requestPostServiceReceiveString(contextAddress.getServerAddress(ContextAddress.SI_SERVER) + AddressStore.SI_SUBSCRIPTION_URI, requestBody);
// ResponseData{ "code" : "2000", "message" : "", "content" : "" }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.pineone.icbms.so.device.store;

import com.pineone.icbms.so.device.store.mongo.DeviceSubscriptionObject;

public interface DeviceSubscriptionStore {
void create(DeviceSubscriptionObject deviceSubscriptionObject);
DeviceSubscriptionObject retrieve(String commandId);
void delete(String commandId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.pineone.icbms.so.device.store.mongo;

import com.pineone.icbms.so.device.store.DeviceSubscriptionStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
* MongoDB와 DeviceSubscrption 연결
*/
@Repository
public class DeviceSubscriptionMongoStore implements DeviceSubscriptionStore {

public static final Logger logger = LoggerFactory.getLogger(DeviceSubscriptionMongoStore.class);

@Autowired
DeviceSubscriptionRepository deviceSubscriptionRepository;

@Override
public void create(DeviceSubscriptionObject deviceSubscriptionObject) {
logger.debug("Device = " + deviceSubscriptionObject.toString());
deviceSubscriptionRepository.save(deviceSubscriptionObject);
}

@Override
public DeviceSubscriptionObject retrieve(String commandId) {
logger.debug("Command ID = " + commandId);
return deviceSubscriptionRepository.findBy_commandId(commandId);
}

@Override
public void delete(String commandId) {
logger.debug("Command ID = " + commandId);
deviceSubscriptionRepository.delete(commandId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.pineone.icbms.so.device.store.mongo;

import org.springframework.data.mongodb.core.mapping.Document;

/**
* Device Subscription에 대한 데이터
*/
@Document(collection = "DeviceSubscription")
public class DeviceSubscriptionObject {
//

private String _id;

private String _commandId;

private String deviceStatus;

public DeviceSubscriptionObject() {
}

public DeviceSubscriptionObject(String _commandId, String deviceStatus) {
this._commandId = _commandId;
this.deviceStatus = deviceStatus;
}

public DeviceSubscriptionObject(String _id, String _commandId, String deviceStatus) {
this._id = _id;
this._commandId = _commandId;
this.deviceStatus = deviceStatus;
}

public String get_commandId() {
return _commandId;
}

public void set_commandId(String _commandId) {
this._commandId = _commandId;
}

public String getDeviceStatus() {
return deviceStatus;
}

public void setDeviceStatus(String deviceStatus) {
this.deviceStatus = deviceStatus;
}

public String get_id() {
return _id;
}

public void set_id(String _id) {
this._id = _id;
}

@Override
public String toString() {
return "DeviceSubscriptionObject{" +
"_id='" + _id + '\'' +
", _commandId='" + _commandId + '\'' +
", deviceStatus='" + deviceStatus + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.pineone.icbms.so.device.store.mongo;

import org.springframework.data.mongodb.repository.MongoRepository;

public interface DeviceSubscriptionRepository extends MongoRepository<DeviceSubscriptionObject, String> {

DeviceSubscriptionObject findBy_commandId(String commandId);

}
Loading

0 comments on commit e20673a

Please sign in to comment.