private DeviceDb ChangeStateDevice(DeviceDb deviceDb, IDevicable device)
 {
     if (deviceDb is TVDb)
     {
         ((TVDb)deviceDb).Volume  = ((IVolumenable)device).Volume;
         ((TVDb)deviceDb).Channel = ((ISwitchable)device).Current;
     }
     if (deviceDb is SoundDeviceDb)
     {
         ((SoundDeviceDb)deviceDb).Volume  = ((IVolumenable)device).Volume;
         ((SoundDeviceDb)deviceDb).Channel = ((ISwitchable)device).Current;
     }
     if (deviceDb is HeaterDb)
     {
         ((HeaterDb)deviceDb).Temperature = ((ITemperaturable)device).Temperature;
     }
     if (deviceDb is ConditionerDb)
     {
         ((ConditionerDb)deviceDb).Temperature   = ((ITemperaturable)device).Temperature;
         ((ConditionerDb)deviceDb).LevelSpeedAir = ((ISpeedAirable)device).LevelSpeed;
     }
     if (deviceDb is BlowerDb)
     {
         ((BlowerDb)deviceDb).LevelSpeedAir = ((ISpeedAirable)device).LevelSpeed;
     }
     return(deviceDb);
 }
        public ActionResult ActiveDevice(string parameter) // поменять на работу с id устройством
        {
            deviceDataView.DeviceList = GetAllDevices();
            devicesList = deviceDataView.DeviceList;
            IDevicable activDevice      = devicesList.Find(device => device.Name == parameter);
            int        indexActivDevice = devicesList.IndexOf(activDevice);

            deviceDataView.DeviceActive = indexActivDevice;
            deviceDataView.Message      = null;
            TempData["deviceData"]      = deviceDataView;
            return(RedirectToAction("Index"));
        }
        public DeviceDb GetDeviceDb(IDevicable device)
        {
            DeviceDb deviceDb = null;

            if (device is IVolumenable && device is ISwitchable)
            {
                deviceDb = new TVDb {
                    Channel = ((ISwitchable)device).Current, Volume = ((IVolumenable)device).Volume
                };
            }
            else if (device is IVolumenable && device is ISwitchable && device is IBassable)
            {
                deviceDb = new SoundDeviceDb {
                    Channel = ((ISwitchable)device).Current, Volume = ((IVolumenable)device).Volume, Bass = ((IBassable)device).BassLevel
                };
            }
            else if (device is ITemperaturable && device is ISpeedAirable)
            {
                deviceDb = new ConditionerDb {
                    Temperature = ((ITemperaturable)device).Temperature, LevelSpeedAir = ((ISpeedAirable)device).LevelSpeed
                };
            }
            else if (device is ITemperaturable)
            {
                deviceDb = new HeaterDb {
                    Temperature = ((ITemperaturable)device).Temperature
                };
            }
            else if (device is ISpeedAirable)
            {
                deviceDb = new BlowerDb {
                    LevelSpeedAir = ((ISpeedAirable)device).LevelSpeed
                };
            }
            else
            {
            }
            deviceDb.Name  = device.Name;
            deviceDb.State = device.State;
            return(deviceDb);
        }
        public string LinkIconDevice(IDevicable device)
        {
            string urlImage = "";

            if (device is IVolumenable && device is ISwitchable && device is IColorRedable)
            {
                if (device.State && ((ISwitchable)device).Current == 1)
                {
                    urlImage = "~/Content/TVCats.gif";
                }
                else if (device.State && ((ISwitchable)device).Current == 2)
                {
                    urlImage = "~/Content/TVDogs.gif";
                }
                else if (device.State)
                {
                    urlImage = "~/Content/TVMontains.gif";
                }
                else
                {
                    urlImage = "~/Content/tvOff.png";
                }
            }
            else if (device is IVolumenable && device is ISwitchable && device is IBassable)
            {
                if (device.State)
                {
                    urlImage = "~/Content/SoundDeviceOn.gif";
                }
                else
                {
                    urlImage = "~/Content/SoundDeviceOff.png";
                }
            }
            else if (device is ITemperaturable && device is ISpeedAirable)
            {
                if (device.State && ((ISpeedAirable)device).LevelSpeed == Speed.Low)
                {
                    urlImage = "~/Content/conditionerOnLow.gif";
                }
                else if (device.State && ((ISpeedAirable)device).LevelSpeed == Speed.Medium)
                {
                    urlImage = "~/Content/conditionerOnMedium.gif";
                }
                else if (device.State && ((ISpeedAirable)device).LevelSpeed == Speed.Hight)
                {
                    urlImage = "~/Content/conditionerOnHight.gif";
                }
                else
                {
                    urlImage = "~/Content/conditionerOff.png";
                }
            }
            else if (device is ITemperaturable)
            {
                if (device.State)
                {
                    urlImage = "~/Content/heaterOn.gif";
                }
                else
                {
                    urlImage = "~/Content/heaterOff.png";
                }
            }
            else if (device is ISpeedAirable)
            {
                if (device.State && ((ISpeedAirable)device).LevelSpeed == Speed.Low)
                {
                    urlImage = "~/Content/blowerOnLow.gif";
                }
                else if (device.State && ((ISpeedAirable)device).LevelSpeed == Speed.Medium)
                {
                    urlImage = "~/Content/blowerOnMedium.gif";
                }
                else if (device.State && ((ISpeedAirable)device).LevelSpeed == Speed.Hight)
                {
                    urlImage = "~/Content/blowerOnHight.gif";
                }
                else
                {
                    urlImage = "~/Content/blowerOff.png";
                }
            }
            return(urlImage);
        }
 //public DeviceIconLink ObjectIconLink { private get; set; }
 public string LinkIconDevice(IDevicable device)
 {
     return(objectIconLink.LinkIconDevice(device));
 }
        public string Put(string id, [FromBody] string [] parameters)//textBox
        {
            string nameDevice   = parameters[0];
            string textBoxValue = null;

            if (parameters.Length == 2)
            {
                textBoxValue = parameters[1];
            }

            List <DeviceDb> devicesDbList = deviceDbContext.Devices.ToList();
            DeviceDb        deviceDb      = devicesDbList.Find(dev => dev.Name == nameDevice);
            IDevicable      device        = mapper.GetDeviceModel(deviceDb);

            string result;

            if (device.State == true)
            {
                switch (id)
                {
                case "onOff":
                {
                    device.Off();
                    result = device.State.ToString();
                    //deviceDb.State = device.State;
                    break;
                }

                case "volumeDown":
                {
                    ((IVolumenable)device).VolumeDown();
                    result = ((IVolumenable)device).Volume.ToString();
                    //deviceDb = ChangeStateDevice(deviceDb, device);
                    break;
                }

                case "volumeUp":
                {
                    ((IVolumenable)device).VolumeUp();
                    result = ((IVolumenable)device).Volume.ToString();
                    break;
                }

                case "volumeMute":
                {
                    ((IVolumenable)device).Volume = 0;
                    result = "0";
                    break;
                }

                case "volume":
                {
                    byte data;
                    byte.TryParse(textBoxValue, out data);
                    ((IVolumenable)device).Volume = data;
                    result = ((IVolumenable)device).Volume.ToString();
                    break;
                }

                case "chanelPrevios":
                {
                    ((ISwitchable)device).Previous();
                    result = ((ISwitchable)device).Current.ToString();
                    break;
                }

                case "chanelNext":
                {
                    ((ISwitchable)device).Next();
                    result = ((ISwitchable)device).Current.ToString();
                    break;
                }

                case "current":
                {
                    int data;
                    int.TryParse(textBoxValue, out data);
                    ((ISwitchable)device).Current = data;
                    result = ((ISwitchable)device).Current.ToString();
                    break;
                }

                case "tempDown":
                {
                    ((ITemperaturable)device).TemperatureDown();
                    result = ((ITemperaturable)device).Temperature.ToString();
                    break;
                }

                case "tempUp":
                {
                    ((ITemperaturable)device).TemperatureUp();
                    result = ((ITemperaturable)device).Temperature.ToString();
                    break;
                }

                case "temperature":
                {
                    byte data;
                    byte.TryParse(textBoxValue, out data);
                    ((ITemperaturable)device).Temperature = data;
                    result = ((ITemperaturable)device).Temperature.ToString();
                    break;
                }

                case "bassDown":
                {
                    ((IBassable)device).BassDown();
                    result = ((IBassable)device).BassLevel.ToString();
                    break;
                }

                case "bassUp":
                {
                    ((IBassable)device).BassUp();
                    result = ((IBassable)device).BassLevel.ToString();
                    break;
                }

                case "bass":
                {
                    byte data;
                    byte.TryParse(textBoxValue, out data);
                    ((IBassable)device).BassLevel = data;
                    result = ((IBassable)device).BassLevel.ToString();
                    break;
                }

                case "speedAirLow":
                {
                    ((ISpeedAirable)device).SpeedAirLow();
                    result = id;
                    break;
                }

                case "speedAirMedium":
                {
                    ((ISpeedAirable)device).SpeedAirMedium();
                    result = id;
                    break;
                }

                case "speedAirHight":
                {
                    ((ISpeedAirable)device).SpeedAirHight();
                    result = id;
                    break;
                }

                default:
                {
                    result = "неизвестная команда";
                    break;
                }
                }
            }
            else//false
            {
                switch (id)
                {
                case "onOff":
                {
                    device.On();
                    result = device.State.ToString();

                    break;
                }

                default:
                {
                    result = "deviceErrorStateFalse";
                    break;
                }
                }
            }

            deviceDb.State = device.State;
            deviceDb       = ChangeStateDevice(deviceDb, device);
            //int idDevice = deviceDb.Id;
            //deviceDb = mapper.GetDeviceDb(device);
            //deviceDb.Id = idDevice;

            deviceDbContext.Entry(deviceDb).State = System.Data.Entity.EntityState.Modified;
            deviceDbContext.SaveChanges();

            return(result);
        }
        public IDevicable GetDeviceModel(DeviceDb deviceDb)
        {
            IDevicable device = null;

            if (deviceDb is TVDb)
            {
                device = factory.CreatorTV(deviceDb.Name);
                ((ISwitchable)device).Current = ((TVDb)deviceDb).Channel;
                ((IVolumenable)device).Volume = ((TVDb)deviceDb).Volume;
            }
            else if (deviceDb is SoundDeviceDb)
            {
                device = factory.CreatorSound(deviceDb.Name);
                ((ISwitchable)device).Current = ((SoundDeviceDb)deviceDb).Channel;
                ((IVolumenable)device).Volume = ((SoundDeviceDb)deviceDb).Volume;
            }
            else if (deviceDb is ConditionerDb)
            {
                device = factory.CreatorConditioner(deviceDb.Name);
                ((ITemperaturable)device).Temperature = ((ConditionerDb)deviceDb).Temperature;

                if (((ConditionerDb)deviceDb).LevelSpeedAir == Speed.Low)
                {
                    ((ISpeedAirable)device).SpeedAirLow();
                }
                else if (((ConditionerDb)deviceDb).LevelSpeedAir == Speed.Medium)
                {
                    ((ISpeedAirable)device).SpeedAirMedium();
                }
                else
                {
                    ((ISpeedAirable)device).SpeedAirHight();
                }
            }
            else if (deviceDb is HeaterDb)
            {
                device = factory.CreatorHeater(deviceDb.Name);
                ((ITemperaturable)device).Temperature = ((HeaterDb)deviceDb).Temperature;
            }
            else if (deviceDb is BlowerDb)
            {
                device = factory.CreatorBlower(deviceDb.Name);
                if (((BlowerDb)deviceDb).LevelSpeedAir == Speed.Low)
                {
                    ((ISpeedAirable)device).SpeedAirLow();
                }
                else if (((BlowerDb)deviceDb).LevelSpeedAir == Speed.Medium)
                {
                    ((ISpeedAirable)device).SpeedAirMedium();
                }
                else
                {
                    ((ISpeedAirable)device).SpeedAirHight();
                }
            }
            else
            {
            }
            device.State = deviceDb.State;

            return(device);
        }