Go to the documentation of this file.00001 package com.ros.turtlebot.apps.rocon;
00002
00003 import java.net.URI;
00004 import java.net.URISyntaxException;
00005 import java.util.ArrayList;
00006 import java.util.List;
00007
00008 import org.ros.address.InetAddressFactory;
00009 import org.ros.android.RosActivity;
00010 import org.ros.exception.ServiceException;
00011 import org.ros.internal.node.response.Response;
00012 import org.ros.node.NodeConfiguration;
00013 import org.ros.node.NodeMainExecutor;
00014 import org.ros.node.service.ServiceResponseBuilder;
00015
00016 import android.annotation.SuppressLint;
00017 import android.content.Context;
00018 import android.content.Intent;
00019 import android.net.Uri;
00020 import android.net.wifi.WifiManager;
00021 import android.os.Build;
00022 import android.os.Bundle;
00023 import android.os.Handler;
00024 import android.os.Message;
00025 import android.util.Log;
00026 import android.widget.TextView;
00027 import android.widget.Toast;
00028
00029 import rocon_app_manager_msgs.AppDescription;
00030 import rocon_app_manager_msgs.Constants;
00031 import rocon_app_manager_msgs.ErrorCodes;
00032 import rocon_app_manager_msgs.GetAppListRequest;
00033 import rocon_app_manager_msgs.GetAppListResponse;
00034 import rocon_app_manager_msgs.GetPlatformInfo;
00035 import rocon_app_manager_msgs.GetPlatformInfoRequest;
00036 import rocon_app_manager_msgs.GetPlatformInfoResponse;
00037 import rocon_app_manager_msgs.InviteRequest;
00038 import rocon_app_manager_msgs.InviteResponse;
00039 import rocon_app_manager_msgs.PlatformInfo;
00040 import rocon_app_manager_msgs.StartAppRequest;
00041 import rocon_app_manager_msgs.StartAppResponse;
00042 import rocon_app_manager_msgs.StatusRequest;
00043 import rocon_app_manager_msgs.StatusResponse;
00044
00045 public abstract class RosBaseActivity extends RosActivity {
00046
00047 private NodeConfiguration nodeConfiguration;
00048 private NodeMainExecutor nodeMainExecutor;
00049
00050 protected TextView txtInformation = null ;
00051
00052
00053 protected String appName = "" ;
00054 protected String nodeName = "" ;
00055 protected String platform = "" ;
00056 protected String deviceName = "" ;
00057 protected String versionName = "" ;
00058 protected String remoteTargetname = "" ;
00059 protected String applicationNameSpace = "" ;
00060 protected String appStatus = "" ;
00061
00062
00063 protected String nfc_table = "";
00064 protected String nfc_ssid = "";
00065 protected String nfc_password = "";
00066 protected String nfc_masteruri = "";
00067 protected String nfc_weblink = "";
00068
00069
00070 RoconAppManager platformInfo_manager = null ;
00071 RoconAppManager appList_manager = null ;
00072 RoconAppManager invite_manager = null ;
00073 RoconAppManager status_manager = null ;
00074 RoconAppManager startApp_manager = null ;
00075
00076 public RosBaseActivity() {
00077 super("Rocon Service", "Rocon Service");
00078
00079 }
00080
00081 @SuppressLint("NewApi")
00082 @Override
00083 protected void onCreate(Bundle savedInstanceState) {
00084
00085 Log.d("RosBaseActivity", "onCreate");
00086 super.onCreate(savedInstanceState);
00087 appName = "android" ;
00088 platform = "android" ;
00089 nodeName = appName + java.util.UUID.randomUUID().toString().replace("-", "") ;
00090 deviceName = android.os.Build.MODEL.replace(" ", "_").replace(".", "_") ;
00091 versionName = Util.getAndroidVersionName().replace(" ", "_").replace(".", "_");
00092 appStatus = Constants.APP_STOPPED ;
00093 }
00094
00095 @Override
00096 protected void onResume() {
00097
00098 super.onResume();
00099
00100 appStatus = Constants.APP_STOPPED ;
00101 }
00102
00103
00104 @Override
00105 protected void onPause() {
00106
00107 super.onPause();
00108 Log.d("RosBaseActivity", "onPause()");
00109 }
00110
00111 @Override
00112 protected void onDestroy() {
00113
00114 Log.d("RosBaseActivity", "onDestroy()");
00115 super.onDestroy();
00116
00117
00118 System.runFinalizersOnExit(true);
00119 System.exit(0);
00120
00121 }
00122
00123 public void onServerConnected(String action) {
00124 Log.d("RosBaseActivity", "onServerConnected()-" + action) ;
00125 if(action.equals(RoconAppManager.ACTION_INVITE)) {
00126 runOnUiThread(new Runnable() {
00127 @Override
00128 public void run() {
00129
00130 txtInformation.setText("주문 페이지로 이동중입니다");
00131 }
00132 }) ;
00133 }
00134 }
00135
00136
00137 @Override
00138 protected void init(NodeMainExecutor nodeMainExecutor) {
00139
00140 Log.d("RosBaseActivity", "init" + getMasterUri());
00141 this.nodeMainExecutor = nodeMainExecutor ;
00142 WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
00143 this.nodeConfiguration = NodeConfiguration.newPublic(Util.getWifiAddress(wifiManager, 2000), getMasterUri());
00144
00145 platformInfo_manager = new RoconAppManager(nodeName, RoconAppManager.ACTION_PLATFORMINFO);
00146 platformInfo_manager.setParent(this);
00147 init_PlatformInfo() ;
00148
00149 appList_manager = new RoconAppManager(nodeName, RoconAppManager.ACTION_APPLIST) ;
00150 appList_manager.setParent(this);
00151 init_AppList() ;
00152
00153 status_manager = new RoconAppManager(nodeName, RoconAppManager.ACTION_STATUS) ;
00154 status_manager.setParent(this);
00155 init_Status() ;
00156
00157 invite_manager = new RoconAppManager(nodeName, RoconAppManager.ACTION_INVITE) ;
00158 invite_manager.setParent(this);
00159 init_Invite() ;
00160
00161
00162
00163
00164 }
00165
00166 private void init_PlatformInfo() {
00167 Log.d("RosBaseActivity", "init_PlatformInfo()");
00168 platformInfo_manager.setPlatformInfoResponseBuilder(
00169 new ServiceResponseBuilder<GetPlatformInfoRequest, GetPlatformInfoResponse>() {
00170
00171 @Override
00172 public void build(GetPlatformInfoRequest request, GetPlatformInfoResponse response)
00173 throws ServiceException {
00174
00175
00176 PlatformInfo msg = platformInfo_manager.getConnectedNode().getTopicMessageFactory().newFromType(PlatformInfo._TYPE);
00177 msg.setName(appName);
00178 msg.setPlatform(platform);
00179 msg.setRobot(deviceName);
00180 msg.setSystem(versionName);
00181
00182 response.setPlatformInfo(msg);
00183 }
00184 });
00185
00186 nodeMainExecutor.execute(platformInfo_manager, nodeConfiguration.setNodeName(nodeName + "/" + RoconAppManager.ACTION_PLATFORMINFO));
00187 }
00188
00189 private void init_AppList() {
00190 Log.d("RosBaseActivity", "init_AppList()");
00191 appList_manager.setAppListResponseBuilder(new ServiceResponseBuilder<GetAppListRequest, GetAppListResponse>() {
00192
00193 @Override
00194 public void build(GetAppListRequest request, GetAppListResponse response)
00195 throws ServiceException {
00196
00197
00198 List<rocon_app_manager_msgs.AppDescription> app_list = new ArrayList<rocon_app_manager_msgs.AppDescription>();
00199 AppDescription app = appList_manager.getConnectedNode().getTopicMessageFactory().newFromType(AppDescription._TYPE);
00200 app.setDescription("Orders drinks");
00201 app.setDisplay("Cafe Order App");
00202 app.setName("OrderDrinks");
00203 app.setPlatform(appName + "." + versionName + "." + deviceName);
00204 app.setStatus(appStatus) ;
00205 app_list.add(app);
00206 response.setApps(app_list);
00207 }
00208 });
00209
00210 nodeMainExecutor.execute(appList_manager, nodeConfiguration.setNodeName(nodeName + "/" + RoconAppManager.ACTION_APPLIST));
00211 }
00212
00213 private void init_Invite() {
00214 Log.d("RosBaseActivity", "init_Invite()");
00215 invite_manager.setInviteResponseBuilder(new ServiceResponseBuilder<InviteRequest, InviteResponse>() {
00216
00217 @Override
00218 public void build(InviteRequest request, InviteResponse response)
00219 throws ServiceException {
00220
00221 remoteTargetname = request.getRemoteTargetName() ;
00222 applicationNameSpace = request.getApplicationNamespace() ;
00223
00224 response.setResult(true);
00225
00226 startApp_manager = new RoconAppManager(nodeName, RoconAppManager.ACTION_STARTAPP) ;
00227 init_StartApp() ;
00228 }
00229 }) ;
00230
00231 nodeMainExecutor.execute(invite_manager, nodeConfiguration.setNodeName(nodeName + "/" + RoconAppManager.ACTION_INVITE)) ;
00232 }
00233
00234 private void init_Status() {
00235 Log.d("RosBaseActivity", "init_Status()");
00236 status_manager.setStatusResponseBuilder(new ServiceResponseBuilder<StatusRequest, StatusResponse>() {
00237
00238 @Override
00239 public void build(StatusRequest request, StatusResponse response)
00240 throws ServiceException {
00241
00242
00243 response.setApplicationNamespace(applicationNameSpace);
00244 response.setAppStatus(appStatus);
00245 response.setRemoteController(remoteTargetname);
00246 }
00247 }) ;
00248
00249 nodeMainExecutor.execute(status_manager, nodeConfiguration.setNodeName(nodeName + "/" + RoconAppManager.ACTION_STATUS));
00250 }
00251
00252 private void init_StartApp() {
00253 Log.d("RosBaseActivity", "init_StartApp()");
00254 startApp_manager.setStartAppResponseBuilder(new ServiceResponseBuilder<StartAppRequest, StartAppResponse>() {
00255
00256 @Override
00257 public void build(StartAppRequest request, StartAppResponse response)
00258 throws ServiceException {
00259
00260 try {
00261
00262 String application_name = request.getName() ;
00263 List<rocon_app_manager_msgs.Remapping> list_remapping = request.getRemappings() ;
00264
00265 response.setAppNamespace(applicationNameSpace);
00266 response.setMessage("message");
00267 response.setStarted(true);
00268 response.setErrorCode(ErrorCodes.SUCCESS);
00269
00270
00271 String app_uri = nfc_weblink + "?tableid=" + nfc_table + "&ssid=" + nfc_ssid + "&password=" + nfc_password + "&concertaddress=" + getMasterUri().getHost() ;
00272
00273 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(app_uri));
00274 startActivity(intent);
00275
00276 runOnUiThread(new Runnable() {
00277 @Override
00278 public void run() {
00279
00280 txtInformation.setText("감사합니다");
00281 }
00282 });
00283
00284
00285 appStatus = Constants.APP_RUNNING ;
00286
00287 } catch(Exception e) {
00288 e.printStackTrace();
00289 runOnUiThread(new Runnable() {
00290 @Override
00291 public void run() {
00292
00293 Toast.makeText(getApplicationContext(),"Exception occurred - StartApp", Toast.LENGTH_LONG).show();
00294 }
00295 });
00296
00297 }
00298 }
00299 }) ;
00300
00301 nodeMainExecutor.execute(startApp_manager, nodeConfiguration.setNodeName(nodeName + "/" + RoconAppManager.ACTION_STARTAPP));
00302 }
00303 }