public ActionResult AddEmptyToYour(int DeviceSerial)
 {
     using (var context = new DevicesEntities())
     {
         var result = (from ent in context.Device
                       where ent.DeviceSerial == DeviceSerial
                       select ent).First();
         result.UserName = User.Identity.Name;
         UpdateModel(result);
         context.SaveChanges();
         return RedirectToAction("emptyDevices");
     }
 }
        public string AddDevice(int DeviceSerial, int Type, int State)
        {
            using (var context = new DevicesEntities())
            {

                Device device = new Device();
                device.DeviceSerial = DeviceSerial;
                device.Type = Type;
                device.State = State;
                device.UserName = "******";
                try
                {
                    context.Device.Add(device);
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }

                return "Device" + device.DeviceSerial + "Connected";

            }
        }
        public string AddSensor(int DeviceSerial, int Type, int State, int Tconst, int Temp)
        {
            using (var context = new DevicesEntities())
            {

                Device device = new Device();
                device.DeviceSerial = DeviceSerial;
                device.Type = Type;
                device.State = State;
                device.UserName = "******";

                Sensor sensor = new Sensor();
                sensor.DeviceSerial = DeviceSerial;
                sensor.tconst = Tconst;
                sensor.temperature = Temp;
                try
                {
                    context.Device.Add(device);
                    context.Sensor.Add(sensor);
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }

                return "Sensor" + device.DeviceSerial + "Connected";

            }
        }
        public ActionResult TurnOnOff(int DeviceSerial)
        {
            using (var context = new DevicesEntities())
            {
                var device = (from entity in context.Device
                              where entity.DeviceSerial == DeviceSerial
                              select entity).First();
                if (device.State == 0)
                {
                    device.State = 1;
                }
                else
                {
                    device.State = 0;
                }
                UpdateModel(device);
                context.SaveChanges();
                TcpClient Client = new TcpClient();
                Client.Connect(IPAddress.Parse(IpPort.ip), IpPort.port);

                Socket Sock = Client.Client;
                Sock.Send(MySerialize.serialize(device, typeof(Device)));

                Sock.Close();
                Client.Close();

                return RedirectToAction("Index", "Home");
            }
        }
        public ActionResult TconstSet(Sensor model)
        {
            using (var context = new DevicesEntities())
            {

                var device = (from entity in context.Sensor
                              where entity.DeviceSerial == model.DeviceSerial
                              select entity).First();

                device.tconst = model.tconst;

                UpdateModel(device);
                context.SaveChanges();
                TcpClient Client = new TcpClient();
                Client.Connect(IPAddress.Parse(IpPort.ip), IpPort.port);

                Socket Sock = Client.Client;
                Sock.Send(MySerialize.serialize(device, typeof(Sensor)));

                Sock.Close();
                Client.Close();
            }

            return RedirectToAction("Index", "Home");
        }
        public ActionResult setDeviceOnMap(int DeviceSerial, string lat, string lng)
        {
            using (var context = new DevicesEntities())
            {
                var result = (from ent in context.Device
                              where ent.DeviceSerial == DeviceSerial
                              select ent).First();

                result.mapX = lat;
                result.mapY = lng;
                result.UserName = User.Identity.Name;
                UpdateModel(result);
                context.SaveChanges();
                return RedirectToAction("emptyDevices");

            }
        }
        public ActionResult ignoreAlarmAndMail(int DeviceSerial)
        {
            using (var context = new DevicesEntities())
            {
                var deviceAlarm = (from entity in context.Alarm
                                   where entity.DeviceSerial == DeviceSerial
                                   select entity).First();
                context.Alarm.Attach(deviceAlarm);
                context.Alarm.Remove(deviceAlarm);
                context.SaveChanges();
            }

            var user = Membership.GetUser(User.Identity.Name);

            var email = user.Email;

            var subject = "Some trouble with device " + DeviceSerial;
            var text = "Device " + DeviceSerial + " Alarmed " + Environment.NewLine +
                "This Report is autogenerated";
            MailSender.sendMail(subject, email, text);

            return RedirectToAction("Index");
        }
        public ActionResult ignoreAlarm(int DeviceSerial)
        {
            using (var context = new DevicesEntities())
            {
                var deviceAlarm = (from entity in context.Alarm
                                   where entity.DeviceSerial == DeviceSerial
                                   select entity).First();

                context.Alarm.Attach(deviceAlarm);
                context.Alarm.Remove(deviceAlarm);
                context.SaveChanges();
                return RedirectToAction("Index");
            }
        }
 public ActionResult DeleteShedule(int id, FormCollection collection)
 {
     using (var context = new DevicesEntities())
     {
         var employeeDelete = (from entity in context.Time
                               where entity.Id == id
                               select entity).First();
         try
         {
             context.Time.Attach(employeeDelete);
             context.Time.Remove(employeeDelete);
             context.SaveChanges();
             return RedirectToAction("Schedule", new { DeviceSerial = employeeDelete.DeviceSerial });
         }
         catch
         {
             return View(employeeDelete);
         }
     }
 }
 public string DelDevice(int DeviceSerial)
 {
     using (var context = new DevicesEntities())
     {
         var device = (from entity in context.Device
                       where entity.DeviceSerial == DeviceSerial
                       select entity).First();
         try
         {
             context.Device.Attach(device);
             context.Device.Remove(device);
             context.SaveChanges();
         }
         catch
         {
             return "Error deleting device:" + device.DeviceSerial;
         }
         return "Device deleted" + device.DeviceSerial;
     }
 }
 public ActionResult CreateShedule(Time time)
 {
     using (var context = new DevicesEntities())
     {
         try
         {
             if (ModelState.IsValid)
             {
                 context.Time.Add(time);
                 context.SaveChanges();
                 return RedirectToAction("Schedule", new { DeviceSerial = time.DeviceSerial });
             }
         }
         catch (Exception ex)
         {
             ModelState.AddModelError(String.Empty, ex);
         }
         return View(time);
     }
 }
        public string ChangeTemperature(int DeviceSerial, int temperature)
        {
            using (var context = new DevicesEntities())
            {
                var device = (from entity in context.Sensor
                              where entity.DeviceSerial == DeviceSerial
                              select entity).First();
                device.temperature = temperature;

                UpdateModel(device);
                context.SaveChanges();
                return " New Temperature" + device.temperature;
            }

            //public ActionResult Send()
            //{
            //    return View();
            //}
            //[HttpPost]
            //public ActionResult Send(EmailModel model)
            //{
            // //   if (ModelState.IsValid)
            //  //  {
            //        new EmailController().SendEmail(model).Deliver();
            //    //}
            //    return View(model);
            //}
        }
        public string ChangeState(int DeviceSerial, int state)
        {
            using (var context = new DevicesEntities())
            {
                var device = (from entity in context.Device
                              where entity.DeviceSerial == DeviceSerial
                              select entity).First();

                device.State = state;

                UpdateModel(device);
                context.SaveChanges();
                return "New State is " + device.State;
            }
        }
        public string AlarmSignalization(int DeviceSerial)
        {
            using (var context = new DevicesEntities())
            {
                Alarm alarm = new Alarm();
                alarm.DeviceSerial = DeviceSerial;

                try
                {
                    context.Alarm.Add(alarm);
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }

                return "Signalization" + alarm.DeviceSerial + "Allarmed";
            }
        }