public ActionResult Edit(ThermostatEditViewModel thermostatEditViewModel)
        {
            if (ModelState.IsValid)
            {
                var editModel = this.Data.Thermostats.All()
                    .Where(t => t.Id == thermostatEditViewModel.Id)
                    .Select(s => new
                    {
                        Thermostat = s,
                        TermostatDevicePin = s.Device.AttachedPin,
                        SensorIdInArray = s.Sensor.ArduinoArraySensorsId,
                        ReceiverIp = s.Room.Floor.House.ReceiverIp
                    })
                    .SingleOrDefault();
                if (editModel == null)
                {
                    return HttpNotFound();
                }
                editModel.Thermostat.Behavior = thermostatEditViewModel.Behavior;
                editModel.Thermostat.DeviceId = thermostatEditViewModel.DeviceId;
                editModel.Thermostat.State = thermostatEditViewModel.State;
                editModel.Thermostat.TargetTemp = thermostatEditViewModel.TargetTemp;

                ThermostatRCModel thermostatRCModel = new ThermostatRCModel
                {
                    ReceiverIp = editModel.ReceiverIp,
                    Id = editModel.Thermostat.ArduinoArrayTermostatId,
                    State = editModel.Thermostat.State,
                    Behavior = editModel.Thermostat.Behavior,
                    TargetTemp = editModel.Thermostat.TargetTemp,
                    SensorId = editModel.SensorIdInArray,
                    TermostatDevicePin = editModel.TermostatDevicePin
                };

                using (TransactionScope transaction = new TransactionScope())
                {
                    this.Data.Thermostats.Update(editModel.Thermostat);
                    this.Data.SaveChanges();

                    thermostatRCModel.TermostatDevicePin = editModel.TermostatDevicePin;
                    this.RemoteControl.SendThermostatSettings(thermostatRCModel);
                    transaction.Complete();
                }

                return RedirectToAction("RoomDetails", "Rooms", new { RoomId = thermostatEditViewModel.RoomId });
            }
            thermostatEditViewModel.Devices = HelperClass.GetDevicesInRoom(this.Data, thermostatEditViewModel.RoomId, thermostatEditViewModel.DeviceId);
            return View(thermostatEditViewModel);
        }
 public ActionResult Edit(int? id)
 {
     if (id == null)
     {
         return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
     }
     var thermostat = this.Data.Thermostats.All()
         .Where(t => t.Id == id.Value)
         .Select(t => new
        {
            State = t.State,
            Behavior = t.Behavior,
            TargetTemp = t.TargetTemp,
            DeviceId = t.DeviceId,
            Devices = t.Room.Devices.Select(d => new
            {
                Id = d.Id,
                Name = d.Name
            }),
            RoomId = t.RoomId
        }).SingleOrDefault();
     if (thermostat == null)
     {
         return HttpNotFound();
     }
     ThermostatEditViewModel thermostatEditViewModel = new ThermostatEditViewModel()
     {
         Id = id.Value,
         State = thermostat.State,
         Behavior = thermostat.Behavior,
         TargetTemp = thermostat.TargetTemp,
         DeviceId = thermostat.DeviceId,
         Devices = new SelectList(thermostat.Devices, "Id", "Name", thermostat.DeviceId),
         RoomId = thermostat.RoomId
     };
     return View(thermostatEditViewModel);
 }