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);
        }
        public void SendThermostatSettings(ThermostatRCModel thermostatRCModel)
        {
            var modelThermostat = new
            {
                SetTermostat = new Dictionary<string, object>()
            };
            modelThermostat.SetTermostat.Add("TermostatId", thermostatRCModel.Id);
            modelThermostat.SetTermostat.Add("SensorId", thermostatRCModel.SensorId);
            modelThermostat.SetTermostat.Add("TermostatDevicePin", thermostatRCModel.TermostatDevicePin);
            modelThermostat.SetTermostat.Add("TermostatState", thermostatRCModel.State);

            if (thermostatRCModel.State)
            {
                modelThermostat.SetTermostat.Add("Behavior", thermostatRCModel.Behavior);
                modelThermostat.SetTermostat.Add("TargetTemp", thermostatRCModel.TargetTemp);
            }
            this.Sender.SendMessageToReceiver(modelThermostat, thermostatRCModel.ReceiverIp);
        }
        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);
        }