DefaultDashboard.java
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright (c) 2011, Willow Garage, Inc.
00005  * All rights reserved.
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  *  * Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  *  * Redistributions in binary form must reproduce the above
00013  *    copyright notice, this list of conditions and the following
00014  *    disclaimer in the documentation and/or other materials provided
00015  *    with the distribution.
00016  *  * Neither the name of Willow Garage, Inc. nor the names of its
00017  *    contributors may be used to endorse or promote products derived
00018  *    from this software without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031  * POSSIBILITY OF SUCH DAMAGE.
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                     // TODO: set color red/yellow/green based on level (maybe with
00108                     // level-set
00109                     // in XML)
00110             } catch(NumberFormatException ex) {
00111                     // TODO: make battery level gray
00112             } catch(ArithmeticException ex) {
00113                     // TODO: make battery level gray
00114             } catch(NullPointerException ex) {
00115                     // Do nothing: data wasn't there.
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                                         // TODO Auto-generated catch block
00169                                         e.printStackTrace();
00170                                 }
00171         }
00172         }
00173 }


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