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 
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                     // TODO: set color red/yellow/green based on level (maybe with
00110                     // level-set
00111                     // in XML)
00112             } catch(NumberFormatException ex) {
00113                     // TODO: make battery level gray
00114             } catch(ArithmeticException ex) {
00115                     // TODO: make battery level gray
00116             } catch(NullPointerException ex) {
00117                     // Do nothing: data wasn't there.
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                                         // TODO Auto-generated catch block
00168                                         e.printStackTrace();
00169                                 }
00170         }
00171         }
00172 }


android_apps
Author(s): Daniel Stonier , Kazuto Murase
autogenerated on Fri Aug 28 2015 10:04:40