132 lines
4.1 KiB
C#
132 lines
4.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.ServiceProcess;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
using System.Web.Hosting;
|
|
using System.Web.Mvc;
|
|
using System.Web.Routing;
|
|
using System.Windows.Forms;
|
|
using Newtonsoft.Json;
|
|
using TrainInfomationProviderService.StationInfo;
|
|
using TrainInfomationProviderService.StationInfo.Entities;
|
|
using TrainInfomationProviderService.TrainInfo;
|
|
using TrainInfomationProviderService.TrainInfo.Entities;
|
|
using Timer = System.Threading.Timer;
|
|
|
|
[assembly: System.Web.PreApplicationStartMethod(typeof(TrainInfomationProviderService.Program), "WebLoader")]
|
|
|
|
namespace TrainInfomationProviderService
|
|
{
|
|
public static class Program
|
|
{
|
|
static Timer _timer;
|
|
|
|
public static void WebLoader()
|
|
{
|
|
if (ConfigurationManager.AppSettings["local_disable_train_provider"] == "1")
|
|
return;
|
|
|
|
RouteTable.Routes.MapRoute("traintransit", "st/{action}/{id}", new { area = "", controller = "Transit", id = UrlParameter.Optional }, new { action = "(keepalive|ls|ss|tor|dt|ls2|ssa)" }, new[] { "TrainInfomationProviderService.Web" });
|
|
Task.Run(() =>
|
|
{
|
|
var keepAliveUrl = ConfigurationManager.AppSettings["12306_keepaliveurl"];
|
|
if (!string.IsNullOrEmpty(keepAliveUrl))
|
|
{
|
|
_timer = new Timer(_ =>
|
|
{
|
|
try
|
|
{
|
|
new WebClient().DownloadString(keepAliveUrl);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
Trace.TraceInformation("KEEP ALIVE已激活。");
|
|
}, null, new TimeSpan(0, 0, 10, 0), new TimeSpan(0, 0, 10, 0));
|
|
Trace.TraceInformation("KEEP ALIVE已启用");
|
|
}
|
|
else
|
|
{
|
|
Trace.TraceInformation("KEEP ALIVE未启用");
|
|
}
|
|
|
|
try
|
|
{
|
|
Main(new string[0]);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
File.WriteAllText(HostingEnvironment.MapPath("~/err.log"), ex.ToString());
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 应用程序的主入口点。
|
|
/// </summary>
|
|
private static void Main(string[] args)
|
|
{
|
|
if (args.Length > 0 && args[0].IsIgnoreCaseEqualTo("--service"))
|
|
{
|
|
var servicesToRun = new ServiceBase[]
|
|
{
|
|
new InformationUpdaterService()
|
|
};
|
|
ServiceBase.Run(servicesToRun);
|
|
}
|
|
else
|
|
{
|
|
if (RunTimeContext.IsWeb)
|
|
{
|
|
var file = PathUtility.Combine(RunTimeContext.DataStorageRoot, "logs", "traininfo_" + DateTime.Now.ToString("yyyyMmdd_hhmmss") + ".log");
|
|
Directory.CreateDirectory(Path.GetDirectoryName(file));
|
|
|
|
var writer = new TextWriterTraceListener(file);
|
|
Trace.Listeners.Add(writer);
|
|
Trace.AutoFlush = true;
|
|
}
|
|
else
|
|
{
|
|
Trace.Listeners.Add(new ConsoleTraceListener());
|
|
}
|
|
|
|
StationManager.Instance.Init();
|
|
TrainInfoManager.Instance.Init();
|
|
SameStationManager.Init();
|
|
TrainInfoSearchProvider.Instance.Init();
|
|
|
|
if (!RunTimeContext.IsWeb)
|
|
{
|
|
//搜索?
|
|
var searchProvider = TrainInfoSearchProvider.Instance;
|
|
//var lines = searchProvider.FindDirectTrains(DateTime.Parse("2014-12-12"), "NVH", "JJG").ToArray();
|
|
//var maxTimeRage = lines.Max(s => s.CalculatedMinutesBase);
|
|
|
|
var opt = new TrainTransitSearchOptions();
|
|
opt.InitLimit(1357);
|
|
var altLines = searchProvider.FindOnceTransitTrains(DateTime.Parse("2016-02-04"), "GZQ", "XAY", opt).ToArray();
|
|
|
|
//var availableLines = lines.Select(s => s.Train.Code + "," + s.FromStation.Name + "," + s.ToStation.Name + "," + s.ElapsedTime).ToArray();
|
|
Array.ForEach(altLines.Select(s =>
|
|
s.First.Train.Code + "," + s.First.FromStation.Name + "," + s.First.ToStation.Name + "," + s.First.From.Left.Value + " - " + s.First.To.Arrive.Value + " / " + s.First.ElapsedTime
|
|
+ " -> " + s.Second.Train.Code + "," + s.Second.FromStation.Name + "," + s.Second.ToStation.Name + "," + s.Second.From.Left.Value + " - " + s.Second.To.Arrive.Value + " / " + s.Second.ElapsedTime + " / 等待 " + s.WaitElaspsedTime + " / 总耗时 " + s.TotalElapsedTime
|
|
+ (s.NotRecommand ? " / 不推荐" : "")
|
|
).ToArray(), _ => Trace.TraceInformation(_));
|
|
|
|
//runtime mode
|
|
MessageBox.Show(StationManager.Instance.Storage.Version.ToString());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|