using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using Zeptomoby.OrbitTools;
using DynamicListViewPlus;
namespace NORADiator
{
public partial class FormNoradiator : Form
{
enum Stage { Undefined, Login, Home, Search, NameQuery, RecentPositions, Ascii };
Stage prevStage = Stage.Undefined;
public FormNoradiator()
{
InitializeComponent();
Text = "Quick and Dirty NORAD";
webBrowser1.Navigate("http://www.space-track.org/perl/login.pl");
webBrowser1.DocumentCompleted += delegate
{
var stage = GetStage(webBrowser1.DocumentText);
switch (stage)
{
case Stage.Login:
statusStrip1.Text = "Login";
webBrowser1.Document.All["username"].InnerText = "***";
webBrowser1.Document.All["password"].InnerText = "***";
webBrowser1.Document.All["_submit"].InvokeMember("Click");
break;
case Stage.Home: toolStripStatusLabel1.Text = "Home page: Select action either from menu or using links"; break;
case Stage.Search: toolStripStatusLabel1.Text = "Search for satellite ID"; break;
case Stage.NameQuery: toolStripStatusLabel1.Text = "You may copy desired Catalog number on clipboard and select \"Recent Positions\""; break;
case Stage.RecentPositions:
toolStripStatusLabel1.Text = "Specify Epoch and Submit";
if (prevStage != Stage.Ascii)
{
int id;
if (int.TryParse(Clipboard.GetText(), out id))
webBrowser1.Document.All["ids"].InnerText = id.ToString();
}
webBrowser1.Document.All["timeframe"].SetAttribute("checked", "timespan");
webBrowser1.Document.All["common_name"].SetAttribute("checked", "true");
webBrowser1.Document.All["ascii"].SetAttribute("checked", "checked");
break;
case Stage.Ascii:
var str = Regex.Split(webBrowser1.DocumentText, @"[\r\n]+");
var data = new List<TleWrap>();
int i;
for (i = 0; i < str.Length && !str[i].Contains("<pre>"); i++) ;
for (i++; i < str.Length; i += 3)
{
try
{
var t = new TleWrap(str, i);
data.Add(t);
}
catch
{
}
}
if (data.Count > 0)
{
var frm = new Form();
frm.Size = new Size(400, 800);
frm.Text = data[0].sat;
ListViewPlus.Grid<TleWrap>(frm, new ListClassData<TleWrap>(data), "sat", "epoch", "lon","incl");
frm.Owner = this;
frm.Show();
}
webBrowser1.GoBack();
break;
default: statusStrip1.Text = "Application is in a non-specific state"; break;
}
prevStage = stage;
};
homeToolStripMenuItem.Click += delegate { webBrowser1.Navigate("http://www.space-track.org/perl/home.pl"); };
searchToolStripMenuItem.Click += delegate { webBrowser1.Navigate("http://www.space-track.org/perl/name_query.pl"); };
recentPositionsToolStripMenuItem.Click += delegate { webBrowser1.Navigate("http://www.space-track.org/perl/id_query.pl"); };
}
Stage GetStage(string text)
{
if (text.Contains("<title>Space-Track - Login</title>")) return Stage.Login;
if (text.Contains("<h2>Welcome to Space-Track tcesky</h2>")) return Stage.Home;
if (text.Contains("<title>Space-Track - Satellite Common Name Query</title>")) return Stage.Search;
if (text.Contains("<title>Space-Track - Common Name Query Results</title>")) return Stage.NameQuery;
if (text.Contains("<title>Space-Track - Satellite Catalog Number Query</title>")) return Stage.RecentPositions;
if (text.Contains("<pre>") && !text.Contains("<title>") && !text.Contains("<head>")) return Stage.Ascii;
return Stage.Undefined;
}
}
public class TleWrap
{
TleItem z_itm;
[DisplayName("Satellite"), Description("Norad name of the satellite"), DisplayFormatAttribute("", 100, HorizontalAlignment.Left)]
public string sat { get { return z_itm.Name; } }
[DisplayName("Epoch"), Description("Epoch of the record"), DisplayFormatAttribute("dd.MM.yyyy HH:mm:ss", 150, HorizontalAlignment.Center)]
public DateTime epoch { get { return z_itm.noradDate; } }
[DisplayName("Lon"), Description("Longitude at the Epoch"), DisplayFormatAttribute("0.00", 80, HorizontalAlignment.Right)]
public double lon { get { return z_itm.lon_at_epoch > 180 ? z_itm.lon_at_epoch - 360 : z_itm.lon_at_epoch; } }
[DisplayName("Inclination"), Description("Inclination at the Epoch"), DisplayFormatAttribute("0.00", 80, HorizontalAlignment.Right)]
public double incl { get { return z_itm.InclinationDegree; } }
public TleWrap() { z_itm = null; }
public TleWrap(string[] line, int ind) { z_itm = new TleItem(line[ind], line[ind + 1], line[ind + 2]); }
}
}