X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=altoslib%2FAltosMag.java;h=56add8f3445da8d0de9fee7d2ec2f5150d4ee739;hp=cb6826f3412fd56fdca25c51c1710cdb9fef48c9;hb=5b976a6651f4eb05d30afc08b9e1f27c7e52ae00;hpb=b984ff81d6b8979574e0248ffe8876634b8e1942 diff --git a/altoslib/AltosMag.java b/altoslib/AltosMag.java index cb6826f3..56add8f3 100644 --- a/altoslib/AltosMag.java +++ b/altoslib/AltosMag.java @@ -15,13 +15,29 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -package org.altusmetrum.altoslib_1; +package org.altusmetrum.altoslib_2; + +import java.util.concurrent.*; public class AltosMag implements Cloneable { public int x; public int y; public int z; + public boolean parse_string(String line) { + if (!line.startsWith("X:")) + return false; + + String[] items = line.split("\\s+"); + + if (items.length >= 6) { + x = Integer.parseInt(items[1]); + y = Integer.parseInt(items[3]); + z = Integer.parseInt(items[5]); + } + return true; + } + public AltosMag clone() { AltosMag n = new AltosMag(); @@ -30,5 +46,35 @@ public class AltosMag implements Cloneable { n.z = z; return n; } + + public AltosMag() { + x = AltosLib.MISSING; + y = AltosLib.MISSING; + z = AltosLib.MISSING; + } + + static public void update_state(AltosState state, AltosLink link, AltosConfigData config_data) { + try { + AltosMag mag = new AltosMag(link); + + if (mag != null) + state.set_mag(mag); + } catch (TimeoutException te) { + } catch (InterruptedException ie) { + } + } + + public AltosMag(AltosLink link) throws InterruptedException, TimeoutException { + this(); + link.printf("M\n"); + for (;;) { + String line = link.get_reply_no_dialog(5000); + if (line == null) { + throw new TimeoutException(); + } + if (parse_string(line)) + break; + } + } } \ No newline at end of file