protected SmartHouseConfig GetConfig() { Configuration config = WebConfigurationManager.OpenWebConfiguration(HostingEnvironment.ApplicationVirtualPath); SmartHouseConfig shSection = (SmartHouseConfig)config.GetSection(SmartHouseConfig.SectionName); return(shSection); }
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); }
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); }
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)); }
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); }
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)); }
private SmartHouseConfig GetConfig() { Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); SmartHouseConfig shSection = (SmartHouseConfig)config.GetSection(SmartHouseConfig.SectionName); return(shSection); }
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)); }
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)); }
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); } } }
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); }
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); } } }
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)); }