altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / altoslib / AltosUnitInfo.java
1 /*
2  * Copyright © 2018 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, either version 2 of the License, or
7  * (at your option) any later version.
8  *
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.
13  */
14
15 package org.altusmetrum.altoslib_13;
16
17 import java.io.*;
18 import java.lang.*;
19 import java.util.*;
20 import java.util.concurrent.*;
21 import java.net.*;
22 import java.text.*;
23
24 public class AltosUnitInfo extends Thread {
25         int                     sn;
26         int                     rfcal;
27         AltosUnitInfoListener   listener;
28         String                  json_string;
29
30         public int sn() {
31                 return sn;
32         }
33
34         public int rfcal() {
35                 return rfcal;
36         }
37
38         void add(String line) {
39                 if (json_string == null) {
40                         json_string = line;
41                 } else {
42                         json_string = json_string + "\n" + line;
43                 }
44         }
45
46         void notify_complete() {
47                 rfcal = AltosLib.MISSING;
48
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;
54                         try {
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");
63                         }
64                 }
65                 listener.notify_unit_info(this);
66         }
67
68         public void run() {
69                 try {
70                         String  format;
71
72                         format = System.getenv(AltosLib.unit_info_env);
73                         if (format == null)
74                                 format = AltosLib.unit_info_url;
75
76                         String path = String.format(format, sn);
77
78                         URL url = new URL(path);
79
80                         System.out.printf("URL: %s\n", path);
81
82                         URLConnection uc = url.openConnection();
83
84                         InputStreamReader in_stream = new InputStreamReader(uc.getInputStream(), AltosLib.unicode_set);
85                         BufferedReader in = new BufferedReader(in_stream);
86
87                         for (;;) {
88                                 String line = in.readLine();
89                                 if (line == null)
90                                         break;
91                                 add(line);
92                         }
93                 } catch (Exception e) {
94                         System.out.printf("file exception %s\n", e.toString());
95                 } finally {
96                         notify_complete();
97                 }
98         }
99
100         public AltosUnitInfo(int sn, AltosUnitInfoListener listener) {
101                 this.listener = listener;
102                 this.sn = sn;
103                 this.rfcal = AltosLib.MISSING;
104                 start();
105         }
106 }