X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=altosuilib%2FAltosBTDevice.java;fp=altosuilib%2FAltosBTDevice.java;h=beefa532b0affc86c0194a816b1e7d101738a853;hb=3cf030fffffd223c3717011e03aac82346295d71;hp=0000000000000000000000000000000000000000;hpb=607fbb01710be1cb263625337f5be3d0fb48d5e7;p=fw%2Faltos diff --git a/altosuilib/AltosBTDevice.java b/altosuilib/AltosBTDevice.java new file mode 100644 index 00000000..beefa532 --- /dev/null +++ b/altosuilib/AltosBTDevice.java @@ -0,0 +1,125 @@ +/* + * Copyright © 2011 Keith Packard + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +package org.altusmetrum.altosuilib_2; + +import libaltosJNI.*; +import org.altusmetrum.altoslib_4.*; + +public class AltosBTDevice extends altos_bt_device implements AltosDevice { + + public String getProductName() { + String name = getName(); + if (name == null) + return "Altus Metrum"; + int dash = name.lastIndexOf("-"); + if (dash < 0) + return name; + return name.substring(0,dash); + } + + public int getProduct() { + if (AltosLib.bt_product_telebt.equals(getProductName())) + return AltosLib.product_telebt; + return 0; + } + + public String getPath() { + return getAddr(); + } + + public String getErrorString() { + altos_error error = new altos_error(); + + libaltos.altos_get_last_error(error); + return String.format("%s (%d)", error.getString(), error.getCode()); + } + + public int getSerial() { + String name = getName(); + if (name == null) + return 0; + int dash = name.lastIndexOf("-"); + if (dash < 0 || dash >= name.length()) + return 0; + String sn = name.substring(dash + 1, name.length()); + try { + return Integer.parseInt(sn); + } catch (NumberFormatException ne) { + return 0; + } + } + + public String toString() { + return String.format("%-20.20s %4d %s", + getProductName(), getSerial(), getAddr()); + } + + public String toShortString() { + return String.format("%s %d %s", + getProductName(), getSerial(), getAddr()); + + } + + public SWIGTYPE_p_altos_file open() { + return libaltos.altos_bt_open(this); + } + + /* + private boolean isAltusMetrum() { + if (getName().startsWith(Altos.bt_product_telebt)) + return true; + return false; + } + */ + + public boolean matchProduct(int want_product) { + +// if (!isAltusMetrum()) +// return false; + + if (want_product == AltosLib.product_any) + return true; + + if (want_product == AltosLib.product_basestation) + return matchProduct(AltosLib.product_telebt); + + if (want_product == getProduct()) + return true; + + return false; + } + + public boolean equals(Object o) { + if (!(o instanceof AltosBTDevice)) + return false; + AltosBTDevice other = (AltosBTDevice) o; + return getName().equals(other.getName()) && getAddr().equals(other.getAddr()); + } + + public int hashCode() { + return getName().hashCode() ^ getAddr().hashCode(); + } + + public AltosBTDevice(String name, String addr) { + AltosUILib.load_library(); + libaltos.altos_bt_fill_in(name, addr,this); + } + + public AltosBTDevice() { + } +}