2 * Copyright © 2018 Keith Packard <keithp@keithp.com>
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, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
15 package org.altusmetrum.altoslib_14;
20 import java.util.concurrent.*;
24 public class AltosUnitInfo extends Thread {
27 AltosUnitInfoListener listener;
38 void add(String line) {
39 if (json_string == null) {
42 json_string = json_string + "\n" + line;
46 void notify_complete() {
47 rfcal = AltosLib.MISSING;
49 if (json_string != null) {
50 System.out.printf("json_string: %s\n", json_string);
51 AltosJson json = AltosJson.fromString(json_string);
52 System.out.printf("json: %s\n", json);
53 String rfcal_string = null;
55 AltosJson unitinfo = json.get("unitinfo");
56 rfcal_string = unitinfo.get_string("rfcal", null);
57 if (rfcal_string != null)
58 rfcal = Integer.parseInt(rfcal_string);
59 } catch (NumberFormatException ne) {
60 System.out.printf("mal-formed integer %s\n", rfcal_string);
61 } catch (IllegalArgumentException ie) {
62 System.out.printf("mal-formed json\n");
65 listener.notify_unit_info(this);
72 format = System.getenv(AltosLib.unit_info_env);
74 format = AltosLib.unit_info_url;
76 String path = String.format(format, sn);
78 URL url = new URL(path);
80 System.out.printf("URL: %s\n", path);
82 URLConnection uc = url.openConnection();
84 InputStreamReader in_stream = new InputStreamReader(uc.getInputStream(), AltosLib.unicode_set);
85 BufferedReader in = new BufferedReader(in_stream);
88 String line = in.readLine();
93 } catch (Exception e) {
94 System.out.printf("file exception %s\n", e.toString());
100 public AltosUnitInfo(int sn, AltosUnitInfoListener listener) {
101 this.listener = listener;
103 this.rfcal = AltosLib.MISSING;