Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            ISmartHouseCreator shc = Manufacture.GetManufacture(modelAssembly);

            if (!IsPostBack)
            {
                CreateDeviceTypeDropDownList();
                Session["showAddDevice"] = null;
            }

            if (Session["SmartHouse"] == null)
            {
                ISmartDevice    dev;
                IBrightable     ibri;
                IHaveThermostat iterm;

                sh = shc.CreateSmartHouse();

                dev = shc.CreateDevice("SmartLamp", "l1");

                ibri = dev as IBrightable;
                ibri.BrightnessMax  = 100;
                ibri.BrightnessMin  = 10;
                ibri.BrightnessStep = 10;
                ibri.Brightness     = ibri.BrightnessMax;
                sh.AddDevice(dev);

                dev = shc.CreateDevice("SmartLamp", "l2");

                ibri = dev as IBrightable;
                ibri.BrightnessMax  = 100;
                ibri.BrightnessMin  = 10;
                ibri.BrightnessStep = 15;
                ibri.Brightness     = ibri.BrightnessMax;
                sh.AddDevice(dev);

                dev = shc.CreateDevice("Fridge", "fr1");

                iterm          = dev as IHaveThermostat;
                iterm.TempMax  = 0;
                iterm.TempMin  = -5;
                iterm.TempStep = 1;
                dev.On();
                iterm.DecreaseTemperature();
                sh.AddDevice(dev);

                dev = shc.CreateDevice("Clock", "clk1");

                dev.On();
                sh.AddDevice(dev);

                Session.Add("SmartHouse", sh);
            }
            else
            {
                sh = Session["SmartHouse"] as ISmartHouse;
            }

            RefreshControls();
        }
Exemplo n.º 3
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));
        }
Exemplo n.º 4
0
        // POST: api/Device
        public IHttpActionResult Post([FromBody] string value)
        {
            IHttpActionResult           result = BadRequest();
            Dictionary <string, string> fields;
            DataContractJsonSerializer  js = new DataContractJsonSerializer(typeof(Dictionary <string, string>));

            using (Stream str = GenerateStreamFromString(value))
            {
                fields = (Dictionary <string, string>)js.ReadObject(str);
            }
            ISmartHouse sh = LoadSmartHouse();

            if (sh[fields[CreateDeviceFields.name]] == null)
            {
                Assembly           modelAssembly = Assembly.Load("SmartHouse");
                ISmartHouseCreator shc           = Manufacture.GetManufacture(modelAssembly);

                ISmartDevice dev = shc.CreateDevice(fields[CreateDeviceFields.devType], fields[CreateDeviceFields.name]);
                if (dev != null)
                {
                    if (dev is IBrightable)
                    {
                        IBrightable idev = dev as IBrightable;
                        idev.BrightnessMax  = int.Parse(fields[CreateDeviceFields.brightnessMax]);
                        idev.BrightnessMin  = int.Parse(fields[CreateDeviceFields.brightnessMin]);
                        idev.BrightnessStep = int.Parse(fields[CreateDeviceFields.brightnessStep]);
                    }

                    if (dev is IHaveThermostat)
                    {
                        IHaveThermostat idev = dev as IHaveThermostat;
                        idev.TempMax  = int.Parse(fields[CreateDeviceFields.temperatureMax]);
                        idev.TempMin  = int.Parse(fields[CreateDeviceFields.temperatureMin]);
                        idev.TempStep = int.Parse(fields[CreateDeviceFields.temperatureStep]);
                    }


                    sh.AddDevice(dev);
                    SaveSmartHouse(sh);
                    result = Ok();
                }
            }
            else
            {
                result = BadRequest(string.Format("Устройство {0} уже есть в системе", fields[CreateDeviceFields.name]));
            }
            return(result);
        }
Exemplo n.º 5
0
        private ISmartHouse CreateTestSet()
        {
            ISmartHouseCreator shc = Manufacture.GetManufacture(modelAssembly);

            ISmartHouse sh = null;

            ISmartDevice    dev;
            IBrightable     ibri;
            IHaveThermostat iterm;

            sh = shc.CreateSmartHouse();

            dev = shc.CreateDevice("SmartLamp", "l1");

            ibri = dev as IBrightable;
            ibri.BrightnessMax  = 100;
            ibri.BrightnessMin  = 10;
            ibri.BrightnessStep = 10;
            ibri.Brightness     = ibri.BrightnessMax;
            sh.AddDevice(dev);

            dev = shc.CreateDevice("SmartLamp", "l2");

            ibri = dev as IBrightable;
            ibri.BrightnessMax  = 100;
            ibri.BrightnessMin  = 10;
            ibri.BrightnessStep = 15;
            ibri.Brightness     = ibri.BrightnessMax;
            sh.AddDevice(dev);

            dev = shc.CreateDevice("Fridge", "fr1");

            iterm          = dev as IHaveThermostat;
            iterm.TempMax  = 0;
            iterm.TempMin  = -5;
            iterm.TempStep = 1;
            dev.On();
            iterm.DecreaseTemperature();
            sh.AddDevice(dev);

            dev = shc.CreateDevice("Clock", "clk1");

            dev.On();
            sh.AddDevice(dev);

            return(sh);
        }
Exemplo n.º 6
0
        private DeviceCreationContext GetDeviceCreationContext(string devType)
        {
            DeviceCreationContext dcc        = null;
            ISmartHouseCreator    devCreator = Manufacture.GetManufacture(modelAssembly);

            ISmartDevice dev = devCreator.CreateDevice(devType, "dummy");

            if (dev != null)
            {
                dcc = new DeviceCreationContext();
                dcc.DevTypeTranslation = dev.DeviceType;
                dcc.DevType            = devType;
                dcc.DevIsBrightable    = dev is IBrightable;
                dcc.DevHasThermostat   = dev is IHaveThermostat;
            }

            return(dcc);
        }
Exemplo n.º 7
0
        public static ISmartHouseCreator GetManufacture(Assembly smartHouseAssembly)
        {
            ISmartHouseCreator shc = null;
            Type shcType           = null;

            var res = from t in smartHouseAssembly.GetTypes()
                      where t.GetInterfaces().Contains(typeof(ISmartHouseCreator))
                      select t;

            shcType = res.FirstOrDefault();

            if (shcType != null)
            {
                object[] constructorArgs = new object[1];
                constructorArgs[0] = smartHouseAssembly;

                shc = Activator.CreateInstance(shcType, constructorArgs) as ISmartHouseCreator;
            }

            return(shc);
        }
Exemplo n.º 8
0
        protected void btnAddDevice_OnClick(object sender, EventArgs e)
        {
            ISmartHouseCreator shc = Manufacture.GetManufacture(Assembly.Load("SmartHouse"));

            string name;

            name = (FindControl(idName) as TextBox).Text.ToLower();

            ISmartDevice dev = shc.CreateDevice(DevType, name);

            //TODO: Вот где-то здесь какая-то верификация должна быть. Наверное.

            if (dev is IHaveThermostat)
            {
                int             min, max, step;
                IHaveThermostat iterm = dev as IHaveThermostat;

                TextBox tbMin  = FindControl(idTempMin) as TextBox;
                TextBox tbMax  = FindControl(idTempMax) as TextBox;
                TextBox tbStep = FindControl(idTempStep) as TextBox;

                if (!int.TryParse(tbMax.Text, out max))
                {
                    int.TryParse(tbMax.Attributes["placeholder"], out max);
                }
                if (!int.TryParse(tbMin.Text, out min))
                {
                    int.TryParse(tbMin.Attributes["placeholder"], out min);
                }
                if (!int.TryParse(tbStep.Text, out step))
                {
                    int.TryParse(tbStep.Attributes["placeholder"], out step);
                }

                iterm.TempMax     = max;
                iterm.TempMin     = min;
                iterm.TempStep    = step;
                iterm.Temperature = max;
            }

            if (dev is IBrightable)
            {
                int         min, max, step;
                IBrightable ibri = dev as IBrightable;

                TextBox tbMin  = FindControl(idBrightMin) as TextBox;
                TextBox tbMax  = FindControl(idBrightMax) as TextBox;
                TextBox tbStep = FindControl(idBrightStep) as TextBox;

                if (!int.TryParse(tbMax.Text, out max))
                {
                    int.TryParse(tbMax.Attributes["placeholder"], out max);
                }
                if (!int.TryParse(tbMin.Text, out min))
                {
                    int.TryParse(tbMin.Attributes["placeholder"], out min);
                }
                if (!int.TryParse(tbStep.Text, out step))
                {
                    int.TryParse(tbStep.Attributes["placeholder"], out step);
                }

                ibri.BrightnessMax  = max;
                ibri.BrightnessMin  = min;
                ibri.BrightnessStep = step;
                ibri.Brightness     = max;
            }

            SmartHouse.AddDevice(dev);

            Session["showAddDevice"] = null;
            ParentForm.RefreshControls();
        }