Ejemplo n.º 1
0
        public ActionResult Edit(DeviceEditViewModel deviceEditViewModel)
        {
            if (ModelState.IsValid)
            {
                var editModel = this.Data.Devices.All()
                    .Where(d => d.Id == deviceEditViewModel.Id)
                    .Select(d => new
                {
                    Device = d,
                    ReceiverIp = d.Room.Floor.House.ReceiverIp
                }).SingleOrDefault();
                if (editModel == null)
                {
                    return HttpNotFound();
                }
                Int16? attachedPinForDeleting = null;
                if (editModel.Device.AttachedPin != deviceEditViewModel.AttachedPin)
                {
                    attachedPinForDeleting = editModel.Device.AttachedPin;
                }
                editModel.Device.AttachedPin = deviceEditViewModel.AttachedPin;
                editModel.Device.Name = deviceEditViewModel.Name;
                editModel.Device.State = deviceEditViewModel.State;

                using (TransactionScope transaction = new TransactionScope())
                {
                    this.Data.Devices.Update(editModel.Device);
                    this.Data.SaveChanges();

                    if (attachedPinForDeleting != null)
                    {
                        this.RemoteControl.SendDeviceDelete(
                            new DeviceDeleteRCModel
                        {
                            ReceiverIp = editModel.ReceiverIp,
                            AttachedPin = attachedPinForDeleting.Value
                        });
                    }

                    this.RemoteControl.SendDeviceSettings(
                        new DeviceRCModel
                        {
                            ReceiverIp = editModel.ReceiverIp,
                            Pin = deviceEditViewModel.AttachedPin,
                            State = deviceEditViewModel.State
                        });
                    transaction.Complete();
                }

                return RedirectToAction("RoomDetails", "Rooms", new { RoomId = deviceEditViewModel.RoomId });
            }
            return View(deviceEditViewModel);
        }
Ejemplo n.º 2
0
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var device = this.Data.Devices.All()
              .Where(d => d.Id == id.Value)
              .Select(d => new
              {
                  AttachedPin = d.AttachedPin,
                  Id = d.Id,
                  Name = d.Name,
                  RoomId = d.RoomId,
                  State = d.State,
                  HouseId = d.Room.Floor.HouseId

              })
              .SingleOrDefault();
            if (device == null)
            {
                return HttpNotFound();
            }
            var allowedPin = HelperClass.GetAllowedPins(this.Data, device.HouseId, device.AttachedPin);
            DeviceEditViewModel deviceEditViewModel = new DeviceEditViewModel
            {
                AttachedPin = device.AttachedPin,
                Id = device.Id,
                Name = device.Name,
                RoomId = device.RoomId,
                State = device.State,
                AllowedPins = new SelectList(allowedPin, device.AttachedPin)

            };
            return View(deviceEditViewModel);
        }