Example #1
0
 public ActionResult Create(Car newCar)
 {
     try
     {
         // TODO: Add insert logic here
         _context.Car.Add(newCar);
         _context.SaveChanges();
         return(RedirectToAction("Index"));
     }
     catch
     {
         return(View(newCar));
     }
 }
 public ActionResult Create(Fridge newFridge)
 {
     try
     {
         // TODO: Add insert logic here
         _context.Fridge.Add(newFridge);
         _context.SaveChanges();
         return(RedirectToAction("Index"));
     }
     catch
     {
         return(View(newFridge));
     }
 }
Example #3
0
        // POST: api/Fridge
        public string Post([FromBody] Fridge newFridge)
        {
            try
            {
                if (newFridge != null)
                {
                    _context.Fridge.Add(newFridge);
                    _context.SaveChanges();
                    return("New fridge with id " + newFridge.id + " is created");
                }

                return("New fridge is not valid.");
            }
            catch (Exception e)
            {
                return(e?.Message + ":" + e?.InnerException?.Message);
            }
        }
Example #4
0
        public string Post([FromBody] Car newCar)
        {
            try
            {
                if (newCar != null)
                {
                    _context.Car.Add(newCar);
                    _context.SaveChanges();
                    return("New car with id " + newCar.id + " is created");
                }

                return("New car is not valid.");
            }
            catch (Exception e)
            {
                return(e?.Message + ":" + e?.InnerException?.Message);
            }
        }
Example #5
0
        public void OperateFridges()
        {
            SmartDeviceContext _context = new SmartDeviceContext();
            Random             rnd      = new Random();
            int  operation   = rnd.Next(1, 8);
            int  iceLevel    = rnd.Next(0, 10);
            int  fluidLevel  = rnd.Next(1, 5);
            int  temperature = rnd.Next(-4, 40);
            bool newValue;

            var fridges = _context.Fridge.ToList();

            if (fridges.Count == 0)
            {
                Fridge newFridge = new Fridge();
                newFridge.iceLevel = iceLevel.ToString();

                Boolean.TryParse(rnd.Next(0, 1).ToString(), out newValue);
                newFridge.waterLeaks = newValue;
                Boolean.TryParse(rnd.Next(0, 1).ToString(), out newValue);
                newFridge.defrostAlarm = newValue;
                newFridge.temperature  = temperature;
                newFridge.location     = "Location-" + rnd.Next(1000000).ToString();
                newFridge.Name         = "Fridge-" + rnd.Next(1000000).ToString();
                _context.Fridge.Add(newFridge);
                _context.SaveChanges();
            }

            foreach (var fridge in fridges)
            {
                try
                {
                    if (operation >= 1 && operation <= 3)
                    {
                        _context.Fridge.Remove(fridge);
                        _context.SaveChanges();
                        break;
                    }
                    else if (operation >= 4 && operation <= 5)
                    {
                        Fridge newFridge = new Fridge();
                        newFridge.temperature = temperature;
                        newFridge.iceLevel    = iceLevel.ToString();
                        Boolean.TryParse(rnd.Next(0, 1).ToString(), out newValue);
                        newFridge.defrostAlarm = newValue;
                        Boolean.TryParse(rnd.Next(0, 1).ToString(), out newValue);
                        newFridge.waterLeaks = newValue;
                        newFridge.location   = "Location-" + rnd.Next(1000000).ToString();
                        newFridge.Name       = "Fridge-" + rnd.Next(1000000).ToString();
                        _context.Fridge.Add(newFridge);
                        _context.SaveChanges();
                    }
                    else if (operation >= 6 && operation <= 8)
                    {
                        var fridgeToUpdate = _context.Fridge.Single(c => c.id == fridge.id);
                        fridgeToUpdate.temperature = temperature;
                        fridgeToUpdate.iceLevel    = iceLevel.ToString();
                        Boolean.TryParse(rnd.Next(0, 1).ToString(), out newValue);
                        fridgeToUpdate.defrostAlarm = newValue;
                        Boolean.TryParse(rnd.Next(0, 1).ToString(), out newValue);
                        fridgeToUpdate.waterLeaks = newValue;

                        _context.Fridge.Remove(fridgeToUpdate);
                        _context.SaveChanges();
                        _context.Fridge.Add(fridgeToUpdate);
                        _context.SaveChanges();
                    }
                }
                catch
                {
                    return;
                }
            }
        }
Example #6
0
        public void OperateCars()
        {
            SmartDeviceContext _context = new SmartDeviceContext();
            Random             rnd      = new Random();
            int operation    = rnd.Next(1, 8);
            int temp         = rnd.Next(15, 32);
            int fluidLevel   = rnd.Next(1, 5);
            int tirePressure = rnd.Next(11, 40);

            var cars = _context.Car.ToList();

            if (cars.Count == 0)
            {
                Car newCar = new Car();
                newCar.engineTemperature = temp;
                newCar.fluidLevel        = fluidLevel.ToString();
                newCar.tirePressure      = tirePressure;
                newCar.location          = "Location-" + rnd.Next(1000000).ToString();
                newCar.Name = "Car-" + rnd.Next(1000000).ToString();
                _context.Car.Add(newCar);
                _context.SaveChanges();
            }

            foreach (var car in cars)
            {
                try
                {
                    if (operation >= 1 && operation <= 3)
                    {
                        _context.Car.Remove(car);
                        _context.SaveChanges();
                        break;
                    }
                    else if (operation >= 4 && operation <= 5)
                    {
                        Car newCar = new Car();
                        newCar.engineTemperature = temp;
                        newCar.fluidLevel        = fluidLevel.ToString();
                        newCar.tirePressure      = tirePressure;
                        newCar.location          = "Location-" + rnd.Next(1000000).ToString();
                        newCar.Name = "Car-" + rnd.Next(1000000).ToString();
                        _context.Car.Add(newCar);
                        _context.SaveChanges();
                    }
                    else if (operation >= 6 && operation <= 8)
                    {
                        var carToUpdate = _context.Car.Single(c => c.id == car.id);
                        carToUpdate.engineTemperature = temp;
                        carToUpdate.fluidLevel        = fluidLevel.ToString();
                        carToUpdate.tirePressure      = tirePressure;

                        _context.Car.Remove(car);
                        _context.SaveChanges();
                        _context.Car.Add(carToUpdate);
                        _context.SaveChanges();
                    }
                }
                catch
                {
                    return;
                }
            }
        }