예제 #1
0
        protected SmartHouseConfig GetConfig()
        {
            Configuration    config    = WebConfigurationManager.OpenWebConfiguration(HostingEnvironment.ApplicationVirtualPath);
            SmartHouseConfig shSection = (SmartHouseConfig)config.GetSection(SmartHouseConfig.SectionName);

            return(shSection);
        }
예제 #2
0
        private ISmartHouse LoadFromStorage()
        {
            ISmartHouse result = null;

            SmartHouseConfig shConfig    = GetConfig();
            string           storagePath = Path.Combine(shConfig.StorageFilePath, shConfig.StorageFileName);

            storagePath = HostingEnvironment.MapPath(storagePath);
            FileInfo fi = null;

            try
            {
                fi = new FileInfo(storagePath);
            }
            catch { }

            if (fi != null && fi.Exists)
            {
                using (FileStream fs = fi.Open(FileMode.Open))
                {
                    try
                    {
                        BinaryFormatter bf = new BinaryFormatter();
                        result = bf.Deserialize(fs) as ISmartHouse;
                    }
                    catch { }
                }
            }

            return(result);
        }
예제 #3
0
        protected ISmartHouse LoadSmartHouse()
        {
            ISmartHouse        sh       = null;
            SmartHouseConfig   shConfig = GetConfig();
            ISmartHouseCreator shc      = Manufacture.GetManufacture(modelAssembly);
            Type smartHouseType         = shc.SmartHouseType;

            if (smartHouseType.IsSubclassOf(typeof(DbContext)))
            {
                sh = shc.CreateSmartHouse();
            }
            else
            {
                if (shConfig.UseSession)
                {
                    var Session = HttpContext.Current.Session;
                    sh = Session["SmartHouse"] as ISmartHouse;
                }
                else
                {
                    sh = LoadFromStorage();
                }
            }
            return(sh);
        }
예제 #4
0
        public ActionResult CreateDevice()
        {
            ViewBag.Title = title;

            SmartHouseContext  shContext  = LoadContext();
            ISmartHouseCreator devCreator = Manufacture.GetManufacture(modelAssembly);

            string devType = Request.Params[CreateDeviceFields.devType];

            string devName = Request.Params[CreateDeviceFields.name];

            if (devName == "" || devName == null)
            {
                devName = CreateDevicePlaceholders.name;
            }

            ISmartDevice dev = devCreator.CreateDevice(devType, devName);

            if (dev != null)
            {
                FillIHaveThermostat(dev);
                FillIBrightable(dev);

                shContext.SmartHouse.AddDevice(dev);
            }

            SmartHouseConfig shConfig = GetConfig();

            SaveSmartHouse(shContext.SmartHouse);

            return(View("Index", shContext as object));
        }
예제 #5
0
        private bool SaveToStorage(ISmartHouse sh)
        {
            bool saved = false;

            SmartHouseConfig shConfig    = GetConfig();
            string           storagePath = Path.Combine(shConfig.StorageFilePath, shConfig.StorageFileName);

            storagePath = HostingEnvironment.MapPath(storagePath);
            FileInfo fi = null;

            try
            {
                fi = new FileInfo(storagePath);
            }
            catch { }

            if (fi != null)
            {
                using (FileStream fs = fi.Open(FileMode.Create))
                {
                    try
                    {
                        BinaryFormatter bf = new BinaryFormatter();
                        bf.Serialize(fs, sh);
                        saved = true;
                    }
                    catch { }
                }
            }

            return(saved);
        }
예제 #6
0
        public ActionResult AdjustBrightness(string id, string direction)
        {
            ViewBag.Title = title;

            SmartHouseContext shContext = LoadContext();

            ISmartDevice dev = shContext.SmartHouse[id];

            if (dev is IBrightable)
            {
                IBrightable thermo = dev as IBrightable;
                switch (direction)
                {
                case AdjustDirections.increase:
                    thermo.IncreaseBrightness();
                    break;

                case AdjustDirections.decrease:
                    thermo.DecreaseBrightness();
                    break;
                }
            }

            SmartHouseConfig shConfig = GetConfig();

            SaveSmartHouse(shContext.SmartHouse);

            return(View("Index", shContext as object));
        }
예제 #7
0
        private SmartHouseConfig GetConfig()
        {
            Configuration    config    = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
            SmartHouseConfig shSection = (SmartHouseConfig)config.GetSection(SmartHouseConfig.SectionName);

            return(shSection);
        }
예제 #8
0
        public ActionResult Delete(string id)
        {
            ViewBag.Title = title;

            SmartHouseContext shContext = LoadContext();

            shContext.SmartHouse.RemoveDevice(id);

            SmartHouseConfig shConfig = GetConfig();

            SaveSmartHouse(shContext.SmartHouse);

            return(View("Index", shContext as object));
        }
예제 #9
0
        public ActionResult ToggleOpenClose(string id)
        {
            ViewBag.Title = title;

            SmartHouseContext shContext = LoadContext();

            ISmartDevice dev = shContext.SmartHouse[id];

            if (dev is IOpenCloseable)
            {
                (dev as IOpenCloseable).IsOpened ^= true;
            }

            SmartHouseConfig shConfig = GetConfig();

            SaveSmartHouse(shContext.SmartHouse);

            return(View("Index", shContext as object));
        }
예제 #10
0
        private void SaveSmartHouse(ISmartHouse sh)
        {
            SmartHouseConfig shConfig = GetConfig();

            if (sh is DbContext)
            {
                (sh as DbContext).SaveChanges();
            }
            else
            {
                if (shConfig.UseSession)
                {
                    Session.Add("SmartHouse", sh);
                }
                else
                {
                    SaveToStorage(sh);
                }
            }
        }
예제 #11
0
        private SmartHouseContext LoadContext()
        {
            ISmartHouse       sh;
            SmartHouseContext shContext = new SmartHouseContext();
            SmartHouseConfig  shConfig  = GetConfig();

            sh = LoadSmartHouse();

            if (sh == null)
            {
                sh = CreateTestSet();
                SaveSmartHouse(sh);
            }

            shContext.SmartHouse         = sh;
            shContext.TypesAvailable     = LoadAvailableDevTypes();
            shContext.DevCreationContext = null;

            return(shContext);
        }
예제 #12
0
        protected void SaveSmartHouse(ISmartHouse sh)
        {
            SmartHouseConfig shConfig = GetConfig();

            if (sh is DbContext)
            {
                (sh as DbContext).SaveChanges();
            }
            else
            {
                if (shConfig.UseSession)
                {
                    var Session = HttpContext.Current.Session;
                    Session.Add("SmartHouse", sh);
                }
                else
                {
                    SaveToStorage(sh);
                }
            }
        }
예제 #13
0
        public ActionResult TogglePower(string id)
        {
            ViewBag.Title = title;

            SmartHouseContext shContext = LoadContext();

            switch (shContext.SmartHouse[id].State)
            {
            case EPowerState.On:
                shContext.SmartHouse[id].State = EPowerState.Off;
                break;

            case EPowerState.Off:
                shContext.SmartHouse[id].State = EPowerState.On;
                break;
            }

            SmartHouseConfig shConfig = GetConfig();

            SaveSmartHouse(shContext.SmartHouse);

            return(View("Index", shContext as object));
        }