public static Delays Load() { if (File.Exists(FileName)) { return(JsonConvert.DeserializeObject <Delays>(File.ReadAllText(FileName))); } else { Delays delays = NewInstance(); delays.Save(); return(delays); } }
protected override void OnStart(string[] args) { try { string wsServer = ConfigurationManager.AppSettings["ws-server"]; string stanox = ConfigurationManager.AppSettings["stanox"]; if (string.IsNullOrEmpty(wsServer) || string.IsNullOrEmpty(stanox)) { throw new ArgumentNullException("", "both ws-server and stanox must be set in app.config"); } _statistics = Delays.Load(); if (_statistics.Created.Date < DateTime.Now.Date) { const string start = "Early Departures for {0:dd-MM-yy}:"; var statsByMinute = _statistics.EarlyDepartures.GroupBy(v => v); ICollection <string> values = new List <string>(statsByMinute.Count()); const string format = "{0} mins early:{1}"; foreach (var minutes in statsByMinute) { values.Add(string.Format(format, minutes.Key, minutes.Count())); } SendTweet(string.Concat(string.Format(start, _statistics.Created), string.Join(",", values.ToArray()))); _statistics = Delays.NewInstance(); _statistics.Save(); } double lat, lng; if (double.TryParse(ConfigurationManager.AppSettings["Lat"], out lat) && double.TryParse(ConfigurationManager.AppSettings["Lng"], out lng)) { Latitude = lat; Longitude = lng; } Trace.TraceInformation("Connecting to server on {0}", wsServer); _wsClient = new WebSocketClient(wsServer) { OnReceive = OnReceive, OnDisconnect = OnDisconnect }; _wsClient.Connect(); Trace.TraceInformation("Subscribing to stanox {0}", stanox); _wsClient.Send(string.Format("substanox:{0}", stanox)); } catch (Exception e) { Trace.TraceError("Error starting service: {0}", e); throw; } }
private static void OnReceive(UserContext context) { try { var data = JsonConvert.DeserializeObject <dynamic>(context.DataFrame.ToString()); var response = data.Response[0]; if (Convert.ToByte((string)response.header.msg_type) == 3 && ((string)response.body.event_type).Equals("DEPARTURE", StringComparison.InvariantCultureIgnoreCase)) { DateTime expectedTs = UnixTsToDateTime(double.Parse((string)response.body.gbtt_timestamp)); DateTime?actualTs = null; // actual time stamp property name seems to get corrupted // so just search for anything containing "act" foreach (var value in response.body) { if (value.Name.Contains("act")) { actualTs = UnixTsToDateTime(double.Parse((string)value.Value)); break; } } if (actualTs.HasValue && actualTs < expectedTs) { string trainId = (string)response.body.train_id; string tweet = string.Format("Train {0} expected to depart at {1:HH:mm:ss} actual departure {2:HH:mm:ss} - {3}{4}", trainId, expectedTs, actualTs, _url, trainId); var minsEarly = (int)(expectedTs - actualTs).Value.TotalMinutes; _statistics.EarlyDepartures.Add(minsEarly); _statistics.Save(); try { var train = _repository.GetTrain(trainId); if (train != null) { tweet = string.Format("{0} from {1}({2}) to {3}({4}) expected to depart {5:HH:mm:ss}, actual {6:HH:mm:ss} {7}{8}/{9:yyyy-MM-dd}", train.Headcode, train.OriginName, train.OriginCRS, train.DestinationName, train.DestinationCRS, expectedTs, actualTs, _url, train.TrainUid, (DateTime)train.OriginDepartTimestamp); } } catch { } Trace.TraceInformation(tweet); Trace.Flush(); SendTweet(tweet); } } } catch (Exception e) { Trace.TraceError("Unable to parse message : {0} - {1}", context.DataFrame.ToString(), e); } }
protected override void OnStart(string[] args) { try { string wsServer = ConfigurationManager.AppSettings["ws-server"]; string stanox = ConfigurationManager.AppSettings["stanox"]; if (string.IsNullOrEmpty(wsServer) || string.IsNullOrEmpty(stanox)) throw new ArgumentNullException("", "both ws-server and stanox must be set in app.config"); _statistics = Delays.Load(); if (_statistics.Created.Date < DateTime.Now.Date) { const string start = "Early Departures for {0:dd-MM-yy}:"; var statsByMinute = _statistics.EarlyDepartures.GroupBy(v => v); ICollection<string> values = new List<string>(statsByMinute.Count()); const string format = "{0} mins early:{1}"; foreach (var minutes in statsByMinute) { values.Add(string.Format(format, minutes.Key, minutes.Count())); } SendTweet(string.Concat(string.Format(start, _statistics.Created), string.Join(",", values.ToArray()))); _statistics = Delays.NewInstance(); _statistics.Save(); } double lat, lng; if (double.TryParse(ConfigurationManager.AppSettings["Lat"], out lat) && double.TryParse(ConfigurationManager.AppSettings["Lng"], out lng)) { Latitude = lat; Longitude = lng; } Trace.TraceInformation("Connecting to server on {0}", wsServer); _wsClient = new WebSocketClient(wsServer) { OnReceive = OnReceive, OnDisconnect = OnDisconnect }; _wsClient.Connect(); Trace.TraceInformation("Subscribing to stanox {0}", stanox); _wsClient.Send(string.Format("substanox:{0}", stanox)); } catch (Exception e) { Trace.TraceError("Error starting service: {0}", e); throw; } }