PairSubscriber.java
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.message.MessageListener;
00008 import org.ros.namespace.GraphName;
00009 import org.ros.node.ConnectedNode;
00010 import org.ros.node.Node;
00011 import org.ros.node.NodeMain;
00012 import org.ros.node.topic.Subscriber;
00013 
00014 import rocon_interaction_msgs.Pair;
00015 
00022 public class PairSubscriber implements NodeMain {
00023     public  static final String NODE_NAME = "remocon_pair_subscriber_node";
00024 
00025     private static PairSubscriber instance = null;
00026 
00027     private Subscriber<Pair> pair_subsciber;
00028 
00029     private boolean initialized = false;
00030     private int paired_app_hash;
00031     StatusPublisher statusPublisher = StatusPublisher.getInstance();
00032 
00033 
00034     private PairSubscriber() {}
00035 
00036     public static PairSubscriber getInstance() {
00037         if (instance == null) {
00038             instance = new PairSubscriber();
00039             Log.i("Remocon", "Remocon pair subscriber created");
00040         }
00041 
00042         return instance;
00043     }
00044 
00045     public boolean isInitialized() {
00046         return initialized;
00047     }
00048     public void setAppHash(int app_hash){
00049         paired_app_hash = app_hash;
00050     }
00051 
00052     @Override
00053     public GraphName getDefaultNodeName() {
00054         return GraphName.of(NODE_NAME);
00055     }
00056 
00057     @Override
00058     public void onStart(ConnectedNode connectedNode) {
00059         Preconditions.checkArgument(! initialized, "Remocon pair subscriber already initialized");
00060 
00061         paired_app_hash = 0;
00062         pair_subsciber =
00063                 connectedNode.newSubscriber("interactions/pairing",Pair._TYPE);
00064 
00065         pair_subsciber.addMessageListener(new MessageListener<Pair>() {
00066             @Override
00067             public void onNewMessage(Pair pair) {
00068                 if (paired_app_hash != 0){
00069                     if(pair.getRemocon().equals(statusPublisher.REMOCON_FULL_NAME) && pair.getRapp().equals("")){
00070                         paired_app_hash = 0;
00071                         statusPublisher.update(false,paired_app_hash, null);
00072                     }
00073                 }
00074             }
00075         });
00076 
00077         initialized = true;
00078         Log.i("Remocon", "Remocon pair subscriber initialized");
00079     }
00080 
00081     @Override
00082     public void onShutdown(Node node) {
00083         Preconditions.checkArgument(initialized, "Remocon pair subscriber not initialized");
00084         pair_subsciber.shutdown();
00085         initialized = false;
00086         Log.i("Remocon", "Remocon pair subscriber shutdown");
00087     }
00088 
00089     @Override
00090     public void onShutdownComplete(Node node) {
00091     }
00092 
00093     @Override
00094     public void onError(Node node, Throwable throwable) {
00095         Log.e("Remocon", "Remocon status publisher error: " + throwable.getMessage());
00096     }
00097 }


android_remocons
Author(s): Daniel Stonier, Kazuto Murase
autogenerated on Sat Jun 8 2019 19:32:24