Esempio n. 1
0
 public ActionResult Create(int roomId)
 {
     var room = this.Data.Rooms.All()
         .Where(r => r.Id == roomId)
        .Select(r => new
        {
            SensorId = r.SensorId,
            HouseId = r.Floor.HouseId
        }).SingleOrDefault();
     if (room == null)
     {
         return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
     }
     if (!HelperClass.CheckArraySizeSensors(this.Data, room.HouseId))
     {
         return PartialView("_ErrorMessagePartial", "The number of sensors for this house has reached maximum value");
     }
     if (room.SensorId != null)
     {
         return PartialView("_ErrorMessagePartial", "Аlready exists sensor for this room ");
     }
     var allowedPin = HelperClass.GetAllowedPins(this.Data, room.HouseId, null);
     if (!(allowedPin.Count() > 0))
     {
         return PartialView("_ErrorMessagePartial", "Тhere are no free pins");
     }
     SensorCreteViewModel sensorCreteViewModel = new SensorCreteViewModel
     {
         AllowedPins = new SelectList(allowedPin),
         RoomId = roomId
     };
     return View(sensorCreteViewModel);
 }
Esempio n. 2
0
        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);
        }