コード例 #1
0
        void Demo1()
        {
            Random ran = new Random();

            System.Diagnostics.Debug.WriteLine(DateTime.Now + " Tick Count:" + iCounter);
            if (iCounter == 5)
            {
                TowTruck2 truck = null;

                truck                  = new TowTruck2();
                truck.TruckId          = 123;
                truck.StateImage       = @"http://localhost/FSP.Web/Content/Images/NIS.png";
                truck.StateDescription = "Waiting for Driver to Login in";
                truck.Heading          = 20;
                truck.BeatId           = 1;
                truck.ContractorId     = 1;
                truck.Lat              = 33.7319;
                truck.Lon              = -117.7927;
                truck.Speed            = 25;
                truck.LastUpdate       = "3 seconds ago";

                towTrucks2.Add(truck);
                Hub.Clients.addOrUpdateTruck(truck);
                System.Diagnostics.Debug.WriteLine(DateTime.Now + ": Added truck: " + truck.TruckId);


                truck                  = new TowTruck2();
                truck.TruckId          = 456;
                truck.StateImage       = @"http://localhost/FSP.Web/Content/Images/MajorAlarm.png";
                truck.StateDescription = "Accident with fire truck";
                truck.Heading          = 90;
                truck.BeatId           = 1;
                truck.ContractorId     = 1;
                truck.Lat              = 33.6619;
                truck.Lon              = -117.7527;
                truck.Speed            = 25;
                truck.LastUpdate       = "10 seconds ago";

                towTrucks2.Add(truck);
                Hub.Clients.addOrUpdateTruck(truck);
                System.Diagnostics.Debug.WriteLine(DateTime.Now + ": Added truck: " + truck.TruckId);
            }
            else if (iCounter > 5)
            {
                //we have a trucks in the list. Now update their location and data
                foreach (var truck in towTrucks2)
                {
                    //set random speed
                    truck.Speed = ran.Next(0, 70);

                    //add a degree to heading
                    truck.Heading = truck.Heading + 10;

                    int number = ran.Next(0, 20);
                    if (number % 2 != 0)
                    {
                        truck.Lat = truck.Lat - 0.0020;
                        truck.Lon = truck.Lon + 0.0020;
                    }
                    else
                    {
                        truck.Lat = truck.Lat + 0.0020;
                        truck.Lon = truck.Lon - 0.0020;
                    }

                    if (truck.Lat < 33.6539 || truck.Lat > 33.7939)
                    {
                        truck.Lat = 33.7319;
                    }


                    if (truck.Lon < -117.9597 || truck.Lon > -117.6597)
                    {
                        truck.Lon = -117.7927;
                    }

                    truck.LastUpdate = ran.Next(0, 10).ToString() + " seconds ago";


                    Hub.Clients.addOrUpdateTruck(truck);

                    System.Diagnostics.Debug.WriteLine(DateTime.Now + ": Updated truck: " + truck.TruckId);
                }
            }



            if (iCounter == 30)
            {
                TowTruck2 truck = towTrucks2.FirstOrDefault();

                towTrucks2.Remove(truck);
                // Notify the connected clients
                Hub.Clients.deleteTruck(truck.TruckId);

                System.Diagnostics.Debug.WriteLine(DateTime.Now + ": Removed truck: " + truck.TruckId);
            }


            iCounter += 1;
        }
コード例 #2
0
        void Demo2()
        {
            try
            {
                Random ran = new Random();
                int    iTruckUpdatesCount = 0;
                int    iTruckDeleteCount  = 0;

                lock (towTrucks2)
                {
                    int NewTruckId = ran.Next(1, 5);

                    if (towTrucks2.Any(p => p.TruckId == NewTruckId))
                    {
                        #region update
                        TowTruck2 truck = towTrucks2.Single(p => p.TruckId == NewTruckId);
                        truck.Speed = ran.NextDouble() * 100;
                        //add a degree to heading
                        truck.Heading    = truck.Heading + 10;
                        truck.LastUpdate = ran.Next(0, 20) + " seconds ago";

                        iTruckUpdatesCount += 1;

                        int number = ran.Next(0, 20);
                        if (number % 2 != 0)
                        {
                            truck.Lat = truck.Lat - 0.0050;
                            truck.Lon = truck.Lon + 0.0050;
                        }
                        else
                        {
                            truck.Lat = truck.Lat + 0.0050;
                            truck.Lon = truck.Lon - 0.0050;
                        }

                        if (truck.Lat < 33.6539 || truck.Lat > 33.7939)
                        {
                            truck.Lat = 33.7319;
                        }


                        if (truck.Lon < -117.9597 || truck.Lon > -117.6597)
                        {
                            truck.Lon = -117.7927;
                        }

                        // Notify the connected clients
                        Hub.Clients.addOrUpdateTruck(truck);

                        System.Diagnostics.Debug.WriteLine(DateTime.Now + " HUB: Updating Truck " + truck.TruckId);

                        #endregion
                    }
                    else
                    {
                        #region add

                        iTruckUpdatesCount += 1;

                        TowTruck2 truck = new TowTruck2();
                        truck.Old              = false;
                        truck.TruckId          = NewTruckId;
                        truck.StateImage       = @"http://localhost/FSP.Web/Content/Images/NIS.png";
                        truck.StateDescription = "Waiting for Driver to Login in";
                        truck.Heading          = 20;
                        truck.BeatId           = 1;
                        truck.ContractorId     = 1;
                        truck.Lat              = 33.7319;
                        truck.Lon              = -117.7927;
                        truck.Speed            = 25;
                        truck.LastUpdate       = "0 seconds ago";

                        towTrucks2.Add(truck);

                        // Notify the connected clients
                        Hub.Clients.addOrUpdateTruck(truck);

                        System.Diagnostics.Debug.WriteLine(DateTime.Now + " HUB: Adding Truck " + truck.TruckId);

                        #endregion
                    }


                    #region remove

                    var removeItem = false;

                    //removeItem = NewId % 2 == 0;
                    removeItem = iCounter % 20 == 0;

                    if (removeItem)
                    {
                        TowTruck2 truck = towTrucks2.FirstOrDefault();
                        truck.Old = true;
                        //towTrucks2.Remove(truck);

                        Hub.Clients.deleteTruck(truck.TruckId);
                        System.Diagnostics.Debug.WriteLine(DateTime.Now + " HUB: Removing Truck " + truck.TruckId);

                        towTrucks2.RemoveAll(p => p.Old == true);
                    }

                    #endregion
                }


                System.Diagnostics.Debug.WriteLine("================Server: Total Number of trucks in local list: " + towTrucks2.Count());
                System.Diagnostics.Debug.WriteLine("================Server: Total Number of trucks updated: " + iTruckUpdatesCount);
                System.Diagnostics.Debug.WriteLine("================Server: Total Number of trucks deleted: " + iTruckDeleteCount);

                iCounter += 1;
            }
            catch (Exception ex)
            {
                Util.WriteToLog(ex.Message);
            }
        }
コード例 #3
0
        void QueryService()
        {
            TowTruck2 truck = null;


            int iTruckUpdatesCount = 0;
            int iTruckDeleteCount  = 0;

            try
            {
                Util.WriteToLog(DateTime.Now + " Contacting Service");

                TowTruckServiceRef.TowTruckServiceClient service = new TowTruckServiceRef.TowTruckServiceClient();
                TowTruckData[] serviceTowTrucks = service.CurrentTrucks();
                Util.WriteToLog(DateTime.Now + " " + serviceTowTrucks.Count() + " tow trucks retrieved from Service");

                Boolean addTruck = false;

                lock (towTrucks2)
                {
                    for (int i = 0; i < serviceTowTrucks.Length; i++)
                    {
                        TowTruckData serviceTowTruck = serviceTowTrucks[i];

                        if (towTrucks2.Any(p => p.TruckId.ToString() == serviceTowTruck.TruckID))
                        {
                            truck               = towTrucks2.Single(p => p.TruckId.ToString() == serviceTowTruck.TruckID);
                            addTruck            = false;
                            iTruckUpdatesCount += 1;
                        }
                        else
                        {
                            iTruckUpdatesCount += 1;
                            truck     = new TowTruck2();
                            truck.Old = false;
                            addTruck  = true;
                        }

                        int truckId;
                        Int32.TryParse(serviceTowTruck.TruckID, out truckId);

                        truck.TruckId    = truckId;
                        truck.Speed      = serviceTowTruck.Speed;
                        truck.Lat        = serviceTowTruck.Lat;
                        truck.Lon        = serviceTowTruck.Lon;
                        truck.Heading    = serviceTowTruck.Heading;
                        truck.LastUpdate = "0 seconds ago";

                        #region State

                        truck.StateDescription = serviceTowTruck.VehicleState;

                        if (serviceTowTruck.VehicleState == "Waiting for Driver Login")
                        {
                            truck.StateImage = @"http://localhost/FSP.Web/Content/Images/NIS.png";
                        }

                        #endregion


                        if (addTruck)
                        {
                            towTrucks2.Add(truck);
                        }

                        // Notify the connected clients
                        Hub.Clients.addOrUpdateTruck(truck);
                    }


                    #region remove

                    try
                    {
                        for (int i = 0; i < towTrucks2.Count(); i++)
                        {
                            try
                            {
                                TowTruck2 uiTowTruck = towTrucks2[i];

                                if (!serviceTowTrucks.Any(p => p.TruckID == uiTowTruck.TruckId.ToString()))
                                {
                                    iTruckDeleteCount += 1;
                                    uiTowTruck.Old     = true;
                                    //this ui tow truck is not in service any more. so remove it
                                    //towTrucks2.Remove(uiTowTruck);

                                    //Hub notification
                                    Hub.Clients.deleteTruck(uiTowTruck.TruckId);
                                }
                            }
                            catch (Exception ex)
                            {
                                Util.WriteToLog(ex.Message);
                            }
                        }
                        towTrucks2.RemoveAll(p => p.Old == true);
                    }
                    catch (Exception ex)
                    {
                        Util.WriteToLog(ex.Message);
                    }



                    #endregion

                    Util.WriteToLog(DateTime.Now + "================Server: Total Number of trucks in local list: " + towTrucks2.Count());
                    Util.WriteToLog(DateTime.Now + "================Server: Total Number of trucks updated: " + iTruckUpdatesCount);
                    Util.WriteToLog(DateTime.Now + "================Server: Total Number of trucks deleted: " + iTruckDeleteCount);
                }
            }
            catch (Exception ex)
            {
                Util.WriteToLog(ex.Message);
            }
        }