123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- using DevExpress.XtraEditors;
- using XdCxRhDW.App.EFContext;
- using XdCxRhDW.App.Model;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Data.Entity;
- using System.Data.Entity.Migrations;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Net.Http;
- namespace XdCxRhDW.App.UserControl
- {
- public partial class CtrlXl : DevExpress.XtraEditors.XtraUserControl
- {
- List<XlInfo> list = new List<XlInfo>();
- public CtrlXl()
- {
- InitializeComponent();
- }
- private async void CtrlXl_Load(object sender, EventArgs e)
- {
- gridXl.Init().UseSort().UseFilter().UseMultiSelect().UseRowNumber();
- gridXl.DataSource = list;
- await LoadData();
- }
- private void btnOpen_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
- {
- if (xtraOpenFileDialog1.ShowDialog() == DialogResult.OK)
- {
- btnOpen.Text = xtraOpenFileDialog1.FileName;
- }
- }
- public async Task LoadData()
- {
- try
- {
- list.Clear();
- using (RHDWContext db = new RHDWContext())
- {
- var res = db.XlInfos.OrderBy(p => p.SatName).OrderByDescending(p => p.TimeBJ);
- list.AddRange(await res.ToListAsync());
- }
- gridView1.RefreshData();
- }
- catch (Exception ex)
- {
- Serilog.Log.Error(ex,"加载星历信息异常");
- DxHelper.MsgBoxHelper.ShowError("加载星历信息异常");
- }
- }
- private async void btnImp_Click(object sender, EventArgs e)
- {
- //https://www.space-track.org/documentation#tle网站上有星历文件格式说明
- string line = null;
- try
- {
- this.Enabled = false;
- await Task.Run(async () =>
- {
- var lines = File.ReadAllLines(btnOpen.Text).ToList();
- lines.RemoveAll(p => string.IsNullOrWhiteSpace(p));
- List<XlInfo> tmp = new List<XlInfo>();
- for (int i = 0; i < lines.Count; i += 3)
- {
- line = lines[i];
- var satName = lines[i].Trim();
- if (satName.StartsWith("0 "))
- satName = satName.Substring(2).Trim();
- if (satName.StartsWith("TBA"))//待发布的卫星
- continue;
- XlInfo xl = new XlInfo()
- {
- SatName = satName,
- Line1 = lines[i + 1],
- Line2 = lines[i + 2],
- SatCode = Convert.ToInt32(lines[i + 1].Substring(2, 5))
- };
- var timeStr = lines[i + 1].Substring(18, 14).Replace(" ","");//https://www.space-track.org/documentation#tle星历接口中说这里面可以接受空格
- var yearStr = timeStr.Substring(0, 2);
- var dayStr = timeStr.Substring(2, timeStr.Length - 2);
- var day = Convert.ToDouble(dayStr);
- var year = 2000 + Convert.ToInt32(yearStr);
- DateTime dt = new DateTime(year, 1, 1, 0, 0, 0);
- dt = dt.AddDays(day);
- xl.TimeBJ = dt.AddHours(8);
- tmp.Add(xl);
- }
- using (RHDWContext db = new RHDWContext())
- {
- foreach (var item in db.XlInfos)
- {
- var findItem = tmp.Find(p => p.SatCode == item.SatCode && p.TimeBJ == item.TimeBJ);
- if (findItem != null)
- {
- item.Line1 = findItem.Line1;
- item.Line2 = findItem.Line2;
- item.UpdateTime = DateTime.Now;
- }
- tmp.Remove(findItem);
- }
- db.XlInfos.AddRange(tmp);
- await db.SaveChangesAsync();
- }
- await LoadData();
-
- });
- Serilog.Log.Information("星历导入成功");
- DxHelper.MsgBoxHelper.ShowInfo("星历导入成功");
- }
- catch (Exception ex)
- {
- Serilog.Log.Error(ex, $"{line}星历导入异常");
- DxHelper.MsgBoxHelper.ShowError("星历导入异常");
- }
- finally
- {
- this.Enabled = true;
- }
- }
- private async void btnDel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
- {
- try
- {
- this.Enabled = false;
- await Task.Run(async () =>
- {
- var ids = gridView1.GetSelectedRows();
- List<int> listID = new List<int>();
- foreach (var idx in ids)
- {
- var id = (gridView1.GetRow(idx) as XlInfo).ID;
- listID.Add(id);
- }
- using (RHDWContext db = new RHDWContext())
- {
- var delItems = await db.XlInfos.Where(p => listID.Contains(p.ID)).ToListAsync();
- db.XlInfos.RemoveRange(delItems);
- await db.SaveChangesAsync();
- }
- });
- gridView1.DeleteSelectedRows();
- }
- catch (Exception ex)
- {
- Serilog.Log.Error(ex, "删除选中的星历时异常");
- DxHelper.MsgBoxHelper.ShowError("删除选中的星历时异常");
- }
- finally
- {
- this.Enabled = true;
- }
- }
- private void gridView1_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
- {
- if (gridView1.FocusedRowObject != null)
- popupMenu1.ShowPopup(MousePosition);
- }
- }
- }
|