altos: Make sure pyro remains valid during delay
[fw/altos] / altosuilib / AltosBTDevice.java
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altosuilib_3;
19
20 import libaltosJNI.*;
21 import org.altusmetrum.altoslib_5.*;
22
23 public class AltosBTDevice extends altos_bt_device implements AltosDevice {
24
25         public String getProductName() {
26                 String  name = getName();
27                 if (name == null)
28                         return "Altus Metrum";
29                 int     dash = name.lastIndexOf("-");
30                 if (dash < 0)
31                         return name;
32                 return name.substring(0,dash);
33         }
34
35         public int getProduct() {
36                 if (AltosLib.bt_product_telebt.equals(getProductName()))
37                         return AltosLib.product_telebt;
38                 return 0;
39         }
40
41         public String getPath() {
42                 return getAddr();
43         }
44
45         public String getErrorString() {
46                 altos_error     error = new altos_error();
47
48                 libaltos.altos_get_last_error(error);
49                 return String.format("%s (%d)", error.getString(), error.getCode());
50         }
51
52         public int getSerial() {
53                 String name = getName();
54                 if (name == null)
55                         return 0;
56                 int dash = name.lastIndexOf("-");
57                 if (dash < 0 || dash >= name.length())
58                         return 0;
59                 String sn = name.substring(dash + 1, name.length());
60                 try {
61                         return Integer.parseInt(sn);
62                 } catch (NumberFormatException ne) {
63                         return 0;
64                 }
65         }
66
67         public String toString() {
68                 return String.format("%-20.20s %4d %s",
69                                      getProductName(), getSerial(), getAddr());
70         }
71
72         public String toShortString() {
73                 return String.format("%s %d %s",
74                                      getProductName(), getSerial(), getAddr());
75
76         }
77
78         public SWIGTYPE_p_altos_file open() {
79                 return libaltos.altos_bt_open(this);
80         }
81
82         /*
83         private boolean isAltusMetrum() {
84                 if (getName().startsWith(Altos.bt_product_telebt))
85                         return true;
86                 return false;
87         }
88         */
89
90         public boolean matchProduct(int want_product) {
91
92 //              if (!isAltusMetrum())
93 //                      return false;
94
95                 if (want_product == AltosLib.product_any)
96                         return true;
97
98                 if (want_product == AltosLib.product_basestation)
99                         return matchProduct(AltosLib.product_telebt);
100
101                 if (want_product == getProduct())
102                         return true;
103
104                 return false;
105         }
106
107         public boolean equals(Object o) {
108                 if (!(o instanceof AltosBTDevice))
109                         return false;
110                 AltosBTDevice other = (AltosBTDevice) o;
111                 return getName().equals(other.getName()) && getAddr().equals(other.getAddr());
112         }
113
114         public int hashCode() {
115                 return getName().hashCode() ^ getAddr().hashCode();
116         }
117
118         public AltosBTDevice(String name, String addr) {
119                 AltosUILib.load_library();
120                 libaltos.altos_bt_fill_in(name, addr,this);
121         }
122
123         public AltosBTDevice() {
124         }
125 }