示例#1
0
        public IHttpActionResult AddTranToWatch(LogDto data)
        {
            var logRecord   = _context.Log.SingleOrDefault(l => l.TrainId == data.TrainId);
            var nextStation = FindNextStationHelpers.FindNextStation(data.TrainId, data.Direction, -1);
            var lastStop    = _context.Trains.Single(t => t.TID == data.TrainId).StartStation;

            if (nextStation == -5)
            {
                return(BadRequest());
            }
            int locationLogId = -2;

            //Add new record to LocationLog If only status is 1(active Train)
            if (data.Status != "Active")
            {
                return(Conflict());
            }
            {
                var location = new LocationLog {
                    TrainId = data.TrainId, DateTime = DateTime.Now.ToShortDateString()
                };
                _context.Location.Add(location);
                _context.SaveChanges();
                locationLogId = location.LocationLogId;
                string path = $"Train-id-{data.TrainId}/Train-on-{data.Direction}-{DateTime.Now:y-MM-dd-HH-mm}.txt";
                FileHandler.WriteToFile(path, $"Created :{DateTime.Now:y-MM-dd-HH-mm}");
                if (!ActiveTrainDetails.ActiveTrainDictionary.ContainsKey(data.TrainId))
                {
                    ActiveTrainDetails.ActiveTrainDictionary.Add(data.TrainId, new ActiveTrain(path));
                }
                else
                {
                    ActiveTrainDetails.ActiveTrainDictionary[data.TrainId].PinLocations.Clear();
                    ActiveTrainDetails.ActiveTrainDictionary[data.TrainId].file = path;
                }
            }

            //check whether there is existing record on log table if not add new one
            if (logRecord != null)
            {
                logRecord.Status       = data.Status;
                logRecord.LogId        = locationLogId;
                logRecord.Direction    = data.Direction;
                logRecord.StartTime    = DateTime.Now.TimeOfDay.ToString(@"hh\:mm");
                logRecord.NextStop     = nextStation;
                logRecord.LastStop     = lastStop;
                logRecord.MaxSpeed     = logRecord.Speed = 0;
                logRecord.Delay        = TimeSpan.Zero;
                logRecord.LastLocation = logRecord.LastReceive = "";
            }
            else
            {
                var log = new Log
                {
                    TrainId     = data.TrainId,
                    DeviceId    = _context.Devices.First(d => d.TID == data.TrainId).DID,
                    Delay       = TimeSpan.Zero,
                    Status      = data.Status,
                    LogId       = locationLogId,
                    Direction   = data.Direction,
                    StartTime   = DateTime.Now.TimeOfDay.ToString(@"hh\:mm"),
                    NextStop    = nextStation,
                    LastStop    = lastStop,
                    LastReceive = TimeSpan.Zero.ToString(@"hh\:mm"),
                };
                _context.Log.Add(log);
            }

            _context.SaveChanges();
            return(Ok("Activated"));
        }
示例#2
0
        //Update LogTable
        private void UpdateLog(SaveData str1, SaveData str2, int trainId, bool direction, double speed)
        {
            if (string.IsNullOrEmpty(str1.Lo))
            {
                return;
            }
            //Last and Current Location difference
            //Last location
            var location = str1.Lo.Split(':');
            var point1   = new GeoCoordinate(Convert.ToDouble(location[0]), Convert.ToDouble(location[1]));

            //Current location
            location = str2.Lo.Split(':');
            var point2 = new GeoCoordinate(Convert.ToDouble(location[0]), Convert.ToDouble(location[1]));

            var gap = point1.GetDistanceTo(point2);

            //Get relevant LogTable recode
            var log = _dbContext.Log.Single(l => l.TrainId == trainId);

            //Get Log recodes next stop station data
            var station       = _dbContext.Stations.Single(s => s.SID == log.NextStop);
            var locationSplit = station.Location.Split(':');
            var nextStop      = new GeoCoordinate(Convert.ToDouble(locationSplit[0]), Convert.ToDouble(locationSplit[1]));

            //Get data about next stop station timetable
            var stopAt   = _dbContext.StopAts.SingleOrDefault(s => (s.TID == trainId) && (s.Direction == direction) && (s.SID == station.SID));
            var distance = point2.GetDistanceTo(nextStop);

            Debug.WriteLine("gap between last receive point : " + gap);
            Debug.WriteLine("gap between next stop : " + distance);

            //Train moves
            if (_defaultGap < gap)
            {
                //distance to Next stop station
                if (_defaultSpeed > speed)
                {
                    speed = _defaultSpeed;
                }
                //Time to arrive next station
                var time         = TimeSpan.FromSeconds(Math.Round(distance / (speed * 10 / 36), 2));
                var estimateTime = DateTime.Now.TimeOfDay + time;
                var delay        = estimateTime.Subtract(TimeSpan.FromSeconds(stopAt.Atime)).Duration();
                _dbContext.Location.Single(l => l.LocationLogId == log.LogId).Delay = (int)delay.TotalSeconds;
                log.Status = delay.TotalMinutes < 5 ? "Delay less than 5 min" : "Delayed";
                log.Delay  = delay;
            }
            //Train stops
            else
            {
                if (_defaultPlatformLength > distance)
                {
                    log.Status   = $"Stop At {station.Name}";
                    log.LastStop = log.NextStop;
                    log.NextStop = FindNextStationHelpers.FindNextStation(log.TrainId, log.Direction, log.NextStop);
                    if (log.NextStop == -1)
                    {
                        log.LogId   = -2;
                        log.Status  = $"At End Station";
                        log.EndTime = DateTime.Now.TimeOfDay.ToString(@"hh\:mm");
                    }
                }
                else
                {
                    var lastStop           = _dbContext.Stations.Single(s => s.SID == log.LastStop);
                    var lastStopLocation   = lastStop.Location.Split(':');
                    var distanceToLastStop =
                        point2.GetDistanceTo(new GeoCoordinate(Convert.ToDouble(lastStopLocation[0]),
                                                               Convert.ToDouble(lastStopLocation[1])));
                    log.Status = _defaultPlatformLength > distanceToLastStop ? $"Stop At {lastStop.Name}" : "Waiting For Signals";
                }
            }
            _dbContext.SaveChanges();
            _writeToFile = $"\nSpeed:{log.Speed},Status:{log.Status},Delay:{log.Delay}";
        }