Exemplo n.º 1
0
 public void AddEvent(TrafficEvent ev)
 {
     if (ev != TrafficEvent.TE_NONE && trackData_)
     {
         eventCount_[(int)ev]++;
     }
 }
Exemplo n.º 2
0
 public void trafficUpload(TrafficEvent tevent)
 {
     ////#MLog.println("上传 "+event.getTraffic());
     netStatus.addUpload(tevent.getTraffic());
     lastTrafficTime                           = DateTime.Now.Millisecond
                                    uploadSum += tevent.getTraffic();
 }
Exemplo n.º 3
0
        public void EventTimeUponConstruction_GivenArguments_ExpectSuccess()
        {
            //Arrange
            var time   = DateTime.Now;
            var span   = new TimeSpan(3);
            var @event = new TrafficEvent(time, span, new Stage(), stage => stage.Activate(time));

            //Act && Assert
            Assert.IsTrue(@event.Time == time);
            Assert.IsTrue(@event.Span == span);
        }
Exemplo n.º 4
0
 public void StoreEvent(TrafficEvent trafficEvent)
 {
     if (_database != null)
     {
         _eventsCollection.Insert(trafficEvent);
     }
     else
     {
         _logger.LogError($"database: {_options.File} and collection: {DefaultCollectionName} are not initialized.");
     }
 }
Exemplo n.º 5
0
 // Update is called once per frame
 void Update()
 {
     visible_ = GetComponent <Renderer>().isVisible;
     if (visible_)
     {
         if (curEvent_ != prevEvent_ && curEvent_ != TrafficEvent.TE_NONE)
         {
             tracker_.AddEvent(curEvent_);
         }
     }
     prevEvent_ = curEvent_;
 }
Exemplo n.º 6
0
        /// <summary>
        /// 添加一个持续时长拥堵事件
        /// </summary>
        /// <param name="context"></param>
        /// <param name="region"></param>
        /// <param name="time"></param>
        /// <param name="durationMinutes"></param>
        /// <returns></returns>
        private DateTime AddDuration(DensityContext context, TrafficRegion region, DateTime time, int durationMinutes)
        {
            TrafficEvent trafficEvent = new TrafficEvent
            {
                DataId   = region.DataId,
                DateTime = time,
                EndTime  = time.AddMinutes(durationMinutes)
            };

            context.Events.Add(trafficEvent);
            return(time.AddMinutes(durationMinutes));
        }
Exemplo n.º 7
0
        public void Peek_GivenQueue_ExpectSuccess()
        {
            //Arrange
            var queue  = new PriorityQueue <TrafficEvent>();
            var time   = DateTime.Now;
            var @event = new TrafficEvent(time, new TimeSpan(3), new Stage(), stage => stage.Activate(time));

            //Act
            queue.Add(@event);

            //Assert
            Assert.IsTrue(queue.Peek().Equals(@event));
        }
Exemplo n.º 8
0
 /// <summary>
 /// 填充事件缓存
 /// </summary>
 /// <param name="memoryCache">缓存</param>
 /// <param name="trafficEvent">事件数据</param>
 /// <returns>事件</returns>
 public static TrafficEvent FillEvent(this IMemoryCache memoryCache, TrafficEvent trafficEvent)
 {
     if (trafficEvent != null)
     {
         TrafficRegion region = memoryCache.GetRegion(trafficEvent.DataId);
         if (region != null)
         {
             trafficEvent.RegionName   = region.RegionName;
             trafficEvent.CrossingId   = region.Channel.CrossingId ?? 0;
             trafficEvent.CrossingName = region.Channel.RoadCrossing?.CrossingName;
         }
     }
     return(trafficEvent);
 }
Exemplo n.º 9
0
        public void Add_GivenTrafficEvent_ExpectSuccess()
        {
            //Arrange
            var queue  = new PriorityQueue <TrafficEvent>();
            var time   = DateTime.Now;
            var @event = new TrafficEvent(time, new TimeSpan(3), new Stage(), stage => stage.Activate(time));
            var event1 = new TrafficEvent(new DateTime(30), new TimeSpan(3), new Stage(1), stage => stage.Activate(time));

            //Act
            queue.Add(@event);
            queue.Add(event1);

            //Assert
            Assert.IsTrue(queue.Count == 2);
            Assert.IsTrue(queue.Peek().Equals(event1));
        }
Exemplo n.º 10
0
        public void Process_GivenEvent_ExpectSuccess()
        {
            //Arrange
            var lights = Enumerable.Range(0, 4).Select(i => new Light(Colour.Red, new Position(i))).ToList();
            var stages = Enumerable.Range(0, 3).Select(i => new Stage(i)
            {
                Lights = lights
            }).ToList();

            stages[0].Next = stages[1];
            stages[1].Next = stages[2];
            stages[2].Next = stages[0];

            var time   = DateTime.Now;
            var @event = new TrafficEvent(time, new TimeSpan(3), stages[0], stage => stage.Activate(time));

            //Act
            @event.Process();

            //Assert
            Assert.IsTrue(stages[0].IsActive);
        }
Exemplo n.º 11
0
        public Route(int mode, int port = 150)
        {
            LocalClientId = Ran.Next();
            Mode = mode;

            if (mode == ClientMode)
            {
                MyUdpClient = new UdpClient();
            }
            else if (mode == ClientMode)
            {
                MyUdpClient = new UdpClient(port);
            }

            MyClientManager = new ClientManager(this);
            Task.Factory.StartNew(() => {
                while (true)
                {
                    try
                    {
                        IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
                        byte[] data = MyUdpClient.Receive(ref RemoteIpEndPoint);
                        DatagramPacket datagramPacket = new DatagramPacket(data, data.Length);
                        PacketBuffer.Add(datagramPacket);
                    }
                    catch (Exception e)
                    {
                        Thread.Sleep(1);
                        continue;
                    }
                }
            });

            Task.Factory.StartNew(() => {
                while (true)
                {
                    DatagramPacket datagramPacket = PacketBuffer.Take();
                    if (datagramPacket == null) continue;

                    byte[] data = datagramPacket.Data;
                    if (data.Length < 4)
                    {
                        return;
                    }

                    int sType = PacketCheck.CheckSType(datagramPacket);
                    int connectId = BitConverter.ToInt32(data, 4);
                    int remoteClientId = BitConverter.ToInt32(data, 8);

                    if (ClosedTable.Contains(connectId) && connectId != 0) continue;

                    ClientControl clientControl;
                    if (sType == PacketType.PingPacket || sType == PacketType.PingPacket2)
                    {
                        if (mode == ClientMode)
                        {
                            string key = datagramPacket.Host + ":" + datagramPacket.Port;
                            int simClientId = Math.Abs(key.GetHashCode());
                            clientControl = MyClientManager.GetClientControl(simClientId, datagramPacket.Host, datagramPacket.Port);
                        }
                        else if (mode == ServerMode)
                        {
                            clientControl = MyClientManager.GetClientControl(remoteClientId, datagramPacket.Host, datagramPacket.Port);
                        }
                    }
                    else
                    {
                        if (mode == ClientMode)
                        {
                            if (!SettedTable.Contains(remoteClientId))
                            {
                                string key = datagramPacket.Host + ":" + datagramPacket.Port;
                                int simClientId = Math.Abs(key.GetHashCode());
                                clientControl = MyClientManager.GetClientControl(simClientId, datagramPacket.Host, datagramPacket.Port);

                                if (clientControl.ClientIdReal == -1)
                                {
                                    clientControl.ClientIdReal = remoteClientId;
                                }
                                else if (clientControl.ClientIdReal != remoteClientId)
                                {
                                    clientControl.UpdateClientId(remoteClientId);
                                }

                                SettedTable.Add(remoteClientId);
                            }
                        }
                        else if (mode == ServerMode)
                        {
                            GetConnection2(datagramPacket.Host, datagramPacket.Port, connectId, remoteClientId);
                        }

                        ConnectionUDP conn = ConnTable[connectId];
                        if (conn != null)
                        {
                            conn.MyRecevier.OnReceivePacket(datagramPacket);
                            if (sType == PacketType.DataPacket)
                            {
                                TrafficEvent e = new TrafficEvent("", Ran.Next(), datagramPacket.Data.Length, TrafficEvent.Download);
                                FireEvent(e);
                            }
                        }
                    }
                }
            });
        }
Exemplo n.º 12
0
 public static void FireEvent(TrafficEvent e)
 {
     foreach (TrafficListener listener in ListenerList)
     {
         int type = e.Type;
         if (type == TrafficEvent.Download)
         {
             listener.TrafficDownload(e);
         }
         else if (type == TrafficEvent.Upload)
         {
             listener.TrafficUpload(e);
         }
     }
 }
Exemplo n.º 13
0
 private void OnTraffic(long pingSeconds)
 {
     TrafficEvent?.Invoke(this, new TrafficEventArgs(pingSeconds));
 }
Exemplo n.º 14
0
 public void SendEvent(TrafficEvent ev)
 {
     curEvent_ = ev;
 }
Exemplo n.º 15
0
        public static Dictionary <TrafficEvent, int> CreateData(IServiceProvider serviceProvider, List <DensityDevice> devices, DateTime startDate, DateTime endDate, DataCreateMode mode, bool initDatabase = false)
        {
            if (initDatabase)
            {
                ResetDatabase(serviceProvider);
            }

            using (IServiceScope serviceScope = serviceProvider.CreateScope())
            {
                using (DensityContext context = serviceScope.ServiceProvider.GetRequiredService <DensityContext>())
                {
                    Dictionary <TrafficEvent, int> result = new Dictionary <TrafficEvent, int>();
                    Random random = new Random();

                    int hours = Convert.ToInt32((endDate - startDate).TotalHours + 24);
                    foreach (DensityDevice device in devices)
                    {
                        foreach (var relation in device.DensityDevice_DensityChannels)
                        {
                            foreach (TrafficRegion region in relation.Channel.Regions)
                            {
                                if (mode == DataCreateMode.Fixed)
                                {
                                    for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))
                                    {
                                        for (int h = 0; h < 24; ++h)
                                        {
                                            TrafficEvent trafficEvent1 = new TrafficEvent
                                            {
                                                DataId   = region.DataId,
                                                DateTime = date.AddHours(h),
                                                EndTime  = date.AddHours(h).AddMinutes(1)
                                            };
                                            context.Events.Add(trafficEvent1);
                                            TrafficEvent trafficEvent2 = new TrafficEvent
                                            {
                                                DataId   = region.DataId,
                                                DateTime = date.AddHours(h).AddMinutes(30),
                                                EndTime  = date.AddHours(h).AddMinutes(30).AddMinutes(1)
                                            };
                                            context.Events.Add(trafficEvent2);
                                        }
                                    }
                                }
                                else if (mode == DataCreateMode.Random)
                                {
                                    int value = random.Next(1, hours);
                                    result.Add(new TrafficEvent
                                    {
                                        DataId = region.DataId
                                    }, value);
                                    for (int h = 0; h < value; ++h)
                                    {
                                        TrafficEvent trafficEvent = new TrafficEvent
                                        {
                                            DataId   = region.DataId,
                                            DateTime = startDate.AddHours(h),
                                            EndTime  = startDate.AddHours(h).AddMinutes(1)
                                        };
                                        context.Events.Add(trafficEvent);
                                    }
                                }
                            }
                        }
                    }

                    context.BulkSaveChanges(options => options.BatchSize = 1000);
                    return(result);
                }
            }
        }
Exemplo n.º 16
0
 private static void TrafficEventReceived(TrafficEvent trafficEvent)
 {
     Console.WriteLine(trafficEvent);
 }
Exemplo n.º 17
0
        /// <summary>
        /// 时间段交集
        /// </summary>
        /// <param name="dataId">数据编号</param>
        /// <param name="list1">事件集合1</param>
        /// <param name="list2">事件集合2</param>
        /// <returns>交集结果</returns>
        private List <TrafficEvent> Intersect(string dataId, List <TrafficEvent> list1, List <TrafficEvent> list2)
        {
            if (list1.Count == 0 || list2.Count == 0)
            {
                return(new List <TrafficEvent>());
            }

            int days = Convert.ToInt32((new DateTime(list2[0].DateTime.Year, list2[0].DateTime.Month, list2[0].DateTime.Day) -
                                        new DateTime(list1[0].DateTime.Year, list1[0].DateTime.Month, list1[0].DateTime.Day)).TotalDays);
            List <DateTime> d1 = new List <DateTime>();

            foreach (TrafficEvent trafficEvent in list1)
            {
                for (DateTime d = trafficEvent.DateTime; d <= trafficEvent.EndTime; d = d.AddSeconds(1))
                {
                    d1.Add(d);
                }
            }

            List <DateTime> d2 = new List <DateTime>();

            foreach (TrafficEvent trafficEvent in list2)
            {
                for (DateTime d = trafficEvent.DateTime; d <= trafficEvent.EndTime; d = d.AddSeconds(1))
                {
                    d2.Add(d.AddDays(-days));
                }
            }

            List <DateTime> d3 = d1.Intersect(d2).OrderBy(d => d).ToList();

            List <TrafficEvent> list = new List <TrafficEvent>();
            TrafficEvent        temp = new TrafficEvent();

            for (int i = 0; i < d3.Count; ++i)
            {
                if (temp.DateTime == DateTime.MinValue)
                {
                    temp.DateTime = d3[i];
                    temp.EndTime  = d3[i];
                }
                else
                {
                    if (d3[i - 1].AddSeconds(1) == d3[i])
                    {
                        temp.EndTime = d3[i];
                    }
                    else
                    {
                        list.Add(new TrafficEvent
                        {
                            DataId   = dataId,
                            DateTime = temp.DateTime,
                            EndTime  = temp.EndTime
                        });
                        temp.DateTime = d3[i];
                        temp.EndTime  = d3[i];
                    }

                    if (i == d3.Count - 1)
                    {
                        list.Add(new TrafficEvent
                        {
                            DataId   = dataId,
                            DateTime = temp.DateTime,
                            EndTime  = temp.EndTime
                        });
                    }
                }
            }
            return(list);
        }
Exemplo n.º 18
0
        /// <summary>
        /// 时间段并集
        /// </summary>
        /// <param name="dataId">数据编号</param>
        /// <param name="list1">事件集合1</param>
        /// <param name="list2">事件集合2</param>
        /// <returns>并集结果</returns>
        private List <TrafficEvent> Union(string dataId, List <TrafficEvent> list1, List <TrafficEvent> list2)
        {
            List <DateTime> d1 = new List <DateTime>();

            foreach (TrafficEvent trafficEvent in list1)
            {
                for (DateTime d = trafficEvent.DateTime; d <= trafficEvent.EndTime; d = d.AddSeconds(1))
                {
                    d1.Add(d);
                }
            }

            List <DateTime> d2 = new List <DateTime>();

            foreach (TrafficEvent trafficEvent in list2)
            {
                for (DateTime d = trafficEvent.DateTime; d <= trafficEvent.EndTime; d = d.AddSeconds(1))
                {
                    d2.Add(d);
                }
            }

            List <DateTime> d3 = d1.Union(d2).OrderBy(d => d).ToList();


            List <TrafficEvent> list = new List <TrafficEvent>();

            TrafficEvent temp = new TrafficEvent();

            for (int i = 0; i < d3.Count; ++i)
            {
                if (temp.DateTime == DateTime.MinValue)
                {
                    temp.DateTime = d3[i];
                    temp.EndTime  = d3[i];
                }
                else
                {
                    if (d3[i - 1].AddSeconds(1) == d3[i])
                    {
                        temp.EndTime = d3[i];
                    }
                    else
                    {
                        list.Add(new TrafficEvent
                        {
                            DataId   = dataId,
                            DateTime = temp.DateTime,
                            EndTime  = temp.EndTime
                        });
                        temp.DateTime = d3[i];
                        temp.EndTime  = d3[i];
                    }

                    if (i == d3.Count - 1)
                    {
                        list.Add(new TrafficEvent
                        {
                            DataId   = dataId,
                            DateTime = temp.DateTime,
                            EndTime  = temp.EndTime
                        });
                    }
                }
            }

            return(list);
        }