public ActionResult Home(HomeClientModelView SenorName)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                string    ClientEmail = Session["UserId"].ToString();
                Resetings reset       = new Resetings();
                Senzors   senzor      = new Senzors();

                try
                {
                    senzor            = db.senzors.FirstOrDefault(s => s.Name == SenorName.SenzorName);
                    reset.SenzorsId   = senzor.id;
                    reset.BateryLevel = senzor.BateryLevel;
                    reset.ResetDate   = DateTime.Now;
                    reset.HasChecked  = true;
                    db.resetings.Add(reset);
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                    ViewBag.Error = "Something went wrong finding the senzor";
                }
            }
            return(Home());
        }
Пример #2
0
        public ActionResult Edit()
        {
            AssignedSenzor senzorClients = new AssignedSenzor(true);

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                var clients = db.client.ToList();

                foreach (var c in clients)
                {
                    Clients       client  = new Clients();
                    List <string> senzors = new List <string>();
                    try
                    {
                        senzors = db.senzors.Where(s => s.ClientId == c.id).Select(d => d.Name).ToList();
                        senzorClients.assignedSenzor.Add(c.Email, senzors);
                    }
                    catch (Exception e)
                    {
                        Senzors senzor = new Senzors();
                        senzor.Name = "No senzor assigned to the user";
                        senzors.Add(senzor.Name);
                        senzorClients.assignedSenzor.Add(c.Email, senzors);
                    }
                }
            }
            return(View(senzorClients));
        }
        public ActionResult Home()
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                string ClientEmail       = Session["UserId"].ToString();
                HomeClientModelView home = new HomeClientModelView();

                Senzors senzor = new Senzors();
                try
                {
                    var client = db.client.FirstOrDefault(c => c.Email == ClientEmail);
                    home.Email     = ClientEmail;
                    home.FirstName = client.FirstName;
                    home.LastName  = client.LastName;
                    home.ImagePath = client.PathImage;
                    home.senzors   = db.senzors.Where(s => s.ClientId == client.id).ToList();
                    if (home.senzors.Count() > 0)
                    {
                        string KeySenzor = "";
                        foreach (var reset in home.senzors)
                        {
                            List <string> SenzorInfo = new List <string>();
                            Resetings     reseting   = new Resetings();

                            KeySenzor = reset.Name;
                            //SenzorInfo.Add(reset.Name);
                            SenzorInfo.Add(reset.BateryLevel.ToString());

                            try
                            {
                                //  reseting = db.resetings.LastOrDefault(r => r.SenzorsId == reset.id);
                                var getall = db.resetings.GroupBy(r => r.SenzorsId).Select(p => p.FirstOrDefault(w => w.id == p.Max(m => m.id)))
                                             .OrderBy(p => p.SenzorsId).ToList();
                                reseting = getall.Where(s => s.SenzorsId == reset.id).FirstOrDefault();
                                SenzorInfo.Add(reseting.BateryLevel.ToString());
                                SenzorInfo.Add(reseting.HasChecked.ToString());
                            }
                            catch (Exception e)
                            {
                                SenzorInfo.Add("TBD");
                                SenzorInfo.Add("New Senzor");
                            }
                            home.SenzorInfo.Add(KeySenzor, SenzorInfo);
                        }
                    }
                    return(View(home));
                }
                catch (Exception e)
                {
                    ViewBag.Error = "";
                }
                return(View(home));
            }
        }
Пример #4
0
        public ActionResult AddSenzor(SenzorAdd NewSenzor)
        {
            if (!ModelState.IsValid)
            {
                return(View(NewSenzor));
            }

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Clients client          = new Clients();
                string  clientEmail     = "";
                var     CheckSenzorName = db.senzors.Where(S => S.Name == NewSenzor.NameOfSenzor);
                if (CheckSenzorName.Count() > 0)
                {
                    ViewBag.Error = "Name exist";
                    return(View());
                }
                try
                {
                    clientEmail = db.client.FirstOrDefault(c => c.Email == "*****@*****.**").FirstName;
                    client      = db.client.First(c => c.Email == "*****@*****.**");
                }
                catch (Exception e)
                {
                    Clients MasterClient = new Clients();
                    MasterClient.Email     = "*****@*****.**";
                    MasterClient.FirstName = "Master";
                    MasterClient.LastName  = "Master";
                    MasterClient.Password  = "******";
                    client = db.client.Add(MasterClient);
                    db.SaveChanges();
                }

                Senzors senzor = new Senzors();
                senzor.Clients     = client;
                senzor.ClientId    = client.id;
                senzor.Name        = NewSenzor.NameOfSenzor;
                senzor.ValueType   = NewSenzor.ValueType;
                senzor.BateryLevel = 100;
                db.senzors.Add(senzor);
                db.SaveChanges();
            }
            return(RedirectToAction("/Index"));
        }
Пример #5
0
 public ActionResult Index(AssignedSenzor assigned)
 {
     using (ApplicationDbContext db = new ApplicationDbContext())
     {
         Clients client = new Clients();
         client = db.client.FirstOrDefault(c => c.Email == assigned.EmailClient);
         if (client != null)
         {
             Senzors senzor = new Senzors();
             senzor = db.senzors.First(s => s.Name == assigned.SenzorName);
             if (senzor != null)
             {
                 senzor.ClientId        = client.id;
                 senzor.Clients         = client;
                 db.Entry(senzor).State = EntityState.Modified;
                 db.SaveChanges();
             }
         }
     }
     return(Index());
 }
Пример #6
0
        public ActionResult Edit(AssignedSenzor senzorClients)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                try
                {
                    Clients client = new Clients();
                    client = db.client.FirstOrDefault(c => c.Email == senzorClients.EmailClient);
                    Senzors senzor = new Senzors();
                    senzor                 = db.senzors.FirstOrDefault(s => s.Name == senzorClients.SenzorName);
                    senzor.ClientId        = 1;
                    db.Entry(senzor).State = EntityState.Modified;
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                    ViewBag.Error = "On error has occur while trying to remove the senzor";
                }
            }

            return(Edit());
        }