예제 #1
0
        public ActionResult Create(ThermostatCreateViewModel thermostatCreateViewModel)
        {
            if (ModelState.IsValid)
            {
                var createModel = this.Data.Rooms.All()
                   .Where(r => r.Id == thermostatCreateViewModel.RoomId)
                   .Select(s => new
                   {
                       Room = s,
                       ThermostatDevicePin = s.Devices
                            .Where(d => d.Id == thermostatCreateViewModel.DeviceId)
                            .FirstOrDefault().AttachedPin,
                       HouseId = s.Floor.HouseId,
                       ThermostatCounts = s.Floor.Rooms.Where(r => r.Thermostat != null).Count(),
                       SensorIdInArray = s.Sensor.ArduinoArraySensorsId,
                       ReceiverIp = s.Floor.House.ReceiverIp
                   })
                   .SingleOrDefault();
                if (createModel == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                Thermostat thermostat = new Thermostat
                {
                    SensorId = createModel.Room.SensorId.Value,
                    Behavior = thermostatCreateViewModel.Behavior,
                    DeviceId = thermostatCreateViewModel.DeviceId,
                    RoomId = thermostatCreateViewModel.RoomId,
                    State = thermostatCreateViewModel.State,
                    TargetTemp = thermostatCreateViewModel.TargetTemp,
                    ArduinoArrayTermostatId = createModel.ThermostatCounts + 1
                };
                ThermostatRCModel thermostatRCModel = new ThermostatRCModel
                {
                    ReceiverIp = createModel.ReceiverIp,
                    Id = thermostat.ArduinoArrayTermostatId,
                    State = thermostat.State,
                    Behavior = thermostat.Behavior,
                    TargetTemp = thermostat.TargetTemp,
                    SensorId = createModel.SensorIdInArray,
                    TermostatDevicePin = createModel.ThermostatDevicePin,

                };
                createModel.Room.Thermostat = thermostat;

                using (TransactionScope transaction = new TransactionScope())
                {
                    this.Data.Rooms.Update(createModel.Room);
                    this.Data.SaveChanges();

                    this.RemoteControl.SendThermostatSettings(thermostatRCModel);
                    transaction.Complete();
                }

                return RedirectToAction("RoomDetails", "Rooms", new { RoomId = thermostat.RoomId });
            }
            thermostatCreateViewModel.Devices = HelperClass.GetDevicesInRoom(this.Data, thermostatCreateViewModel.RoomId, thermostatCreateViewModel.DeviceId);
            return View(thermostatCreateViewModel);
        }
예제 #2
0
        public ActionResult Create(int roomId)
        {
            var room = this.Data.Rooms.All()
               .Where(r => r.Id == roomId)
               .Select(r => new
               {

                   ThermostatId = r.ThermostatId,
                   SensorId = r.SensorId,
                   Devices = r.Devices.Select(d => new
                    {
                        Id = d.Id,
                        Name = d.Name
                    }),
                   r.Floor.HouseId
               }).SingleOrDefault();
            if (room == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            if (!HelperClass.CheckArraySizeThermostats(this.Data, room.HouseId))
            {
                return PartialView("_ErrorMessagePartial", "The number of thermostats for this house has reached maximum value");
            }
            if (room.ThermostatId != null)
            {
                return PartialView("_ErrorMessagePartial", "Аlready exists thermostat for this room ");
            }
            if (room.SensorId == null || !(room.Devices.Count() > 0))
            {
                return PartialView("_ErrorMessagePartial", "Тhis room has no sensor or deveices");
            }
            var thermostatCreateViewModel = new ThermostatCreateViewModel
                {
                    RoomId = roomId,
                    Devices = new SelectList(room.Devices, "Id", "Name")
                };

            return View(thermostatCreateViewModel);
        }