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