Example #1
0
      protected override void OnPeriodicElapsed(object sender, System.Timers.ElapsedEventArgs e) {
         var messages = _reader.TMC.Messages;
         if (messages == null || messages.Count == 0)
            return;

         BplLanguage.RuntimeMode = BplRuntimeMode.Batch;
         var res = new List<TrafficEvent>();
         foreach (var msg in messages) {

            var origin = new TrafficLocation {
               CountryCode = msg.CC,
               LocationTableNumber = msg.LTN,
               LocationCode = msg.LocationCode
            };

            var evt = new TrafficEvent {
               //Age = new TimeFrame(DateTime.Now, DateTime.Now.AddMinutes(msg.Duration)),
               Direction = msg.DirectionFlag ? 1 : 0,
               Extent = msg.Extent,
               IsTwoWay = msg.IsTwoWay,
               LastUpdate = DateTime.Now,
               Origin = origin
            };            
            
            evt.Codes = new BplArray<int>(msg.EventCodes);

            if (evt.Codes.Count > 0) {
               res.Add(evt);
            }
         }
         UpdateTrafficEvents(res.ToArray());
      }
Example #2
0
 /// <summary>Gets whether the traffic event is the sequel of other traffic event (has the same type, boundaries and characteristics.</summary>
 /// <param name="other">The other <see cref="TrafficEvent"/> for comparison.</param>
 /// <returns></returns>
 public bool IsSequelOf(TrafficEvent other) {
    if (other == null) return false;
    return other != null &&
       (Codes == other.Codes || Codes.SequenceEqual(other.Codes)) &&
       Load == other.Load &&
       TrafficLocation.MemberwiseEquals(Origin, other.Origin) &&
       TrafficLocation.MemberwiseEquals(Destination, other.Destination) &&
       Extent == other.Extent &&
       Direction == other.Direction &&
       IsTwoWay == other.IsTwoWay;
 }
Example #3
0
      internal void Handle(GetTrafficEvents input) {
         var output = new ResultSet<TrafficEvent>();

         BplCollection<TrafficEventInfo> dbEvents;

         using (var dbConn = DatabaseManager.DbConn()) {
            if ((input.Region == null) || (input.Region.IsEmpty)) {
               dbEvents = dbConn.ExecuteBpl(new TrafficEventGetByOperator { Operator = input.Operator });
            } else {
               dbEvents = dbConn.ExecuteBpl(
                  new TrafficEventGetByOperatorRegion {
                     Operator = input.Operator,
                     North = input.Region.North,
                     South = input.Region.South,
                     West = input.Region.West,
                     East = input.Region.East
                  });
            }

            foreach (var ei in dbEvents) {
               var oe = new Mpcr.Model.Vehicles.TrafficEvent();

               oe.Id = ei.EventId;
               oe.Codes = ei.Codes;
               oe.Direction = ei.Direction;
               oe.Extent = ei.Extent;
               oe.IsTwoWay = ei.IsTwoWay;

               oe.Origin = new TrafficLocation();
               oe.Origin.LocationCode = ei.OriginLocationCode;
               oe.Origin.CountryCode = ei.OriginCountryCode;
               oe.Origin.LocationTableNumber = ei.OriginLocationTableNumber;
               oe.Origin.Location = ei.OriginLocation;

               if (ei.DestinationLocation != ei.OriginLocation) {
                  oe.Destination = new TrafficLocation();
                  oe.Destination.LocationCode = ei.DestinationLocationCode;
                  oe.Destination.CountryCode = ei.DestinationCountryCode;
                  oe.Destination.LocationTableNumber = ei.DestinationLocationTableNumber;
                  oe.Destination.Location = ei.DestinationLocation;
               }

               oe.ApproximatedDelay = ei.ApproximatedDelay;
               oe.ApproximatedSpeed = ei.ApproximatedSpeed;
               oe.LastUpdate = ei.LastUpdate.DateTime;
               oe.Length = ei.Length;
               oe.Load = ei.Load;

               output.result.Add(oe);
            }
         }

         Reply(output);
      }
Example #4
0
      private void _sendTrafficState() {
         if (_reader == null || _reader.TMC == null) {
            Log.Info("No TMC data");
            return;
         }

         var messages = _reader.TMC.Messages;
         if (messages == null || messages.Count == 0) {
            Log.Info("No traffic events");
            return;
         }

         var res = new List<TrafficEvent>();
         foreach (var msg in messages) {

            var origin = new TrafficLocation {
               CountryCode = msg.CC,
               LocationTableNumber = msg.LTN,
               LocationCode = msg.LocationCode
            };

            var evt = new TrafficEvent {
               //Age = new TimeFrame(DateTime.Now, DateTime.Now.AddMinutes(msg.Duration)),
               Direction = msg.DirectionFlag ? 1 : 0,
               Extent = msg.Extent,
               IsTwoWay = msg.IsTwoWay,
               LastUpdate = DateTime.Now,
               Origin = origin
            };

            evt.Codes = new BplArray<int>(msg.EventCodes);

            if (evt.Codes.Count > 0) {
               res.Add(evt);
            }
         }

         Log.Info("Provider name: {0}", _reader.TMC.ProviderName);
         Log.Info("-------------------- Traffic Events List ----------");
         var fmtr = new Mpcr.Core.Persistence.BplXmlFormatter();
         var cnt = 1;
         foreach (var evt in res) {
            if (fmtr.Format(evt)) {
               Log.Info("\tEvent #{0}: {1}", cnt++, fmtr.Output);
            } else
               Log.Info("Cannot format event: {0}", evt);
         }
         Log.Info("===================================================");
      }
Example #5
0
      private TrafficEvent[] _processTraffic(XmlDocument data) {
         BplLanguage.RuntimeMode = BplRuntimeMode.Batch;
         var res = new List<TrafficEvent>();
         var nodes = data.SelectNodes("/Events/Event");
         foreach (XmlNode node in nodes) {
            var e = new TrafficEvent {
               ApproximatedDelay = TimeSpan.FromSeconds(int.Parse(node.SelectSingleNode("@Delay").Value)),
               ApproximatedSpeed = new Speed(int.Parse(node.SelectSingleNode("@Speed").Value)),
               Destination = new TrafficLocation {
                  CountryCode = int.Parse(node.SelectSingleNode("Destination/@CountryCode").Value),
                  Location = new Coordinate(double.Parse(node.SelectSingleNode("Destination/@latitude").Value), double.Parse(node.SelectSingleNode("Destination/@longitude").Value)),
                  LocationCode = int.Parse(node.SelectSingleNode("Destination/@LocationCode").Value),
                  LocationTableNumber = int.Parse(node.SelectSingleNode("Destination/@LocationTableNumber").Value),
               },
               Direction = int.Parse(node.SelectSingleNode("@TMC_Direction").Value) == 0 ? 1 : 0, // decell bug fix - direction bit is queue grow direction, not traffic 
               Extent = int.Parse(node.SelectSingleNode("@TMC_Extent").Value),
               IsTwoWay = false,
               LastUpdate = DateTime.ParseExact(node.SelectSingleNode("@LastUpdateTime").Value, "dd/MM/yyyy H:mm:ss", CultureInfo.InvariantCulture),
               Length = Distance.FromMeters(int.Parse(node.SelectSingleNode("@Length").Value)),
               Load = new Percent(int.Parse(node.SelectSingleNode("@Load").Value) / 4d),
               Origin = new TrafficLocation {
                  CountryCode = int.Parse(node.SelectSingleNode("Origin/@CountryCode").Value),
                  Location = new Coordinate(double.Parse(node.SelectSingleNode("Origin/@latitude").Value), double.Parse(node.SelectSingleNode("Origin/@longitude").Value)),
                  LocationCode = int.Parse(node.SelectSingleNode("Origin/@LocationCode").Value),
                  LocationTableNumber = int.Parse(node.SelectSingleNode("Origin/@LocationTableNumber").Value),
               },
            };
            var codes = node.SelectSingleNode("@TMCEventCode").Value.Split(';').Select(c => int.Parse(c));
            e.Codes = new BplArray<int>(codes);
            var parts = e.Codes.Select(c => c.ToString()).ToList();
            parts.AddRange(
               e.Origin.LocationCode.ToString(),
                  e.Destination.LocationCode.ToString(),
                  e.Origin.LocationTableNumber.ToString(),
                  e.Destination.LocationTableNumber.ToString(),
                  e.Extent.ToString(),
                  e.Direction.ToString()
                  );

            e.Id = BplIdentity.Get(parts.ToArray().Join("-"));
            if (e.Codes.Count > 0) res.Add(e);
         }
         return res.ToArray();
      }
Example #6
0
      private void _processTraffic(XmlDocument data) {
         BplLanguage.RuntimeMode = BplRuntimeMode.Batch;
         var res = new List<TrafficEvent>();
         var nodes = data.SelectNodes("/Inrix/Incidents/Incident");
         foreach (XmlNode node in nodes) {

            try {

               var rdsNode = node.SelectSingleNode("RDS");

               var destination = new TrafficLocation {
                  CountryCode = int.Parse(rdsNode.SelectSingleNode("@tmcCountry").Value),
                  LocationTableNumber = int.Parse(rdsNode.SelectSingleNode("@tmcRegion").Value),
                  LocationCode = int.Parse(rdsNode.SelectSingleNode("@tmcLocation").Value)
               };

               var originTmc = node.SelectSingleNode("TMCs/TMC[last()]/@code").Value;
               var origin = new TrafficLocation {
                  CountryCode = int.Parse(originTmc.At(0)),
                  LocationTableNumber = int.Parse(originTmc.Between(1, 2)),
                  LocationCode = int.Parse(originTmc.Between(4, 8))
               };

               var e = new TrafficEvent {
                  ApproximatedDelay = TimeSpan.FromMinutes(float.Parse(node.SelectSingleNode("DelayImpact/@fromFreeFlowMinutes").Value)),
                  ApproximatedSpeed = Speed.Undefined,
                  Destination = destination,
                  Direction = int.Parse(rdsNode.SelectSingleNode("@direction").Value),
                  Extent = int.Parse(rdsNode.SelectSingleNode("@extent").Value),
                  IsTwoWay = node.SelectNodes(@"ParameterizedDescription/Direction[. = ""BOTH""]").Count > 0,
                  LastUpdate = DateTime.Now,
                  Length = Distance.FromMeters(1000.0 * float.Parse(node.SelectSingleNode("DelayImpact/@distance").Value)),
                  Load = new Percent(int.Parse(node.SelectSingleNode("@severity").Value) / 4d),
                  Origin = origin
               };

               var eventCodes = rdsNode.SelectNodes("EventCodes/EventCode/@value");
               var codes = new int[eventCodes.Count];
               for (var i = 0; i < eventCodes.Count; i++) codes[i] = int.Parse(eventCodes[i].Value);
               e.Codes = new BplArray<int>(codes);

               var parts = e.Codes.Select(c => c.ToString()).ToList();
               parts.AddRange(
                     e.Origin.LocationCode.ToString(),
                     e.Destination.LocationCode.ToString(),
                     e.Origin.LocationTableNumber.ToString(),
                     e.Destination.LocationTableNumber.ToString(),
                     e.Extent.ToString(),
                     e.Direction.ToString()
                     );
               e.Id = parts.ToArray().Join("-");

               if (e.Codes.Count > 0) {
                  res.Add(e);
               }
            } catch (Exception) {
               //Skip <Incident> with empty <TMCs> or any other ill-formed incident
            }
         }
         UpdateTrafficEvents(res.ToArray());
      }