Skip to content

Commit

Permalink
Add satellites file
Browse files Browse the repository at this point in the history
  • Loading branch information
albertomn86 committed Dec 7, 2023
1 parent 28935ae commit 1785b9a
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 7 deletions.
2 changes: 2 additions & 0 deletions TLEGenerator.Program/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class Config
{
public string NoradUrl { get; set; } = "https://celestrak.com/NORAD/elements/gp.php";
public List<string> Groups { get; set; } = [];
public string SatellitesListPath { get; set; } = "./satellites.json";
public string TempFolder { get; set; } = "/tmp";
public int TempFilesDays { get; set; } = 1;

Expand All @@ -22,6 +23,7 @@ public void ReadConfigFile(string configFile = "./config.json")
{
NoradUrl = items.NoradUrl;
Groups = items.Groups;
SatellitesListPath = items.SatellitesListPath;
TempFolder = items.TempFolder;
TempFilesDays = items.TempFilesDays;
}
Expand Down
23 changes: 20 additions & 3 deletions TLEGenerator.Program/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace TLEGenerator.Program;
using System.Text;

namespace TLEGenerator.Program;

class Program
{
Expand All @@ -7,14 +9,29 @@ static void Main(string[] args)
Config config = new();
config.ReadConfigFile();

List<string> satellites = new(["25338", "28654", "33591"]);
List<string> satellites = SatellitesReader.ReadList(config.SatellitesListPath);
TLEDataManager tleDataManager = new(config);
tleDataManager.RetrieveGroupsData();

StringBuilder tleFileContent = new();
int satellitesFound = 0;

foreach (var satellite in satellites)
{
var tle = tleDataManager.GetTLE(satellite);
Console.WriteLine(tle?.Title);

if (tle != null) {
Console.WriteLine($"✓ Saved TLE for {tle.Title.Trim()} ({satellite})");
satellitesFound += 1;
tleFileContent.AppendLine(tle.ToString());
continue;
}

Console.WriteLine($"✗ Could not find TLE for {satellite}");
}

Console.WriteLine($"TLEs retrieved: {satellitesFound}/{satellites.Count}");
Console.WriteLine($"API requests: {tleDataManager.GetApiRequestsNumber()}");
Console.WriteLine(tleFileContent.ToString());
}
}
20 changes: 20 additions & 0 deletions TLEGenerator.Program/SatellitesReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Text.Json;

namespace TLEGenerator.Program;

public static class SatellitesReader
{
public static List<string> ReadList(string filePath)
{
if (!File.Exists(filePath))
{
throw new FileNotFoundException(filePath);
}

using StreamReader reader = File.OpenText(filePath);

string json = reader.ReadToEnd();

return JsonSerializer.Deserialize<List<string>>(json) ?? [];
}
}
5 changes: 5 additions & 0 deletions TLEGenerator.Program/TLE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ public string GetId()
{
return Line2.Split()[1];
}

public override string ToString()
{
return $"{Title}\r\n{Line1}\r\n{Line2}";
}
}
9 changes: 8 additions & 1 deletion TLEGenerator.Program/TLEDataDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ public class TLEDataDownloader
{
private readonly HttpClient httpClient;
private readonly Config config;
private int ApiRequests;

public TLEDataDownloader(Config config)
{
httpClient ??= new();
httpClient = new();
this.config = config;
}

Expand Down Expand Up @@ -39,6 +40,7 @@ private async Task DownloadAndSaveAsync(string url, string destinationPath)

private async Task<Stream> GetFileStream(string url)
{
ApiRequests += 1;
Stream fileStream = await httpClient.GetStreamAsync(url);

return fileStream;
Expand All @@ -50,4 +52,9 @@ private static async Task SaveStream(Stream fileStream, string destinationPath)

await fileStream.CopyToAsync(outputFileStream);
}

public int GetApiRequestsNumber()
{
return ApiRequests;
}
}
5 changes: 5 additions & 0 deletions TLEGenerator.Program/TLEDataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,9 @@ private bool IsOldFile(string path){

return (DateTime.Now - lastModified).TotalDays >= config.TempFilesDays;
}

public int GetApiRequestsNumber()
{
return tleDataDownloader.GetApiRequestsNumber();
}
}
8 changes: 5 additions & 3 deletions TLEGenerator.Program/TLEFileParser.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Data;

namespace TLEGenerator.Program;

public class TLEFileParser
Expand All @@ -15,7 +17,7 @@ public void ParseFile(string filePath)
ArgumentException.ThrowIfNullOrWhiteSpace(filePath);

using StreamReader sr = File.OpenText(filePath);
string line;
string? line;

while ((line = sr.ReadLine()) != null)
{
Expand All @@ -24,8 +26,8 @@ public void ParseFile(string filePath)
TLE tle = new()
{
Title = line,
Line1 = sr.ReadLine(),
Line2 = sr.ReadLine()
Line1 = sr.ReadLine() ?? throw new NoNullAllowedException(),
Line2 = sr.ReadLine() ?? throw new NoNullAllowedException()
};
TLEData.TryAdd(tle.GetId(), tle);
}
Expand Down
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"NoradUrl" : "https://celestrak.com/NORAD/elements/gp.php",
"Groups" : [ "weather" ],
"SatellitesListPath" : "./satellites.json",
"TempFolder" : "/tmp",
"TempFilesDays" : 1
}
7 changes: 7 additions & 0 deletions satellites.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
"57166",
"25338",
"28654",
"33591",
"25544"
]

0 comments on commit 1785b9a

Please sign in to comment.