Exemple #1
0
        public async Task <IActionResult> AddVehicle(VehicleVM vehicle)
        {
            try
            {
                List <Device> devices = await TraccarService.GetDevices();

                if (devices != null)
                {
                    if (devices.Where(x => x.name == vehicle.VehicleNumber).Any())
                    {
                        return(BadRequest($"A device with vehicle number {vehicle.VehicleNumber} already exists"));
                    }
                    else
                    {
                        Driver driver = (from d in dbContext.Drivers where d.VehicleNumber == vehicle.VehicleNumber select d).SingleOrDefault();
                        if (driver != null)
                        {
                            if (await TraccarService.AddDevice(vehicle))
                            {
                                Vehicle newVehicle = new Vehicle()
                                {
                                    Id            = Guid.NewGuid(),
                                    Active        = true,
                                    FuelAverage   = 0,
                                    Breakdowns    = 0,
                                    CostThisMonth = 0,
                                    IMEI          = vehicle.IMEI,
                                    Ranking       = 0,
                                    Region        = vehicle.Region,
                                    SIM           = vehicle.SIM,
                                    Status        = "maintained",
                                    SubRegion     = vehicle.SubRegion,
                                    Station       = vehicle.Station,
                                    VehicleNumber = vehicle.VehicleNumber,
                                    Score         = vehicle.Score,
                                    CardNumber    = vehicle.CardNumber,
                                    GasolineType  = vehicle.GasolineType
                                };
                                await dbContext.Vehicles.AddAsync(newVehicle);

                                if (await dbContext.SaveChangesAsync() > 0)
                                {
                                    return(Ok($"Device is successfully added with vehicle number {vehicle.VehicleNumber}"));
                                }
                                else
                                {
                                    return(BadRequest($"A device with vehicle number {vehicle.VehicleNumber} could not be added to FMS."));
                                }
                            }
                            else
                            {
                                return(BadRequest($"A device could not be added with vehicle number {vehicle.VehicleNumber}"));
                            }
                        }
                        else
                        {
                            return(BadRequest($"Please assign driver to vehicle number {vehicle.VehicleNumber}"));
                        }
                    }
                }
                else
                {
                    return(BadRequest($"A device could not be added with vehicle number {vehicle.VehicleNumber}. It seems some temporary problem. Please try again. If problem persists, contact support staff for assistance."));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.ToString()));
            }
        }
Exemple #2
0
        public async Task <IActionResult> GetAllFMS()
        {
            List <VehicleVM> rbVehicles = new List <VehicleVM>();

            try
            {
                if (User.Claims.Any())
                {
                    List <Position> positions = await TraccarService.GetPositions();

                    List <Device> devices = await TraccarService.GetDevices();

                    IEnumerable <DevicePosition> devicePositions = from d in devices
                                                                   join p in positions on d.id equals p.deviceId
                                                                   select new DevicePosition()
                    {
                        id            = d.id,
                        name          = d.name,
                        latitude      = p.latitude,
                        longitude     = p.longitude,
                        distance      = p.attributes.distance,
                        hours         = p.attributes.hours,
                        odometer      = p.attributes.odometer,
                        totalDistance = p.attributes.totalDistance
                    };
                    if (User.IsInRole("SA") || User.IsInRole("HMT"))
                    {
                        rbVehicles = await(from v in dbContext.Vehicles
                                           join d in dbContext.Drivers on v.VehicleNumber equals d.VehicleNumber
                                           select new VehicleVM()
                        {
                            VehicleId       = v.Id,
                            Active          = v.Active,
                            DriverName      = d.Name,
                            IMEI            = v.IMEI,
                            Region          = v.Region,
                            SIM             = v.SIM,
                            SubRegion       = v.SubRegion,
                            Station         = v.Station,
                            VehicleNumber   = v.VehicleNumber,
                            Type            = v.Status,
                            Score           = v.Score,
                            FuelAverage     = v.FuelAverage,
                            Breakdowns      = v.Breakdowns,
                            CostThisMonth   = v.CostThisMonth,
                            Ranking         = v.Ranking,
                            PeriodicRemarks = v.PeriodicStatus == Shared.Enums.PeriodicMaintenanceStatus.Done ? "Periodically Maintained" : "Periodic Maintenance Pending"
                        }).ToListAsync();
                    }
                    else
                    if (User.IsInRole("RMTO"))
                    {
                        ApplicationUser user   = (from u in dbContext.Users where u.Email == User.Identity.Name select u).FirstOrDefault();
                        Region          region = (from r in dbContext.Regions where r.XDescription == user.Region select r).FirstOrDefault();

                        rbVehicles = await(from v in dbContext.Vehicles
                                           join d in dbContext.Drivers on v.VehicleNumber equals d.VehicleNumber
                                           where v.Region == region.XDescription
                                           select new VehicleVM()
                        {
                            VehicleId       = v.Id,
                            Active          = v.Active,
                            DriverName      = d.Name,
                            IMEI            = v.IMEI,
                            Region          = v.Region,
                            SIM             = v.SIM,
                            SubRegion       = v.SubRegion,
                            Station         = v.Station,
                            VehicleNumber   = v.VehicleNumber,
                            Type            = v.Status,
                            Score           = v.Score,
                            FuelAverage     = v.FuelAverage,
                            Breakdowns      = v.Breakdowns,
                            CostThisMonth   = v.CostThisMonth,
                            Ranking         = v.Ranking,
                            PeriodicRemarks = v.PeriodicStatus == Shared.Enums.PeriodicMaintenanceStatus.Done ? "Periodically Maintained" : "Periodic Maintenance Pending"
                        }).ToListAsync();
                    }
                    else
                    {
                        rbVehicles = await(from v in dbContext.Vehicles
                                           join d in dbContext.Drivers on v.VehicleNumber equals d.VehicleNumber
                                           select new VehicleVM()
                        {
                            VehicleId       = v.Id,
                            Active          = v.Active,
                            DriverName      = d.Name,
                            IMEI            = v.IMEI,
                            Region          = v.Region,
                            SIM             = v.SIM,
                            SubRegion       = v.SubRegion,
                            Station         = v.Station,
                            VehicleNumber   = v.VehicleNumber,
                            Type            = v.Status,
                            Score           = v.Score,
                            FuelAverage     = v.FuelAverage,
                            Breakdowns      = v.Breakdowns,
                            CostThisMonth   = v.CostThisMonth,
                            Ranking         = v.Ranking,
                            PeriodicRemarks = v.PeriodicStatus == Shared.Enums.PeriodicMaintenanceStatus.Done ? "Periodically Maintained" : "Periodic Maintenance Pending"
                        }).ToListAsync();
                    }
                    var list = from v in rbVehicles
                               join p in devicePositions on v.VehicleNumber equals p.name
                               select new VehicleVM()
                    {
                        VehicleId          = v.Id,
                        Active             = v.Active,
                        DriverName         = v.DriverName,
                        IMEI               = v.IMEI,
                        Region             = v.Region,
                        SIM                = v.SIM,
                        SubRegion          = v.SubRegion,
                        Station            = v.Station,
                        VehicleNumber      = v.VehicleNumber,
                        Type               = v.Type,
                        Latitude           = p.latitude,
                        Longitude          = p.longitude,
                        Odometer           = p.odometer,
                        Distance           = p.distance,
                        TotalDistance      = p.totalDistance / 1000,
                        Hours              = p.hours,
                        NumberOfTripsToday = TraccarService.GetNumberOfTripsTodayByDeviceId(p.id).Result,
                        Score              = v.Score,
                        FuelAverage        = v.FuelAverage,
                        Breakdowns         = v.Breakdowns,
                        CostThisMonth      = v.CostThisMonth,
                        Ranking            = v.Ranking,
                        PeriodicRemarks    = v.PeriodicRemarks
                    };

                    foreach (var l in list)
                    {
                        Vehicle vehicle = (from v in dbContext.Vehicles
                                           where v.VehicleNumber == l.VehicleNumber
                                           select v).SingleOrDefault();
                        vehicle.Distance = l.TotalDistance;
                    }
                    await dbContext.SaveChangesAsync();

                    return(Ok(list));
                }
                else
                {
                    return(BadRequest("User not logged in."));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.ToString()));
            }
        }