diff --git a/src/main/java/ru/r2cloud/tle/Housekeeping.java b/src/main/java/ru/r2cloud/tle/Housekeeping.java index 6df10bd1..1c1e785e 100644 --- a/src/main/java/ru/r2cloud/tle/Housekeeping.java +++ b/src/main/java/ru/r2cloud/tle/Housekeeping.java @@ -152,16 +152,23 @@ private void reloadTle() { // store only supported satellites Map updated = new HashMap<>(); for (Satellite cur : dao.findAll()) { - Tle curTle; - if (cur.getTle() != null) { - curTle = cur.getTle(); - } else { - curTle = tleDao.find(cur.getId(), cur.getName()); + Tle oldTle = cur.getTle(); + Tle newTle = tleDao.find(cur.getId(), cur.getName()); + if (oldTle == null && newTle == null) { + continue; + } + if (oldTle == null && newTle != null) { + cur.setTle(newTle); + } + if (oldTle != null && newTle == null) { + cur.setTle(oldTle); } - cur.setTle(curTle); - if (curTle != null) { - updated.put(cur.getId(), cur.getTle()); + if (oldTle != null && newTle != null) { + // always update to new one + // even if it is the same + cur.setTle(newTle); } + updated.put(cur.getId(), cur.getTle()); } if (reloadTle) { tleDao.saveTle(updated); diff --git a/src/test/java/ru/r2cloud/tle/HousekeepingTest.java b/src/test/java/ru/r2cloud/tle/HousekeepingTest.java index b98af4fc..75ee15a9 100644 --- a/src/test/java/ru/r2cloud/tle/HousekeepingTest.java +++ b/src/test/java/ru/r2cloud/tle/HousekeepingTest.java @@ -1,5 +1,6 @@ package ru.r2cloud.tle; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.mockito.ArgumentMatchers.any; @@ -97,6 +98,30 @@ public void testReloadFailure() { assertNotNull(sat.getTle()); } + @Test + public void testTleUpdate() throws Exception { + Housekeeping reloader = new Housekeeping(config, satelliteDao, threadPool, celestrak, tleDao, null, leosatdata, null, priorityService); + reloader.run(); + Satellite meteor = satelliteDao.findById("40069"); + assertNotNull(meteor); + assertNotNull(meteor.getTle()); + assertEquals(meteor.getTle().getRaw()[1], "1 40069U 14037A 18286.52491495 -.00000023 00000-0 92613-5 0 9990"); + assertEquals(meteor.getTle().getRaw()[2], "2 40069 98.5901 334.4030 0004544 256.4188 103.6490 14.20654800221188"); + + config.setProperty("housekeeping.tle.periodMillis", "-10000"); + + Tle newTle = new Tle(new String[] { "METEOR M-2", "1 40069U 14037A 24217.61569736 .00000303 00000-0 15898-3 0 9993", "2 40069 98.4390 209.6955 0005046 206.9570 153.1346 14.21009510522504" }); + tleData.put("40069", newTle); + + reloader.run(); + + meteor = satelliteDao.findById("40069"); + assertNotNull(meteor); + assertNotNull(meteor.getTle()); + assertEquals(meteor.getTle().getRaw()[1], newTle.getRaw()[1]); + assertEquals(meteor.getTle().getRaw()[2], newTle.getRaw()[2]); + } + @Test public void testSuccess() throws Exception { Housekeeping reloader = new Housekeeping(config, satelliteDao, threadPool, celestrak, tleDao, null, leosatdata, null, priorityService); @@ -133,7 +158,7 @@ public void start() throws Exception { 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()); + config.setProperty("satellites.satnogs.location", new File(tempFolder.getRoot(), "satnogs.json").getAbsolutePath()); config.update(); satelliteDao = new SatelliteDao(config); @@ -141,7 +166,7 @@ public void start() throws Exception { celestrak = mock(CelestrakClient.class); when(celestrak.downloadTle()).thenReturn(tleData); tleDao = new TleDao(config); - + priorityService = new PriorityService(config, new DefaultClock()); threadPool = mock(ThreadPoolFactory.class);