Go to the documentation of this file.00001 package com.github.rosjava.android_remocons.rocon_remocon;
00002
00003 import android.util.Log;
00004
00005 import com.google.common.base.Preconditions;
00006
00007 import org.ros.namespace.GraphName;
00008 import org.ros.node.ConnectedNode;
00009 import org.ros.node.Node;
00010 import org.ros.node.NodeMain;
00011 import org.ros.node.topic.Publisher;
00012
00013 import java.util.UUID;
00014
00015 import rocon_interaction_msgs.RemoconStatus;
00016 import rocon_std_msgs.Strings;
00017
00018 import static com.github.rosjava.android_remocons.common_tools.rocon.Constants.ANDROID_PLATFORM_INFO;
00019
00026 public class StatusPublisher implements NodeMain {
00027 public static final String NODE_NAME = "remocon_status_pub_node";
00028
00029 private static final String REMOCON_NAME = "android_remocon";
00030 private static final String REMOCON_UUID = UUID.randomUUID().toString().replaceAll("-", "");
00031 public static final String REMOCON_FULL_NAME = REMOCON_NAME+"_"+REMOCON_UUID;
00032 public static final String ROCON_VERSION = Strings.ROCON_VERSION;
00033
00034 private static StatusPublisher instance;
00035
00036 private Publisher<RemoconStatus> publisher;
00037
00038 private boolean initialized = false;
00039 private RemoconStatus status;
00040
00041 private StatusPublisher() {}
00042
00043 public static StatusPublisher getInstance() {
00044 if (instance == null) {
00045 instance = new StatusPublisher();
00046 Log.d("RoconRemocon", "Remocon status publisher created");
00047 }
00048
00049 return instance;
00050 }
00051
00052 public boolean isInitialized() {
00053 return initialized;
00054 }
00055
00056 @Override
00057 public GraphName getDefaultNodeName() {
00058 return GraphName.of(NODE_NAME);
00059 }
00060
00061 @Override
00062 public void onStart(ConnectedNode connectedNode) {
00063 Preconditions.checkArgument(! initialized, "Remocon status publisher already initialized");
00064
00065
00066 publisher = connectedNode.newPublisher("/remocons/" + REMOCON_NAME + "_" + REMOCON_UUID,
00067 rocon_interaction_msgs.RemoconStatus._TYPE);
00068 publisher.setLatchMode(true);
00069 int[] running_interactions = new int[0];
00070
00071
00072 status = publisher.newMessage();
00073 status.setPlatformInfo(ANDROID_PLATFORM_INFO);
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 status.setVersion(ROCON_VERSION);
00085 status.setUuid(REMOCON_UUID);
00086 status.setRunningInteractions(running_interactions);
00087
00088
00089
00090 publisher.publish(status);
00091
00092 initialized = true;
00093 Log.i("Remocon", "Remocon status publisher initialized");
00094 }
00095
00096 @Override
00097 public void onShutdown(Node node) {
00098 Preconditions.checkArgument(initialized, "Remocon status publisher not initialized");
00099
00100 publisher.shutdown();
00101 initialized = false;
00102 Log.i("Remocon", "Remocon status publisher shutdown");
00103 }
00104
00105 @Override
00106 public void onShutdownComplete(Node node) {
00107 }
00108
00109 @Override
00110 public void onError(Node node, Throwable throwable) {
00111 Log.e("Remocon", "Remocon status publisher error: " + throwable.getMessage());
00112 }
00113
00114 public void update(boolean is_runnging, int runningApp_hash, String appName) {
00115
00116 int[] running_interactions = null;
00117 if (is_runnging) {
00118 Log.i("Remocon", "Remocon status publisher updated. Running "+appName+": " + runningApp_hash);
00119 running_interactions = new int[1];
00120 running_interactions[0] = runningApp_hash;
00121 status.setRunningInteractions(running_interactions);
00122 }
00123 else{
00124 Log.i("Remocon", "Remocon status publisher updated. Fail running "+appName+": " + runningApp_hash);
00125 running_interactions = new int[0];
00126 status.setRunningInteractions(running_interactions);
00127 }
00128 publisher.publish(status);
00129 }
00130 }