Skip to content

Commit

Permalink
populate source and last update time on each tle
Browse files Browse the repository at this point in the history
  • Loading branch information
dernasherbrezon committed Jan 21, 2024
1 parent fca5f35 commit cd618a0
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/main/java/ru/r2cloud/cloud/LeoSatDataClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ private Tle readTle(JsonValue tle) {
Tle result = new Tle(new String[] { line1, line2, line3 });
// assume downloaded TLE is always fresh
result.setLastUpdateTime(clock.millis());
result.setSource(hostname);
return result;
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/ru/r2cloud/cloud/SatnogsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ private Tle readTle(String body) {
// if satellite is new launch, then there is no other source for tle
// readTle is called only for new launches
result.setLastUpdateTime(clock.millis());
result.setSource(hostname);
return result;
}

Expand Down
22 changes: 19 additions & 3 deletions src/main/java/ru/r2cloud/model/Tle.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class Tle {

private final String[] raw;
private long lastUpdateTime;
private String source;

public Tle(String[] tle) {
this.raw = tle;
Expand All @@ -16,15 +17,23 @@ public Tle(String[] tle) {
public String[] getRaw() {
return raw;
}

public void setLastUpdateTime(long lastUpdateTime) {
this.lastUpdateTime = lastUpdateTime;
}

public long getLastUpdateTime() {
return lastUpdateTime;
}

public String getSource() {
return source;
}

public void setSource(String source) {
this.source = source;
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down Expand Up @@ -56,6 +65,10 @@ public JsonObject toJson() {
if (raw.length > 2) {
json.add("line3", raw[2]);
}
json.add("updated", lastUpdateTime);
if (source != null) {
json.add("source", source);
}
return json;
}

Expand All @@ -64,7 +77,10 @@ public static Tle fromJson(JsonObject json) {
raw[0] = json.getString("line1", null);
raw[1] = json.getString("line2", null);
raw[2] = json.getString("line3", null);
return new Tle(raw);
Tle result = new Tle(raw);
result.setLastUpdateTime(json.getLong("updated", 0));
result.setSource(json.getString("source", null));
return result;
}

}
5 changes: 4 additions & 1 deletion src/main/java/ru/r2cloud/tle/CelestrakClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ private Map<String, Tle> downloadTle(String location) {
break;
}
String noradId = line2.substring(2, 2 + 5).trim();
result.put(noradId, new Tle(new String[] { curLine.trim(), line1, line2 }));
Tle value = new Tle(new String[] { curLine.trim(), line1, line2 });
value.setLastUpdateTime(System.currentTimeMillis());
value.setSource(obj.getHost());
result.put(noradId, value);
}
}
}
Expand Down
45 changes: 21 additions & 24 deletions src/main/java/ru/r2cloud/tle/TleDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonArray;

import ru.r2cloud.model.Tle;
import ru.r2cloud.util.Configuration;
import ru.r2cloud.util.Util;
Expand Down Expand Up @@ -63,7 +66,7 @@ private void index(Map<String, Tle> tleById) {
cacheByName.clear();
putAll(tleById);
}

public void putAll(Map<String, Tle> tleById) {
cache.putAll(tleById);
for (Tle cur : tleById.values()) {
Expand All @@ -82,17 +85,14 @@ public long getLastUpdateTime() {
}

private static void saveTle(Path file, Map<String, Tle> tle) {
JsonArray output = new JsonArray();
for (Tle cur : tle.values()) {
output.add(cur.toJson());
}
// ensure temp and output are on the same filestore
Path tempOutput = file.getParent().resolve("tle.txt.tmp");
Path tempOutput = file.getParent().resolve("tle.json.tmp");
try (BufferedWriter w = Files.newBufferedWriter(tempOutput)) {
for (Tle cur : tle.values()) {
w.append(cur.getRaw()[0]);
w.newLine();
w.append(cur.getRaw()[1]);
w.newLine();
w.append(cur.getRaw()[2]);
w.newLine();
}
output.writeTo(w);
} catch (IOException e) {
Util.logIOException(LOG, "unable to save tle: " + file.toAbsolutePath(), e);
return;
Expand All @@ -110,23 +110,20 @@ private static Map<String, Tle> loadTle(Path file) {
if (!Files.exists(file)) {
return result;
}
JsonArray output = null;
try (BufferedReader in = Files.newBufferedReader(file)) {
// only first line matters
String curLine = null;
while ((curLine = in.readLine()) != null) {
String line1 = in.readLine();
if (line1 == null) {
break;
}
String line2 = in.readLine();
if (line2 == null) {
break;
}
String noradId = line2.substring(2, 2 + 5).trim();
result.put(noradId, new Tle(new String[] { curLine.trim(), line1, line2 }));
}
output = Json.parse(in).asArray();
} catch (IOException e) {
Util.logIOException(LOG, "unable to load tle from cache: " + file.toAbsolutePath(), e);
return result;
}
for (int i = 0; i < output.size(); i++) {
Tle cur = Tle.fromJson(output.get(i).asObject());
if (cur == null) {
continue;
}
String noradId = cur.getRaw()[cur.getRaw().length - 1].substring(2, 2 + 5).trim();
result.put(noradId, cur);
}
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/config-common.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ddns.interval.seconds=300
#1 - Monday, 7 - Sunday
tle.urls=http://r4uab.ru/satonline.txt,https://celestrak.org/NORAD/elements/gp.php?GROUP=satnogs&FORMAT=tle,https://celestrak.org/NORAD/elements/gp.php?GROUP=active&FORMAT=tle
tle.timeout=60000
tle.cacheFileLocation=./data/tle.txt
tle.cacheFileLocation=./data/tle.json

housekeeping.periodMillis=3600000
housekeeping.tle.periodMillis=172800000
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/ru/r2cloud/tle/HousekeepingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void start() throws Exception {
}

config = new TestConfiguration(tempFolder, FileSystems.getDefault());
config.setProperty("tle.cacheFileLocation", new File(tempFolder.getRoot(), "tle.txt").getAbsolutePath());
config.setProperty("tle.cacheFileLocation", new File(tempFolder.getRoot(), "tle.json").getAbsolutePath());
config.setProperty("satellites.leosatdata.location", new File(tempFolder.getRoot(), "leosatdata.json").getAbsolutePath());
config.setProperty("satellites.leosatdata.new.location", new File(tempFolder.getRoot(), "leosatdata.new.json").getAbsolutePath());
config.setProperty("satellites.satnogs.location", new File(tempFolder.getRoot(), "satnogs.json").getAbsolutePath());
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/ru/r2cloud/tle/TleDaoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void testSaveLoadAndFail() {
public void start() throws Exception {
fs = new MockFileSystem(FileSystems.getDefault());
config = new TestConfiguration(tempFolder, fs);
fileLocation = new File(tempFolder.getRoot(), "tle.txt").getAbsolutePath();
fileLocation = new File(tempFolder.getRoot(), "tle.json").getAbsolutePath();
config.setProperty("tle.cacheFileLocation", fileLocation);
dao = new TleDao(config);
}
Expand Down

0 comments on commit cd618a0

Please sign in to comment.