Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 package com.github.rosjava.android_remocons.common_tools.dashboards;
00035
00036 import android.content.Context;
00037 import android.util.AttributeSet;
00038 import android.view.LayoutInflater;
00039 import android.widget.LinearLayout;
00040
00041 import com.github.rosjava.android_extras.gingerbread.view.BatteryLevelView;
00042 import com.github.rosjava.android_remocons.common_tools.R;
00043
00044 import org.ros.exception.RosException;
00045 import org.ros.message.MessageListener;
00046 import org.ros.namespace.GraphName;
00047 import org.ros.namespace.NameResolver;
00048 import org.ros.node.ConnectedNode;
00049 import org.ros.node.Node;
00050 import org.ros.node.topic.Subscriber;
00051
00052 import java.util.HashMap;
00053 import java.util.List;
00054
00055 import diagnostic_msgs.DiagnosticArray;
00056 import diagnostic_msgs.DiagnosticStatus;
00057 import diagnostic_msgs.KeyValue;
00058
00059 public class DefaultDashboard extends LinearLayout implements Dashboard.DashboardInterface {
00060 private BatteryLevelView robotBattery;
00061 private BatteryLevelView laptopBattery;
00062 private ConnectedNode connectedNode;
00063 private Subscriber<DiagnosticArray> diagnosticSubscriber;
00064 private boolean powerOn = false;
00065
00066 public DefaultDashboard(Context context) {
00067 super(context);
00068 inflateSelf(context);
00069 }
00070 public DefaultDashboard(Context context, AttributeSet attrs) {
00071 super(context, attrs);
00072 inflateSelf(context);
00073 }
00074 private void inflateSelf(Context context) {
00075 LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
00076 inflater.inflate(R.layout.default_dashboard, this);
00077 robotBattery = (BatteryLevelView) findViewById(R.id.robot_battery);
00078 laptopBattery = (BatteryLevelView) findViewById(R.id.laptop_battery);
00079 }
00090 private void handleDiagnosticArray(DiagnosticArray msg) {
00091 for(DiagnosticStatus status : msg.getStatus()) {
00092 if(status.getName().equals("/Power System/Battery")) {
00093 populateBatteryFromStatus(robotBattery, status);
00094 }
00095 if(status.getName().equals("/Power System/Laptop Battery")) {
00096 populateBatteryFromStatus(laptopBattery, status);
00097 }
00098 }
00099 }
00100
00101
00102 private void populateBatteryFromStatus(BatteryLevelView view, DiagnosticStatus status) {
00103 HashMap<String, String> values = keyValueArrayToMap(status.getValues());
00104 try {
00105 float percent = 100 * Float.parseFloat(values.get("Charge (Ah)")) / Float.parseFloat(values.get("Capacity (Ah)"));
00106 view.setBatteryPercent((int) percent);
00107
00108
00109
00110 } catch(NumberFormatException ex) {
00111
00112 } catch(ArithmeticException ex) {
00113
00114 } catch(NullPointerException ex) {
00115
00116 }
00117 try {
00118 view.setPluggedIn(Float.parseFloat(values.get("Current (A)")) > 0);
00119 } catch(NumberFormatException ex) {
00120 } catch(ArithmeticException ex) {
00121 } catch(NullPointerException ex) {
00122 }
00123 }
00124 private HashMap<String, String> keyValueArrayToMap(List<KeyValue> list) {
00125 HashMap<String, String> map = new HashMap<String, String>();
00126 for(KeyValue kv : list) {
00127 map.put(kv.getKey(), kv.getValue());
00128 }
00129 return map;
00130 }
00131
00132 @Override
00133 public void onShutdown(Node node) {
00134 if(diagnosticSubscriber != null) {
00135 diagnosticSubscriber.shutdown();
00136 }
00137 diagnosticSubscriber = null;
00138 connectedNode = null;
00139
00140 }
00141
00142 @Override
00143 public void onStart(ConnectedNode connectedNode) {
00144
00145 this.connectedNode = connectedNode;
00146 try {
00147 if(diagnosticSubscriber == null){
00148
00149 }
00150 diagnosticSubscriber = connectedNode.newSubscriber("diagnostics_agg", "diagnostic_msgs/DiagnosticArray");
00151 diagnosticSubscriber.addMessageListener(new MessageListener<DiagnosticArray>() {
00152 @Override
00153 public void onNewMessage(final DiagnosticArray message) {
00154 DefaultDashboard.this.post(new Runnable() {
00155 @Override
00156 public void run() {
00157 DefaultDashboard.this.handleDiagnosticArray(message);
00158 }
00159 });
00160 }
00161 });
00162 NameResolver resolver = connectedNode.getResolver().newChild(GraphName.of("/turtlebot_node"));
00163 } catch(Exception ex) {
00164 this.connectedNode = null;
00165 try {
00166 throw (new RosException(ex));
00167 } catch (RosException e) {
00168
00169 e.printStackTrace();
00170 }
00171 }
00172 }
00173 }