public ActionResult Edit(SensorEditViewModel sensorEditViewModel) { if (ModelState.IsValid) { var editModel = this.Data.Sensors.All() .Where(s => s.Id == sensorEditViewModel.Id) .Select(s => new { Sensor = s, ReceiverIp = s.Room.Floor.House.ReceiverIp, }).SingleOrDefault(); if (editModel == null) { return HttpNotFound(); } editModel.Sensor.AlarmCheck = sensorEditViewModel.AlarmCheck; editModel.Sensor.MaxTempAlert = sensorEditViewModel.MaxTempAlert; editModel.Sensor.MinTempAlert = sensorEditViewModel.MinTempAlert; editModel.Sensor.AttachedPin = sensorEditViewModel.AttachedPin; SensorRCModel sensorRCModel = new SensorRCModel { ReceiverIp = editModel.ReceiverIp, Id = editModel.Sensor.ArduinoArraySensorsId, AttachedPin = sensorEditViewModel.AttachedPin, AlarmCheck = sensorEditViewModel.AlarmCheck, MinTempAlert = sensorEditViewModel.MinTempAlert, MaxTempAlert = sensorEditViewModel.MaxTempAlert, }; using (TransactionScope transaction = new TransactionScope()) { this.Data.Sensors.Update(editModel.Sensor); this.Data.SaveChanges(); this.RemoteControl.SendSensorSettings(sensorRCModel); transaction.Complete(); } return RedirectToAction("RoomDetails", "Rooms", new { RoomId = sensorEditViewModel.RoomId }); } int houseId = this.Data.Rooms.All() .Where(r => r.Id == sensorEditViewModel.RoomId) .Select(s => s.Floor.HouseId).SingleOrDefault(); var allowedPin = HelperClass.GetAllowedPins(this.Data, houseId, null); sensorEditViewModel.AllowedPins = new SelectList(allowedPin); return View(sensorEditViewModel); }
public ActionResult Create(SensorCreteViewModel sensorCreteViewModel) { if (ModelState.IsValid) { var createModel = this.Data.Rooms.All() .Where(r => r.Id == sensorCreteViewModel.RoomId) .Select(s => new { Room = s, SensorCounts = s.Floor.Rooms.Where(r => r.Sensor != null).Count(), HouseId = s.Floor.HouseId, ReceiverIp = s.Floor.House.ReceiverIp }) .SingleOrDefault(); if (createModel == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Sensor sensor = new Sensor { AlarmCheck = sensorCreteViewModel.AlarmCheck, AttachedPin = sensorCreteViewModel.AttachedPin, MaxTempAlert = sensorCreteViewModel.MaxTempAlert, MinTempAlert = sensorCreteViewModel.MinTempAlert, RoomId = sensorCreteViewModel.RoomId, ArduinoArraySensorsId = createModel.SensorCounts + 1 }; SensorRCModel sensorRCModel = new SensorRCModel { ReceiverIp = createModel.ReceiverIp, Id = createModel.SensorCounts + 1, AttachedPin = sensorCreteViewModel.AttachedPin, AlarmCheck = sensorCreteViewModel.AlarmCheck, MinTempAlert = sensorCreteViewModel.MinTempAlert, MaxTempAlert = sensorCreteViewModel.MinTempAlert, }; createModel.Room.Sensor = sensor; using (TransactionScope transaction = new TransactionScope()) { this.Data.Rooms.Update(createModel.Room); this.Data.SaveChanges(); this.RemoteControl.SendSensorSettings(sensorRCModel); transaction.Complete(); } return RedirectToAction("RoomDetails", "Rooms", new { RoomId = sensor.RoomId }); } int houseId = this.Data.Rooms.All() .Where(r => r.Id == sensorCreteViewModel.RoomId) .Select(s => s.Floor.HouseId).SingleOrDefault(); var allowedPin = HelperClass.GetAllowedPins(this.Data, houseId, null); sensorCreteViewModel.AllowedPins = new SelectList(allowedPin); return View(sensorCreteViewModel); }
public void SendSensorSettings(SensorRCModel sensorRCModel) { var modelSensor = new { SetSensor = new Dictionary<string, object>() }; modelSensor.SetSensor.Add("SensorId", sensorRCModel.Id); modelSensor.SetSensor.Add("SensorPin", sensorRCModel.AttachedPin); modelSensor.SetSensor.Add("AlarmCheck",sensorRCModel.AlarmCheck); if (sensorRCModel.AlarmCheck) { modelSensor.SetSensor.Add("MinTempAlert", sensorRCModel.MinTempAlert); modelSensor.SetSensor.Add("MaxTempAlert",sensorRCModel.MaxTempAlert); } this.Sender.SendMessageToReceiver(modelSensor, sensorRCModel.ReceiverIp); }