public PlaneFeedPluginPlaneInfoList GetPlanes(PlaneFeedPluginArgs args) { PlaneFeedPluginPlaneInfoList planes = new PlaneFeedPluginPlaneInfoList(); // add code for getting plane info here return(planes); }
public PlaneFeedPluginPlaneInfoList GetPlanes(PlaneFeedPluginArgs args) { PlaneFeedPluginPlaneInfoList planes = new PlaneFeedPluginPlaneInfoList(); // time to report planes ArrayList list = Decoder.GetPlanes(); if (list.Count > 0) { // convert to plane info list foreach (ADSBInfo info in list) { PlaneFeedPluginPlaneInfo plane = new PlaneFeedPluginPlaneInfo(); plane.Time = info.Timestamp; plane.Hex = info.ICAO24; // mark call with "@" if option is enabled plane.Call = (Settings.MarkLocal) ? "@" + info.Call : info.Call; plane.Lat = info.Lat; plane.Lon = info.Lon; plane.Alt = info.Alt; plane.Speed = info.Speed; plane.Track = info.Heading; plane.Reg = "[unknown]"; plane.Type = "[unknown]"; plane.Manufacturer = "[unknown]"; plane.Model = "[unknown]"; plane.Category = 0; planes.Add(plane); } // save raw data to file if enabled if (Settings.SaveToFile) { JavaScriptSerializer js = new JavaScriptSerializer(); string json = js.Serialize(planes); using (StreamWriter sw = new StreamWriter(args.TmpDirectory + Path.DirectorySeparatorChar + this.GetType().Name + "_" + DateTime.UtcNow.ToString("yyyy-MM-dd HH_mm_ss") + ".json")) { sw.WriteLine(json); } } } Console.WriteLine("[" + this.GetType().Name + "]: Returning " + planes.Count + " planes"); return(planes); }
public PlaneFeedPluginPlaneInfoList GetPlanes(PlaneFeedPluginArgs args) { // intialize variables VarConverter VC = new VarConverter(); VC.AddVar("APPDIR", args.AppDirectory); VC.AddVar("DATADIR", args.AppDataDirectory); VC.AddVar("LOGDIR", args.LogDirectory); VC.AddVar("DATABASEDIR", args.DatabaseDirectory); VC.AddVar("MINLAT", args.MinLat); VC.AddVar("MAXLAT", args.MaxLat); VC.AddVar("MINLON", args.MinLon); VC.AddVar("MAXLON", args.MaxLon); VC.AddVar("MINALTM", args.MinAlt); VC.AddVar("MAXALTM", args.MaxAlt); VC.AddVar("MINALTFT", (int)UnitConverter.m_ft((double)args.MinAlt)); VC.AddVar("MAXALTFT", (int)UnitConverter.m_ft((double)args.MaxAlt)); // initialize plane info list PlaneFeedPluginPlaneInfoList planes = new PlaneFeedPluginPlaneInfoList(); string json = ""; // calculate url and get json String url = VC.ReplaceAllVars(Settings.URL); Console.WriteLine("[" + this.GetType().Name + "]: Creating web request: " + url); // this will only run on .NET 4.0 if you have installed .NET 4.5 or later frameworks on your machine! // ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; // HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url); // webrequest.Timeout = Settings.Timeout * 1000; // webrequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0"; // webrequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; // Console.WriteLine("[" + this.GetType().Name + "]: Getting web response"); // HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse(); // Console.WriteLine("[" + this.GetType().Name + "]: Reading stream"); // using (StreamReader sr = new StreamReader(webresponse.GetResponseStream())) // { // json = sr.ReadToEnd(); // } json = OSNTlsClient.DownloadFile(url, Settings.Timeout * 1000); // save raw data to file if enabled if (Settings.SaveToFile) { using (StreamWriter sw = new StreamWriter(args.TmpDirectory + Path.DirectorySeparatorChar + this.GetType().Name + "_" + DateTime.UtcNow.ToString("yyyy-MM-dd HH_mm_ss") + ".json")) { sw.WriteLine(json); } } Console.WriteLine("[" + this.GetType().Name + "]: Analyzing data"); // JavaScriptSerializer js = new JavaScriptSerializer(); // dynamic root = js.Deserialize<dynamic>(json); dynamic root = JsonConvert.DeserializeObject(json); try { // analyze json string for planes data // get the planes position list var aclist = root["states"]; foreach (var ac in aclist) { try { // different handling of reading JSON between Windows (Array) & Linux (ArrayList) // access to data values itself is the same int len = 0; len = ac.Count; // skip if too few fields in record if (len < 17) { continue; } PlaneFeedPluginPlaneInfo plane = new PlaneFeedPluginPlaneInfo(); // get hex first plane.Hex = ReadPropertyString(ac, 0).ToUpper(); // get callsign plane.Call = ReadPropertyString(ac, 1); // get position plane.Lon = ReadPropertyDouble(ac, 5); plane.Lat = ReadPropertyDouble(ac, 6); // get altitude (provided in m --> convert to ft) plane.Alt = UnitConverter.m_ft(ReadPropertyDouble(ac, 13)); // get track plane.Track = ReadPropertyDouble(ac, 10); // get speed (provided in m/s --> convert to kts) plane.Speed = UnitConverter.ms_kts(ReadPropertyDouble(ac, 9)); // registration is not provided plane.Reg = ""; // get position timestamp in sec int l = ReadPropertyInt(ac, 3); if (l != int.MinValue) { DateTime timestamp = new System.DateTime(1970, 1, 1, 0, 0, 0, 0); timestamp = timestamp.AddSeconds(l); plane.Time = timestamp; } else { // skip plane if no valid timestamp found continue; } // get type info plane.Type = ReadPropertyString(ac, 5); planes.Add(plane); } catch (Exception ex) { Console.WriteLine("[" + System.Reflection.MethodBase.GetCurrentMethod().Name + "]" + ex.Message); } } } catch (Exception ex) { Console.WriteLine("[" + System.Reflection.MethodBase.GetCurrentMethod().Name + "]" + ex.Message); } Console.WriteLine("[" + this.GetType().Name + "]: Returning " + planes.Count + " planes"); return(planes); }
public PlaneFeedPluginPlaneInfoList GetPlanes(PlaneFeedPluginArgs args) { // intialize variables VarConverter VC = new VarConverter(); VC.AddVar("APPDIR", args.AppDirectory); VC.AddVar("DATADIR", args.AppDataDirectory); VC.AddVar("LOGDIR", args.LogDirectory); VC.AddVar("DATABASEDIR", args.DatabaseDirectory); VC.AddVar("MINLAT", args.MinLat); VC.AddVar("MAXLAT", args.MaxLat); VC.AddVar("MINLON", args.MinLon); VC.AddVar("MAXLON", args.MaxLon); VC.AddVar("MINALTM", args.MinAlt); VC.AddVar("MAXALTM", args.MaxAlt); VC.AddVar("MINALTFT", (int)UnitConverter.m_ft((double)args.MinAlt)); VC.AddVar("MAXALTFT", (int)UnitConverter.m_ft((double)args.MaxAlt)); // initialize plane info list PlaneFeedPluginPlaneInfoList planes = new PlaneFeedPluginPlaneInfoList(); string json = ""; // calculate url and get json String url = VC.ReplaceAllVars(Settings.URL); Console.WriteLine("[" + this.GetType().Name + "]: Creating web request: " + url); HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url); webrequest.Referer = "http://www.radarbox24.com/"; webrequest.Timeout = Settings.Timeout * 1000; webrequest.UserAgent = "Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130807 Firefox/17.0"; Console.WriteLine("[" + this.GetType().Name + "]: Getting web response"); HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse(); Console.WriteLine("[" + this.GetType().Name + "]: Reading stream"); using (StreamReader sr = new StreamReader(webresponse.GetResponseStream())) { json = sr.ReadToEnd(); } // save raw data to file if enabled if (Settings.SaveToFile) { using (StreamWriter sw = new StreamWriter(args.TmpDirectory + Path.DirectorySeparatorChar + this.GetType().Name + "_" + DateTime.UtcNow.ToString("yyyy-MM-dd HH_mm_ss") + ".json")) { sw.WriteLine(json); } } Console.WriteLine("[" + this.GetType().Name + "]: Analyzing data"); JavaScriptSerializer js = new JavaScriptSerializer(); dynamic root = js.Deserialize <dynamic>(json); try { // analyze json string for planes data // get the planes position list var aclist = root[0]; foreach (var ac in aclist) { try { // different handling of reading JSON between Windows (Array) & Linux (ArrayList) // access to data values itself is the same int len = 0; if (ac.Value.GetType() == typeof(ArrayList)) { len = ac.Value.Count; } else if (ac.Value.GetType() == typeof(Object[])) { len = ac.Value.Length; } // skip if too few fields in record if (len < 14) { continue; } PlaneFeedPluginPlaneInfo plane = new PlaneFeedPluginPlaneInfo(); // get hex first // Radarbox24 does not provide a HEX info // leave it empty and let it fill by Reg in planefeed main thread plane.Hex = ""; // get position plane.Lat = ReadPropertyDouble(ac, 1); plane.Lon = ReadPropertyDouble(ac, 2); // get altitude plane.Alt = ReadPropertyDouble(ac, 4); // get callsign plane.Call = ReadPropertyString(ac, 0); // get registration plane.Reg = ReadPropertyString(ac, 9); // get track plane.Track = ReadPropertyDouble(ac, 7); // get speed plane.Speed = ReadPropertyDouble(ac, 6); // get position timestamp in msec long l = ReadPropertyLong(ac, 3); if (l != long.MinValue) { DateTime timestamp = new System.DateTime(1970, 1, 1, 0, 0, 0, 0); timestamp = timestamp.AddMilliseconds(l); plane.Time = timestamp; } else { // skip plane if no valid timestamp found continue; } // get type info plane.Type = ReadPropertyString(ac, 5); planes.Add(plane); } catch (Exception ex) { Console.WriteLine("[" + System.Reflection.MethodBase.GetCurrentMethod().Name + "]" + ex.Message); } } } catch (Exception ex) { // do nothing if property is not found } Console.WriteLine("[" + this.GetType().Name + "]: Returning " + planes.Count + " planes"); return(planes); }
public PlaneFeedPluginPlaneInfoList GetPlanes(PlaneFeedPluginArgs args) { // intialize variables VarConverter VC = new VarConverter(); VC.AddVar("APPDIR", args.AppDirectory); VC.AddVar("DATADIR", args.AppDataDirectory); VC.AddVar("LOGDIR", args.LogDirectory); VC.AddVar("DATABASEDIR", args.DatabaseDirectory); VC.AddVar("MINLAT", args.MinLat); VC.AddVar("MAXLAT", args.MaxLat); VC.AddVar("MINLON", args.MinLon); VC.AddVar("MAXLON", args.MaxLon); VC.AddVar("MINALTM", args.MinAlt); VC.AddVar("MAXALTM", args.MaxAlt); VC.AddVar("MINALTFT", (int)UnitConverter.m_ft((double)args.MinAlt)); VC.AddVar("MAXALTFT", (int)UnitConverter.m_ft((double)args.MaxAlt)); // initialize plane info list PlaneFeedPluginPlaneInfoList planes = new PlaneFeedPluginPlaneInfoList(); string json = ""; // calculate url and get json String url = VC.ReplaceAllVars(Settings.URL); Console.WriteLine("[" + this.GetType().Name + "]: Creating web request: " + url); HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url); webrequest.Timeout = Settings.Timeout * 1000; webrequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0"; Console.WriteLine("[" + this.GetType().Name + "]: Getting web response"); HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse(); Console.WriteLine("[" + this.GetType().Name + "]: Reading stream"); using (StreamReader sr = new StreamReader(webresponse.GetResponseStream())) { json = sr.ReadToEnd(); } // save raw data to file if enabled if (Settings.SaveToFile) { using (StreamWriter sw = new StreamWriter(args.TmpDirectory + Path.DirectorySeparatorChar + this.GetType().Name + "_" + DateTime.UtcNow.ToString("yyyy-MM-dd HH_mm_ss") + ".json")) { sw.WriteLine(json); } } Console.WriteLine("[" + this.GetType().Name + "]: Analyzing data"); try { Console.WriteLine("[" + this.GetType().Name + "]: Deserializing data from JSON"); // JavaScriptSerializer js = new JavaScriptSerializer(); // dynamic root = js.Deserialize<dynamic>(json); dynamic root = JsonConvert.DeserializeObject(json); Console.WriteLine("[" + this.GetType().Name + "]: Created object from JSON is " + root.GetType().ToString()); // analyze json string for planes data // get the planes position list Console.WriteLine("[" + this.GetType().Name + "]: Getting root of planes list"); var aclist = root["planes"]; Console.WriteLine("[" + this.GetType().Name + "]: Created root object is " + aclist.GetType().ToString()); foreach (var ac in aclist) { try { // different handling of reading JSON between Windows (Array) & Linux (ArrayList) // access to data values itself is the same int len = 0; len = ac.Value.Count; // skip if too few fields in record if (len < 13) { continue; } PlaneFeedPluginPlaneInfo plane = new PlaneFeedPluginPlaneInfo(); // get hex first plane.Hex = ac.Name.Trim().Replace("\"", ""); // get position plane.Lat = ReadPropertyDouble(ac, 4); plane.Lon = ReadPropertyDouble(ac, 5); // get altitude plane.Alt = ReadPropertyDouble(ac, 6); // get callsign plane.Call = ReadPropertyString(ac, 5); // get registration plane.Reg = ReadPropertyString(ac, 2); // get track plane.Track = ReadPropertyDouble(ac, 7); // get speed plane.Speed = ReadPropertyDouble(ac, 8); // get position timestamp long l = ReadPropertyLong(ac, 9); if (l != long.MinValue) { DateTime timestamp = new System.DateTime(1970, 1, 1, 0, 0, 0, 0); timestamp = timestamp.AddSeconds(l); plane.Time = timestamp; } else { // skip plane if no valid timestamp found continue; } // get type info plane.Type = ReadPropertyString(ac, 0); planes.Add(plane); } catch (Exception ex) { Console.WriteLine("[" + this.GetType().Name + "]: " + ex.Message); } } } catch (Exception ex) { string filename = args.TmpDirectory + Path.DirectorySeparatorChar + this.GetType().Name + "_" + DateTime.UtcNow.ToString("yyyy-MM-dd HH_mm_ss") + ".json"; Console.WriteLine("[" + this.GetType().Name + "]: " + ex.Message + "\n\nJSON response saved as: " + filename); // save the JSON file try { using (StreamWriter sw = new StreamWriter(filename)) { sw.WriteLine(json); } } catch { // do nothing if saving fails } // forward exception to parent thread throw new Exception(ex.Message, ex.InnerException); } Console.WriteLine("[" + this.GetType().Name + "]: Returning " + planes.Count + " planes"); return(planes); }
protected override void OnDoWork(DoWorkEventArgs e) { Log.WriteMessage("Started."); Arguments = (PlaneFeedWorkEventArgs)e.Argument; if (String.IsNullOrEmpty(Thread.CurrentThread.Name)) { Thread.CurrentThread.Name = this.GetType().Name; } // use PlaneInfoConverter for plausibility check PlaneInfoConverter C = new PlaneInfoConverter(); // check boundaries if ((Arguments.MaxLat <= Arguments.MinLat) || (Arguments.MaxLon <= Arguments.MinLon)) { Status = STATUS.ERROR; this.ReportProgress((int)PROGRESS.ERROR, "Area boundaries mismatch. Check your Covered Area parameters!"); Log.WriteMessage("Area boundaries mismatch. Check your Covered Area parameters!", LogLevel.Error); } else { if (Arguments.Feed == null) { Status = STATUS.ERROR; this.ReportProgress((int)PROGRESS.ERROR, "Plane feed plugin not found. Check your settings!"); Log.WriteMessage("Plane feed plugin not found. Check your settings!", LogLevel.Error); } else { do { try { Status = STATUS.OK; int interval = Arguments.Interval; // build arguments PlaneFeedPluginArgs feedargs = new PlaneFeedPluginArgs(); feedargs.AppDirectory = Arguments.AppDirectory; feedargs.AppDataDirectory = Arguments.AppDataDirectory; feedargs.LogDirectory = Arguments.LogDirectory; feedargs.TmpDirectory = Arguments.TmpDirectory; feedargs.DatabaseDirectory = Arguments.DatabaseDirectory; feedargs.MaxLat = Arguments.MaxLat; feedargs.MinLon = Arguments.MinLon; feedargs.MinLat = Arguments.MinLat; feedargs.MaxLon = Arguments.MaxLon; feedargs.MyLat = Arguments.MyLat; feedargs.MyLon = Arguments.MyLon; feedargs.DXLat = Arguments.DXLat; feedargs.DXLon = Arguments.DXLon; feedargs.MinAlt = Arguments.MinAlt; feedargs.MaxAlt = Arguments.MaxAlt; feedargs.KeepHistory = Arguments.KeepHistory; feedargs.InstanceID = Arguments.InstanceID; feedargs.SessionKey = Arguments.SessionKey; feedargs.GetKeyURL = Arguments.GetKeyURL; // do start procedure Arguments.Feed.Start(feedargs); // run inner loop do { // call plugin's interface to get the planes try { Stopwatch st = new Stopwatch(); st.Start(); // get plane raw data and do addtional checks PlaneFeedPluginPlaneInfoList acs = Arguments.Feed.GetPlanes(feedargs); PlaneInfoList planes = new PlaneInfoList(); int total = acs.Count; int count = 0; int errors = 0; foreach (PlaneFeedPluginPlaneInfo ac in acs) { // skip without error when on ground if (ac.Ground) { continue; } // copy raw data to new PlaneInfo object PlaneInfo plane = new PlaneInfo(); plane.Hex = ac.Hex; plane.Lat = ac.Lat; plane.Lon = ac.Lon; plane.Alt = ac.Alt; plane.Call = ac.Call; plane.Reg = ac.Reg; plane.Track = ac.Track; plane.Speed = ac.Speed; plane.Time = ac.Time; plane.From = ac.From; plane.To = ac.To; plane.VSpeed = ac.VSpeed; try { plane.Category = (PLANECATEGORY)ac.Category; } catch { plane.Category = PLANECATEGORY.NONE; } plane.Type = ac.Type; plane.Model = ac.Model; plane.Manufacturer = ac.Manufacturer; // start checks // assuming that at least a timestamp is set! // do basic check on hex --> is strictly needed as identifier if (!PlaneInfoChecker.Check_Hex(plane.Hex)) { // try to fill hex from reg if (!PlaneInfoChecker.Check_Reg(plane.Reg)) { if (Arguments.LogErrors) { Log.WriteMessage("Incorrect aircraft data received [Hex]: " + plane.Hex, LogLevel.Warning); } errors++; continue; } AircraftDesignator ad = AircraftData.Database.AircraftFindByReg(plane.Reg); if (ad == null) { if (Arguments.LogErrors) { Log.WriteMessage("Incorrect aircraft data received [Hex]: " + plane.Hex, LogLevel.Warning); } errors++; continue; } plane.Hex = ad.Hex; } // check latitude if (!PlaneInfoChecker.Check_Lat(plane.Lat)) { if (Arguments.LogErrors) { Log.WriteMessage("Incorrect aircraft data received [Lat]: " + plane.Lat.ToString("F8", CultureInfo.InvariantCulture), LogLevel.Warning); } errors++; continue; } // skip without error when latitude is out of scope if ((plane.Lat < Arguments.MinLat) || (plane.Lat > Arguments.MaxLat)) { continue; } // check longitude if (!PlaneInfoChecker.Check_Lon(plane.Lon)) { if (Arguments.LogErrors) { Log.WriteMessage("Incorrect aircraft data received [Lon]: " + plane.Lon.ToString("F8", CultureInfo.InvariantCulture), LogLevel.Warning); } errors++; continue; } // skip without error when longitude is out of scope if ((plane.Lon < Arguments.MinLon) || (plane.Lon > Arguments.MaxLon)) { continue; } // check altitude if (!PlaneInfoChecker.Check_Alt(plane.Alt)) { // try to recover altitude from previuos messages PlaneInfo info = null; if (PlanePositions.TryGetValue(plane.Hex, out info)) { plane.Alt = info.Alt; } else { if (Arguments.LogErrors) { Log.WriteMessage("Incorrect aircraft data received [Alt]: " + plane.Alt.ToString("F8", CultureInfo.InvariantCulture), LogLevel.Warning); } errors++; continue; } } // skip without error when altitude_ is out of bounds if ((plane.Alt_m < Arguments.MinAlt) || (plane.Alt_m > Arguments.MaxAlt)) { continue; } // check call if (!PlaneInfoChecker.Check_Call(plane.Call)) { // try to recover from cache if check fails or set it to [unknown] PlaneInfo info = null; if (PlanePositions.TryGetValue(plane.Hex, out info)) { plane.Call = info.Call; } else { plane.Call = "[unknown]"; } } // still unknown call --> try to recover last known call from database if (!PlaneInfoChecker.Check_Call(plane.Call)) { AircraftDesignator ad = AircraftData.Database.AircraftFindByHex(plane.Hex); if (ad != null) { plane.Call = ad.Call; } else { plane.Call = "[unknown]"; } } // check registration if (!PlaneInfoChecker.Check_Reg(plane.Reg)) { // try to recover from cache if check fails or set it to [unknown] PlaneInfo info = null; if (PlanePositions.TryGetValue(plane.Hex, out info)) { plane.Reg = info.Reg; } else { plane.Reg = "[unknown]"; } } // still unknown --> try to recover last known reg from database if (!PlaneInfoChecker.Check_Reg(plane.Reg)) { AircraftDesignator ad = AircraftData.Database.AircraftFindByHex(plane.Hex); if (ad != null) { plane.Reg = ad.Reg; } else { plane.Reg = "[unknown]"; } } // check speed if (!PlaneInfoChecker.Check_Track(plane.Track)) { if (Arguments.LogErrors) { Log.WriteMessage("Incorrect aircraft data received [Track]: " + plane.Track.ToString("F8", CultureInfo.InvariantCulture), LogLevel.Warning); } errors++; continue; } // check speed if (!PlaneInfoChecker.Check_Speed(plane.Speed)) { // try to recover speed from previous messages PlaneInfo info = null; if (PlanePositions.TryGetValue(plane.Hex, out info)) { plane.Speed = info.Speed; } else { if (Arguments.LogErrors) { Log.WriteMessage("Incorrect aircraft data received [Speed]: " + plane.Speed.ToString("F8", CultureInfo.InvariantCulture), LogLevel.Warning); } errors++; continue; } } // check type if (!PlaneInfoChecker.Check_Type(plane.Type)) { AircraftDesignator ad = AircraftData.Database.AircraftFindByHex(plane.Hex); if (ad != null) { plane.Type = ad.TypeCode; // getrest of info later later } else { // set all type info to unknown plane.Type = "[unknown]"; plane.Model = "[unknown]"; plane.Manufacturer = "[unknown]"; plane.Category = PLANECATEGORY.NONE; } } // try to recover type info from database if check fails or unknown if (!PlaneInfoChecker.Check_Manufacturer(plane.Manufacturer) || !PlaneInfoChecker.Check_Model(plane.Model) || (plane.Manufacturer == "[unkonwn]") || (plane.Model == "[unknown]")) { AircraftTypeDesignator td = AircraftData.Database.AircraftTypeFindByICAO(plane.Type); if (td != null) { plane.Manufacturer = td.Manufacturer; plane.Model = td.Model; plane.Category = td.Category; } else { plane.Manufacturer = "[unknown]"; plane.Model = "[unknown]"; plane.Category = PLANECATEGORY.NONE; } } // remove manufacturer info if part of model description if (plane.Model.StartsWith(plane.Manufacturer)) { plane.Model = plane.Model.Remove(0, plane.Manufacturer.Length).Trim(); } // check position against estimated position from last konwn if possible PlaneInfo oldplane = PlanePositions.Get(plane.Hex, plane.Time, 5); double dist = 0; if (Arguments.ExtendedPlausibilityCheck_Enable && (oldplane != null) && ((dist = LatLon.Distance(oldplane.Lat, oldplane.Lon, plane.Lat, plane.Lon)) > Arguments.ExtendedPlausiblityCheck_MaxErrorDist)) { // report error if (Arguments.LogErrors) { Log.WriteMessage("Incorrect aircraft position received [(" + oldplane.Lat.ToString("F8") + "," + oldplane.Lon.ToString("F8") + ")<" + dist.ToString("F0") + "km>(" + plane.Lat.ToString("F8") + "," + plane.Lon.ToString("F8") + ")]: " + plane.ToString(), LogLevel.Warning); } errors++; continue; } // all checks successfully done --> add plane to list planes.Add(plane); count++; // cancel thread if requested if (this.CancellationPending) { return; } } // update local cache this.PlanePositions.BulkInsertOrUpdateIfNewer(planes); // report planes to main program this.ReportProgress((int)PROGRESS.PLANES, planes); // update global database AircraftData.Database.PlaneInfoBulkInsertOrUpdateIfNewer(this, planes); // update position database if enabled if (Arguments.KeepHistory) { AircraftPositionData.Database.PlaneInfoBulkInsertOrUpdateIfNewer(planes); } st.Stop(); string msg = "[" + DateTime.UtcNow.ToString("HH:mm:ss") + "] " + total.ToString() + " Positions updated from " + Arguments.Feed.Name + ", " + st.ElapsedMilliseconds.ToString() + " ms. OK: " + count.ToString() + ", Errors: " + errors.ToString(); this.ReportProgress((int)PROGRESS.STATUS, msg); // write all planes to file try { using (StreamWriter sw = new StreamWriter(Path.Combine(Arguments.TmpDirectory, "planes.csv"))) { sw.WriteLine("Time;Hex;Lat;Lon;Alt;Track;Speed;Call;Reg;From;To;VSpeed"); foreach (PlaneInfo plane in planes) { sw.WriteLine(plane.Time + ";" + plane.Hex + ";" + plane.Lat + ";" + plane.Lon + ";" + plane.Alt + ";" + plane.Track + ";" + plane.Speed + ";" + plane.Call + ";" + plane.Reg + ";" + plane.From + ";" + plane.To + ";" + plane.VSpeed); } } } catch (Exception ex) { // do nothing } } catch (Exception ex) { Status = STATUS.ERROR; this.ReportProgress((int)PROGRESS.ERROR, "Plane Feed Execption: " + ex.Message); Log.WriteMessage(ex.ToString(), LogLevel.Error); } // wait for next execution int i = 0; while (!CancellationPending && (i < interval)) { Thread.Sleep(1000); i++; } }while (!CancellationPending); // do stop procedure Arguments.Feed.Stop(feedargs); } catch (Exception ex) { Status = STATUS.ERROR; this.ReportProgress((int)PROGRESS.ERROR, "Plane Feed Execption: " + ex.Message); Log.WriteMessage(ex.ToString(), LogLevel.Error); Console.WriteLine("Plane Feed Execption: " + ex.ToString(), LogLevel.Error); } }while (!this.CancellationPending); } } this.ReportProgress((int)PROGRESS.FINISHED); Log.WriteMessage("Finished."); }
public PlaneFeedPluginPlaneInfoList GetPlanes(PlaneFeedPluginArgs args) { // intialize variables VarConverter VC = new VarConverter(); VC.AddVar("APPDIR", args.AppDirectory); VC.AddVar("DATADIR", args.AppDataDirectory); VC.AddVar("LOGDIR", args.LogDirectory); VC.AddVar("DATABASEDIR", args.DatabaseDirectory); VC.AddVar("MINLAT", args.MinLat); VC.AddVar("MAXLAT", args.MaxLat); VC.AddVar("MINLON", args.MinLon); VC.AddVar("MAXLON", args.MaxLon); VC.AddVar("MINALTM", args.MinAlt); VC.AddVar("MAXALTM", args.MaxAlt); VC.AddVar("MINALTFT", (int)UnitConverter.m_ft((double)args.MinAlt)); VC.AddVar("MAXALTFT", (int)UnitConverter.m_ft((double)args.MaxAlt)); // initialize plane info list PlaneFeedPluginPlaneInfoList planes = new PlaneFeedPluginPlaneInfoList(); string json = ""; // calculate url and get json String url = VC.ReplaceAllVars(Settings.URL); Console.WriteLine("[" + this.GetType().Name + "]: Creating web request: " + url); // HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url); // webrequest.Referer = "http://www.vrs-world.com/"; // webrequest.Timeout = Settings.Timeout * 1000; // webrequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0"; // webrequest.Accept = "application/json, text/javascript, */*;q=0.01"; // webrequest.AutomaticDecompression = System.Net.DecompressionMethods.Deflate | System.Net.DecompressionMethods.GZip; // webrequest.Headers.Add("api-auth:" + APIKey); // Console.WriteLine("[" + this.GetType().Name + "]: Getting web response"); // HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse(); // Console.WriteLine("[" + this.GetType().Name + "]: Reading stream"); // // using (StreamReader sr = new StreamReader(webresponse.GetResponseStream())) // { // json = sr.ReadToEnd(); // } // */ json = VRSTlsClient.DownloadFile(url, Settings.Timeout * 1000, APIKey); // save raw data to file if enabled if (Settings.SaveToFile) { using (StreamWriter sw = new StreamWriter(args.TmpDirectory + Path.DirectorySeparatorChar + this.GetType().Name + "_" + DateTime.UtcNow.ToString("yyyy-MM-dd HH_mm_ss") + ".json")) { sw.WriteLine(json); } } Console.WriteLine("[" + this.GetType().Name + "]: Analyzing data"); // JavaScriptSerializer js = new JavaScriptSerializer(); // dynamic root = js.Deserialize<dynamic>(json); dynamic root = JsonConvert.DeserializeObject(json); // 2017-07-23: workaround for "jumping planes" due to incorrect time stamps // try to get the server time to adjust the time stamps in plane positions // --> compare server time with local time and calculate offset // default offset is 0 long toffset = 0; try { // get local time of request in milliseconds DateTime lt = DateTime.UtcNow; long ltime = (long)(lt - new DateTime(1970, 1, 1)).TotalMilliseconds; // get server time in milliseconds long stime = ReadPropertyLong(root, "stm"); DateTime sti = new System.DateTime(1970, 1, 1, 0, 0, 0, 0).AddMilliseconds(stime); // calculate offset in milliseconds toffset = ltime - stime; // check value string message = "Server timestamp difference (server <> local): " + sti.ToString("yyyy-MM-dd HH:mm:ss,fffZ") + " <> " + lt.ToString("yyyy-MM-dd HH:mm:ss,fffZ"); // server time is more than 10.000 ms in the future --> keep offset for correction and log an error message // else --> set offset to 0 to work with real timestamps of entries without correction if (toffset <= -10000) { message += " --> timestamp correction applied!"; // Log.WriteMessage(message); } else { // clear offset toffset = 0; } // analyze json string for planes data // get the planes position list var aclist = root["acList"]; foreach (var ac in aclist) { try { PlaneFeedPluginPlaneInfo plane = new PlaneFeedPluginPlaneInfo(); // get hex first plane.Hex = ReadPropertyString(ac, "Icao").Trim().Replace("\"", ""); // get position plane.Lat = ReadPropertyDouble(ac, "Lat"); plane.Lon = ReadPropertyDouble(ac, "Long"); // get altitude // 2017-07-23: take "GAlt" (corrected altitude by air pressure) rather than "Alt" plane.Alt = ReadPropertyInt(ac, "GAlt"); // get callsign plane.Call = ReadPropertyString(ac, "Call"); // get registration plane.Reg = ReadPropertyString(ac, "Reg"); // get track plane.Track = ReadPropertyInt(ac, "Trak"); // get speed plane.Speed = ReadPropertyInt(ac, "Spd"); // get position timestamp // CAUTION!! time is UNIX time in milliseconds long l = ReadPropertyLong(ac, "PosTime"); if (l != long.MinValue) { // 2017-07-23: correct timestamp with offset l = l + toffset; DateTime timestamp = new System.DateTime(1970, 1, 1, 0, 0, 0, 0); timestamp = timestamp.AddMilliseconds(l); plane.Time = timestamp; } else { // skip plane if no valid timestamp found continue; } // get type info plane.Type = ReadPropertyString(ac, "Type"); // get extended plane type info plane.Manufacturer = ReadPropertyString(ac, "Man"); plane.Model = ReadPropertyString(ac, "Mdl"); long cat = ReadPropertyLong(ac, "WTC"); switch (cat) { case 1: plane.Category = 1; break; case 2: plane.Category = 2; break; case 3: plane.Category = 3; break; case 4: plane.Category = 4; break; default: plane.Category = 0; break; } // do correction of A380 as "SuperHeavy" is not supported if (plane.Type == "A388") { plane.Category = 4; } // get country plane.Country = ReadPropertyString(ac, "Cou"); // get departure airport plane.From = ReadPropertyString(ac, "From"); // get destination airport plane.To = ReadPropertyString(ac, "To"); // get vertical speed plane.VSpeed = ReadPropertyInt(ac, "Vsi"); // add plane to list planes.Add(plane); } catch (Exception ex) { Console.WriteLine("[" + System.Reflection.MethodBase.GetCurrentMethod().Name + "]" + ex.Message); } } } catch (Exception ex) { // do nothing if property is not found } Console.WriteLine("[" + this.GetType().Name + "]: Returning " + planes.Count + " planes"); return(planes); }