Example #1
0
 private TowTruckData[] GetAllTrucksInService()
 {
     TowTruckServiceRef.TowTruckServiceClient service = new TowTruckServiceRef.TowTruckServiceClient();
     TowTruckData[] serviceTowTrucks = service.CurrentTrucks();
     Util.WriteToLog(DateTime.Now + " " + serviceTowTrucks.Count() + " tow trucks retrieved from Service");
     return(serviceTowTrucks);
 }
Example #2
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);
            }
        }
Example #3
0
        void UpdateAllTrucks()
        {
            UITowTruck 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;
                Boolean truckHasChanged = false;

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


                            //check if this tow truck already exist in list
                            if (towTrucks.Any(p => p.TruckNumber.ToString() == serviceTowTruck.TruckNumber))
                            {
                                truck               = towTrucks.Single(p => p.TruckNumber == serviceTowTruck.TruckNumber);
                                addTruck            = false;
                                iTruckUpdatesCount += 1;

                                if (truck.Lat != serviceTowTruck.Lat)
                                {
                                    truckHasChanged = true;
                                }

                                if (truck.Lon != serviceTowTruck.Lon)
                                {
                                    truckHasChanged = true;
                                }

                                if (truck.Speed != serviceTowTruck.Speed)
                                {
                                    truckHasChanged = true;
                                }

                                if (truck.Heading != serviceTowTruck.Heading)
                                {
                                    truckHasChanged = true;
                                }

                                if (truck.Heading != serviceTowTruck.Heading)
                                {
                                    truckHasChanged = true;
                                }

                                if (truck.BeatNumber != serviceTowTruck.BeatNumber.ToString())
                                {
                                    truckHasChanged = true;
                                }

                                if (truck.LastMessage != serviceTowTruck.LastMessage.ToString())
                                {
                                    truckHasChanged = true;
                                }

                                if (truck.DriverName != serviceTowTruck.DriverName.ToString())
                                {
                                    truckHasChanged = true;
                                }

                                if (truck.ContractorName != serviceTowTruck.ContractorName.ToString())
                                {
                                    truckHasChanged = true;
                                }

                                if (truck.Location != serviceTowTruck.Location.ToString())
                                {
                                    truckHasChanged = true;
                                }
                            }
                            else
                            {
                                //new tow truck
                                iTruckUpdatesCount += 1;
                                truck            = new UITowTruck();
                                truck.LastUpdate = 0;
                                truck.Old        = false;
                                addTruck         = true;
                            }

                            truck.TruckNumber       = serviceTowTruck.TruckNumber == null ? "Not set" : serviceTowTruck.TruckNumber;
                            truck.BeatNumber        = serviceTowTruck.BeatNumber == null ? "Not set" : serviceTowTruck.BeatNumber;
                            truck.BeatSegmentNumber = "Not set";

                            truck.Speed       = serviceTowTruck.Speed;
                            truck.Lat         = serviceTowTruck.Lat;
                            truck.Lon         = serviceTowTruck.Lon;
                            truck.Heading     = serviceTowTruck.Heading;
                            truck.LastMessage = serviceTowTruck.LastMessage.ToString();

                            truck.DriverName     = serviceTowTruck.DriverName;
                            truck.ContractorName = serviceTowTruck.ContractorName;
                            truck.Location       = serviceTowTruck.Location;


                            #region State

                            truck.VehicleState = serviceTowTruck.VehicleState;

                            if (serviceTowTruck.VehicleState == "Waiting for Driver Login")
                            {
                                truck.VehicleStateIconUrl = "NIS.png";
                            }
                            else if (serviceTowTruck.VehicleState == "Driver Logged On")
                            {
                                truck.VehicleStateIconUrl = "InService.png";
                            }
                            else if (serviceTowTruck.VehicleState == "On Patrol")
                            {
                                truck.VehicleStateIconUrl = "InService.png";
                            }
                            else if (serviceTowTruck.VehicleState == "On Assist")
                            {
                                truck.VehicleStateIconUrl = "OnAssist.png";
                            }
                            else if (serviceTowTruck.VehicleState == "On Break")
                            {
                                truck.VehicleStateIconUrl = "OnBreak.png";
                            }
                            else if (serviceTowTruck.Alarms)
                            {
                                if (serviceTowTruck.SpeedingAlarm)
                                {
                                    truck.VehicleStateIconUrl = "MajorAlarm.png";
                                }
                                else if (serviceTowTruck.OutOfBoundsAlarm)
                                {
                                    truck.VehicleStateIconUrl = "MajorAlarm.png";
                                }
                            }

                            else
                            {
                                truck.VehicleStateIconUrl = "OCTA.png";
                            }
                            #endregion


                            if (addTruck)
                            {
                                towTrucks.Add(truck);

                                //Notify clients about new truck
                                Hub.Clients.addOrUpdateTruck(truck);
                            }
                            else
                            {
                                if (truckHasChanged)
                                {
                                    Util.WriteToLog("HUB: Truck " + truck.TruckNumber + " changed ");

                                    truck.LastUpdate = 0;
                                    //Notify clients about an existing truck only if truck has "changed"
                                    Hub.Clients.addOrUpdateTruck(truck);
                                }
                                else
                                {
                                    Util.WriteToLog("HUB: NO CHANGES TO " + truck.TruckNumber);

                                    truck.LastUpdate += Convert.ToInt32(ConfigurationManager.AppSettings["UpdateIntervalInMilliseconds"]) / 1000;

                                    //Notify clients about an existing truck only if truck has "changed"
                                    Hub.Clients.updateLastUpdateTime(truck.TruckNumber, truck.LastUpdate, truck.LastMessage);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Util.WriteToLog(ex.Message);
                        }
                    }


                    #region remove

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

                                if (!serviceTowTrucks.Any(p => p.TruckNumber == uiTowTruck.TruckNumber.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.TruckNumber);
                                }
                            }
                            catch (Exception ex)
                            {
                                Util.WriteToLog(ex.Message);
                            }
                        }
                        towTrucks.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: " + towTrucks.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);
            }
        }
Example #4
0
        public ActionResult GetTowTrucks()
        {
            Random   ran   = new Random();
            TowTruck truck = null;

            try
            {
                if (ConfigurationManager.AppSettings["IsDemo"] == "true")
                {
                    #region Demo
                    Util.WriteToLog("Doing Tow Truck Demo...");

                    if (towTrucks == null)
                    {
                        //set up initial locations
                        towTrucks = new List <TowTruck>();

                        //#region Create a Two Truck for each State

                        //truck = new TowTruck();
                        //truck.TruckId = 123;
                        //truck.Speed = ran.Next(0, 70);
                        //truck.Lat = 33.7319;
                        //truck.Lon = -117.7927;
                        //truck.Heading = ran.Next(0, 180);
                        //truck.LastUpdate = DateTime.Now;
                        //truck.State = "InService";
                        //towTrucks.Add(truck);

                        //truck = new TowTruck();
                        //truck.TruckId = 124;
                        //truck.Speed = ran.Next(0, 70);
                        //truck.Lat = 33.7619;
                        //truck.Lon = -117.8227;
                        //truck.Heading = ran.Next(0, 180);
                        //truck.LastUpdate = DateTime.Now;
                        //truck.State = "MajorAlarm";
                        //towTrucks.Add(truck);

                        //truck = new TowTruck();
                        //truck.TruckId = 234;
                        //truck.Speed = ran.Next(0, 70);
                        //truck.Lat = 33.7919;
                        //truck.Lon = -117.8527;
                        //truck.Heading = ran.Next(0, 180);
                        //truck.LastUpdate = DateTime.Now;
                        //truck.State = "MinorAlarm";
                        //towTrucks.Add(truck);

                        //truck = new TowTruck();
                        //truck.TruckId = 235;
                        //truck.Speed = ran.Next(0, 70);
                        //truck.Lat = 33.7939;
                        //truck.Lon = -117.7597;
                        //truck.Heading = ran.Next(0, 180);
                        //truck.LastUpdate = DateTime.Now;
                        //truck.State = "ModerateAlarm";
                        //towTrucks.Add(truck);

                        //truck = new TowTruck();
                        //truck.TruckId = 345;
                        //truck.Speed = ran.Next(0, 70);
                        //truck.Lat = 33.7939;
                        //truck.Lon = -117.8997;
                        //truck.Heading = ran.Next(0, 180);
                        //truck.LastUpdate = DateTime.Now;
                        //truck.State = "NIS";
                        //towTrucks.Add(truck);

                        //truck = new TowTruck();
                        //truck.TruckId = 346;
                        //truck.Speed = ran.Next(0, 70);
                        //truck.Lat = 33.7939;
                        //truck.Lon = -117.9597;
                        //truck.Heading = ran.Next(0, 180);
                        //truck.LastUpdate = DateTime.Now;
                        //truck.State = "OCTA";
                        //towTrucks.Add(truck);

                        //truck = new TowTruck();
                        //truck.TruckId = 456;
                        //truck.Speed = ran.Next(0, 70);
                        //truck.Lat = 33.8939;
                        //truck.Lon = -117.9597;
                        //truck.Heading = ran.Next(0, 180);
                        //truck.LastUpdate = DateTime.Now;
                        //truck.State = "OnAssist";
                        //towTrucks.Add(truck);

                        //truck = new TowTruck();
                        //truck.TruckId = 457;
                        //truck.Speed = ran.Next(0, 70);
                        //truck.Lat = 33.6939;
                        //truck.Lon = -117.9597;
                        //truck.Heading = ran.Next(0, 180);
                        //truck.LastUpdate = DateTime.Now;
                        //truck.State = "OnBreak";
                        //towTrucks.Add(truck);

                        //truck = new TowTruck();
                        //truck.TruckId = 567;
                        //truck.Speed = ran.Next(0, 70);
                        //truck.Lat = 33.6939;
                        //truck.Lon = -117.6597;
                        //truck.Heading = ran.Next(0, 180);
                        //truck.LastUpdate = DateTime.Now;
                        //truck.State = "TBD";
                        //towTrucks.Add(truck);

                        //#endregion

                        Util.WriteToLog(towTrucks.Count() + " tow trucks created for demo.");
                    }
                    else
                    {
                        //we have a trucks in the list. Now update their location and data
                        foreach (var towTruck in towTrucks)
                        {
                            //set random speed
                            towTruck.Speed = ran.Next(0, 70);

                            //add a degree to heading
                            towTruck.Heading = towTruck.Heading + 1;

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

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


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


                            towTruck.LastUpdate = DateTime.Now;
                        }

                        Util.WriteToLog(towTrucks.Count() + " tow trucks updated for demo.");
                    }


                    #endregion
                }
                else
                {
                    Util.WriteToLog("Contacting Service");

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


                    if (towTrucks == null)
                    {
                        towTrucks = new List <TowTruck>();
                    }


                    Boolean addTruck = false;


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

                            if (towTrucks.Any(p => p.TruckId == serviceTowTruck.TruckID))
                            {
                                truck    = towTrucks.Single(p => p.TruckId == serviceTowTruck.TruckID);
                                addTruck = false;
                            }
                            else
                            {
                                truck    = new TowTruck();
                                addTruck = true;
                            }

                            truck.TruckId    = serviceTowTruck.TruckID;
                            truck.Speed      = serviceTowTruck.Speed;
                            truck.Lat        = serviceTowTruck.Lat;
                            truck.Lon        = serviceTowTruck.Lon;
                            truck.Heading    = serviceTowTruck.Heading;
                            truck.LastUpdate = DateTime.Now;

                            #region State

                            if (serviceTowTruck.VehicleState == "Waiting for Driver Login")
                            {
                                truck.State = "NIS";
                            }


                            #endregion


                            if (addTruck)
                            {
                                towTrucks.Add(truck);
                            }
                        }
                    }
                }
            }
            catch { }

            return(Json(towTrucks, JsonRequestBehavior.AllowGet));
        }