Exemplo n.º 1
0
        public Device GetDeviceById(int id)
        {
            DatabaseMapping databaseMapping = _deviceContext.DatabaseMappings.Find(id);

            if (databaseMapping == null)
            {
                return(null);
            }
            Device device = null;

            switch (databaseMapping.DeviceTypeId)
            {
            case 1:
                device = databaseMapping.Clock;
                break;

            case 2:
                device = databaseMapping.Microwave;
                ((ITimer)device).CheckIsReady();
                break;

            case 3:
                device = databaseMapping.Oven;
                ((ITimer)device).CheckIsReady();
                break;

            case 4:
                device = databaseMapping.Fridge;
                break;
            }
            return(device);
        }
Exemplo n.º 2
0
        public void RemoveById(int id)
        {
            DatabaseMapping databaseMapping = _deviceContext.DatabaseMappings.Find(id);

            if (databaseMapping == null)
            {
                return;
            }

            Device device = GetDeviceById(id);

            if (device is Clock)
            {
                _deviceContext.Clocks.Remove((Clock)device);
            }
            else if (device is Microwave)
            {
                _deviceContext.Microwaves.Remove((Microwave)device);
            }
            else if (device is Oven)
            {
                _deviceContext.Ovens.Remove((Oven)device);
            }
            else if (device is Fridge)
            {
                _deviceContext.Fridges.Remove((Fridge)device);
            }
            _deviceContext.DatabaseMappings.Remove(databaseMapping);
            _deviceContext.SaveChanges();
        }
 public void AddDevice(string device = "", string name = "", string fabricator = "")
 {
     DatabaseMapping databaseMapping = null;
     switch (device)
     {
         case "clock":
             Clock clock = new Clock(name);
             _deviceContext.Clocks.Add(clock);
             databaseMapping = new DatabaseMapping { DeviceTypeId = 1, Clock = clock };
             break;
         case "microwave":
             MicrowaveFabricatorInfo mi = microwaveFabricatorInfo[fabricator];
             Microwave microwave = new Microwave(name, mi.Volume, mi.Lamp);
             _deviceContext.Microwaves.Add(microwave);
             databaseMapping = new DatabaseMapping { DeviceTypeId = 2, Microwave = microwave };
             break;
         case "oven":
             OvenFabricatorInfo oi = ovenFabricatorInfo[fabricator];
             Oven oven = new Oven(name, oi.Volume, oi.Lamp);
             _deviceContext.Ovens.Add(oven);
             databaseMapping = new DatabaseMapping { DeviceTypeId = 3, Oven = oven };
             break;
         case "fridge":
             FridgeFabricatorInfo fi = fridgeFabricatorInfo[fabricator];
             Fridge fridge = new Fridge(name, fi.Coldstore, fi.Freezer);
             _deviceContext.Fridges.Add(fridge);
             databaseMapping = new DatabaseMapping { DeviceTypeId = 4, Fridge = fridge };
             break;
         default: return;
     }
     _deviceContext.DatabaseMappings.Add(databaseMapping);
     _deviceContext.SaveChanges();
 }
Exemplo n.º 4
0
        public void AddDevice(string device = "", string name = "", string fabricator = "")
        {
            DatabaseMapping databaseMapping = null;

            switch (device)
            {
            case "clock":
                Clock clock = new Clock(name);
                _deviceContext.Clocks.Add(clock);
                databaseMapping = new DatabaseMapping {
                    DeviceTypeId = 1, Clock = clock
                };
                break;

            case "microwave":
                MicrowaveFabricatorInfo mi = microwaveFabricatorInfo[fabricator];
                Microwave microwave        = new Microwave(name, mi.Volume, mi.Lamp);
                _deviceContext.Microwaves.Add(microwave);
                databaseMapping = new DatabaseMapping {
                    DeviceTypeId = 2, Microwave = microwave
                };
                break;

            case "oven":
                OvenFabricatorInfo oi = ovenFabricatorInfo[fabricator];
                Oven oven             = new Oven(name, oi.Volume, oi.Lamp);
                _deviceContext.Ovens.Add(oven);
                databaseMapping = new DatabaseMapping {
                    DeviceTypeId = 3, Oven = oven
                };
                break;

            case "fridge":
                FridgeFabricatorInfo fi = fridgeFabricatorInfo[fabricator];
                Fridge fridge           = new Fridge(name, fi.Coldstore, fi.Freezer);
                _deviceContext.Fridges.Add(fridge);
                databaseMapping = new DatabaseMapping {
                    DeviceTypeId = 4, Fridge = fridge
                };
                break;

            default: return;
            }
            _deviceContext.DatabaseMappings.Add(databaseMapping);
            _deviceContext.SaveChanges();
        }
Exemplo n.º 5
0
        public void UpdateDeviceById(int id, Device device)
        {
            DatabaseMapping databaseMapping = _deviceContext.DatabaseMappings.Find(id);

            if (databaseMapping == null)
            {
                return;
            }
            switch (databaseMapping.DeviceTypeId)
            {
            case 1:
                if (device is Clock)
                {
                    _deviceContext.Entry((Clock)device).State = EntityState.Modified;
                    _deviceContext.SaveChanges();
                }
                break;

            case 2:
                if (device is Microwave)
                {
                    _deviceContext.Entry((Microwave)device).State = EntityState.Modified;
                    _deviceContext.SaveChanges();
                }
                break;

            case 3:
                if (device is Oven)
                {
                    _deviceContext.Entry((Oven)device).State = EntityState.Modified;
                    _deviceContext.SaveChanges();
                }
                break;

            case 4:
                if (device is Fridge)
                {
                    _deviceContext.Entry((Fridge)device).State = EntityState.Modified;
                    _deviceContext.SaveChanges();
                }
                break;
            }
        }