public Object AddDevice([FromBody]DevNew devNew)
        {
            switch (devNew.Type)
            {
                case "Lamp":
                    dev = db.Lamps.Add(new Lamp(devNew.Name, true));
                    break;
                case "Fan":
                    dev = db.Fans.Add(new Fan(devNew.Name, true, 1));
                    break;
                case "Louvers":
                    dev = db.LouversSet.Add(new Louvers(devNew.Name, true, 1));
                    break;
                case "Tv":
                    dev = db.TvSet.Add(new Tv(devNew.Name, true, Channels.football, 2));
                    break;
            }

            if (dev != null)
            {
                try
                {
                    db.SaveChanges();
                    return dev;
                }
                catch
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }
        public PartialViewResult AjaxDown(int id, string sType, string sParam)
        {
            ModelContext db = new ModelContext();
            IDevice dev = new Device();

            switch (sType)
            {
                case "Fan":
                    dev = db.Fans.Include("Speed").FirstOrDefault(p => p.Id == id);
                    ((Fan)dev).Speed.Down();
                    break;
                case "Louvers":
                    dev = db.LouversSet.Include("Open").FirstOrDefault(p => p.Id == id);
                    ((Louvers)dev).Open.Down();
                    break;
                case "Tv":
                    dev = db.TvSet.Include("Volume").FirstOrDefault(p => p.Id == id);
                    switch (sParam)
                    {
                        case "Volume":
                            ((Tv)dev).Volume.Down();
                            break;
                        case "Program":
                            ((Tv)dev).PreviousChannel();
                            break;
                    }
                    break;
            }
            db.Entry(dev).State = EntityState.Modified;
            db.SaveChanges();

            return PartialView("DivDevice", dev);
        }
        public sbyte ChangeParam([FromBody]DevParam devInfo)
        {
            sbyte newValue = -1;

            if (devInfo.Cmd == "onoff")
            {
                switch (devInfo.Type)
                {
                    case "Lamp":
                        dev = db.Lamps.Find(devInfo.Id);
                        break;
                    case "Fan":
                        dev = db.Fans.Include("Speed").FirstOrDefault(p => p.Id == devInfo.Id);
                        break;
                    case "Louvers":
                        dev = db.LouversSet.Include("Open").FirstOrDefault(p => p.Id == devInfo.Id);
                        break;
                    case "Tv":
                        dev = db.TvSet.Include("Volume").FirstOrDefault(p => p.Id == devInfo.Id);
                        break;
                }
                dev.OnOff();
                if (dev.State)
                {
                    newValue = 1;
                }
                else
                {
                    newValue = 0;
                }
            }
            else
            {
                switch (devInfo.Type)
                {
                    case "Fan":
                        dev = db.Fans.Include("Speed").FirstOrDefault(p => p.Id == devInfo.Id);
                        if (devInfo.Cmd == "down")
                            newValue = (sbyte)((Fan)dev).Speed.Down();
                        else
                            newValue = (sbyte)((Fan)dev).Speed.Up();
                        break;
                    case "Louvers":
                        dev = db.LouversSet.Include("Open").FirstOrDefault(p => p.Id == devInfo.Id);
                        if (devInfo.Cmd == "down")
                            newValue = (sbyte)((Louvers)dev).Open.Down();
                        else
                            newValue = (sbyte)((Louvers)dev).Open.Up();
                        break;
                    case "Tv":
                        switch (devInfo.Param)
                        {
                            case "Volume":
                                dev = db.TvSet.Include("Volume").FirstOrDefault(p => p.Id == devInfo.Id);
                                if (devInfo.Cmd == "down")
                                    newValue = (sbyte)((Tv)dev).Volume.Down();
                                else
                                    newValue = (sbyte)((Tv)dev).Volume.Up();
                                break;
                            case "Program":
                                dev = db.TvSet.Find(devInfo.Id);
                                if (devInfo.Cmd == "down")
                                    newValue = (sbyte)((Tv)dev).PreviousChannel();
                                else
                                    newValue = (sbyte)((Tv)dev).NextChannel();
                                break;
                        }
                        break;
                }
            }

            if (dev != null)
            {
                try
                {
                    db.Entry(dev).State = EntityState.Modified;
                    db.SaveChanges();

                    return newValue;
                }
                catch
                {
                    return -1;
                }
            }
            else
            {
                return -1;
            }
        }
        // Get data of device
        public Object GetDevice(int id, string type)
        {
            switch (type)
            {
                case "Lamp":
                    dev = db.Lamps.Find(id);
                    break;
                case "Fan":
                    dev = db.Fans.Include("Speed").FirstOrDefault(p => p.Id == id);
                    break;
                case "Louvers":
                    dev = db.LouversSet.Include("Open").FirstOrDefault(p => p.Id == id);
                    break;
                case "Tv":
                    dev = db.TvSet.Include("Volume").FirstOrDefault(p => p.Id == id);
                    break;
            }

            if (dev != null)
            {
                return dev;
            }
            else
            {
                return null;
            }
        }
        public PartialViewResult AjaxOnOff(int id, string sType)
        {
            ModelContext db = new ModelContext();
            IDevice dev = new Device();

            switch (sType)
            {
                case "Lamp":
                    db.Lamps.Find(id).OnOff();
                    dev = db.Lamps.Find(id);
                    break;
                case "Fan":
                    db.Fans.Find(id).OnOff();
                    dev = db.Fans.Include("Speed").FirstOrDefault(p => p.Id == id);
                    break;
                case "Louvers":
                    db.LouversSet.Find(id).OnOff();
                    dev = db.LouversSet.Include("Open").FirstOrDefault(p => p.Id == id);
                    break;
                case "Tv":
                    db.TvSet.Find(id).OnOff();
                    dev = db.TvSet.Include("Volume").FirstOrDefault(p => p.Id == id);
                    break;
            }
            db.SaveChanges();

            return PartialView("DivDevice", dev);
        }