public void SendDevicesSettings(DevicesRCModel devicesRCModel)
 {
     this.Sender.SendMessageToReceiver(new
     {
         SetDevices = devicesRCModel.Devices
     }, devicesRCModel.ReceiverIp);
 }
        public ActionResult EditDevices(IEnumerable<DeviceViewModel> devicesViewModel)
        {
            DevicesRCModel devicesRCModel = new DevicesRCModel
            {
                Devices = new List<BaseDeviceRCModel>()
            };
            using (TransactionScope transaction = new TransactionScope())
            {
                bool isFirst = true;
                foreach (var deviceViewModel in devicesViewModel)
                {
                    Device device = null;
                    if(isFirst)
                    {
                        var deviceModel = this.Data.Devices.All()
                            .Where(d => d.Id == deviceViewModel.Id)
                            .Select(d => new
                            {
                                device = d,
                                ReceiverIp = d.Room.Floor.House.ReceiverIp

                            }).SingleOrDefault();
                        if (deviceModel == null)
                        {
                            throw new HttpException(500, "No device with this Id");
                        }
                        device = deviceModel.device;
                        devicesRCModel.ReceiverIp = deviceModel.ReceiverIp;
                        isFirst = false;
                    }
                    else
                    {
                       device = this.Data.Devices.GetById(deviceViewModel.Id);
                        if (device == null)
                        {
                            throw new HttpException(500, "No device with this Id");
                        }
                    }
                    device.State = deviceViewModel.State;

                    devicesRCModel.Devices.Add(new BaseDeviceRCModel
                    {
                        Pin = device.AttachedPin,
                        State = device.State
                    });
                    this.Data.Devices.Update(device);
                }
                this.Data.SaveChanges();

                if (devicesRCModel.Devices.Count > 0)
                {
                    this.RemoteControl.SendDevicesSettings(devicesRCModel);
                }
                transaction.Complete();
            }
            return new HttpStatusCodeResult(200);
        }