diff --git a/Wangzj b/Wangzj new file mode 100644 index 0000000..e69de29 diff --git a/src/AndroidStudio_projects/.gitignore b/src/AndroidStudio_projects/.gitignore new file mode 100644 index 0000000..aa724b7 --- /dev/null +++ b/src/AndroidStudio_projects/.gitignore @@ -0,0 +1,15 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx +local.properties diff --git a/src/AndroidStudio_projects/.idea/.gitignore b/src/AndroidStudio_projects/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/src/AndroidStudio_projects/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/src/AndroidStudio_projects/.idea/.name b/src/AndroidStudio_projects/.idea/.name new file mode 100644 index 0000000..1a82d1e --- /dev/null +++ b/src/AndroidStudio_projects/.idea/.name @@ -0,0 +1 @@ +Hamburger \ No newline at end of file diff --git a/src/AndroidStudio_projects/.idea/compiler.xml b/src/AndroidStudio_projects/.idea/compiler.xml new file mode 100644 index 0000000..fb7f4a8 --- /dev/null +++ b/src/AndroidStudio_projects/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/AndroidStudio_projects/.idea/deploymentTargetDropDown.xml b/src/AndroidStudio_projects/.idea/deploymentTargetDropDown.xml new file mode 100644 index 0000000..6c67e23 --- /dev/null +++ b/src/AndroidStudio_projects/.idea/deploymentTargetDropDown.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/AndroidStudio_projects/.idea/gradle.xml b/src/AndroidStudio_projects/.idea/gradle.xml new file mode 100644 index 0000000..a2d7c21 --- /dev/null +++ b/src/AndroidStudio_projects/.idea/gradle.xml @@ -0,0 +1,19 @@ + + + + + + + \ No newline at end of file diff --git a/src/AndroidStudio_projects/.idea/misc.xml b/src/AndroidStudio_projects/.idea/misc.xml new file mode 100644 index 0000000..bdd9278 --- /dev/null +++ b/src/AndroidStudio_projects/.idea/misc.xml @@ -0,0 +1,10 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/.gitignore b/src/AndroidStudio_projects/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/src/AndroidStudio_projects/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/.idea/.gitignore b/src/AndroidStudio_projects/app/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/src/AndroidStudio_projects/app/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/src/AndroidStudio_projects/app/.idea/gradle.xml b/src/AndroidStudio_projects/app/.idea/gradle.xml new file mode 100644 index 0000000..b898c0a --- /dev/null +++ b/src/AndroidStudio_projects/app/.idea/gradle.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/.idea/misc.xml b/src/AndroidStudio_projects/app/.idea/misc.xml new file mode 100644 index 0000000..6ff4d26 --- /dev/null +++ b/src/AndroidStudio_projects/app/.idea/misc.xml @@ -0,0 +1,10 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/CMakeLists.txt b/src/AndroidStudio_projects/app/CMakeLists.txt new file mode 100644 index 0000000..4fb9716 --- /dev/null +++ b/src/AndroidStudio_projects/app/CMakeLists.txt @@ -0,0 +1,44 @@ +# For more information about using CMake with Android Studio, read the +# documentation: https://d.android.com/studio/projects/add-native-code.html + +# Sets the minimum version of CMake required to build the native library. + +cmake_minimum_required(VERSION 3.4.1) + +# Creates and names a library, sets it as either STATIC +# or SHARED, and provides the relative paths to its source code. +# You can define multiple libraries, and CMake builds them for you. +# Gradle automatically packages shared libraries with your APK. + +add_library( # Sets the name of the library. + native-test + + # Sets the library as a shared library. + SHARED + + # Provides a relative path to your source file(s). + src/main/jni/native-test.c ) + +# Searches for a specified prebuilt library and stores the path as a +# variable. Because CMake includes system libraries in the search path by +# default, you only need to specify the name of the public NDK library +# you want to add. CMake verifies that the library exists before +# completing its build. + +find_library( # Sets the name of the path variable. + log-lib + + # Specifies the name of the NDK library that + # you want CMake to locate. + log ) + +# Specifies libraries CMake should link to your target library. You +# can link multiple libraries, such as libraries you define in this +# build script, prebuilt third-party libraries, or system libraries. + +target_link_libraries( # Specifies the target library. + native-test + + # Links the target library to the log library + # included in the NDK. + ${log-lib} ) \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/build.gradle b/src/AndroidStudio_projects/app/build.gradle new file mode 100644 index 0000000..221593c --- /dev/null +++ b/src/AndroidStudio_projects/app/build.gradle @@ -0,0 +1,75 @@ +plugins { + id 'com.android.application' +} + +android { + namespace 'com.example.hamburger' + compileSdk 32 + + defaultConfig { + applicationId "com.example.hamburger" + minSdk 21 + targetSdk 32 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + externalNativeBuild { + cmake { + cppFlags "" + } + } + + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + externalNativeBuild { + cmake { + path "CMakeLists.txt" + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + buildFeatures { + viewBinding true + } +} + +dependencies { + + + + + implementation 'androidx.appcompat:appcompat:1.4.1' + implementation 'com.google.android.material:material:1.5.0' + implementation 'androidx.constraintlayout:constraintlayout:2.1.3' + implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.1' + implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1' + implementation 'androidx.navigation:navigation-fragment:2.4.1' + implementation 'androidx.navigation:navigation-ui:2.4.1' + implementation files('libs\\mysql-connector-java-5.1.49.jar') + testImplementation 'junit:junit:4.13.2' + androidTestImplementation 'androidx.test.ext:junit:1.1.3' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' + + implementation 'com.jakewharton:butterknife:10.0.0' + annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0' + //noinspection GradleCompatible + androidTestImplementation 'com.android.support.test:runner:1.0.2' + //noinspection GradleCompatible + //implementation 'com.android.support:recyclerview-v7:28.0.0' + implementation 'androidx.appcompat:appcompat:1.0.0' + //noinspection DuplicatePlatformClasses + implementation fileTree(include: ['*.jar'], dir: 'libs') + implementation files('libs/json-simple-1.1.jar') + + + +} \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/libs/eventbus.jar b/src/AndroidStudio_projects/app/libs/eventbus.jar new file mode 100644 index 0000000..3e62b44 Binary files /dev/null and b/src/AndroidStudio_projects/app/libs/eventbus.jar differ diff --git a/src/AndroidStudio_projects/app/libs/java_websocket.jar b/src/AndroidStudio_projects/app/libs/java_websocket.jar new file mode 100644 index 0000000..bb5caeb Binary files /dev/null and b/src/AndroidStudio_projects/app/libs/java_websocket.jar differ diff --git a/src/AndroidStudio_projects/app/libs/json-simple-1.1.jar b/src/AndroidStudio_projects/app/libs/json-simple-1.1.jar new file mode 100644 index 0000000..f395f41 Binary files /dev/null and b/src/AndroidStudio_projects/app/libs/json-simple-1.1.jar differ diff --git a/src/AndroidStudio_projects/app/libs/mysql-connector-java-5.1.49.jar b/src/AndroidStudio_projects/app/libs/mysql-connector-java-5.1.49.jar new file mode 100644 index 0000000..d3c8b41 Binary files /dev/null and b/src/AndroidStudio_projects/app/libs/mysql-connector-java-5.1.49.jar differ diff --git a/src/AndroidStudio_projects/app/proguard-rules.pro b/src/AndroidStudio_projects/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/src/AndroidStudio_projects/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/androidTest/java/com/example/hamburger/ExampleInstrumentedTest.java b/src/AndroidStudio_projects/app/src/androidTest/java/com/example/hamburger/ExampleInstrumentedTest.java new file mode 100644 index 0000000..95943b8 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/androidTest/java/com/example/hamburger/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.example.hamburger; + +import android.content.Context; + +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + assertEquals("com.example.hamburger", appContext.getPackageName()); + } +} \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/debug/ic_launcher-playstore.png b/src/AndroidStudio_projects/app/src/debug/ic_launcher-playstore.png new file mode 100644 index 0000000..8fd3645 Binary files /dev/null and b/src/AndroidStudio_projects/app/src/debug/ic_launcher-playstore.png differ diff --git a/src/AndroidStudio_projects/app/src/debug/res/mipmap-anydpi-v26/ic_launcher.xml b/src/AndroidStudio_projects/app/src/debug/res/mipmap-anydpi-v26/ic_launcher.xml new file mode 100644 index 0000000..036d09b --- /dev/null +++ b/src/AndroidStudio_projects/app/src/debug/res/mipmap-anydpi-v26/ic_launcher.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/debug/res/mipmap-anydpi-v26/ic_launcher_round.xml b/src/AndroidStudio_projects/app/src/debug/res/mipmap-anydpi-v26/ic_launcher_round.xml new file mode 100644 index 0000000..036d09b --- /dev/null +++ b/src/AndroidStudio_projects/app/src/debug/res/mipmap-anydpi-v26/ic_launcher_round.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/debug/res/mipmap-hdpi/ic_launcher.png b/src/AndroidStudio_projects/app/src/debug/res/mipmap-hdpi/ic_launcher.png new file mode 100644 index 0000000..f479211 Binary files /dev/null and b/src/AndroidStudio_projects/app/src/debug/res/mipmap-hdpi/ic_launcher.png differ diff --git a/src/AndroidStudio_projects/app/src/debug/res/mipmap-hdpi/ic_launcher_foreground.png b/src/AndroidStudio_projects/app/src/debug/res/mipmap-hdpi/ic_launcher_foreground.png new file mode 100644 index 0000000..1a3380f Binary files /dev/null and b/src/AndroidStudio_projects/app/src/debug/res/mipmap-hdpi/ic_launcher_foreground.png differ diff --git a/src/AndroidStudio_projects/app/src/debug/res/mipmap-hdpi/ic_launcher_round.png b/src/AndroidStudio_projects/app/src/debug/res/mipmap-hdpi/ic_launcher_round.png new file mode 100644 index 0000000..661686f Binary files /dev/null and b/src/AndroidStudio_projects/app/src/debug/res/mipmap-hdpi/ic_launcher_round.png differ diff --git a/src/AndroidStudio_projects/app/src/debug/res/mipmap-mdpi/ic_launcher.png b/src/AndroidStudio_projects/app/src/debug/res/mipmap-mdpi/ic_launcher.png new file mode 100644 index 0000000..9467357 Binary files /dev/null and b/src/AndroidStudio_projects/app/src/debug/res/mipmap-mdpi/ic_launcher.png differ diff --git a/src/AndroidStudio_projects/app/src/debug/res/mipmap-mdpi/ic_launcher_foreground.png b/src/AndroidStudio_projects/app/src/debug/res/mipmap-mdpi/ic_launcher_foreground.png new file mode 100644 index 0000000..d089659 Binary files /dev/null and b/src/AndroidStudio_projects/app/src/debug/res/mipmap-mdpi/ic_launcher_foreground.png differ diff --git a/src/AndroidStudio_projects/app/src/debug/res/mipmap-mdpi/ic_launcher_round.png b/src/AndroidStudio_projects/app/src/debug/res/mipmap-mdpi/ic_launcher_round.png new file mode 100644 index 0000000..c82b85b Binary files /dev/null and b/src/AndroidStudio_projects/app/src/debug/res/mipmap-mdpi/ic_launcher_round.png differ diff --git a/src/AndroidStudio_projects/app/src/debug/res/mipmap-xhdpi/ic_launcher.png b/src/AndroidStudio_projects/app/src/debug/res/mipmap-xhdpi/ic_launcher.png new file mode 100644 index 0000000..273374d Binary files /dev/null and b/src/AndroidStudio_projects/app/src/debug/res/mipmap-xhdpi/ic_launcher.png differ diff --git a/src/AndroidStudio_projects/app/src/debug/res/mipmap-xhdpi/ic_launcher_foreground.png b/src/AndroidStudio_projects/app/src/debug/res/mipmap-xhdpi/ic_launcher_foreground.png new file mode 100644 index 0000000..9111144 Binary files /dev/null and b/src/AndroidStudio_projects/app/src/debug/res/mipmap-xhdpi/ic_launcher_foreground.png differ diff --git a/src/AndroidStudio_projects/app/src/debug/res/mipmap-xhdpi/ic_launcher_round.png b/src/AndroidStudio_projects/app/src/debug/res/mipmap-xhdpi/ic_launcher_round.png new file mode 100644 index 0000000..246f4ea Binary files /dev/null and b/src/AndroidStudio_projects/app/src/debug/res/mipmap-xhdpi/ic_launcher_round.png differ diff --git a/src/AndroidStudio_projects/app/src/debug/res/mipmap-xxhdpi/ic_launcher.png b/src/AndroidStudio_projects/app/src/debug/res/mipmap-xxhdpi/ic_launcher.png new file mode 100644 index 0000000..703166a Binary files /dev/null and b/src/AndroidStudio_projects/app/src/debug/res/mipmap-xxhdpi/ic_launcher.png differ diff --git a/src/AndroidStudio_projects/app/src/debug/res/mipmap-xxhdpi/ic_launcher_foreground.png b/src/AndroidStudio_projects/app/src/debug/res/mipmap-xxhdpi/ic_launcher_foreground.png new file mode 100644 index 0000000..8224030 Binary files /dev/null and b/src/AndroidStudio_projects/app/src/debug/res/mipmap-xxhdpi/ic_launcher_foreground.png differ diff --git a/src/AndroidStudio_projects/app/src/debug/res/mipmap-xxhdpi/ic_launcher_round.png b/src/AndroidStudio_projects/app/src/debug/res/mipmap-xxhdpi/ic_launcher_round.png new file mode 100644 index 0000000..21c43b6 Binary files /dev/null and b/src/AndroidStudio_projects/app/src/debug/res/mipmap-xxhdpi/ic_launcher_round.png differ diff --git a/src/AndroidStudio_projects/app/src/debug/res/mipmap-xxxhdpi/ic_launcher.png b/src/AndroidStudio_projects/app/src/debug/res/mipmap-xxxhdpi/ic_launcher.png new file mode 100644 index 0000000..49388fd Binary files /dev/null and b/src/AndroidStudio_projects/app/src/debug/res/mipmap-xxxhdpi/ic_launcher.png differ diff --git a/src/AndroidStudio_projects/app/src/debug/res/mipmap-xxxhdpi/ic_launcher_foreground.png b/src/AndroidStudio_projects/app/src/debug/res/mipmap-xxxhdpi/ic_launcher_foreground.png new file mode 100644 index 0000000..b9f9313 Binary files /dev/null and b/src/AndroidStudio_projects/app/src/debug/res/mipmap-xxxhdpi/ic_launcher_foreground.png differ diff --git a/src/AndroidStudio_projects/app/src/debug/res/mipmap-xxxhdpi/ic_launcher_round.png b/src/AndroidStudio_projects/app/src/debug/res/mipmap-xxxhdpi/ic_launcher_round.png new file mode 100644 index 0000000..346e0c7 Binary files /dev/null and b/src/AndroidStudio_projects/app/src/debug/res/mipmap-xxxhdpi/ic_launcher_round.png differ diff --git a/src/AndroidStudio_projects/app/src/debug/res/values/ic_launcher_background.xml b/src/AndroidStudio_projects/app/src/debug/res/values/ic_launcher_background.xml new file mode 100644 index 0000000..1e36900 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/debug/res/values/ic_launcher_background.xml @@ -0,0 +1,4 @@ + + + #0E3B53 + \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/AndroidManifest.xml b/src/AndroidStudio_projects/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..f9b17be --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/AndroidManifest.xml @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/java/com/dadac/testrosbridge/RCApplication.java b/src/AndroidStudio_projects/app/src/main/java/com/dadac/testrosbridge/RCApplication.java new file mode 100644 index 0000000..2ecc33c --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/dadac/testrosbridge/RCApplication.java @@ -0,0 +1,42 @@ +package com.dadac.testrosbridge; + +import android.app.Application; + +import com.jilk.ros.rosbridge.ROSBridgeClient; + +/** + * @ Create by dadac on 2018/10/8. + * @Function: + * @Return: + */ +public class RCApplication extends Application { + + + ROSBridgeClient client; + + @Override + public void onCreate() { + super.onCreate(); + } + + @Override + public void onTerminate() { + if (client != null) + client.disconnect(); + super.onTerminate(); + + } + + public ROSBridgeClient getRosClient() { + return client; + } + + public void setRosClient(ROSBridgeClient client) { + this.client = client; + } + + +} + + + diff --git a/src/AndroidStudio_projects/app/src/main/java/com/dadac/testrosbridge/RosBridgeActivity.java b/src/AndroidStudio_projects/app/src/main/java/com/dadac/testrosbridge/RosBridgeActivity.java new file mode 100644 index 0000000..1249cbe --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/dadac/testrosbridge/RosBridgeActivity.java @@ -0,0 +1,215 @@ +package com.dadac.testrosbridge; + +import android.annotation.SuppressLint; +import android.app.Activity; +import android.os.Bundle; +import android.util.Log; +import android.view.MotionEvent; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.TextView; +import android.widget.Toast; + + +import com.example.hamburger.R; +import com.jilk.ros.ROSClient; +import com.jilk.ros.rosbridge.ROSBridgeClient; +import com.jilk.ros.rosbridge.implementation.PublishEvent; + +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + + +import de.greenrobot.event.EventBus; + + +/** + * @ Create by dadac on 2018/10/8. + * @Function: 开始啪啦啪啦的进行数据的传输,需要放在service里面进行传输,比较保险,可以一直在传输数据 + * @Return: + */ +public class RosBridgeActivity extends Activity implements View.OnClickListener { + + ROSBridgeClient client; + String ip = "10.8.87.23"; //虚拟机的 IP + // String ip = "192.168.10.20"; //半残废机器人的IP + // String ip = "192.168.10.200"; //机器人的IP + String port = "9090"; + + boolean isSubscrible = true; + private static int flagSubscrible = 0; + + private Button DC_Button_Subscrible; + private Button DC_Button_Publish; + private EditText DC_EditText_EnterWord; + private TextView DC_TextView_ShowData; + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.fragment_dashboard); + EventBus.getDefault().register(this); + onConnect(ip, port); + subMenuShow(); + } + + //初始化界面的参数 + private void subMenuShow() { + DC_Button_Subscrible = (Button) findViewById(R.id.DC_Button_Subscrible); + DC_Button_Subscrible.setOnClickListener(this); + DC_Button_Publish = (Button) findViewById(R.id.DC_Button_Publish); + DC_Button_Publish.setOnTouchListener(new ComponentOnTouch()); + DC_EditText_EnterWord = (EditText) findViewById(R.id.DC_EditText_EnterWord); + DC_TextView_ShowData = (TextView) findViewById(R.id.DC_TextView_ShowData); + } + + + /** + * @Function: 建立连接 + * @Return: + */ + public void onConnect(String ip, String port) { + + client = new ROSBridgeClient("ws://" + ip + ":" + port); + boolean conneSucc = client.connect(new ROSClient.ConnectionStatusListener() { + @Override + public void onConnect() { + client.setDebug(true); + ((RCApplication) getApplication()).setRosClient(client); + showTip("Connect ROS success"); + Log.d("dachen", "Connect ROS success"); + } + + + @Override + public void onDisconnect(boolean normal, String reason, int code) { + showTip("ROS disconnect"); + Log.d("dachen", "ROS disconnect"); + } + + @Override + public void onError(Exception ex) { + ex.printStackTrace(); + showTip("ROS communication error"); + Log.d("dachen", "ROS communication error"); + } + }); + } + + //接收来自Ros端的数据 + private void ReceiveDataToRos() { + if (isSubscrible == true) { + String msg1 = "{\"op\":\"subscribe\",\"topic\":\"/chatter\"}"; + client.send(msg1); + } else if (isSubscrible == false) { + String msg2 = "{\"op\":\"unsubscribe\",\"topic\":\"/chatter\"}"; + client.send(msg2); + } + } + + //发送数据到ROS端 + private void SendDataToRos(String data) { + String msg1 = "{ \"op\": \"publish\", \"topic\": \"/chatter\", \"msg\": { \"data\": \"" + data + " \" }}"; + // String msg2 = "{\"op\":\"publish\",\"topic\":\"/cmd_vel\",\"msg\":{\"linear\":{\"x\":" + 0 + ",\"y\":" + + // 0 + ",\"z\":0},\"angular\":{\"x\":0,\"y\":0,\"z\":" + 0.5 + "}}}"; + client.send(msg1); + } + + + private void showTip(final String tip) { + runOnUiThread(new Runnable() { + @Override + public void run() { + Toast.makeText(RosBridgeActivity.this, tip, Toast.LENGTH_SHORT).show(); + } + }); + } + + public void onEvent(final PublishEvent event) { + if ("/chatter".equals(event.name)) { + parseChatterTopic(event); + return; + } + Log.d("dachen", event.msg); + } + + private void parseChatterTopic(PublishEvent event) { + try { + JSONParser parser = new JSONParser(); + org.json.simple.JSONObject jsonObject = (org.json.simple.JSONObject) parser.parse(event.msg); + String jsondata = (String) jsonObject.get("data"); + DC_TextView_ShowData.setText(jsondata); + Log.i("dachen", jsondata); + } catch (ParseException e) { + e.printStackTrace(); + } + } + + @SuppressLint("NonConstantResourceId") + @Override + public void onClick(View v) { + switch (v.getId()) { + case R.id.DC_Button_Subscrible: + if (flagSubscrible % 2 == 0) { + isSubscrible = true; + DC_Button_Subscrible.setText("Subscrible"); + } + if (flagSubscrible % 2 == 1) { + isSubscrible = false; + DC_Button_Subscrible.setText("unSubscrible"); + } + flagSubscrible++; + ReceiveDataToRos(); + break; + default: + break; + } + } + + private class ComponentOnTouch implements View.OnTouchListener { + @SuppressLint("NonConstantResourceId") + @Override + public boolean onTouch(View v, MotionEvent event) { + switch (v.getId()) { + case R.id.DC_Button_Publish: + onTouchChange("up", event.getAction()); + break; + default: + break; + } + return true; + } + } + private boolean Btn_LongPress = false; + class MyThread extends Thread { + @Override + public void run() { + while (Btn_LongPress) { + try { + Thread.sleep(10); + } catch (InterruptedException e) { + e.printStackTrace(); + } + SendDataToRos("3"); + } + } + } + private void onTouchChange(String methodName, int eventAction) { + MyThread myThread = new MyThread(); + // 按下松开分别对应启动停止线程方法 + if ("up".equals(methodName)) { + if (eventAction == MotionEvent.ACTION_DOWN) { + myThread.start(); + Btn_LongPress = true; + } else if (eventAction == MotionEvent.ACTION_UP) { + SendDataToRos("stop"); + if (myThread != null) + Btn_LongPress = false; + } + } + } + +} + diff --git a/src/AndroidStudio_projects/app/src/main/java/com/example/dadac/testrosbridge/JNICallAll.java b/src/AndroidStudio_projects/app/src/main/java/com/example/dadac/testrosbridge/JNICallAll.java new file mode 100644 index 0000000..1d04c40 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/example/dadac/testrosbridge/JNICallAll.java @@ -0,0 +1,23 @@ +package com.example.dadac.testrosbridge; + +/** + * @ Create by dadac on 2018/10/8. + * @Function: 存放所有的 JNI 调用的文件 + * @Return: + */ +public class JNICallAll { + + // Used to load the 'native-lib' library on application startup. + static { + System.loadLibrary("native-test"); + } + + /** + * A native method that is implemented by the 'native-lib' native library, + * which is packaged with this application. + */ + public native String stringFromJNI(); + + + +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/example/dadac/testrosbridge/TransferSta.java b/src/AndroidStudio_projects/app/src/main/java/com/example/dadac/testrosbridge/TransferSta.java new file mode 100644 index 0000000..a1dd58a --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/example/dadac/testrosbridge/TransferSta.java @@ -0,0 +1,29 @@ +package com.example.dadac.testrosbridge; + +import android.content.Intent; +//import android.support.v7.app.AppCompatActivity; +import androidx.appcompat.app.AppCompatActivity; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; + +import com.dadac.testrosbridge.RosBridgeActivity; +import com.example.hamburger.R; + +public class TransferSta extends AppCompatActivity { + + private Button DC_Button_JumpToRos; + + /* @Override + /*protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.fragment_dashboard); + DC_Button_JumpToRos = (Button) findViewById(R.id.DC_Button_JumpToRos); + } + + + public void JumpToActivity(View view) { + Intent myIntentRos = new Intent(TransferSta.this, RosBridgeActivity.class); + startActivity(myIntentRos); + }*/ +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/MainActivity.java b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/MainActivity.java new file mode 100644 index 0000000..a03505f --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/MainActivity.java @@ -0,0 +1,115 @@ +package com.example.hamburger; + +import androidx.appcompat.app.AppCompatActivity; + +import android.content.Intent; +import android.os.Bundle; +import android.widget.EditText; +import android.view.View; +import android.widget.Toast; +import android.widget.Button; +import android.os.Handler; +import android.os.Message; +import android.annotation.SuppressLint; +import com.example.hamburger.dao.UserDao; + +public class MainActivity extends AppCompatActivity { + + //这个类主要用来进行,账号密码的验证,以及监听事件的编写。 + // 所以首先需要将一开始设计的输入文本框id name 和passwd获取。 + EditText name;//创建账号 + EditText passwd;//创建密码 + + private static final String TAG="mysql-hamburger-MainActivity"; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + name=findViewById(R.id.name);//获取输入的账号 + passwd=findViewById(R.id.passwd);//获取输入的密码 + + + //这是能够登录的账号密码 + String Usename = "admin"; + String Upwd = "12345"; + + //创建两个String类,储存从输入文本框获取到的内容 + String user = name.getText().toString().trim(); + String pwd = passwd.getText().toString().trim(); + + //获取按钮 + Button button = (Button) findViewById(R.id.button); + Button button_1 = (Button) findViewById(R.id.register); + + button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick (View v){ + //监听按钮,如果点击,就跳转 + Intent intent = new Intent(); + //前一个(MainActivity.this)是目前页面,后面一个是要跳转的下一个页面 + intent.setClass(MainActivity.this, MainActivity2.class); + startActivity(intent); + //Toast.makeText(MainActivity.this, "欢迎你", Toast.LENGTH_SHORT).show(); + } + }); + + /*//进行判断,如果两个内容都相等,就显现第一条语句,反之,第二条。 + if(user.equals(Usename) & pwd.equals(Upwd)){ + //按钮进行监听 + + }*/ + //else{ + // Toast.makeText(MainActivity.this, "身份验证错误,禁止访问", Toast.LENGTH_SHORT).show(); + //} + //按钮进行监听 + button_1.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + //监听按钮,如果点击,就跳转 + Intent intent = new Intent(); + //前一个(MainActivity.this)是目前页面,后面一个是要跳转的下一个页面 + intent.setClass(MainActivity.this, register.class); + startActivity(intent); + } + }); + } + + public void login(View view){ + + EditText EditTextAccount = findViewById(R.id.name); + EditText EditTextPassword = findViewById(R.id.passwd); + + new Thread(){ + @Override + public void run() { + UserDao userDao = new UserDao(); + int msg = userDao.login(EditTextAccount.getText().toString(),EditTextPassword.getText().toString()); + hand1.sendEmptyMessage(msg); + } + }.start(); + + } + + public void reg(View view){ + startActivity(new Intent(getApplicationContext(),register.class)); + } + + @SuppressLint("HandlerLeak") + final Handler hand1 = new Handler() { + @Override + public void handleMessage(Message msg) { + if (msg.what == 0){ + Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_LONG).show(); + } else if (msg.what == 1) { + Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_LONG).show(); + } else if (msg.what == 2){ + Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_LONG).show(); + } else if (msg.what == 3){ + Toast.makeText(getApplicationContext(), "账号不存在", Toast.LENGTH_LONG).show(); + } + } + }; + +} \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/MainActivity2.java b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/MainActivity2.java new file mode 100644 index 0000000..522eda9 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/MainActivity2.java @@ -0,0 +1,37 @@ +package com.example.hamburger; + +import android.os.Bundle; + +import com.google.android.material.bottomnavigation.BottomNavigationView; + +import androidx.appcompat.app.AppCompatActivity; +import androidx.navigation.NavController; +import androidx.navigation.Navigation; +import androidx.navigation.ui.AppBarConfiguration; +import androidx.navigation.ui.NavigationUI; + +import com.example.hamburger.databinding.ActivityMain2Binding; + +public class MainActivity2 extends AppCompatActivity { + + private ActivityMain2Binding binding; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + binding = ActivityMain2Binding.inflate(getLayoutInflater()); + setContentView(binding.getRoot()); + + BottomNavigationView navView = findViewById(R.id.nav_view); + // Passing each menu ID as a set of Ids because each + // menu should be considered as top level destinations. + AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder( + R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications) + .build(); + NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main2); + NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration); + NavigationUI.setupWithNavController(binding.navView, navController); + } + +} \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/add.java b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/add.java new file mode 100644 index 0000000..1866186 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/add.java @@ -0,0 +1,38 @@ +package com.example.hamburger; +import androidx.appcompat.app.AppCompatActivity; + +import android.content.Intent; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; + +import com.example.hamburger.R; +import com.example.hamburger.notifications; +import com.example.hamburger.ui.notifications.NotificationsFragment; + +public class add extends AppCompatActivity { + + EditText mytext; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_add); + mytext = findViewById(R.id.mt); + + } + + public void Confirm() { + Button button = (Button) findViewById(R.id.conf); + + button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(); + intent.setClass(add.this, NotificationsFragment.class); + startActivity(intent); + } + }); + } +} \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/dao/UserDao.java b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/dao/UserDao.java new file mode 100644 index 0000000..2ab20f0 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/dao/UserDao.java @@ -0,0 +1,165 @@ +package com.example.hamburger.dao; + + +import com.example.hamburger.entity.User; +import com.example.hamburger.utils.JDBCUtils; +import android.util.Log; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.util.HashMap; + +/** + * author: yan + * date: 2022.02.17 + * **/ +public class UserDao { + + private static final String TAG = "mysql-party-UserDao"; + + /** + * function: 登录 + * */ + public int login(String userAccount, String userPassword){ + + HashMap map = new HashMap<>(); + // 根据数据库名称,建立连接 + Connection connection = JDBCUtils.getConn(); + int msg = 0; + try { + // mysql简单的查询语句。这里是根据user表的userAccount字段来查询某条记录 + String sql = "select * from user where userAccount = ?"; + if (connection != null){// connection不为null表示与数据库建立了连接 + PreparedStatement ps = connection.prepareStatement(sql); + if (ps != null){ + Log.e(TAG,"账号:" + userAccount); + //根据账号进行查询 + ps.setString(1, userAccount); + // 执行sql查询语句并返回结果集 + ResultSet rs = ps.executeQuery(); + int count = rs.getMetaData().getColumnCount(); + //将查到的内容储存在map里 + while (rs.next()){ + // 注意:下标是从1开始的 + for (int i = 1;i <= count;i++){ + String field = rs.getMetaData().getColumnName(i); + map.put(field, rs.getString(field)); + } + } + connection.close(); + ps.close(); + + if (map.size()!=0){ + StringBuilder s = new StringBuilder(); + //寻找密码是否匹配 + for (String key : map.keySet()){ + if(key.equals("userPassword")){ + if(userPassword.equals(map.get(key))){ + msg = 1; //密码正确 + } + else + msg = 2; //密码错误 + break; + } + } + }else { + Log.e(TAG, "查询结果为空"); + msg = 3; + } + }else { + msg = 0; + } + }else { + msg = 0; + } + }catch (Exception e){ + e.printStackTrace(); + Log.d(TAG, "异常login:" + e.getMessage()); + msg = 0; + } + return msg; + } + + + /** + * function: 注册 + * */ + public boolean register(User user){ + HashMap map = new HashMap<>(); + // 根据数据库名称,建立连接 + Connection connection = JDBCUtils.getConn(); + + try { + String sql = "insert into user(userAccount,userPassword,userName,userType,userState,userDel) values (?,?,?,?,?,?)"; + if (connection != null){// connection不为null表示与数据库建立了连接 + PreparedStatement ps = connection.prepareStatement(sql); + if (ps != null){ + + //将数据插入数据库 + ps.setString(1,user.getUserAccount()); + ps.setString(2,user.getUserPassword()); + ps.setString(3,user.getUserName()); + ps.setInt(4,user.getUserType()); + ps.setInt(5, user.getUserState()); + ps.setInt(6,user.getUserDel()); + + // 执行sql查询语句并返回结果集 + int rs = ps.executeUpdate(); + if(rs>0) + return true; + else + return false; + }else { + return false; + } + }else { + return false; + } + }catch (Exception e){ + e.printStackTrace(); + Log.e(TAG, "异常register:" + e.getMessage()); + return false; + } + + } + + /** + * function: 根据账号进行查找该用户是否存在 + * */ + public User findUser(String userAccount) { + + // 根据数据库名称,建立连接 + Connection connection = JDBCUtils.getConn(); + User user = null; + try { + String sql = "select * from user where userAccount = ?"; + if (connection != null){// connection不为null表示与数据库建立了连接 + PreparedStatement ps = connection.prepareStatement(sql); + if (ps != null) { + ps.setString(1, userAccount); + ResultSet rs = ps.executeQuery(); + + while (rs.next()) { + //注意:下标是从1开始 + int id = rs.getInt(1); + String userAccount1 = rs.getString(2); + String userPassword = rs.getString(3); + String userName = rs.getString(4); + int userType = rs.getInt(5); + int userState = rs.getInt(6); + int userDel = rs.getInt(7); + user = new User(id, userAccount1, userPassword, userName, userType, userState, userDel); + } + } + } + }catch (Exception e){ + e.printStackTrace(); + Log.d(TAG, "异常findUser:" + e.getMessage()); + return null; + } + return user; + } + +} + diff --git a/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/entity/User.java b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/entity/User.java new file mode 100644 index 0000000..9534e82 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/entity/User.java @@ -0,0 +1,83 @@ +package com.example.hamburger.entity; + +public class User { + + private int id; + private String userAccount; + private String userPassword; + private String userName; + private int userType; + private int userState; + private int userDel; + + + public User() { + } + + public User(int id, String userAccount, String userPassword, String userName, int userType, int userState, int userDel) { + this.id = id; + this.userAccount = userAccount; + this.userPassword = userPassword; + this.userName = userName; + this.userType = userType; + this.userState = userState; + this.userDel = userDel; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getUserAccount() { + return userAccount; + } + + public void setUserAccount(String userAccount) { + this.userAccount = userAccount; + } + + public String getUserPassword() { + return userPassword; + } + + public void setUserPassword(String userPassword) { + this.userPassword = userPassword; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public int getUserType() { + return userType; + } + + public void setUserType(int userType) { + this.userType = userType; + } + + public int getUserState() { + return userState; + } + + public void setUserState(int userState) { + this.userState = userState; + } + + public int getUserDel() { + return userDel; + } + + public void setUserDel(int userDel) { + this.userDel = userDel; + } +} + diff --git a/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/main_window.java b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/main_window.java new file mode 100644 index 0000000..783274d --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/main_window.java @@ -0,0 +1,15 @@ +package com.example.hamburger; + +import android.os.Bundle; + +import androidx.appcompat.app.AppCompatActivity; + +public class main_window extends AppCompatActivity { + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + //这个是获取布局文件的,这里是你下一个页面的布局文件//注意这个是跳转界面的不能设置错,应该是第一个 + setContentView(R.layout.main_window); + } + +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/notifications.java b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/notifications.java new file mode 100644 index 0000000..7bdd418 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/notifications.java @@ -0,0 +1,29 @@ +package com.example.hamburger; +import androidx.appcompat.app.AppCompatActivity; + +import android.content.Intent; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +public class notifications extends AppCompatActivity { + + private TextView tv; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.fragment_notifications); + + Button button = (Button) findViewById(R.id.btn1); + button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(); + intent.setClass(notifications.this, add.class); + startActivity(intent); + } + }); + } + +} \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/register.java b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/register.java new file mode 100644 index 0000000..f3997a2 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/register.java @@ -0,0 +1,65 @@ +package com.example.hamburger; + +import android.annotation.SuppressLint; +import android.content.Intent; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; + +import androidx.appcompat.app.AppCompatActivity; + +public class register extends AppCompatActivity { + + EditText name;//创建账号 + EditText passwd;//创建密码 + EditText ackpasswd; + + @SuppressLint("MissingInflatedId") + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + //这个是获取布局文件的,这里是你下一个页面的布局文件//注意这个是跳转界面的不能设置错,应该是第一个 + setContentView(R.layout.activity_register); + + name=findViewById(R.id.user_name);//获取输入的账号 + passwd=findViewById(R.id.new_passwd);//获取输入的密码 + ackpasswd=findViewById(R.id.ack_passwd); + } + + public void Register_(){ + //获取按钮 + Button button = (Button) findViewById(R.id.register_); + + //按钮进行监听 + button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + //监听按钮,如果点击,就跳转 + Intent intent = new Intent(); + //前一个(MainActivity.this)是目前页面,后面一个是要跳转的下一个页面 + intent.setClass(register.this, MainActivity.class); + startActivity(intent); + } + }); + } + + public void Ret(){ + //获取按钮 + Button button = (Button) findViewById(R.id.ret); + + //按钮进行监听 + button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + //监听按钮,如果点击,就跳转 + Intent intent = new Intent(); + //前一个(MainActivity.this)是目前页面,后面一个是要跳转的下一个页面 + intent.setClass(register.this, MainActivity.class); + startActivity(intent); + } + }); + } + +} \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/ui/dashboard/DashboardFragment.java b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/ui/dashboard/DashboardFragment.java new file mode 100644 index 0000000..914176e --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/ui/dashboard/DashboardFragment.java @@ -0,0 +1,37 @@ +package com.example.hamburger.ui.dashboard; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; +import androidx.lifecycle.ViewModelProvider; + +import com.example.hamburger.databinding.FragmentDashboardBinding; + +public class DashboardFragment extends Fragment { + + private FragmentDashboardBinding binding; + + public View onCreateView(@NonNull LayoutInflater inflater, + ViewGroup container, Bundle savedInstanceState) { + DashboardViewModel dashboardViewModel = + new ViewModelProvider(this).get(DashboardViewModel.class); + + binding = FragmentDashboardBinding.inflate(inflater, container, false); + View root = binding.getRoot(); + + final TextView textView = binding.v1; + dashboardViewModel.getText().observe(getViewLifecycleOwner(), textView::setText); + return root; + } + + @Override + public void onDestroyView() { + super.onDestroyView(); + binding = null; + } +} \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/ui/dashboard/DashboardViewModel.java b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/ui/dashboard/DashboardViewModel.java new file mode 100644 index 0000000..4ecff72 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/ui/dashboard/DashboardViewModel.java @@ -0,0 +1,19 @@ +package com.example.hamburger.ui.dashboard; + +import androidx.lifecycle.LiveData; +import androidx.lifecycle.MutableLiveData; +import androidx.lifecycle.ViewModel; + +public class DashboardViewModel extends ViewModel { + + private final MutableLiveData mText; + + public DashboardViewModel() { + mText = new MutableLiveData<>(); + mText.setValue(""); + } + + public LiveData getText() { + return mText; + } +} \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/ui/home/HomeFragment.java b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/ui/home/HomeFragment.java new file mode 100644 index 0000000..26a09fb --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/ui/home/HomeFragment.java @@ -0,0 +1,37 @@ +package com.example.hamburger.ui.home; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; +import androidx.lifecycle.ViewModelProvider; + +import com.example.hamburger.databinding.FragmentHomeBinding; + +public class HomeFragment extends Fragment { + + private FragmentHomeBinding binding; + + public View onCreateView(@NonNull LayoutInflater inflater, + ViewGroup container, Bundle savedInstanceState) { + HomeViewModel homeViewModel = + new ViewModelProvider(this).get(HomeViewModel.class); + + binding = FragmentHomeBinding.inflate(inflater, container, false); + View root = binding.getRoot(); + + final TextView textView = binding.TextView1; + homeViewModel.getText().observe(getViewLifecycleOwner(), textView::setText); + return root; + } + + @Override + public void onDestroyView() { + super.onDestroyView(); + binding = null; + } +} \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/ui/home/HomeViewModel.java b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/ui/home/HomeViewModel.java new file mode 100644 index 0000000..b5dc229 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/ui/home/HomeViewModel.java @@ -0,0 +1,19 @@ +package com.example.hamburger.ui.home; + +import androidx.lifecycle.LiveData; +import androidx.lifecycle.MutableLiveData; +import androidx.lifecycle.ViewModel; + +public class HomeViewModel extends ViewModel { + + private final MutableLiveData mText; + + public HomeViewModel() { + mText = new MutableLiveData<>(); + mText.setValue("This is home fragment"); + } + + public LiveData getText() { + return mText; + } +} \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/ui/notifications/NotificationsFragment.java b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/ui/notifications/NotificationsFragment.java new file mode 100644 index 0000000..1420a42 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/ui/notifications/NotificationsFragment.java @@ -0,0 +1,57 @@ +package com.example.hamburger.ui.notifications; + +import android.content.Intent; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; +import androidx.lifecycle.ViewModelProvider; + +import com.example.hamburger.R; +import com.example.hamburger.databinding.FragmentNotificationsBinding; + +public class NotificationsFragment extends Fragment{ + + private FragmentNotificationsBinding binding; + + private NotificationsViewModel notificationsViewModel; + + private TextView tv; + + public View onCreateView(@NonNull LayoutInflater inflater, + ViewGroup container, Bundle savedInstanceState) { + NotificationsViewModel notificationsViewModel = + new ViewModelProvider(this).get(NotificationsViewModel.class); + + binding = FragmentNotificationsBinding.inflate(inflater, container, false); + View root = binding.getRoot(); + + final TextView textView = binding.textNotifications; + notificationsViewModel.getText().observe(getViewLifecycleOwner(), textView::setText); + + final Button button=root.findViewById(R.id.btn1); + button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(); + intent.setClass(getActivity(), add.class); + startActivity(intent); + } + }); + + return root; + + } + + + @Override + public void onDestroyView() { + super.onDestroyView(); + binding = null; + } +} \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/ui/notifications/NotificationsViewModel.java b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/ui/notifications/NotificationsViewModel.java new file mode 100644 index 0000000..3948dfe --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/ui/notifications/NotificationsViewModel.java @@ -0,0 +1,19 @@ +package com.example.hamburger.ui.notifications; + +import androidx.lifecycle.LiveData; +import androidx.lifecycle.MutableLiveData; +import androidx.lifecycle.ViewModel; + +public class NotificationsViewModel extends ViewModel { + + private final MutableLiveData mText; + + public NotificationsViewModel() { + mText = new MutableLiveData<>(); + mText.setValue("This is notifications fragment"); + } + + public LiveData getText() { + return mText; + } +} \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/ui/notifications/add.java b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/ui/notifications/add.java new file mode 100644 index 0000000..c0e95e4 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/ui/notifications/add.java @@ -0,0 +1,37 @@ +package com.example.hamburger.ui.notifications; +import androidx.appcompat.app.AppCompatActivity; + +import android.content.Intent; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; + +import com.example.hamburger.R; +import com.example.hamburger.notifications; + +public class add extends AppCompatActivity { + + EditText mytext; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_add); + mytext = findViewById(R.id.mt); + + } + + public void Confirm() { + Button button = (Button) findViewById(R.id.conf); + + button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(); + intent.setClass(add.this, NotificationsFragment.class); + startActivity(intent); + } + }); + } +} \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/utils/JDBCUtils.java b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/utils/JDBCUtils.java new file mode 100644 index 0000000..350035c --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/example/hamburger/utils/JDBCUtils.java @@ -0,0 +1,37 @@ +package com.example.hamburger.utils; + +import java.sql.Connection; +import java.sql.DriverManager; + +/** + * function: 数据库工具类,连接数据库用 + */ +public class JDBCUtils { + private static final String TAG = "mysql-party-JDBCUtils"; + + private static String driver = "com.mysql.jdbc.Driver";// MySql驱动 + + private static String dbName = "Hotel_system";// 数据库名称 + + private static String user = "root";// 用户名 + + private static String password = "123456";// 密码 + + public static Connection getConn(){ + + Connection connection = null; + try{ + Class.forName(driver);// 动态加载类 + String ip = "10.0.2.2";// 写成本机地址,不能写成localhost,同时手机和电脑连接的网络必须是同一个 + + // 尝试建立到给定数据库URL的连接 + connection = DriverManager.getConnection("jdbc:mysql://" + ip + ":3306/" + dbName, + user, password); + + }catch (Exception e){ + e.printStackTrace(); + } + return connection; + } +} + diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/Example.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/Example.java new file mode 100644 index 0000000..6e0523c --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/Example.java @@ -0,0 +1,139 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros; + +import com.jilk.ros.message.Clock; +import com.jilk.ros.message.Log; +import com.jilk.ros.rosapi.message.Empty; +import com.jilk.ros.rosapi.message.GetTime; +import com.jilk.ros.rosapi.message.MessageDetails; +import com.jilk.ros.rosapi.message.Type; +import com.jilk.ros.rosbridge.ROSBridgeClient; + +public class Example { + + public Example() {} + + public static void main(String[] args) { + ROSBridgeClient client = new ROSBridgeClient("ws://10.8.87.23:9090"); + client.connect(); + //testTopic(client); + try { + testService(client); + } + catch (RuntimeException ex) { + ex.printStackTrace(); + } + finally { + client.disconnect(); + } + } + + public static void testService(ROSBridgeClient client) { + try { + Service timeService = + new Service("/rosapi/get_time", Empty.class, GetTime.class, client); + timeService.verify(); + //System.out.println("Time (secs): " + timeService.callBlocking(new Empty()).time.sec); + + Service serviceTypeService = + new Service("/rosapi/service_type", + com.jilk.ros.rosapi.message.Service.class, Type.class, client); + serviceTypeService.verify(); + String type = serviceTypeService.callBlocking(new com.jilk.ros.rosapi.message.Service("/rosapi/service_response_details")).type; + + Service serviceDetails = + new Service("/rosapi/service_response_details", + Type.class, MessageDetails.class, client); + serviceDetails.verify(); + //serviceDetails.callBlocking(new Type(type)).print(); + + com.jilk.ros.Topic logTopic = + new com.jilk.ros.Topic("/rosout", Log.class, client); + logTopic.verify(); + + /* + System.out.println("Nodes"); + for (String s : client.getNodes()) + System.out.println(" " + s); + System.out.println("Topics"); + for (String s : client.getTopics()) { + System.out.println(s + ":"); + client.getTopicMessageDetails(s).print(); + } + System.out.println("Services"); + for (String s : client.getServices()) { + System.out.println(s + ":"); + client.getServiceRequestDetails(s).print(); + System.out.println("-----------------"); + client.getServiceResponseDetails(s).print(); + } + */ + } + catch (InterruptedException ex) { + System.out.println("Process was interrupted."); + } + /* + Service topicService = + new Service("/rosapi/topics", Empty.class, Topics.class, client); + Service typeService = + new Service("/rosapi/topic_type", Topic.class, Type.class, client); + Service messageService = + new Service("/rosapi/message_details", Type.class, MessageDetails.class, client); + try { + Topics topics = topicService.callBlocking(new Empty()); + for (String topicString : topics.topics) { + Topic topic = new Topic(); + topic.topic = topicString; + Type type = typeService.callBlocking(topic); + MessageDetails details = messageService.callBlocking(type); + System.out.println("Topic: " + topic.topic + " Type: " + type.type); + details.print(); + System.out.println(); + } + Type type = new Type(); + type.type = "time"; + System.out.print("Single type check on \'time\': "); + messageService.callBlocking(type).print(); + } + catch (InterruptedException ex) { + System.out.println("testService: process was interrupted."); + } + */ + } + + public static void testTopic(ROSBridgeClient client) { + com.jilk.ros.Topic clockTopic = new com.jilk.ros.Topic("/clock", Clock.class, client); + clockTopic.subscribe(); + try { + Thread.sleep(20000);} catch(InterruptedException ex) {} + Clock cl = null; + try { + cl = clockTopic.take(); // just gets one + } + catch (InterruptedException ex) {} + cl.print(); + cl.clock.nsecs++; + clockTopic.unsubscribe(); + clockTopic.advertise(); + clockTopic.publish(cl); + clockTopic.unadvertise(); + } +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/MessageHandler.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/MessageHandler.java new file mode 100644 index 0000000..f7cdd4d --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/MessageHandler.java @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros; + +import com.jilk.ros.message.Message; + +public interface MessageHandler { + public void onMessage(T message); +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/ROSClient.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/ROSClient.java new file mode 100644 index 0000000..f3e8f60 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/ROSClient.java @@ -0,0 +1,68 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros; + +import com.jilk.ros.message.Message; +import com.jilk.ros.rosapi.message.TypeDef; +import com.jilk.ros.rosbridge.FullMessageHandler; +import com.jilk.ros.rosbridge.ROSBridgeClient; +import com.jilk.ros.rosbridge.operation.Operation; + +public abstract class ROSClient { + + public ROSClient() {} + + public static ROSClient create(String uriString) { + // if we ever implement other ROSClient types, we'll key off the URI protocol (e.g., ws://) + // we'd also have to abstract out Topic and Service since they depend on the ROSBridge operations + return new ROSBridgeClient(uriString); + } + + public abstract boolean connect(); + public abstract boolean connect(ConnectionStatusListener listener); + public abstract void disconnect(); + public abstract void send(Operation operation); + public abstract void send(String json); + public abstract void register(Class c, + String s, + Class m, + FullMessageHandler h); + public abstract void unregister(Class c, String s); + public abstract void setDebug(boolean debug); + public abstract String[] getNodes() throws InterruptedException; + public abstract String[] getTopics() throws InterruptedException; + public abstract String[] getServices() throws InterruptedException; + public abstract TypeDef getTopicMessageDetails(String topic) throws InterruptedException; + public abstract TypeDef[] getTopicMessageList(String topic) throws InterruptedException; + public abstract TypeDef getServiceRequestDetails(String service) throws InterruptedException; + public abstract TypeDef[] getServiceRequestList(String service) throws InterruptedException; + public abstract TypeDef getServiceResponseDetails(String service) throws InterruptedException; + public abstract TypeDef[] getServiceResponseList(String service) throws InterruptedException; + public abstract TypeDef getTypeDetails(String type) throws InterruptedException; + public abstract TypeDef[] getTypeList(String type) throws InterruptedException; + public abstract void typeMatch(TypeDef t, Class c) throws InterruptedException; + public abstract Object getUnderlyingClient(); // for debugging + + public interface ConnectionStatusListener { + public void onConnect(); + public void onDisconnect(boolean normal, String reason, int code); + public void onError(Exception ex); + } +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/Service.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/Service.java new file mode 100644 index 0000000..8de5db4 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/Service.java @@ -0,0 +1,134 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros; + +import com.jilk.ros.message.Message; +import com.jilk.ros.rosbridge.FullMessageHandler; +import com.jilk.ros.rosbridge.operation.CallService; +import com.jilk.ros.rosbridge.operation.ServiceResponse; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.CountDownLatch; + +public class Service extends Message implements FullMessageHandler { + private String service; + private Class responseType; + private Class callType; + private ROSClient client; + private Map calls; + + public Service(String service, Class callType, + Class responseType, ROSClient client) { + this.service = service; + this.client = client; + this.responseType = responseType; + this.callType = callType; + calls = new HashMap(); + } + + // A result can only be returned once; it is cleared from the hash before + // being sent/returned. This is to ensure that results do not accumulate + // indefinitely. If callers need to keep these around they can set up their + // own hash. + + @Override + public void onMessage(String id, Message response) { + //System.out.print("Service.onMessage: "); + //response.print(); + CallRecord call = calls.get(id); + if(call == null) { + System.out.print("No caller service response"); + return; + } + if (call.handler != null) { + calls.remove(id); + call.handler.onMessage((ResponseType) response); + } + else { + call.result = (ResponseType) response; + call.latch.countDown(); + } + } + + public String call(CallType args) { + return callImpl(args, null); + } + + public void callWithHandler(CallType args, MessageHandler responseHandler) { + callImpl(args, responseHandler); + } + + public ResponseType callBlocking(CallType args) throws InterruptedException { + return take(call(args)); + } + + private String callImpl(CallType args, MessageHandler responseHandler) { + client.register(ServiceResponse.class, service, responseType, this); // do this once on creation? + CallService messageCallService = new CallService(service, args); + String id = messageCallService.id; + CallRecord callRecord = new CallRecord(responseHandler); + calls.put(id, callRecord); + client.send(messageCallService); + return id; + } + + public ResponseType poll(String id) { + CallRecord call = calls.get(id); + if (call.result != null) + calls.remove(id); + return call.result; + } + + public ResponseType take(String id) throws InterruptedException { + CallRecord call = calls.get(id); + call.latch.await(); + calls.remove(id); + return call.result; + } + + public void verify() throws InterruptedException { + + boolean hasService = false; + for (String s : client.getServices()) { + if (s.equals(service)) { + hasService = true; + break; + } + } + if (!hasService) + throw new RuntimeException("Service \'" + service + "\' not available."); + + client.typeMatch(client.getServiceRequestDetails(service), callType); + client.typeMatch(client.getServiceResponseDetails(service), responseType); + } + + private class CallRecord { + public ResponseType result; + public CountDownLatch latch; + public MessageHandler handler; + + public CallRecord(MessageHandler handler) { + this.result = null; + this.latch = new CountDownLatch(1); + this.handler = handler; + } + } +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/Topic.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/Topic.java new file mode 100644 index 0000000..815ea2e --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/Topic.java @@ -0,0 +1,144 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros; + +import com.jilk.ros.message.Message; +import com.jilk.ros.rosbridge.FullMessageHandler; +import com.jilk.ros.rosbridge.operation.Advertise; +import com.jilk.ros.rosbridge.operation.Operation; +import com.jilk.ros.rosbridge.operation.Publish; +import com.jilk.ros.rosbridge.operation.Subscribe; +import com.jilk.ros.rosbridge.operation.Unadvertise; +import com.jilk.ros.rosbridge.operation.Unsubscribe; + +import java.util.concurrent.LinkedBlockingQueue; + + +public class Topic extends LinkedBlockingQueue implements FullMessageHandler { + protected String topic; + private Class type; + private String messageType; + private ROSClient client; + private Thread handlerThread; + + public Topic(String topic, Class type, ROSClient client) { + this.topic = topic; + this.client = client; + this.type = type; + messageType = Message.getMessageType(type); + handlerThread = null; + } + + @Override + public void onMessage(String id, Message message) { + add((T) message); + } + + + // warning: there is a delay between the completion of this method and + // the completion of the subscription; it takes longer than + // publishing multiple other messages, for example. + public void subscribe(MessageHandler handler) { + startRunner(handler); + subscribe(); + } + + public void subscribe() { + client.register(Publish.class, topic, type, this); + send(new Subscribe(topic, messageType)); + } + + public void unsubscribe() { + // need to handle race conditions in incoming message handler + // so that once unsubscribe has happened the handler gets no more + // messages + send(new Unsubscribe(topic)); + client.unregister(Publish.class, topic); + stopRunner(); + } + + private void startRunner(MessageHandler handler) { + stopRunner(); + handlerThread = new Thread(new MessageRunner(handler)); + handlerThread.setName("Message handler for " + topic); + handlerThread.start(); + } + + private void stopRunner() { + if (handlerThread != null) { + handlerThread.interrupt(); + clear(); + handlerThread = null; + } + } + + + public void advertise() { + send(new Advertise(topic, messageType)); + } + + public void publish(T message) { + send(new Publish(topic, message)); + } + + public void unadvertise() { + send(new Unadvertise(topic)); + } + + private void send(Operation operation) { + client.send(operation); + } + + public void verify() throws InterruptedException { + + boolean hasTopic = false; + for (String s : client.getTopics()) { + if (s.equals(topic)) { + hasTopic = true; + break; + } + } + if (!hasTopic) + throw new RuntimeException("Topic \'" + topic + "\' not available."); + + client.typeMatch(client.getTopicMessageDetails(topic), type); + } + + private class MessageRunner implements Runnable { + private MessageHandler handler; + + public MessageRunner(MessageHandler handler) { + this.handler = handler; + } + + @Override + public void run() { + while (!Thread.interrupted()) { + try { + handler.onMessage(take()); + } + catch (InterruptedException ex) { + break; + } + } + } + } + +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/AudioMsg.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/AudioMsg.java new file mode 100644 index 0000000..29c1e41 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/AudioMsg.java @@ -0,0 +1,9 @@ +package com.jilk.ros.message; + +/** + * Created by xxhong on 16-11-17. + */ +@MessageType(string = "std_msgs/Int16MultiArray") +public class AudioMsg extends Message { + public short[] data; +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/Clock.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/Clock.java new file mode 100644 index 0000000..09505fc --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/Clock.java @@ -0,0 +1,25 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.message; + +@MessageType(string = "rosgraph_msgs/Clock") +public class Clock extends Message { + public TimePrimitive clock; +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/Duration.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/Duration.java new file mode 100644 index 0000000..c08953b --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/Duration.java @@ -0,0 +1,24 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.message; + +@MessageType(string = "std_msgs/Duration") +public class Duration extends DurationPrimitive { +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/DurationPrimitive.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/DurationPrimitive.java new file mode 100644 index 0000000..35ae9bb --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/DurationPrimitive.java @@ -0,0 +1,25 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.message; + +@MessageType(string = "duration") +public class DurationPrimitive extends TimePrimitive { + +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/Empty.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/Empty.java new file mode 100644 index 0000000..55d5a39 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/Empty.java @@ -0,0 +1,24 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.message; + +@MessageType(string = "std_srvs/Empty") +public class Empty extends Message { +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/Header.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/Header.java new file mode 100644 index 0000000..eb3d856 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/Header.java @@ -0,0 +1,27 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.message; + +@MessageType(string = "std_msgs/Header") +public class Header extends Message { + public long seq; + public TimePrimitive stamp; + public String frame_id; +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/Log.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/Log.java new file mode 100644 index 0000000..2c444d7 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/Log.java @@ -0,0 +1,32 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.message; + +@MessageType(string = "rosgraph_msgs/Log") +public class Log extends Message { + public Header header; + public byte level; + public String name; + public String msg; + public String file; + public String function; + public long line; + public String[] topics; +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/Message.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/Message.java new file mode 100644 index 0000000..0b9f968 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/Message.java @@ -0,0 +1,202 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.message; + +import java.lang.reflect.Array; +import java.lang.reflect.Field; +import java.util.Map; + + +@MessageType(string = "message") +public abstract class Message { + + // Some requirements about message types: + // - It must have a MessageType declaration to be recognized on inbound messages + // - Every field must be explicitly designated as public + // - Every field that is not a primitive or near-primitive must be another Message class + // - If there is a non-empty constructor, you must also have an empty constructor + // - If it is set up as an inner class, it needs an explicit nullary constructor + // (note: I have seen an inner class otherwise fail, I have not tested it with the explicit constructor) + + public static void register(Class c, Map messageClasses) { + try { + typecheck(c); + + // Must register the class and not have duplicate + // This is not recursive because only the top level message class + // needs to be determined from the string - others are top-down. + String messageString = getMessageType(c); + Class existingClass = messageClasses.get(messageString); + if (existingClass != null && !existingClass.equals(c)) + throw new MessageException("Message String \'" + messageString + + "\' is assigned to two different classes (" + + c.getName() + " and " + existingClass.getName() + ")"); + messageClasses.put(messageString, c); + } + catch (MessageException ex) { + // should be changed to be a hooked method to give library user control + System.out.println(ex.getMessage()); + } + } + + public static String getMessageType(Class c) { + return ((MessageType) c.getAnnotation(MessageType.class)).string(); + } + + // this has never been used or tested but kind of belongs here + // commented out because it uses ReflectiveOperationException which is not available on Android + /* + public static Message newInstance(String className) { + try { + Class messageClass = Class.forName(className); + if (Message.class.isAssignableFrom(messageClass)) + return (Message) messageClass.newInstance(); + else throw new ClassCastException(); + } + catch (ReflectiveOperationException ex) { + throw new RuntimeException("Unable to create message of class \'" + className + "\'.", ex); + } + } + */ + + // Could probably do more checking here, but not sure what right now + private static void typecheck(Class c) throws MessageException { + + // Must inherit from Message + if (!Message.class.isAssignableFrom(c)) + throw new MessageException("Class \'" + c.getName() + + "\' does not extend Message"); + + // Must have the MessageType annotation + if (getMessageType(c) == null) + throw new MessageException("Class \'" + c.getName() + + "\' is missing the MessageType annotation"); + + // All fields must also be valid Message classes + // Note that this also serves to force-load all the message classes + // so that they get registered + for (Field f : c.getFields()) { + Class fc = f.getType(); + if (fc.isArray()) { + Class ac = fc.getComponentType(); + if (!isPrimitive(ac)) + typecheck(ac); + } + else if (!isPrimitive(fc)) + typecheck(fc); + } + } + + public void print() { + printMessage(this, ""); + } + + private static void printMessage(Object o, String indent) { + for (Field f : o.getClass().getFields()) { + Class c = f.getType(); + Object fieldObject = getFieldObject(f, o); + if (fieldObject != null) { + if (isPrimitive(c)) + System.out.println(indent + f.getName() + ": " + fieldObject); + else if (c.isArray()) { + System.out.println(indent + f.getName() + ": ["); + printArray(fieldObject, indent + " "); + System.out.println(indent + "]"); + } + else { + System.out.println(indent + f.getName() + ":"); + printMessage(fieldObject, indent + " "); + } + } + } + } + + private static void printArray(Object array, String indent) { + Class arrayClass = array.getClass().getComponentType(); + for (int i = 0; i < Array.getLength(array); i++) { + Object elementObject = Array.get(array, i); + if (elementObject != null) { + if (isPrimitive(arrayClass)) + System.out.println(indent + i + ": " + elementObject); + else if (arrayClass.isArray()) { // this is not actually allowed in ROS + System.out.println(indent + i + ": ["); + printArray(elementObject, indent + " "); + System.out.println(indent + "]"); + } + else { + System.out.println(indent + i + ":"); + printMessage(elementObject, indent + " "); + } + } + } + // remember to print array indices + } + + public static boolean isPrimitive(Class c) { + return (c.isPrimitive() || + c.equals(String.class) || + Number.class.isAssignableFrom(c) || + c.equals(Boolean.class)); + } + + + // Copied from com.jilk.ros.rosbridge.JSON + private static Object getFieldObject(Field f, Object o) { + Object fo = null; + try { + fo = f.get(o); + } + catch (IllegalAccessException ex) { + ex.printStackTrace(); + } + return fo; + } + + public void copyFrom(Message source) { + try { + if (source.getClass() != getClass()) + throw new RuntimeException("Attempt to copy non-matching classes"); + for (Field f : getClass().getFields()) { + Class fc = f.getType(); + if (fc.isArray()) + throw new RuntimeException("copyFrom - array types not implemented"); + else if (!isPrimitive(fc)) + ((Message) f.get(this)).copyFrom((Message) f.get(source)); + else { + Object value = f.get(source); + f.set(this, value); + } + } + } + catch (IllegalAccessException ex) { + throw new RuntimeException("copyFrom error", ex); + } + catch (ClassCastException ex) { + throw new RuntimeException("copyFrom error", ex); + } + // ReflectiveOperationException is not available on Android (Java 1.6) + /* + catch (ReflectiveOperationException ex) { + throw new RuntimeException ("copyFrom error", ex); + } + */ + } + +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/MessageException.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/MessageException.java new file mode 100644 index 0000000..c2aa2f7 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/MessageException.java @@ -0,0 +1,32 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.message; + +public class MessageException extends Exception { + + public MessageException(String message) { + super(message); + } + + public MessageException(String message, Throwable cause) { + super(message, cause); + } +} + diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/MessageType.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/MessageType.java new file mode 100644 index 0000000..21d3cc0 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/MessageType.java @@ -0,0 +1,32 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.message; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface MessageType { + String string() default ""; +} + diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/SemanticRequest.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/SemanticRequest.java new file mode 100644 index 0000000..728efa9 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/SemanticRequest.java @@ -0,0 +1,13 @@ +package com.jilk.ros.message; + +/** + * Created by xxhong on 16-11-17. + */ +@MessageType(string = "std_msgs/String") +public class SemanticRequest extends Message { + public SemanticRequest(String args) { + jsonStr = args; + } + + public String jsonStr; +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/SemanticResponse.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/SemanticResponse.java new file mode 100644 index 0000000..624c8c6 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/SemanticResponse.java @@ -0,0 +1,9 @@ +package com.jilk.ros.message; + +/** + * Created by xxhong on 16-11-17. + */ +@MessageType(string = "std_msgs/String") +public class SemanticResponse extends Message { + public String result; +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/StdMsg.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/StdMsg.java new file mode 100644 index 0000000..570d27f --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/StdMsg.java @@ -0,0 +1,9 @@ +package com.jilk.ros.message; + +/** + * Created by xxhong on 16-11-17. + */ +@MessageType(string = "std_msgs/String") +public class StdMsg extends Message { + public String data; +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/Time.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/Time.java new file mode 100644 index 0000000..b0075a0 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/Time.java @@ -0,0 +1,24 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.message; + +@MessageType(string = "std_msgs/Time") +public class Time extends TimePrimitive { +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/TimePrimitive.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/TimePrimitive.java new file mode 100644 index 0000000..238ca6d --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/message/TimePrimitive.java @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.message; + +@MessageType(string = "time") +public class TimePrimitive extends Message { + public int secs; // when requesting this format from ROSbridge, it uses 'sec' (no 's') + public int nsecs; // when requesting this format from ROSbridge, it uses 'nsec' +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/Empty.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/Empty.java new file mode 100644 index 0000000..6bfe139 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/Empty.java @@ -0,0 +1,27 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosapi.message; + +import com.jilk.ros.message.Message; +import com.jilk.ros.message.MessageType; + +@MessageType(string = "std_msgs/Empty") +public class Empty extends Message { +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/GetTime.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/GetTime.java new file mode 100644 index 0000000..de55c5c --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/GetTime.java @@ -0,0 +1,30 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosapi.message; + +import com.jilk.ros.message.Message; +import com.jilk.ros.message.MessageType; +import com.jilk.ros.message.TimePrimitive; + + +@MessageType(string = "rosapi/GetTimeResponse") +public class GetTime extends Message { + public TimePrimitive time; +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/MessageDetails.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/MessageDetails.java new file mode 100644 index 0000000..c75bb67 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/MessageDetails.java @@ -0,0 +1,28 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosapi.message; + +import com.jilk.ros.message.Message; +import com.jilk.ros.message.MessageType; + +@MessageType(string = "rosapi/MessageDetails") +public class MessageDetails extends Message { + public TypeDef[] typedefs; +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/Nodes.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/Nodes.java new file mode 100644 index 0000000..28c87c6 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/Nodes.java @@ -0,0 +1,28 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosapi.message; + +import com.jilk.ros.message.Message; +import com.jilk.ros.message.MessageType; + +@MessageType(string = "rosapi/Nodes") +public class Nodes extends Message { + public String[] nodes; +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/Service.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/Service.java new file mode 100644 index 0000000..a3e8d78 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/Service.java @@ -0,0 +1,34 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosapi.message; + +import com.jilk.ros.message.Message; +import com.jilk.ros.message.MessageType; + +@MessageType(string = "rosapi/Service") +public class Service extends Message { + public String service; + + public Service() {} + + public Service(String service) { + this.service = service; + } +} \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/Services.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/Services.java new file mode 100644 index 0000000..2503ea5 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/Services.java @@ -0,0 +1,28 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosapi.message; + +import com.jilk.ros.message.Message; +import com.jilk.ros.message.MessageType; + +@MessageType(string = "rosapi/Services") +public class Services extends Message { + public String[] services; +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/Topic.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/Topic.java new file mode 100644 index 0000000..3ea3ebc --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/Topic.java @@ -0,0 +1,34 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosapi.message; + +import com.jilk.ros.message.Message; +import com.jilk.ros.message.MessageType; + +@MessageType(string = "rosapi/Topic") +public class Topic extends Message { + public String topic; + + public Topic() {} + + public Topic(String topic) { + this.topic = topic; + } +} \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/Topics.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/Topics.java new file mode 100644 index 0000000..07bab16 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/Topics.java @@ -0,0 +1,28 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosapi.message; + +import com.jilk.ros.message.Message; +import com.jilk.ros.message.MessageType; + +@MessageType(string = "rosapi/Topics") +public class Topics extends Message { + public String[] topics; +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/Type.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/Type.java new file mode 100644 index 0000000..73d53b0 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/Type.java @@ -0,0 +1,34 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosapi.message; + +import com.jilk.ros.message.Message; +import com.jilk.ros.message.MessageType; + +@MessageType(string = "rosapi/Type") +public class Type extends Message { + public String type; + + public Type() {} + + public Type(String type) { + this.type = type; + } +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/TypeDef.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/TypeDef.java new file mode 100644 index 0000000..aff5e47 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosapi/message/TypeDef.java @@ -0,0 +1,55 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosapi.message; + +import com.jilk.ros.message.Message; +import com.jilk.ros.message.MessageType; + +@MessageType(string = "rosapi/TypeDef") +public class TypeDef extends Message { + public String type; + public String[] fieldnames; + public String[] fieldtypes; + public int[] fieldarraylen; + public String[] examples; + + public static boolean match(String type, Class c) { + boolean result = false; + if ( + (type.equals("bool") && ((boolean.class.equals(c)) || (Boolean.class.equals(c)))) || + (type.equals("int8") && ((byte.class.equals(c)) || (Byte.class.equals(c)))) || + (type.equals("byte") && ((byte.class.equals(c)) || (Byte.class.equals(c)))) || // deprecated + (type.equals("uint8") && ((short.class.equals(c)) || (Short.class.equals(c)))) || + (type.equals("char") && ((short.class.equals(c)) || (Short.class.equals(c)))) || // deprecated + (type.equals("int16") && ((short.class.equals(c)) || (Short.class.equals(c)))) || + (type.equals("uint16") && ((int.class.equals(c)) || (Integer.class.equals(c)))) || + (type.equals("int32") && ((int.class.equals(c)) || (Integer.class.equals(c)))) || + (type.equals("uint32") && ((long.class.equals(c)) || (Long.class.equals(c)))) || + (type.equals("int64") && ((long.class.equals(c)) || (Long.class.equals(c)))) || + (type.equals("float32") && ((float.class.equals(c)) || (Float.class.equals(c)))) || + (type.equals("float64") && ((double.class.equals(c)) || (Double.class.equals(c)))) || + (type.equals("uint64") && (java.math.BigInteger.class.equals(c))) || + (type.equals("string") && (String.class.equals(c))) + ) + result = true; + + return result; + } +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/FullMessageHandler.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/FullMessageHandler.java new file mode 100644 index 0000000..62580fd --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/FullMessageHandler.java @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosbridge; + +import com.jilk.ros.message.Message; + +public interface FullMessageHandler { + public void onMessage(String id, T message); +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/ROSBridgeClient.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/ROSBridgeClient.java new file mode 100644 index 0000000..71cbbe8 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/ROSBridgeClient.java @@ -0,0 +1,260 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosbridge; + +import com.jilk.ros.ROSClient; +import com.jilk.ros.Service; +import com.jilk.ros.message.Message; +import com.jilk.ros.message.MessageType; +import com.jilk.ros.rosapi.message.Empty; +import com.jilk.ros.rosapi.message.MessageDetails; +import com.jilk.ros.rosapi.message.Nodes; +import com.jilk.ros.rosapi.message.Services; +import com.jilk.ros.rosapi.message.Topic; +import com.jilk.ros.rosapi.message.Topics; +import com.jilk.ros.rosapi.message.Type; +import com.jilk.ros.rosapi.message.TypeDef; +import com.jilk.ros.rosbridge.implementation.ROSBridgeWebSocketClient; +import com.jilk.ros.rosbridge.operation.Operation; + +import java.lang.reflect.Field; + +public class ROSBridgeClient extends ROSClient { + String uriString; + ROSBridgeWebSocketClient client; + + public ROSBridgeClient(String uriString) { + this.uriString = uriString; + } + + @Override + public boolean connect() { + return connect(null); + } + + @Override + public boolean connect(ROSClient.ConnectionStatusListener listener) { + boolean result = false; + client = ROSBridgeWebSocketClient.create(uriString); + if (client != null) { + client.setListener(listener); + try { + result = client.connectBlocking(); + } + catch (InterruptedException ex) {} + } + return result; + } + + @Override + public void disconnect() { + try { + client.closeBlocking(); + } + catch (InterruptedException ex) {} + } + + @Override + public void send(Operation operation) { + client.send(operation); + } + + @Override + public void send(String json) { + client.send(json); + } + + @Override + public void register(Class c, + String s, + Class m, + FullMessageHandler h) { + client.register(c, s, m, h); + } + + @Override + public void unregister(Class c, String s) { + client.unregister(c, s); + } + + @Override + public void setDebug(boolean debug) { + client.setDebug(debug); + } + + @Override + public String[] getNodes() throws InterruptedException { + Service nodeService = + new Service("/rosapi/nodes", Empty.class, Nodes.class, this); + return nodeService.callBlocking(new Empty()).nodes; + } + + @Override + public String[] getTopics() throws InterruptedException { + Service topicsService = + new Service("/rosapi/topics", Empty.class, Topics.class, this); + return topicsService.callBlocking(new Empty()).topics; + } + + @Override + public String[] getServices() throws InterruptedException { + Service servicesService = + new Service("/rosapi/services", Empty.class, Services.class, this); + return servicesService.callBlocking(new Empty()).services; + } + + @Override + public TypeDef getTopicMessageDetails(String topic) throws InterruptedException { + return getTypeDetails(getTopicType(topic)); + } + + @Override + public TypeDef[] getTopicMessageList(String topic) throws InterruptedException { + return getTypeList(getTopicType(topic)); + } + + @Override + public TypeDef getServiceRequestDetails(String service) throws InterruptedException { + return getTypeDetails(getServiceType(service), "Request", "/rosapi/service_request_details"); + } + + @Override + public TypeDef[] getServiceRequestList(String service) throws InterruptedException { + return getTypeList(getServiceType(service), "Request", "/rosapi/service_request_details"); + } + + @Override + public TypeDef getServiceResponseDetails(String service) throws InterruptedException { + return getTypeDetails(getServiceType(service), "Response", "/rosapi/service_response_details"); + } + + @Override + public TypeDef[] getServiceResponseList(String service) throws InterruptedException { + return getTypeList(getServiceType(service), "Response", "/rosapi/service_response_details"); + } + + @Override + public TypeDef[] getTypeList(String type) throws InterruptedException { + return getTypeList(type, "", "/rosapi/message_details"); + } + + @Override + public TypeDef getTypeDetails(String type) throws InterruptedException { + return getTypeDetails(type, "", "/rosapi/message_details"); + } + + private TypeDef[] getTypeList(String type, String suffix, String serviceName) throws InterruptedException { + Service messageDetailsService = + new Service(serviceName, + Type.class, MessageDetails.class, this); + return messageDetailsService.callBlocking(new Type(type)).typedefs; + } + + private TypeDef getTypeDetails(String type, String suffix, String serviceName) throws InterruptedException { + Service messageDetailsService = + new Service(serviceName, + Type.class, MessageDetails.class, this); + return findType(type + suffix, messageDetailsService.callBlocking(new Type(type)).typedefs); + } + + private String getTopicType(String topic) throws InterruptedException { + Service topicTypeService = + new Service("/rosapi/topic_type", + Topic.class, Type.class, this); + return topicTypeService.callBlocking(new Topic(topic)).type; + } + + private String getServiceType(String service) throws InterruptedException { + Service serviceTypeService = + new Service("/rosapi/service_type", + com.jilk.ros.rosapi.message.Service.class, Type.class, this); + return serviceTypeService.callBlocking(new com.jilk.ros.rosapi.message.Service(service)).type; + } + + private TypeDef findType(String type, TypeDef[] types) { + TypeDef result = null; + for (TypeDef t : types) { + if (t.type.equals(type)) { + result = t; + break; + } + } + //System.out.println("ROSBridgeClient.findType: "); + //result.print(); + return result; + } + + @Override + public void typeMatch(TypeDef t, Class c) throws InterruptedException { + if (c == null) + throw new RuntimeException("No registered message type found for: " + t.type); + Field[] fields = c.getFields(); + for (int i = 0; i < t.fieldnames.length; i++) { + + // Field names + String classFieldName = fields[i].getName(); + String typeFieldName = t.fieldnames[i]; + if (!classFieldName.equals(typeFieldName)) + typeMatchError(t, c, "field name", typeFieldName, classFieldName); + + // Array type of field + boolean typeIsArray = (t.fieldarraylen[i] >= 0); + boolean fieldIsArray = fields[i].getType().isArray(); + if (typeIsArray != fieldIsArray) + typeMatchError(t, c, "array mismatch", typeFieldName, classFieldName); + + // Get base type of field + Class fieldClass = fields[i].getType(); + if (fieldIsArray) + fieldClass = fields[i].getType().getComponentType(); + String type = t.fieldtypes[i]; + + // Field type for primitivesclient + if (Message.isPrimitive(fieldClass)) { + if (!TypeDef.match(type, fieldClass)) + typeMatchError(t, c, "type mismatch", type, fieldClass.getName()); + } + + // Field type for non-primitive classes, and recurse + else { + if (!Message.class.isAssignableFrom(fieldClass)) + throw new RuntimeException("Member " + classFieldName + + " of class " + fieldClass.getName() + " does not extend Message."); + String fieldClassString = ((MessageType) fieldClass.getAnnotation(MessageType.class)).string(); + if (!type.equals(fieldClassString)) + typeMatchError(t, c, "message type mismatch", type, fieldClassString); + typeMatch(getTypeDetails(type), fieldClass); + } + } + } + + private void typeMatchError(TypeDef t, Class c, + String error, String tString, String cString) { + throw new RuntimeException("Type match error between " + + t.type + " and " + c.getName() + ": " + + error + ": \'" + tString + "\' does not match \'" + cString + "\'."); + } + + @Override + public Object getUnderlyingClient() { + return client; + } + +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/implementation/Base64.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/implementation/Base64.java new file mode 100644 index 0000000..75eec18 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/implementation/Base64.java @@ -0,0 +1,575 @@ +package com.jilk.ros.rosbridge.implementation; + +import java.util.Arrays; + +/** A very fast and memory efficient class to encode and decode to and from BASE64 in full accordance + * with RFC 2045.

+ * On Windows XP sp1 with 1.4.2_04 and later ;), this encoder and decoder is about 10 times faster + * on small arrays (10 - 1000 bytes) and 2-3 times as fast on larger arrays (10000 - 1000000 bytes) + * compared to sun.misc.Encoder()/Decoder().

+ * + * On byte arrays the encoder is about 20% faster than Jakarta Commons Base64 Codec for encode and + * about 50% faster for decoding large arrays. This implementation is about twice as fast on very small + * arrays (< 30 bytes). If source/destination is a String this + * version is about three times as fast due to the fact that the Commons Codec result has to be recoded + * to a String from byte[], which is very expensive.

+ * + * This encode/decode algorithm doesn't create any temporary arrays as many other codecs do, it only + * allocates the resulting array. This produces less garbage and it is possible to handle arrays twice + * as large as algorithms that create a temporary array. (E.g. Jakarta Commons Codec). It is unknown + * whether Sun's sun.misc.Encoder()/Decoder() produce temporary arrays but since performance + * is quite low it probably does.

+ * + * The encoder produces the same output as the Sun one except that the Sun's encoder appends + * a trailing line separator if the last character isn't a pad. Unclear why but it only adds to the + * length and is probably a side effect. Both are in conformance with RFC 2045 though.
+ * Commons codec seem to always att a trailing line separator.

+ * + * Note! + * The encode/decode method pairs (types) come in three versions with the exact same algorithm and + * thus a lot of code redundancy. This is to not create any temporary arrays for transcoding to/from different + * format types. The methods not used can simply be commented out.

+ * + * There is also a "fast" version of all decode methods that works the same way as the normal ones, but + * har a few demands on the decoded input. Normally though, these fast verions should be used if the source if + * the input is known and it hasn't bee tampered with.

+ * + * If you find the code useful or you find a bug, please send me a note at base64 @ miginfocom . com. + * + * Licence (BSD): + * ============== + * + * Copyright (c) 2004, Mikael Grev, MiG InfoCom AB. (base64 @ miginfocom . com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this list + * of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * Neither the name of the MiG InfoCom AB nor the names of its contributors may be + * used to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * @version 2.2 + * @author Mikael Grev + * Date: 2004-aug-02 + * Time: 11:31:11 + */ + +public class Base64 +{ + private static final char[] CA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray(); + private static final int[] IA = new int[256]; + static { + Arrays.fill(IA, -1); + for (int i = 0, iS = CA.length; i < iS; i++) + IA[CA[i]] = i; + IA['='] = 0; + } + + // **************************************************************************************** + // * char[] version + // **************************************************************************************** + + /** Encodes a raw byte array into a BASE64 char[] representation i accordance with RFC 2045. + * @param sArr The bytes to convert. If null or length 0 an empty array will be returned. + * @param lineSep Optional "\r\n" after 76 characters, unless end of file.
+ * No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a + * little faster. + * @return A BASE64 encoded array. Never null. + */ + public final static char[] encodeToChar(byte[] sArr, boolean lineSep) + { + // Check special case + int sLen = sArr != null ? sArr.length : 0; + if (sLen == 0) + return new char[0]; + + int eLen = (sLen / 3) * 3; // Length of even 24-bits. + int cCnt = ((sLen - 1) / 3 + 1) << 2; // Returned character count + int dLen = cCnt + (lineSep ? (cCnt - 1) / 76 << 1 : 0); // Length of returned array + char[] dArr = new char[dLen]; + + // Encode even 24-bits + for (int s = 0, d = 0, cc = 0; s < eLen;) { + // Copy next three bytes into lower 24 bits of int, paying attension to sign. + int i = (sArr[s++] & 0xff) << 16 | (sArr[s++] & 0xff) << 8 | (sArr[s++] & 0xff); + + // Encode the int into four chars + dArr[d++] = CA[(i >>> 18) & 0x3f]; + dArr[d++] = CA[(i >>> 12) & 0x3f]; + dArr[d++] = CA[(i >>> 6) & 0x3f]; + dArr[d++] = CA[i & 0x3f]; + + // Add optional line separator + if (lineSep && ++cc == 19 && d < dLen - 2) { + dArr[d++] = '\r'; + dArr[d++] = '\n'; + cc = 0; + } + } + + // Pad and encode last bits if source isn't even 24 bits. + int left = sLen - eLen; // 0 - 2. + if (left > 0) { + // Prepare the int + int i = ((sArr[eLen] & 0xff) << 10) | (left == 2 ? ((sArr[sLen - 1] & 0xff) << 2) : 0); + + // Set last four chars + dArr[dLen - 4] = CA[i >> 12]; + dArr[dLen - 3] = CA[(i >>> 6) & 0x3f]; + dArr[dLen - 2] = left == 2 ? CA[i & 0x3f] : '='; + dArr[dLen - 1] = '='; + } + return dArr; + } + + /** Decodes a BASE64 encoded char array. All illegal characters will be ignored and can handle both arrays with + * and without line separators. + * @param sArr The source array. null or length 0 will return an empty array. + * @return The decoded array of bytes. May be of length 0. Will be null if the legal characters + * (including '=') isn't divideable by 4. (I.e. definitely corrupted). + */ + public final static byte[] decode(char[] sArr) + { + // Check special case + int sLen = sArr != null ? sArr.length : 0; + if (sLen == 0) + return new byte[0]; + + // Count illegal characters (including '\r', '\n') to know what size the returned array will be, + // so we don't have to reallocate & copy it later. + int sepCnt = 0; // Number of separator characters. (Actually illegal characters, but that's a bonus...) + for (int i = 0; i < sLen; i++) // If input is "pure" (I.e. no line separators or illegal chars) base64 this loop can be commented out. + if (IA[sArr[i]] < 0) + sepCnt++; + + // Check so that legal chars (including '=') are evenly divideable by 4 as specified in RFC 2045. + if ((sLen - sepCnt) % 4 != 0) + return null; + + int pad = 0; + for (int i = sLen; i > 1 && IA[sArr[--i]] <= 0;) + if (sArr[i] == '=') + pad++; + + int len = ((sLen - sepCnt) * 6 >> 3) - pad; + + byte[] dArr = new byte[len]; // Preallocate byte[] of exact length + + for (int s = 0, d = 0; d < len;) { + // Assemble three bytes into an int from four "valid" characters. + int i = 0; + for (int j = 0; j < 4; j++) { // j only increased if a valid char was found. + int c = IA[sArr[s++]]; + if (c >= 0) + i |= c << (18 - j * 6); + else + j--; + } + // Add the bytes + dArr[d++] = (byte) (i >> 16); + if (d < len) { + dArr[d++]= (byte) (i >> 8); + if (d < len) + dArr[d++] = (byte) i; + } + } + return dArr; + } + + /** Decodes a BASE64 encoded char array that is known to be resonably well formatted. The method is about twice as + * fast as {@link #decode(char[])}. The preconditions are:
+ * + The array must have a line length of 76 chars OR no line separators at all (one line).
+ * + Line separator must be "\r\n", as specified in RFC 2045 + * + The array must not contain illegal characters within the encoded string
+ * + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.
+ * @param sArr The source array. Length 0 will return an empty array. null will throw an exception. + * @return The decoded array of bytes. May be of length 0. + */ + public final static byte[] decodeFast(char[] sArr) + { + // Check special case + int sLen = sArr.length; + if (sLen == 0) + return new byte[0]; + + int sIx = 0, eIx = sLen - 1; // Start and end index after trimming. + + // Trim illegal chars from start + while (sIx < eIx && IA[sArr[sIx]] < 0) + sIx++; + + // Trim illegal chars from end + while (eIx > 0 && IA[sArr[eIx]] < 0) + eIx--; + + // get the padding count (=) (0, 1 or 2) + int pad = sArr[eIx] == '=' ? (sArr[eIx - 1] == '=' ? 2 : 1) : 0; // Count '=' at end. + int cCnt = eIx - sIx + 1; // Content count including possible separators + int sepCnt = sLen > 76 ? (sArr[76] == '\r' ? cCnt / 78 : 0) << 1 : 0; + + int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytes + byte[] dArr = new byte[len]; // Preallocate byte[] of exact length + + // Decode all but the last 0 - 2 bytes. + int d = 0; + for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) { + // Assemble three bytes into an int from four "valid" characters. + int i = IA[sArr[sIx++]] << 18 | IA[sArr[sIx++]] << 12 | IA[sArr[sIx++]] << 6 | IA[sArr[sIx++]]; + + // Add the bytes + dArr[d++] = (byte) (i >> 16); + dArr[d++] = (byte) (i >> 8); + dArr[d++] = (byte) i; + + // If line separator, jump over it. + if (sepCnt > 0 && ++cc == 19) { + sIx += 2; + cc = 0; + } + } + + if (d < len) { + // Decode last 1-3 bytes (incl '=') into 1-3 bytes + int i = 0; + for (int j = 0; sIx <= eIx - pad; j++) + i |= IA[sArr[sIx++]] << (18 - j * 6); + + for (int r = 16; d < len; r -= 8) + dArr[d++] = (byte) (i >> r); + } + + return dArr; + } + + // **************************************************************************************** + // * byte[] version + // **************************************************************************************** + + /** Encodes a raw byte array into a BASE64 byte[] representation i accordance with RFC 2045. + * @param sArr The bytes to convert. If null or length 0 an empty array will be returned. + * @param lineSep Optional "\r\n" after 76 characters, unless end of file.
+ * No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a + * little faster. + * @return A BASE64 encoded array. Never null. + */ + public final static byte[] encodeToByte(byte[] sArr, boolean lineSep) + { + // Check special case + int sLen = sArr != null ? sArr.length : 0; + if (sLen == 0) + return new byte[0]; + + int eLen = (sLen / 3) * 3; // Length of even 24-bits. + int cCnt = ((sLen - 1) / 3 + 1) << 2; // Returned character count + int dLen = cCnt + (lineSep ? (cCnt - 1) / 76 << 1 : 0); // Length of returned array + byte[] dArr = new byte[dLen]; + + // Encode even 24-bits + for (int s = 0, d = 0, cc = 0; s < eLen;) { + // Copy next three bytes into lower 24 bits of int, paying attension to sign. + int i = (sArr[s++] & 0xff) << 16 | (sArr[s++] & 0xff) << 8 | (sArr[s++] & 0xff); + + // Encode the int into four chars + dArr[d++] = (byte) CA[(i >>> 18) & 0x3f]; + dArr[d++] = (byte) CA[(i >>> 12) & 0x3f]; + dArr[d++] = (byte) CA[(i >>> 6) & 0x3f]; + dArr[d++] = (byte) CA[i & 0x3f]; + + // Add optional line separator + if (lineSep && ++cc == 19 && d < dLen - 2) { + dArr[d++] = '\r'; + dArr[d++] = '\n'; + cc = 0; + } + } + + // Pad and encode last bits if source isn't an even 24 bits. + int left = sLen - eLen; // 0 - 2. + if (left > 0) { + // Prepare the int + int i = ((sArr[eLen] & 0xff) << 10) | (left == 2 ? ((sArr[sLen - 1] & 0xff) << 2) : 0); + + // Set last four chars + dArr[dLen - 4] = (byte) CA[i >> 12]; + dArr[dLen - 3] = (byte) CA[(i >>> 6) & 0x3f]; + dArr[dLen - 2] = left == 2 ? (byte) CA[i & 0x3f] : (byte) '='; + dArr[dLen - 1] = '='; + } + return dArr; + } + + /** Decodes a BASE64 encoded byte array. All illegal characters will be ignored and can handle both arrays with + * and without line separators. + * @param sArr The source array. Length 0 will return an empty array. null will throw an exception. + * @return The decoded array of bytes. May be of length 0. Will be null if the legal characters + * (including '=') isn't divideable by 4. (I.e. definitely corrupted). + */ + public final static byte[] decode(byte[] sArr) + { + // Check special case + int sLen = sArr.length; + + // Count illegal characters (including '\r', '\n') to know what size the returned array will be, + // so we don't have to reallocate & copy it later. + int sepCnt = 0; // Number of separator characters. (Actually illegal characters, but that's a bonus...) + for (int i = 0; i < sLen; i++) // If input is "pure" (I.e. no line separators or illegal chars) base64 this loop can be commented out. + if (IA[sArr[i] & 0xff] < 0) + sepCnt++; + + // Check so that legal chars (including '=') are evenly divideable by 4 as specified in RFC 2045. + if ((sLen - sepCnt) % 4 != 0) + return null; + + int pad = 0; + for (int i = sLen; i > 1 && IA[sArr[--i] & 0xff] <= 0;) + if (sArr[i] == '=') + pad++; + + int len = ((sLen - sepCnt) * 6 >> 3) - pad; + + byte[] dArr = new byte[len]; // Preallocate byte[] of exact length + + for (int s = 0, d = 0; d < len;) { + // Assemble three bytes into an int from four "valid" characters. + int i = 0; + for (int j = 0; j < 4; j++) { // j only increased if a valid char was found. + int c = IA[sArr[s++] & 0xff]; + if (c >= 0) + i |= c << (18 - j * 6); + else + j--; + } + + // Add the bytes + dArr[d++] = (byte) (i >> 16); + if (d < len) { + dArr[d++]= (byte) (i >> 8); + if (d < len) + dArr[d++] = (byte) i; + } + } + + return dArr; + } + + + /** Decodes a BASE64 encoded byte array that is known to be resonably well formatted. The method is about twice as + * fast as {@link #decode(byte[])}. The preconditions are:
+ * + The array must have a line length of 76 chars OR no line separators at all (one line).
+ * + Line separator must be "\r\n", as specified in RFC 2045 + * + The array must not contain illegal characters within the encoded string
+ * + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.
+ * @param sArr The source array. Length 0 will return an empty array. null will throw an exception. + * @return The decoded array of bytes. May be of length 0. + */ + public final static byte[] decodeFast(byte[] sArr) + { + // Check special case + int sLen = sArr.length; + if (sLen == 0) + return new byte[0]; + + int sIx = 0, eIx = sLen - 1; // Start and end index after trimming. + + // Trim illegal chars from start + while (sIx < eIx && IA[sArr[sIx] & 0xff] < 0) + sIx++; + + // Trim illegal chars from end + while (eIx > 0 && IA[sArr[eIx] & 0xff] < 0) + eIx--; + + // get the padding count (=) (0, 1 or 2) + int pad = sArr[eIx] == '=' ? (sArr[eIx - 1] == '=' ? 2 : 1) : 0; // Count '=' at end. + int cCnt = eIx - sIx + 1; // Content count including possible separators + int sepCnt = sLen > 76 ? (sArr[76] == '\r' ? cCnt / 78 : 0) << 1 : 0; + + int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytes + byte[] dArr = new byte[len]; // Preallocate byte[] of exact length + + // Decode all but the last 0 - 2 bytes. + int d = 0; + for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) { + // Assemble three bytes into an int from four "valid" characters. + int i = IA[sArr[sIx++]] << 18 | IA[sArr[sIx++]] << 12 | IA[sArr[sIx++]] << 6 | IA[sArr[sIx++]]; + + // Add the bytes + dArr[d++] = (byte) (i >> 16); + dArr[d++] = (byte) (i >> 8); + dArr[d++] = (byte) i; + + // If line separator, jump over it. + if (sepCnt > 0 && ++cc == 19) { + sIx += 2; + cc = 0; + } + } + + if (d < len) { + // Decode last 1-3 bytes (incl '=') into 1-3 bytes + int i = 0; + for (int j = 0; sIx <= eIx - pad; j++) + i |= IA[sArr[sIx++]] << (18 - j * 6); + + for (int r = 16; d < len; r -= 8) + dArr[d++] = (byte) (i >> r); + } + + return dArr; + } + + // **************************************************************************************** + // * String version + // **************************************************************************************** + + /** Encodes a raw byte array into a BASE64 String representation i accordance with RFC 2045. + * @param sArr The bytes to convert. If null or length 0 an empty array will be returned. + * @param lineSep Optional "\r\n" after 76 characters, unless end of file.
+ * No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a + * little faster. + * @return A BASE64 encoded array. Never null. + */ + public final static String encodeToString(byte[] sArr, boolean lineSep) + { + // Reuse char[] since we can't create a String incrementally anyway and StringBuffer/Builder would be slower. + return new String(encodeToChar(sArr, lineSep)); + } + + /** Decodes a BASE64 encoded String. All illegal characters will be ignored and can handle both strings with + * and without line separators.
+ * Note! It can be up to about 2x the speed to call decode(str.toCharArray()) instead. That + * will create a temporary array though. This version will use str.charAt(i) to iterate the string. + * @param str The source string. null or length 0 will return an empty array. + * @return The decoded array of bytes. May be of length 0. Will be null if the legal characters + * (including '=') isn't divideable by 4. (I.e. definitely corrupted). + */ + public final static byte[] decode(String str) + { + // Check special case + int sLen = str != null ? str.length() : 0; + if (sLen == 0) + return new byte[0]; + + // Count illegal characters (including '\r', '\n') to know what size the returned array will be, + // so we don't have to reallocate & copy it later. + int sepCnt = 0; // Number of separator characters. (Actually illegal characters, but that's a bonus...) + for (int i = 0; i < sLen; i++) // If input is "pure" (I.e. no line separators or illegal chars) base64 this loop can be commented out. + if (IA[str.charAt(i)] < 0) + sepCnt++; + + // Check so that legal chars (including '=') are evenly divideable by 4 as specified in RFC 2045. + if ((sLen - sepCnt) % 4 != 0) + return null; + + // Count '=' at end + int pad = 0; + for (int i = sLen; i > 1 && IA[str.charAt(--i)] <= 0;) + if (str.charAt(i) == '=') + pad++; + + int len = ((sLen - sepCnt) * 6 >> 3) - pad; + + byte[] dArr = new byte[len]; // Preallocate byte[] of exact length + + for (int s = 0, d = 0; d < len;) { + // Assemble three bytes into an int from four "valid" characters. + int i = 0; + for (int j = 0; j < 4; j++) { // j only increased if a valid char was found. + int c = IA[str.charAt(s++)]; + if (c >= 0) + i |= c << (18 - j * 6); + else + j--; + } + // Add the bytes + dArr[d++] = (byte) (i >> 16); + if (d < len) { + dArr[d++]= (byte) (i >> 8); + if (d < len) + dArr[d++] = (byte) i; + } + } + return dArr; + } + + /** Decodes a BASE64 encoded string that is known to be resonably well formatted. The method is about twice as + * fast as {@link #decode(String)}. The preconditions are:
+ * + The array must have a line length of 76 chars OR no line separators at all (one line).
+ * + Line separator must be "\r\n", as specified in RFC 2045 + * + The array must not contain illegal characters within the encoded string
+ * + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.
+ * @param s The source string. Length 0 will return an empty array. null will throw an exception. + * @return The decoded array of bytes. May be of length 0. + */ + public final static byte[] decodeFast(String s) + { + // Check special case + int sLen = s.length(); + if (sLen == 0) + return new byte[0]; + + int sIx = 0, eIx = sLen - 1; // Start and end index after trimming. + + // Trim illegal chars from start + while (sIx < eIx && IA[s.charAt(sIx) & 0xff] < 0) + sIx++; + + // Trim illegal chars from end + while (eIx > 0 && IA[s.charAt(eIx) & 0xff] < 0) + eIx--; + + // get the padding count (=) (0, 1 or 2) + int pad = s.charAt(eIx) == '=' ? (s.charAt(eIx - 1) == '=' ? 2 : 1) : 0; // Count '=' at end. + int cCnt = eIx - sIx + 1; // Content count including possible separators + int sepCnt = sLen > 76 ? (s.charAt(76) == '\r' ? cCnt / 78 : 0) << 1 : 0; + + int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytes + byte[] dArr = new byte[len]; // Preallocate byte[] of exact length + + // Decode all but the last 0 - 2 bytes. + int d = 0; + for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) { + // Assemble three bytes into an int from four "valid" characters. + int i = IA[s.charAt(sIx++)] << 18 | IA[s.charAt(sIx++)] << 12 | IA[s.charAt(sIx++)] << 6 | IA[s.charAt(sIx++)]; + + // Add the bytes + dArr[d++] = (byte) (i >> 16); + dArr[d++] = (byte) (i >> 8); + dArr[d++] = (byte) i; + + // If line separator, jump over it. + if (sepCnt > 0 && ++cc == 19) { + sIx += 2; + cc = 0; + } + } + + if (d < len) { + // Decode last 1-3 bytes (incl '=') into 1-3 bytes + int i = 0; + for (int j = 0; sIx <= eIx - pad; j++) + i |= IA[s.charAt(sIx++)] << (18 - j * 6); + + for (int r = 16; d < len; r -= 8) + dArr[d++] = (byte) (i >> r); + } + + return dArr; + } +} \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/implementation/JSON.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/implementation/JSON.java new file mode 100644 index 0000000..acbe1d7 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/implementation/JSON.java @@ -0,0 +1,335 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosbridge.implementation; + +import com.jilk.ros.message.Message; +import com.jilk.ros.rosbridge.indication.Indication; +import com.jilk.ros.rosbridge.operation.Wrapper; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; + +import java.io.StringReader; +import java.lang.reflect.Array; +import java.lang.reflect.Field; + + +// The slightly crazy abstractions here are designed to isolate knowledge of +// the JSON library and data types from the Operation details of rosbridge. +// Why is this important? A few reasons I can see. First, we might want +// to change JSON libraries and this encapsulates all use of JSON-simple. +// Second, as much as possible I would like the semantics of the rosbridge +// protocol to be encapsulated in the Operation and its subclasses rather +// than in a module that is essentially about serialization. +// +// Unfortunately the hierarchical Message abstraction is a bit broken +// at the top level. Beginning at the actual operation (e.g., Publish), the +// types of the fields are determined either by the fields themselves or by +// an indicator. However, the type of the operation itself is not determined +// this way, because the indicator is in the object itself, which means it +// would have to be created before its type is known. Rather than build in +// exceptions, I elected to create a "Wrapper" operation type that simply +// wraps the concrete operation and copies its "op" field. +// + +public class JSON { + + /** + * Translates a Message recursively into JSON. Normally the Message is also an + * Operation, but it does not have to be. The caller constructs a complete + * message using @Operation and @Message types. This includes situations + * where one or more fields are marked to be turned into arrays, using @AsArray. + * @param m the @Message object to be recursively translated. + * @return the complete JSON string. + */ + public static String toJSON(Message m) { + JSONObject jo = convertObjectToJSONObject(m); // Object to JSON-Simple + return jo.toJSONString(); // JSON-Simple to string + } + + /** + * Translates JSON into a hierarchical Operation/Message structure. + * This includes handling fields that are @Indicated and @AsArray. If the + * @Class parameter is a @Wrapper, this is a special case whereby the + * object is wrapped to create a consistent hierarchy. + * @param json the source JSON string + * @param c the top level class of the JSON. Normally @Wrapper + * @param r the @Registry containing topic registrations + * @return the fully instantiated message hierarchy represented + * by the JSON string. + */ + public static Message toMessage(String json, Class c, Registry r) { + JSONObject joUnwrapped = convertStringToJSONObject(json); // String to JSON-Simple + JSONObject jo = joUnwrapped; + if (Wrapper.class.isAssignableFrom(c)) + jo = wrap(joUnwrapped, c); // wrap: a hack to make the hierarchy homogeneous + return convertJSONObjectToMessage(jo, c, r); // JSON-Simple to Message + } + + // *** Create JSON from Messages *** // + + // Translate the object into a JSON-Simple object, field-by-field, + // recursively via convertElementToJSON. + // except for the case where AsArray is indicated + private static JSONObject convertObjectToJSONObject(Object o) { + JSONObject result = new JSONObject(); + for (Field f : o.getClass().getFields()) { + Object fieldObject = getFieldObject(f, o); + if (fieldObject != null) { + Object resultObject; + if (Indication.isBase64Encoded(f)) + resultObject = convertByteArrayToBase64JSONString(fieldObject); + else if (Indication.asArray(f)) + resultObject = convertObjectToJSONArray(fieldObject); + else resultObject = convertElementToJSON(fieldObject); + result.put(f.getName(), resultObject); + } + } + return result; + } + + // Convert an array type to a JSON-Simple array, element-by-element, + // recursively via convertElementToJSON. + private static JSONArray convertArrayToJSONArray(Object array) { + JSONArray result = new JSONArray(); + for (int i = 0; i < Array.getLength(array); i++) { + Object elementObject = Array.get(array, i); + if (elementObject != null) { + Object resultObject = convertElementToJSON(elementObject); + result.add(resultObject); + } + } + return result; + } + + // For AsArray objects, convert the object to a JSON-Simple array + // NOTE: This relies on later versions of the JDK providing + // the fields in order. + private static JSONArray convertObjectToJSONArray(Object o) { + JSONArray result = new JSONArray(); + for (Field f : o.getClass().getFields()) { + Object fieldObject = getFieldObject(f, o); + if (fieldObject != null) { + Object resultObject = convertElementToJSON(fieldObject); + result.add(resultObject); + } + } + return result; + } + + // Convert the individual field or array element items recursively + private static Object convertElementToJSON(Object elementObject) { + Class elementClass = elementObject.getClass(); + Object resultObject; + if (Message.isPrimitive(elementClass)) + resultObject = elementObject; + else if (elementClass.isArray()) + resultObject = convertArrayToJSONArray(elementObject); + else + resultObject = convertObjectToJSONObject(elementObject); + return resultObject; + } + + // Special case for Base 64-encoded fields + private static Object convertByteArrayToBase64JSONString(Object fieldObject) { + return Base64.encodeToString((byte[]) fieldObject, false); + } + + // This is just to buffer the code from the exception. Better error + // handling needed here. + private static Object getFieldObject(Field f, Object o) { + Object fo = null; + try { + fo = f.get(o); + } + catch (IllegalAccessException ex) { + ex.printStackTrace(); + } + return fo; + } + + // *** Create Messages from JSON *** // + + // Use the JSON-simple parser to create the JSON-Simple object + private static JSONObject convertStringToJSONObject(String json) { + JSONObject result = null; + StringReader r = new StringReader(json); + JSONParser jp = new JSONParser(); + try { + result = (JSONObject) jp.parse(r); + } + catch (Throwable t) { + System.out.println(t.getMessage()); + } + r.close(); + return result; + } + + // A bit of a hack to create a consistent hierarchy with jsonbridge operations + // At least it does not depend on any specific field names, it just copies the + // Indicator and Indicated fields. + private static JSONObject wrap(JSONObject jo, Class c) { + JSONObject result = new JSONObject(); + String indicatorName = Indication.getIndicatorName(c); + String indicatedName = Indication.getIndicatedName(c); + result.put(indicatorName, jo.get(indicatorName)); + result.put(indicatedName, jo); + return result; + } + + // Convert the JSON-Simple object to the indicated message, field-by-field + // recursively via convertElementToField. + private static Message convertJSONObjectToMessage(JSONObject jo, Class c, Registry r) { + //System.out.println("JSON.convertJSONObjectToMessage: " + jo.toJSONString()); + try { + Message result = (Message) c.newInstance(); + for (Field f : c.getFields()) { + Class fc = getFieldClass(result, jo, f, r); + Object lookup = jo.get(f.getName()); + if (lookup != null) { + Object value = convertElementToField(lookup, fc, f, r); + f.set(result, value); + } + } + return result; + } + catch (Exception ex) { + //ex.printStackTrace(); + return null; + } + } + + // Convert the JSON-Simple array to the indicated message, element-by-element + // recursively via convertElementToField. + private static Object convertJSONArrayToArray(JSONArray ja, Class c, Registry r) { + Object result = Array.newInstance(c, ja.size()); + for (int i = 0; i < ja.size(); i++) { + Object lookup = ja.get(i); + Object value = null; + if (lookup != null) { + if (lookup.getClass().equals(JSONObject.class)) + value = convertJSONObjectToMessage((JSONObject) lookup, c, r); + else if (lookup.getClass().equals(JSONArray.class)) // this is not actually allowed in ROS + value = convertJSONArrayToArray((JSONArray) lookup, c.getComponentType(), r); + else + value = convertJSONPrimitiveToPrimitive(lookup, c); + Array.set(result, i, value); + } + } + + return result; + } + + // Convert a JSON-Simple array to a Message, field-by-field of the Message, + // element-by-element of the array, recursively via convertElementToField. + // NOTE: This relies on later versions of the JDK providing + // the fields in order. + private static Message convertJSONArrayToMessage(JSONArray ja, Class c, Registry r) { + try { + Message result = (Message) c.newInstance(); + int arrayIndex = 0; + for (Field f : c.getFields()) { + Class fc = getFieldClass(result, null, f, r); + Object lookup = ja.get(arrayIndex++); // yes we are assuming that the fields are delivered in order + if (lookup != null) { + Object value = convertElementToField(lookup, fc, f, r); + f.set(result, value); + } + } + + return result; + } + catch (Exception ex) { + ex.printStackTrace(); + return null; + } + } + + // Convert an individual array or object element to a field in the Message, + // recursively, and applying AsArray if needed. + private static Object convertElementToField(Object element, Class fc, Field f, Registry r) { + //System.out.println("JSON.convertElementToField: " + f.getName() + " " + fc.getName()); + Object value; + if (element.getClass().equals(JSONObject.class)) { + //System.out.println("JSON.convertElementToField: JSON Object " + ((JSONObject) element).toJSONString()); + value = convertJSONObjectToMessage((JSONObject) element, fc, r); + } + else if (element.getClass().equals(JSONArray.class)) { + //System.out.println("JSON.convertElementToField: JSON Array " + ((JSONArray) element).toJSONString()); + if (Indication.asArray(f)) + value = convertJSONArrayToMessage((JSONArray) element, fc, r); + else value = convertJSONArrayToArray((JSONArray) element, fc, r); + } + else { + //System.out.println("JSON.convertElementToField: Primitive " + element); + if (Indication.isBase64Encoded(f)) + value = convertBase64JSONStringToByteArray(element); + else value = convertJSONPrimitiveToPrimitive(element, fc); + } + + return value; + } + + // Note that this is not checking ranges + public static Object convertJSONPrimitiveToPrimitive(Object o, Class c) { + Object result = o; + if (c.isPrimitive() || Number.class.isAssignableFrom(c)) { + if (c.equals(double.class) || c.equals(Double.class)) + result = new Double(((Number) o).doubleValue()); + else if (c.equals(float.class) || c.equals(Float.class)) + result = new Float(((Number) o).floatValue()); + else if (c.equals(long.class) || c.equals(Long.class)) + result = new Long(((Number) o).longValue()); + else if (int.class.equals(c) || c.equals(Integer.class)) + result = new Integer(((Number) o).intValue()); + else if (c.equals(short.class) || c.equals(Short.class)) + result = new Short(((Number) o).shortValue()); + else if (c.equals(byte.class) || c.equals(Byte.class)) + result = new Byte(((Number) o).byteValue()); + } + return result; + } + + public static byte[] convertBase64JSONStringToByteArray(Object element) { + return Base64.decode((String) element); + } + + // Determine the target class of a field in the object or array, based + // directly on the field's type, or using the Indicator if applicable, + // The Indicator field only provides the topic/service, so we have to look + // up the Class in the registry. + public static Class getFieldClass(Message parent, JSONObject jo, Field f, Registry r) { + Class fc; + fc = f.getType(); + if (fc.isArray()) + fc = f.getType().getComponentType(); + if (Indication.isIndicated(f) && (jo != null)) { + //fc = Indication.getIndication(parent, + // (String) jo.get(Indication.getIndicatorName(parent.getClass()))); + fc = r.lookup(parent.getClass(), + (String) jo.get(Indication.getIndicatorName(parent.getClass()))); + //System.out.println("JSON.getFieldClass: parent class " + parent.getClass().getName() + + // " Indicator: " + Indication.getIndicatorName(parent.getClass()) + + // " result: " + fc.getName()); + } + return fc; + } +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/implementation/PublishEvent.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/implementation/PublishEvent.java new file mode 100644 index 0000000..c7df197 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/implementation/PublishEvent.java @@ -0,0 +1,24 @@ +package com.jilk.ros.rosbridge.implementation; + +import com.jilk.ros.rosbridge.operation.Operation; + +/**EventBus event entity,describe ros server response info + * Created by xxhong on 16-11-22. + */ + +public class PublishEvent { + public String msg; + public String id; + public String name; + public String op; + + + public PublishEvent(Operation operation, String name, String content) { + if(operation != null) { + id = operation.id; + op = operation.op; + } + this.name = name; + msg = content; + } +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/implementation/ROSBridgeWebSocketClient.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/implementation/ROSBridgeWebSocketClient.java new file mode 100644 index 0000000..71d1abb --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/implementation/ROSBridgeWebSocketClient.java @@ -0,0 +1,202 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + *

+ * This file is part of the Java ROSBridge Client. + *

+ * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + *

+ * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + *

+ * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + */ +package com.jilk.ros.rosbridge.implementation; + +import com.jilk.ros.ROSClient; +import com.jilk.ros.message.Message; +import com.jilk.ros.rosbridge.FullMessageHandler; +import com.jilk.ros.rosbridge.operation.Operation; +import com.jilk.ros.rosbridge.operation.Publish; +import com.jilk.ros.rosbridge.operation.ServiceResponse; + +import org.java_websocket.client.WebSocketClient; +import org.java_websocket.framing.CloseFrame; +import org.java_websocket.handshake.ServerHandshake; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import java.lang.reflect.Field; +import java.net.Socket; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.channels.SocketChannel; + +import de.greenrobot.event.EventBus; + +public class ROSBridgeWebSocketClient extends WebSocketClient { + private Registry classes; + private Registry handlers; + private boolean debug; + private ROSClient.ConnectionStatusListener listener; + + ROSBridgeWebSocketClient(URI serverURI) { + super(serverURI); + classes = new Registry(); + handlers = new Registry(); + Operation.initialize(classes); // note, this ensures that the Message Map is initialized too + listener = null; + } + + public static ROSBridgeWebSocketClient create(String URIString) { + ROSBridgeWebSocketClient client = null; + try { + URI uri = new URI(URIString); + client = new ROSBridgeWebSocketClient(uri); + } catch (URISyntaxException ex) { + ex.printStackTrace(); + } + return client; + } + + public void setListener(ROSClient.ConnectionStatusListener listener) { + this.listener = listener; + } + + @Override + public void onOpen(ServerHandshake handshakedata) { + if (listener != null) + listener.onConnect(); + } + + @Override + public void onMessage(String message) { + if (debug) System.out.println(" " + json); + send(json); + } + + public void register(Class c, + String s, + Class m, + FullMessageHandler h) { + Message.register(m, classes.get(Message.class)); + classes.register(c, s, m); + if (h != null) + handlers.register(c, s, h); + } + + public void unregister(Class c, String s) { + handlers.unregister(c, s); + // Note that there is no concept of unregistering a class - it can get replaced is all + } + + public Class getRegisteredMessage(String messageString) { + return classes.lookup(Message.class, messageString); + } + + public void setDebug(boolean debug) { + this.debug = debug; + } +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/implementation/Registry.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/implementation/Registry.java new file mode 100644 index 0000000..4747e6d --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/implementation/Registry.java @@ -0,0 +1,49 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosbridge.implementation; + +import java.util.HashMap; +import java.util.Map; + +public class Registry extends HashMap> { + + public void register(Class c, String s, T t) { + Map table = get(c); + if (table == null) { + table = new HashMap(); + put(c, table); + } + table.put(s, t); + } + + public void unregister(Class c, String s) { + Map table = get(c); + if (table != null) + table.remove(s); + } + + public T lookup(Class c, String s) { + T result = null; + Map table = get(c); + if (table != null) + result = table.get(s); + return result; + } +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/indication/AsArray.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/indication/AsArray.java new file mode 100644 index 0000000..f411ec4 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/indication/AsArray.java @@ -0,0 +1,30 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosbridge.indication; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface AsArray { +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/indication/Base64Encoded.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/indication/Base64Encoded.java new file mode 100644 index 0000000..5e5e04d --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/indication/Base64Encoded.java @@ -0,0 +1,30 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosbridge.indication; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface Base64Encoded { +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/indication/Indicate.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/indication/Indicate.java new file mode 100644 index 0000000..3827ae4 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/indication/Indicate.java @@ -0,0 +1,31 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosbridge.indication; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface Indicate { + // if later we want multiple indicated fields, use an int here +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/indication/Indicated.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/indication/Indicated.java new file mode 100644 index 0000000..f0edc8e --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/indication/Indicated.java @@ -0,0 +1,31 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosbridge.indication; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface Indicated { + // if later we want multiple indicated fields, use an int here +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/indication/Indication.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/indication/Indication.java new file mode 100644 index 0000000..f7c69b7 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/indication/Indication.java @@ -0,0 +1,83 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosbridge.indication; + +import java.lang.reflect.Field; + +public class Indication { + public static boolean isIndicated(Field f) { + return (f.getAnnotation(Indicated.class) != null); + } + + public static boolean asArray(Field f) { + return (f.getAnnotation(AsArray.class) != null); + } + + public static boolean isBase64Encoded(Field f) { + return ((f.getAnnotation(Base64Encoded.class) != null) && + f.getType().isArray() && + f.getType().getComponentType().equals(byte.class)); + } + + public static String getIndicatorName(Class c) { + return getName(c, Indicator.class); + } + + public static String getIndicatedName(Class c) { + return getName(c, Indicated.class); + } + + private static String getName(Class c, Class annotation) { + String result = null; + for (Field f : c.getFields()) { + if (f.getAnnotation(annotation) != null) { + result = f.getName(); + break; + } + } + return result; + } + + /* + public static Class getIndication(Object o, String s) { + Class c = o.getClass(); + Class result = null; + try { + Method m = getIndicateMethod(c); + result = (Class) (m.invoke(o, s)); + } + catch (ReflectiveOperationException ex) { + ex.printStackTrace(); + } + return result; + } + + private static Method getIndicateMethod(Class c) { + Method result = null; + for (Method m : c.getMethods()) { + if (m.getAnnotation(Indicate.class) != null) { + result = m; + break; + } + } + return result; + } + */ +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/indication/Indicator.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/indication/Indicator.java new file mode 100644 index 0000000..d1d2ff9 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/indication/Indicator.java @@ -0,0 +1,31 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosbridge.indication; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface Indicator { + // if later we want multiple indicated fields, use an int here +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Advertise.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Advertise.java new file mode 100644 index 0000000..f41506e --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Advertise.java @@ -0,0 +1,35 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosbridge.operation; + +import com.jilk.ros.message.MessageType; + +@MessageType(string = "advertise") +public class Advertise extends Operation { + public String topic; + public String type; + + public Advertise() {} + + public Advertise(String topic, String type) { + this.topic = topic; + this.type = type; + } +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Authenticate.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Authenticate.java new file mode 100644 index 0000000..517e360 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Authenticate.java @@ -0,0 +1,56 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosbridge.operation; + +import com.jilk.ros.message.MessageType; + +@MessageType(string = "auth") +public class Authenticate extends Operation { + public String mac; + public String client; + public String dest; + public String rand; + public int t; + public String level; + public int end; + + public Authenticate() {} + + public Authenticate( + String mac, + String client, + String dest, + String rand, + int t, + String level, + int end) + { + this.mac = mac; + this.client = client; + this.dest = dest; + this.rand = rand; + this.t = t; + this.level = level; + this.end = end; + + this.id = null; // even though id is on EVERY OTHER operation type + } + +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/CallService.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/CallService.java new file mode 100644 index 0000000..fb9d6fa --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/CallService.java @@ -0,0 +1,41 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosbridge.operation; + +import com.jilk.ros.message.Message; +import com.jilk.ros.message.MessageType; +import com.jilk.ros.rosbridge.indication.AsArray; +import com.jilk.ros.rosbridge.indication.Indicated; +import com.jilk.ros.rosbridge.indication.Indicator; + +@MessageType(string = "call_service") +public class CallService extends Operation { + @Indicator public String service; + @Indicated @AsArray public Message args; + public Integer fragment_size; // use Integer for optional items + public String compression; + + public CallService() {} + + public CallService(String service, Message args) { + this.service = service; + this.args = args; + } +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Fragment.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Fragment.java new file mode 100644 index 0000000..0093eae --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Fragment.java @@ -0,0 +1,37 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosbridge.operation; + +import com.jilk.ros.message.MessageType; + +@MessageType(string = "fragment") +public class Fragment extends Operation { + public String data; + public int num; + public int total; + + public Fragment() {} + + public Fragment(String data, int num, int total) { + this.data = data; + this.num = num; + this.total = total; + } +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Operation.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Operation.java new file mode 100644 index 0000000..98d4db3 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Operation.java @@ -0,0 +1,77 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosbridge.operation; + +import com.jilk.ros.message.Message; +import com.jilk.ros.message.MessageType; +import com.jilk.ros.rosbridge.implementation.JSON; +import com.jilk.ros.rosbridge.implementation.Registry; + +@MessageType(string = "operation") +public class Operation extends Message { + private static Long uid = 0L; + + public String op; + public String id; + + public Operation() { + this.op = getMessageType(getClass()); + this.id = nextId(); + } + + private static synchronized String nextId() { + String result = uid.toString(); + uid++; + return result; + } + + public String toJSON() { + return JSON.toJSON(this); + } + + public static Operation toOperation(String json, Registry registry) { + return ((Wrapper) JSON.toMessage(json, Wrapper.class, registry)).msg; + } + + public static void initialize(Registry registry) { + initClass(registry, Advertise.class); + initClass(registry, Authenticate.class); + initClass(registry, CallService.class); + initClass(registry, Fragment.class); + initClass(registry, Operation.class); + initClass(registry, PNG.class); + initClass(registry, Publish.class); + initClass(registry, ServiceResponse.class); + initClass(registry, SetStatusLevel.class); + initClass(registry, Status.class); + initClass(registry, Subscribe.class); + initClass(registry, Unadvertise.class); + initClass(registry, Unsubscribe.class); + initClass(registry, Wrapper.class); + + registry.register(Wrapper.class, Message.getMessageType(Publish.class), Publish.class); + registry.register(Wrapper.class, Message.getMessageType(CallService.class), CallService.class); + registry.register(Wrapper.class, Message.getMessageType(ServiceResponse.class), ServiceResponse.class); + } + + private static void initClass(Registry registry, Class c) { + registry.register(Message.class, Message.getMessageType(c), c); + } +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/PNG.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/PNG.java new file mode 100644 index 0000000..7cbda69 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/PNG.java @@ -0,0 +1,41 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosbridge.operation; + +import com.jilk.ros.message.MessageType; + +@MessageType(string = "png") +public class PNG extends Operation { + public String data; + public Integer num; // use Integer for optional items + public Integer total; // use Integer for optional items + + public PNG() {} + + public PNG(String data) { + this.data = data; + } + + public PNG(String data, int num, int total) { + this.data = data; + this.num = num; + this.total = total; + } +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Publish.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Publish.java new file mode 100644 index 0000000..8acf725 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Publish.java @@ -0,0 +1,40 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosbridge.operation; + +import com.jilk.ros.message.Message; +import com.jilk.ros.message.MessageType; +import com.jilk.ros.rosbridge.indication.Indicated; +import com.jilk.ros.rosbridge.indication.Indicator; + +@MessageType(string = "publish") +public class Publish extends Operation { + + @Indicator public String topic; + @Indicated + public Message msg; + + public Publish() {} + + public Publish(String topic, Message msg) { + this.topic = topic; + this.msg = msg; + } +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/ServiceResponse.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/ServiceResponse.java new file mode 100644 index 0000000..2d7384b --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/ServiceResponse.java @@ -0,0 +1,38 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosbridge.operation; + +import com.jilk.ros.message.Message; +import com.jilk.ros.message.MessageType; +import com.jilk.ros.rosbridge.indication.Indicated; +import com.jilk.ros.rosbridge.indication.Indicator; + +@MessageType(string = "service_response") +public class ServiceResponse extends Operation { + @Indicator public String service; + public boolean result; + @Indicated public Message values; + + public ServiceResponse() {} + + public ServiceResponse(String service) { + this.service = service; + } +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/SetStatusLevel.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/SetStatusLevel.java new file mode 100644 index 0000000..bf7a16a --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/SetStatusLevel.java @@ -0,0 +1,38 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosbridge.operation; + +import com.jilk.ros.message.MessageType; + +@MessageType(string = "set_level") +public class SetStatusLevel extends Operation { + public String level; + + public SetStatusLevel() {} + + public SetStatusLevel(String level) { + this.level = null; + if ("none".equals(level) || + "warning".equals(level) || + "error".equals(level) || + "info".equals(level)) + this.level = level; + } +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Status.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Status.java new file mode 100644 index 0000000..051a48a --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Status.java @@ -0,0 +1,35 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosbridge.operation; + +import com.jilk.ros.message.MessageType; + +@MessageType(string = "status") +public class Status extends Operation { + String level; + String msg; + + public Status() {} + + public Status(String level, String msg) { + this.level = level; + this.msg = msg; + } +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Subscribe.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Subscribe.java new file mode 100644 index 0000000..a965ced --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Subscribe.java @@ -0,0 +1,39 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosbridge.operation; + +import com.jilk.ros.message.MessageType; + +@MessageType(string = "subscribe") +public class Subscribe extends Operation { + public String topic; + public String type; + public Integer throttle_rate; // use Integer for optional items + public Integer queue_length; // use Integer for optional items + public Integer fragment_size; // use Integer for optional items + public String compression; + + public Subscribe() {} + + public Subscribe(String topic, String type) { + this.topic = topic; + this.type = type; + } +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Unadvertise.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Unadvertise.java new file mode 100644 index 0000000..0c5d2f0 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Unadvertise.java @@ -0,0 +1,33 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosbridge.operation; + +import com.jilk.ros.message.MessageType; + +@MessageType(string = "unadvertise") +public class Unadvertise extends Operation { + public String topic; + + public Unadvertise() {} + + public Unadvertise(String topic) { + this.topic = topic; + } +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Unsubscribe.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Unsubscribe.java new file mode 100644 index 0000000..94d2810 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Unsubscribe.java @@ -0,0 +1,33 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosbridge.operation; + +import com.jilk.ros.message.MessageType; + +@MessageType(string = "unsubscribe") +public class Unsubscribe extends Operation { + public String topic; + + public Unsubscribe() {} + + public Unsubscribe(String topic) { + this.topic = topic; + } +} diff --git a/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Wrapper.java b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Wrapper.java new file mode 100644 index 0000000..c14b3d2 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/java/com/jilk/ros/rosbridge/operation/Wrapper.java @@ -0,0 +1,33 @@ +/** + * Copyright (c) 2014 Jilk Systems, Inc. + * + * This file is part of the Java ROSBridge Client. + * + * The Java ROSBridge Client is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Java ROSBridge Client is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. + * + */ +package com.jilk.ros.rosbridge.operation; + +import com.jilk.ros.message.MessageType; +import com.jilk.ros.rosbridge.indication.Indicated; +import com.jilk.ros.rosbridge.indication.Indicator; + +@MessageType(string = "wrapper") +public class Wrapper extends Operation { + @Indicator public String op; + @Indicated + public Operation msg; + + public Wrapper() {} +} diff --git a/src/AndroidStudio_projects/app/src/main/jni/native-test.c b/src/AndroidStudio_projects/app/src/main/jni/native-test.c new file mode 100644 index 0000000..d375cf9 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/jni/native-test.c @@ -0,0 +1,9 @@ +#include + +JNIEXPORT jstring JNICALL +Java_com_example_dadac_testrosbridge_JNICallAll_stringFromJNI(JNIEnv *env, jobject instance) { + + char *a = "wen"; + + return (*env)->NewStringUTF(env, a); +} \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/src/AndroidStudio_projects/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/res/drawable/hotel_logo.jpg b/src/AndroidStudio_projects/app/src/main/res/drawable/hotel_logo.jpg new file mode 100644 index 0000000..a9a08d8 Binary files /dev/null and b/src/AndroidStudio_projects/app/src/main/res/drawable/hotel_logo.jpg differ diff --git a/src/AndroidStudio_projects/app/src/main/res/drawable/ic_dashboard_black_24dp.xml b/src/AndroidStudio_projects/app/src/main/res/drawable/ic_dashboard_black_24dp.xml new file mode 100644 index 0000000..46fc8de --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/res/drawable/ic_dashboard_black_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/src/AndroidStudio_projects/app/src/main/res/drawable/ic_home_black_24dp.xml b/src/AndroidStudio_projects/app/src/main/res/drawable/ic_home_black_24dp.xml new file mode 100644 index 0000000..f8bb0b5 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/res/drawable/ic_home_black_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/src/AndroidStudio_projects/app/src/main/res/drawable/ic_launcher_background.xml b/src/AndroidStudio_projects/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/AndroidStudio_projects/app/src/main/res/drawable/ic_notifications_black_24dp.xml b/src/AndroidStudio_projects/app/src/main/res/drawable/ic_notifications_black_24dp.xml new file mode 100644 index 0000000..78b75c3 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/res/drawable/ic_notifications_black_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/src/AndroidStudio_projects/app/src/main/res/layout/activity_add.xml b/src/AndroidStudio_projects/app/src/main/res/layout/activity_add.xml new file mode 100644 index 0000000..1ecaa59 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/res/layout/activity_add.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/res/layout/main_window.xml b/src/AndroidStudio_projects/app/src/main/res/layout/main_window.xml new file mode 100644 index 0000000..fee78aa --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/res/layout/main_window.xml @@ -0,0 +1,24 @@ + + + + + + + \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/res/menu/bottom_nav_menu.xml b/src/AndroidStudio_projects/app/src/main/res/menu/bottom_nav_menu.xml new file mode 100644 index 0000000..fb6d040 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/res/menu/bottom_nav_menu.xml @@ -0,0 +1,19 @@ + +

+ + + + + + + + \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/src/AndroidStudio_projects/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml new file mode 100644 index 0000000..eca70cf --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/src/AndroidStudio_projects/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml new file mode 100644 index 0000000..eca70cf --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/res/mipmap-hdpi/ic_launcher.webp b/src/AndroidStudio_projects/app/src/main/res/mipmap-hdpi/ic_launcher.webp new file mode 100644 index 0000000..c209e78 Binary files /dev/null and b/src/AndroidStudio_projects/app/src/main/res/mipmap-hdpi/ic_launcher.webp differ diff --git a/src/AndroidStudio_projects/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp b/src/AndroidStudio_projects/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp new file mode 100644 index 0000000..b2dfe3d Binary files /dev/null and b/src/AndroidStudio_projects/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp differ diff --git a/src/AndroidStudio_projects/app/src/main/res/mipmap-mdpi/ic_launcher.webp b/src/AndroidStudio_projects/app/src/main/res/mipmap-mdpi/ic_launcher.webp new file mode 100644 index 0000000..4f0f1d6 Binary files /dev/null and b/src/AndroidStudio_projects/app/src/main/res/mipmap-mdpi/ic_launcher.webp differ diff --git a/src/AndroidStudio_projects/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp b/src/AndroidStudio_projects/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp new file mode 100644 index 0000000..62b611d Binary files /dev/null and b/src/AndroidStudio_projects/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp differ diff --git a/src/AndroidStudio_projects/app/src/main/res/mipmap-xhdpi/ic_launcher.webp b/src/AndroidStudio_projects/app/src/main/res/mipmap-xhdpi/ic_launcher.webp new file mode 100644 index 0000000..948a307 Binary files /dev/null and b/src/AndroidStudio_projects/app/src/main/res/mipmap-xhdpi/ic_launcher.webp differ diff --git a/src/AndroidStudio_projects/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp b/src/AndroidStudio_projects/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..1b9a695 Binary files /dev/null and b/src/AndroidStudio_projects/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp differ diff --git a/src/AndroidStudio_projects/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp b/src/AndroidStudio_projects/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp new file mode 100644 index 0000000..28d4b77 Binary files /dev/null and b/src/AndroidStudio_projects/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp differ diff --git a/src/AndroidStudio_projects/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp b/src/AndroidStudio_projects/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..9287f50 Binary files /dev/null and b/src/AndroidStudio_projects/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp differ diff --git a/src/AndroidStudio_projects/app/src/main/res/mipmap-xxxhdpi/_1.jpg b/src/AndroidStudio_projects/app/src/main/res/mipmap-xxxhdpi/_1.jpg new file mode 100644 index 0000000..906e971 Binary files /dev/null and b/src/AndroidStudio_projects/app/src/main/res/mipmap-xxxhdpi/_1.jpg differ diff --git a/src/AndroidStudio_projects/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp b/src/AndroidStudio_projects/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp new file mode 100644 index 0000000..aa7d642 Binary files /dev/null and b/src/AndroidStudio_projects/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp differ diff --git a/src/AndroidStudio_projects/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp b/src/AndroidStudio_projects/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..9126ae3 Binary files /dev/null and b/src/AndroidStudio_projects/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp differ diff --git a/src/AndroidStudio_projects/app/src/main/res/mipmap-xxxhdpi/logo.jpg b/src/AndroidStudio_projects/app/src/main/res/mipmap-xxxhdpi/logo.jpg new file mode 100644 index 0000000..65ca6fd Binary files /dev/null and b/src/AndroidStudio_projects/app/src/main/res/mipmap-xxxhdpi/logo.jpg differ diff --git a/src/AndroidStudio_projects/app/src/main/res/navigation/mobile_navigation.xml b/src/AndroidStudio_projects/app/src/main/res/navigation/mobile_navigation.xml new file mode 100644 index 0000000..1b30025 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/res/navigation/mobile_navigation.xml @@ -0,0 +1,25 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/res/values-night/themes.xml b/src/AndroidStudio_projects/app/src/main/res/values-night/themes.xml new file mode 100644 index 0000000..f4c8632 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/res/values-night/themes.xml @@ -0,0 +1,16 @@ + + + + \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/res/values/colors.xml b/src/AndroidStudio_projects/app/src/main/res/values/colors.xml new file mode 100644 index 0000000..f8c6127 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/res/values/colors.xml @@ -0,0 +1,10 @@ + + + #FFBB86FC + #FF6200EE + #FF3700B3 + #FF03DAC5 + #FF018786 + #FF000000 + #FFFFFFFF + \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/res/values/dimens.xml b/src/AndroidStudio_projects/app/src/main/res/values/dimens.xml new file mode 100644 index 0000000..e00c2dd --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/res/values/dimens.xml @@ -0,0 +1,5 @@ + + + 16dp + 16dp + \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/res/values/strings.xml b/src/AndroidStudio_projects/app/src/main/res/values/strings.xml new file mode 100644 index 0000000..5157a57 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/res/values/strings.xml @@ -0,0 +1,7 @@ + + 酒店智慧管理系统 + MainActivity2 + 个人中心 + 物品需求 + 公告栏 + \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/res/values/themes.xml b/src/AndroidStudio_projects/app/src/main/res/values/themes.xml new file mode 100644 index 0000000..f946c10 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/res/values/themes.xml @@ -0,0 +1,16 @@ + + + + \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/res/xml/backup_rules.xml b/src/AndroidStudio_projects/app/src/main/res/xml/backup_rules.xml new file mode 100644 index 0000000..fa0f996 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/res/xml/backup_rules.xml @@ -0,0 +1,13 @@ + + + + \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/main/res/xml/data_extraction_rules.xml b/src/AndroidStudio_projects/app/src/main/res/xml/data_extraction_rules.xml new file mode 100644 index 0000000..9ee9997 --- /dev/null +++ b/src/AndroidStudio_projects/app/src/main/res/xml/data_extraction_rules.xml @@ -0,0 +1,19 @@ + + + + + + + \ No newline at end of file diff --git a/src/AndroidStudio_projects/app/src/test/java/com/example/hamburger/ExampleUnitTest.java b/src/AndroidStudio_projects/app/src/test/java/com/example/hamburger/ExampleUnitTest.java new file mode 100644 index 0000000..3822adf --- /dev/null +++ b/src/AndroidStudio_projects/app/src/test/java/com/example/hamburger/ExampleUnitTest.java @@ -0,0 +1,17 @@ +package com.example.hamburger; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Example local unit test, which will execute on the development machine (host). + * + * @see Testing documentation + */ +public class ExampleUnitTest { + @Test + public void addition_isCorrect() { + assertEquals(4, 2 + 2); + } +} \ No newline at end of file diff --git a/src/AndroidStudio_projects/build.gradle b/src/AndroidStudio_projects/build.gradle new file mode 100644 index 0000000..71b99f4 --- /dev/null +++ b/src/AndroidStudio_projects/build.gradle @@ -0,0 +1,5 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. +plugins { + id 'com.android.application' version '7.3.1' apply false + id 'com.android.library' version '7.3.1' apply false +} \ No newline at end of file diff --git a/src/AndroidStudio_projects/gradle.properties b/src/AndroidStudio_projects/gradle.properties new file mode 100644 index 0000000..15507e8 --- /dev/null +++ b/src/AndroidStudio_projects/gradle.properties @@ -0,0 +1,17 @@ +## For more details on how to configure your build environment visit +# http://www.gradle.org/docs/current/userguide/build_environment.html +# +# Specifies the JVM arguments used for the daemon process. +# The setting is particularly useful for tweaking memory settings. +# Default value: -Xmx1024m -XX:MaxPermSize=256m +# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 +# +# When configured, Gradle will run in incubating parallel mode. +# This option should only be used with decoupled projects. More details, visit +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects +# org.gradle.parallel=true +#Sun Nov 06 12:18:57 CST 2022 +android.nonTransitiveRClass=true +org.gradle.jvmargs=-Xmx2048m -Dfile.encoding\=UTF-8 +android.useAndroidX=true +android.enableJetifier=true diff --git a/src/AndroidStudio_projects/gradle/wrapper/gradle-wrapper.jar b/src/AndroidStudio_projects/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..e708b1c Binary files /dev/null and b/src/AndroidStudio_projects/gradle/wrapper/gradle-wrapper.jar differ diff --git a/src/AndroidStudio_projects/gradle/wrapper/gradle-wrapper.properties b/src/AndroidStudio_projects/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..7697d30 --- /dev/null +++ b/src/AndroidStudio_projects/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Tue Oct 18 19:22:46 CST 2022 +distributionBase=GRADLE_USER_HOME +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip +distributionPath=wrapper/dists +zipStorePath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME diff --git a/src/AndroidStudio_projects/gradlew b/src/AndroidStudio_projects/gradlew new file mode 100644 index 0000000..4f906e0 --- /dev/null +++ b/src/AndroidStudio_projects/gradlew @@ -0,0 +1,185 @@ +#!/usr/bin/env sh + +# +# Copyright 2015 the original author or authors. +# +# Licensed 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 +# +# https://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. +# + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=`expr $i + 1` + done + case $i in + 0) set -- ;; + 1) set -- "$args0" ;; + 2) set -- "$args0" "$args1" ;; + 3) set -- "$args0" "$args1" "$args2" ;; + 4) set -- "$args0" "$args1" "$args2" "$args3" ;; + 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=`save "$@"` + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +exec "$JAVACMD" "$@" diff --git a/src/AndroidStudio_projects/gradlew.bat b/src/AndroidStudio_projects/gradlew.bat new file mode 100644 index 0000000..107acd3 --- /dev/null +++ b/src/AndroidStudio_projects/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/src/AndroidStudio_projects/settings.gradle b/src/AndroidStudio_projects/settings.gradle new file mode 100644 index 0000000..19e7d24 --- /dev/null +++ b/src/AndroidStudio_projects/settings.gradle @@ -0,0 +1,16 @@ +pluginManagement { + repositories { + gradlePluginPortal() + google() + mavenCentral() + } +} +dependencyResolutionManagement { + repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) + repositories { + google() + mavenCentral() + } +} +rootProject.name = "Hamburger" +include ':app' diff --git a/test.txt b/test.txt deleted file mode 100644 index eeaf50c..0000000 --- a/test.txt +++ /dev/null @@ -1,260 +0,0 @@ -冒泡排序: -for(int i=0;iarr[j+1]) - { - int temp = arr[j]; - arr[j]=arr[j+1]; - arr[j+1]=temp; - } - } -} ------------------------------------------------------------------------ -函数高级 -函数的默认参数: -1如果某个位置参数有默认值,那么从这个位置往后,从左向右,必须要有默认值 -2如果函数声明有默认值,函数实现的时候就不能有默认参数 - -函数的占位参数: -语法:返回值类型 函数名(数据类型) - ---------------------------------------------------- -结构体作为函数参数: -将结构体作为函数作为参数向函数中传递 -传递方式有两种: -值传递:形参不会改变实参 -地址传递:会改变实参 -总结:如果不想修改主函数中的数据,用值传递,反之用地址传递 -结构体中const的使用场景: ------------------------------------------------ -1 不带头结点的头插入法创建链表: -LinkList LinkListCreate(const int n) -{ - int i; - LinkList head; - Node *p; - head = NULL; - for(;idata); - p->next = head; - head = p; - - } -} ---------------------- -不带头结点的尾插入法创建链表: -LinkList LinkListCreate(const int n) -{ - int n= 0; - - //创建一个一直指向头结点,一直指向尾结点的指针。 - LinkList head; - Node *p,*rear; - rear = head = NULL; - for(;idata); - - if(NULL == head) head=p;//创建的第一个结点。 - else rear->next = p;//链表非空 - - - rear = p;//移动尾指针 - } -   rear->next = NULL;//最后将链表的结尾设置为NULL。 -} ------------------------------------------------------------- -3 创建带结点的头插入链表。 -1 LinkList LinkListCreate(const int n) - 2 { - 3 int i = 0;LinkList p; - 4 LinkList head = (Node*)malloc(sizeof(Node)); - 5 head->next = NULL; - 6 - 7 for(;idata); -11 p->next = head->next; -12 head->next = p; -13 } -14 -15 return head; -16 } ------------------------------------------------------------------ - -4 带头结点的尾插入法创建链表。 -LinkList LinkListCreate(const int n) -{ - int i = 0;Node *p,*rear; - LinkList head = (Node*)malloc(sizeof(Node)); - rear = head; - - for(;idata); - - rear->next = p; //尾指针 指向新建结点 - rear = p;//rear指针移动 - } - rear->next = NULL; - - return head; -} -vector容器 -头文件 -向vector中插入数据 -v.push_back( ); -通过迭代器访问容器中的数据 -vector<数据类型>::iterator itBrgin = v.brgin();其实迭代器指向容器中第一个元素 -vector<数据类型>::iterator itEnd = v.end();结束迭代器指向容器中最后一个元素的下一个位置 - -STL提供的for_each(起始迭代器,结束迭代器,函数名) - -vector容器中存放自定义数据类型 -class person -{ - 数据 -} -vector -vectorv1; -vectorv2; -vectorv3; - -string构造函数 -string() 创建一个空的字符串 例如:string str -string(const char* s); 使用字符串s初始化 -string(const string& str) 使用一个string对象初始化另一个stringduixiang -string(int n,char c) 使用n个字符c初始化 - -纯虚函数:virtual 函数类型 函数名 ( )= 0; -纯虚函数 -类中只要有一个纯虚函数就是抽象类 -抽象类不能创建对象 -抽象类的子类要重写抽象类中的纯虚函数才能创建对象,否则他也是属于抽象类 -虚析构和纯虚析构 - 共性: - 可以解决父类指针释放子类对象 - 都要有具体的函数实现 -区别: - 纯虚析构无法构建对象 -纯虚析构需要声明(在类内)也需要实现(类外 ) - - - - -一,操作符重载 -实质是利用已有的c++中的关键字operator(操作符)来重写 操作符 -1.成员函数重载加号 -本质调用: -Person p3 =p1.operator+(p2) -2.全局函数重载加号 -数据类型 operator+(数据类型 &p1,数据类型 &p2) -本质调用:Person p3 = p1+p2 -运算符重载也可以发生函数重载 -重载的操作符相同 传入的参数不同 - -二,左移运算符重载 -成员函数重载 -数据类型 operator<<(cout) 简化形式 p< -template ---声明创建模板 typename ---表明其后面的符号是一种数据类型,可以 用class代替 -T---通用的数据类型,名称可以替换,通常为大写字母 -两种方式使用函数模板 -1.自动类型推导 -2.显示指定类型 -函数名<例如:int >() - --函数名模板关键字template --使用函数模板有两种方式:自动类型推导、显式指定类型 --模板的目的是为了提高复用性,将类型参数化 - - -函数模板注意事项 -1.自动类型推导,必须推导出一致的数据类型T才可以使用 -2.模板必须要确定出T的数据类型,才可以使用 - -普通函数和函数模板的区别 -1、普通函数调用可以发生隐式类型转换 -2、函数模板 用自动类型推导,不可以发生隐式类型转换 -3、 用显示指定类型,可以 - -总结:建议使用指定类型的方式,调用函数模板,因为可以自己确定通用类型T ------------------------------------------------------------------------------------- -文件操作 -程序运行时产生的数据都属于临时数据,程序一旦运行结束都会被释放 -通过文件可以将数据持久化 -c++中对文件操作需要包含头文件 -文件类型分为两种 -1文本文件 -文件以文本的ASCII码形式存储在计算机中 -2二进制文件 -文件以文本的二进制形式存储在计算机中,用户一般不能直接读懂他们 - -操作文件的三大类: -1ofstream:写操作 -2ifstream:读操作 -3fstream:读写操作 - -文本文件 写文件 -步骤如下 -1包含头文件#include -2创建流对象 ofstream ofs; -3打开文件 ofs.open("文件路径",打开方式) -4写数据 ofs<<"写入的数据" - --读文件可以利用ifstream,或者fstream --利用is_open函数可以判断文件是否打开成功 --close关闭文件 - - -二进制文件 写文件 -1包含头文件#include -2创建流对象ofstream ofs("文件路径",ios::out|ios::binary) -3打开文件ofs.open("person.txt",ios::out|ios::binary) -4写文件 代码 -5关闭文件 ofs.close() -二进制文件 读文件 -1包含头文件 -2创建流对象 -3打开文件 并判断文件是否打开成功 -ifs.open("文件路径",ios::in|ios::binary) -if(!ifs.open_()){cout<<"文件打开失败"<