示例#1
0
        //Deactivate Device
        public IActionResult DeleteDevice(EditDeviceModel data)
        {
            //initializing DB managers
            DBManagerDevice dbManager = new DBManagerDevice(configuration);

            data.Device.ChangedBy = HttpContext.Session.GetString("uniLogin");

            data.Device.Status = 0;

            //change status of device to deactivated
            int success = dbManager.EditDevice(data);

            if (success > 0)
            {
                ViewBag.Delete = "Enhed slettet";
            }
            else
            {
                ViewBag.Delete = "Enhed er i brug";
            }

            //create blank model
            data = new EditDeviceModel();
            DeviceModel device = new DeviceModel();

            data.Device = device;

            // clear model
            ModelState.Clear();

            return(View("EditView", data));
        }
        internal int EditDevice(EditDeviceModel deviceData)
        {
            SqlConnection con = new SqlConnection(connectionString);

            con.Open();
            SqlCommand cmd = new SqlCommand("EditDevice", con);

            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add("@modelName", System.Data.SqlDbType.VarChar).Value        = deviceData.Device.Model.ModelName;
            cmd.Parameters.Add("@modelDescription", System.Data.SqlDbType.VarChar).Value = deviceData.Device.Model.ModelDescription;
            cmd.Parameters.Add("@categoryName", System.Data.SqlDbType.VarChar).Value     = deviceData.Device.Model.Category.Category;
            cmd.Parameters.Add("@deviceID", System.Data.SqlDbType.Int).Value             = deviceData.Device.DeviceID;
            cmd.Parameters.Add("@status", System.Data.SqlDbType.TinyInt).Value           = deviceData.Device.Status;
            cmd.Parameters.Add("@note", System.Data.SqlDbType.VarChar).Value             = deviceData.Device.Notes;
            cmd.Parameters.Add("@changedBy", System.Data.SqlDbType.VarChar).Value        = deviceData.Device.ChangedBy;
            int success = cmd.ExecuteNonQuery();

            if (success > 0)
            {
                success = 1;
            }

            con.Close();
            return(success);
        }
示例#3
0
        public IActionResult GetLocations(EditDeviceModel data)
        {
            //initializing DB managers
            DBManagerDevice dbManager = new DBManagerDevice(configuration);
            DBManagerShared shared    = new DBManagerShared(configuration);

            //get the logs back again
            List <DeviceModel> logs = dbManager.GetDeviceLogs(data.Device.DeviceID);
            int modelID             = shared.GetModelID(data.Device.Model.ModelName);

            data.Logs = logs;
            EditDeviceModel newdata = data;

            //check if image exists
            string filename  = $"Capture_{modelID}.png";
            string imagepath = (string)AppDomain.CurrentDomain.GetData("webRootPath") + "\\DeviceContent\\" + filename;

            if (System.IO.File.Exists(imagepath))
            {
                newdata.ImagePath = filename;
            }

            //fetch storage locations if user has typed a valid room
            if (data.Room != null)
            {
                //prep data for database
                string[] splittedRoom = data.Room.Split('.');

                //prep data model
                EditDeviceModel      editData        = new EditDeviceModel();
                DeviceModel          device          = new DeviceModel();
                BuildingModel        building        = new BuildingModel(splittedRoom[0], Convert.ToByte(splittedRoom[1]));
                StorageLocationModel storageLocation = new StorageLocationModel();
                storageLocation.Location = building;
                device.Location          = storageLocation;
                editData.Device          = device;

                //get storagelocations
                EditDeviceModel locations = dbManager.GetStorageLocations(editData);

                newdata.Shelfs = locations.Shelfs;
                newdata.Shelf  = null;
            }
            //return the same data without having selected anything
            else
            {
                EditDeviceModel storagelocation = dbManager.GetStorageLocations(null);
                newdata.Rooms = storagelocation.Rooms;
                newdata.Shelf = null;
            }



            // clear model
            ModelState.Clear();



            return(View("EditView", newdata));
        }
示例#4
0
        public ActionResult Edit([Bind(Exclude = nameof(EditDeviceModel.CreationTime))] EditDeviceModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var securityDeviceInfo = this.AmiClient.GetDevice(model.Id.ToString());

                    if (securityDeviceInfo == null)
                    {
                        TempData["error"] = Locale.DeviceNotFound;

                        return(RedirectToAction("Index"));
                    }

                    this.AmiClient.UpdateDevice(model.Id.ToString(), this.ToSecurityDeviceInfo(model, securityDeviceInfo));

                    TempData["success"] = Locale.DeviceUpdatedSuccessfully;

                    return(RedirectToAction("ViewDevice", new { id = securityDeviceInfo.Id.ToString() }));
                }

                model.PoliciesList.AddRange(this.GetAllPolicies().ToSelectList("Name", "Id", null, true));
            }
            catch (Exception e)
            {
                Trace.TraceError($"Unable to update device: {e}");
            }

            TempData["error"] = Locale.UnableToUpdateDevice;

            return(View(model));
        }
示例#5
0
        public ActionResult Edit(Guid id)
        {
            try
            {
                var securityDeviceInfo = this.AmiClient.GetDevice(id.ToString());

                if (securityDeviceInfo == null)
                {
                    TempData["error"] = Locale.DeviceNotFound;

                    return(RedirectToAction("Index"));
                }

                var model = new EditDeviceModel(securityDeviceInfo);

                model.PoliciesList.AddRange(this.GetAllPolicies().ToSelectList("Name", "Id", null, true));

                return(View(model));
            }
            catch (Exception e)
            {
                this.TempData["error"] = Locale.UnexpectedErrorMessage;
                Trace.TraceError($"Unable to retrieve device: {e}");
            }

            return(RedirectToAction("Index"));
        }
示例#6
0
        //saves new location on device to database
        public IActionResult EditLocation(EditDeviceModel data)
        {
            //initializing DB managers
            DBManagerDevice dbManager = new DBManagerDevice(configuration);
            DBManagerShared shared    = new DBManagerShared(configuration);

            data.Device.ChangedBy = HttpContext.Session.GetString("uniLogin");

            //prep data for database
            string[] splittedRoom  = data.Room.Split('.');
            string[] splittedShelf = data.Shelf.Split('.');

            //set data models
            BuildingModel        building        = new BuildingModel(splittedRoom[0], Convert.ToByte(splittedRoom[1]));
            StorageLocationModel storageLocation = new StorageLocationModel(splittedShelf[0], Convert.ToByte(splittedShelf[1]), Convert.ToByte(splittedShelf[2]), building);

            data.Device.Location = storageLocation;
            data.Device.Notes    = "Placering redigeret";

            //send data to database
            data = dbManager.EditDeviceLocation(data);

            List <DeviceModel> logs = dbManager.GetDeviceLogs(data.Device.DeviceID);

            data.Logs = logs;

            //save Device name & other important things
            //send data to database
            int success = dbManager.EditDevice(data);
            int modelID = shared.GetModelID(data.Device.Model.ModelName);

            //check if image exists
            string filename  = $"Capture_{modelID}.png";
            string imagepath = (string)AppDomain.CurrentDomain.GetData("webRootPath") + "\\DeviceContent\\" + filename;

            if (System.IO.File.Exists(imagepath))
            {
                data.ImagePath = filename;
            }

            //set message to be shown in view
            if (success > 0)
            {
                ViewBag.Location = "Placering Gemt";
            }
            else
            {
                ViewBag.Location = "Placering ikke Gemt";
            }

            return(View("EditView", data));
        }
        /// <summary>
        /// Converts an <see cref="EditDeviceModel"/> instance to a <see cref="SecurityDeviceInfo"/> instance.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="deviceInfo">The device information.</param>
        /// <returns>Returns the converted security device information instance.</returns>
        protected SecurityDeviceInfo ToSecurityDeviceInfo(EditDeviceModel model, SecurityDeviceInfo deviceInfo)
        {
            deviceInfo.Device.Key  = model.Id;
            deviceInfo.Device.Name = model.Name;

            var policyList = this.GetNewPolicies(model.Policies.Select(Guid.Parse));

            deviceInfo.Policies.Clear();
            deviceInfo.Policies.AddRange(policyList.Select(p => new SecurityPolicyInfo(p)
            {
                Grant = PolicyGrantType.Grant
            }));

            return(deviceInfo);
        }
示例#8
0
        //getting data from database & return model to view
        public IActionResult EditView(string submit)
        {
            //initializing DB managers
            DBManagerDevice dbManager       = new DBManagerDevice(configuration);
            DBManagerShared dbsharedManager = new DBManagerShared(configuration);

            //return device info to Edit view
            int         ID   = int.Parse(submit);
            DeviceModel data = new DeviceModel();

            data = dbManager.GetDeviceInfoWithLocation(ID);
            List <DeviceModel> logs            = dbManager.GetDeviceLogs(ID);
            List <string>      categories      = dbsharedManager.GetCategories();
            List <string>      modelNames      = dbsharedManager.GetModelNames();
            EditDeviceModel    storagelocation = dbManager.GetStorageLocations(null);
            int modelID = dbsharedManager.GetModelID(data.Model.ModelName);

            EditDeviceModel editdata = new EditDeviceModel();

            editdata.Device = data;
            editdata.Room   = new string($"{data.Location.Location.Building}.{data.Location.Location.RoomNumber.ToString()}");
            editdata.Shelf  = new string($"{data.Location.ShelfName}.{data.Location.ShelfLevel}.{data.Location.ShelfSpot}");

            //check if image exists
            string filename  = $"Capture_{modelID}.png";
            string imagepath = (string)AppDomain.CurrentDomain.GetData("webRootPath") + "\\DeviceContent\\" + filename;

            if (System.IO.File.Exists(imagepath))
            {
                editdata.ImagePath = filename;
            }


            editdata.Logs       = logs;
            editdata.Categories = categories;
            editdata.ModelNames = modelNames;
            editdata.Rooms      = storagelocation.Rooms;

            //test to get all rooms and shelves
            storagelocation = dbManager.GetStorageLocations(editdata);
            editdata.Rooms  = storagelocation.Rooms;
            editdata.Shelfs = storagelocation.Shelfs;


            return(View(editdata));
        }
        //edit device location
        internal EditDeviceModel EditDeviceLocation(EditDeviceModel deviceData)
        {
            SqlConnection con = new SqlConnection(connectionString);

            con.Open();
            SqlCommand cmd = new SqlCommand("EditDeviceLocation", con);

            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add("@buildingName", System.Data.SqlDbType.VarChar).Value = deviceData.Device.Location.Location.Building;
            cmd.Parameters.Add("@roomNr", System.Data.SqlDbType.TinyInt).Value       = deviceData.Device.Location.Location.RoomNumber;
            cmd.Parameters.Add("@shelfName", System.Data.SqlDbType.VarChar).Value    = deviceData.Device.Location.ShelfName;
            cmd.Parameters.Add("@shelfLevel", System.Data.SqlDbType.TinyInt).Value   = deviceData.Device.Location.ShelfLevel;
            cmd.Parameters.Add("@shelfSpot", System.Data.SqlDbType.TinyInt).Value    = deviceData.Device.Location.ShelfSpot;
            cmd.Parameters.Add("@changedBy", System.Data.SqlDbType.VarChar).Value    = deviceData.Device.ChangedBy;
            cmd.Parameters.Add("@note", System.Data.SqlDbType.VarChar).Value         = deviceData.Device.Notes;
            cmd.Parameters.Add("@deviceID", System.Data.SqlDbType.Int).Value         = deviceData.Device.DeviceID;
            cmd.ExecuteNonQuery();
            con.Close();
            return(deviceData);
        }
示例#10
0
        //Add Device to Database
        public IActionResult AddDeviceToDB(CreateDeviceModel deviceData)
        {
            //initializing DB managers
            DBManagerDevice dbManager       = new DBManagerDevice(configuration);
            DBManagerShared dbsharedManager = new DBManagerShared(configuration);
            int             modelID         = dbsharedManager.GetModelID(deviceData.Device.Model.ModelName);

            //check if image exists
            //  string filename = $"Capture_{modelID}.png";
            string imagepath = deviceData.Image;
            string _webroot  = (string)AppDomain.CurrentDomain.GetData("webRootPath");

            if (!imagepath.Contains(_webroot))
            {
                //convet image source to byte array
                string sourceimage = deviceData.Image;
                string base64      = sourceimage.Substring(sourceimage.IndexOf(',') + 1);
                byte[] datastream  = Convert.FromBase64String(base64);

                //convert byte array to image file
                using (MemoryStream m = new MemoryStream(datastream))
                {
                    using (Image image = Image.FromStream(m))
                    {
                        string root    = (string)AppDomain.CurrentDomain.GetData("webRootPath");
                        string webroot = root + "\\DeviceContent";

                        string filename = "\\Capture.png";
                        if (Directory.Exists(webroot))
                        {
                            // save image to directory
                            image.Save(webroot + filename, ImageFormat.Png);
                            m.Dispose();
                            image.Dispose();
                            datastream = null;

                            //get filename
                            deviceData.Image = filename;
                        }
                    }
                }
            }


            //Add device to database
            DeviceModel data = deviceData.Device;

            data.ChangedBy = HttpContext.Session.GetString("uniLogin");
            int deviceID = dbManager.CreateDevice(data);

            //return device info to Edit view
            data = dbManager.GetDeviceInfoWithLocation(deviceID);
            List <DeviceModel> logs       = dbManager.GetDeviceLogs(deviceID);
            List <string>      categories = dbsharedManager.GetCategories();
            List <string>      modelNames = dbsharedManager.GetModelNames();

            modelID = dbsharedManager.GetModelID(data.Model.ModelName);

            EditDeviceModel editdata = new EditDeviceModel();

            editdata.Device     = data;
            editdata.Logs       = logs;
            editdata.Categories = categories;
            editdata.ModelNames = modelNames;
            editdata.Room       = new string($"{data.Location.Location.Building}.{data.Location.Location.RoomNumber.ToString()}");
            editdata.Shelf      = new string($"{data.Location.ShelfName}.{data.Location.ShelfLevel}.{data.Location.ShelfSpot}");
            editdata.ImagePath  = $"Capture_{modelID}.png";

            return(View("EditView", editdata));
        }
示例#11
0
        //saves all edits on device to database
        public IActionResult EditDevice(EditDeviceModel data)
        {
            //initializing DB managers
            DBManagerDevice dbManager       = new DBManagerDevice(configuration);
            EditDeviceModel storagelocation = dbManager.GetStorageLocations(null);

            //check categories
            bool Category_validated = checkUserInput(data.Categories, data.Device.Model.Category.Category);

            //check modelNames
            bool ModelName_validated = checkUserInput(data.ModelNames, data.Device.Model.ModelName);

            //check BuildingNames
            bool Building_validated = checkUserInput(storagelocation.Rooms, data.Room);

            //check RoomNames
            bool Room_validated = checkUserInput(storagelocation.Shelfs, data.Shelf);

            if (Category_validated && ModelName_validated && Building_validated && Room_validated)
            {
                Debug.WriteLine("success");
            }
            else
            {
                //set view bags
                if (!Category_validated)
                {
                    ViewBag.Error = "indtast en gyldig kategori";
                }
                else if (!ModelName_validated)
                {
                    ViewBag.Error = "indtast et gyldigt model navn";
                }
                else if (!Building_validated)
                {
                    ViewBag.LocationError = "indtast en gyldig lokation";
                }
                else if (!Room_validated)
                {
                    ViewBag.LocationError = "indtast en gyldig hylde";
                }

                return(View("EditView", data));
            }


            data.Device.ChangedBy = HttpContext.Session.GetString("uniLogin");
            data.Device.Notes     = "Enhed redigeret";

            #region saving new location
            //prep data for database
            string[] splittedRoom  = data.Room.Split('.');
            string[] splittedShelf = data.Shelf.Split('.');

            //set data models
            BuildingModel        building        = new BuildingModel(splittedRoom[0], Convert.ToByte(splittedRoom[1]));
            StorageLocationModel storageLocation = new StorageLocationModel(splittedShelf[0], Convert.ToByte(splittedShelf[1]), Convert.ToByte(splittedShelf[2]), building);
            data.Device.Location = storageLocation;
            //data.Device.Notes = "Placering redigeret";

            //send data to database
            data = dbManager.EditDeviceLocation(data);
            #endregion

            //get the logs again
            List <DeviceModel> logs = dbManager.GetDeviceLogs(data.Device.DeviceID);
            data.Logs = logs;

            //send data to database
            int success = dbManager.EditDevice(data);

            //set message to be shown in view
            if (success > 0)
            {
                ViewBag.edit = "Enhed Gemt";
            }
            else
            {
                ViewBag.edit = "Enhed ikke Gemt";
            }

            return(View("EditView", data));
        }
        //Get storagelocations from database when changing room
        internal EditDeviceModel GetStorageLocations(EditDeviceModel editData)
        {
            SqlConnection con = new SqlConnection(connectionString);

            con.Open();
            SqlCommand cmd = new SqlCommand("GetStorageLocation", con);

            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            if (editData != null)
            {
                cmd.Parameters.Add("@buildingName", System.Data.SqlDbType.VarChar).Value = editData.Device.Location.Location.Building;
                cmd.Parameters.Add("@roomNr", System.Data.SqlDbType.Int).Value           = (int)editData.Device.Location.Location.RoomNumber;
            }
            else
            {
                cmd.Parameters.Add("@buildingName", System.Data.SqlDbType.VarChar).Value = null;
                cmd.Parameters.Add("@roomNr", System.Data.SqlDbType.Int).Value           = null;
            }


            //get data
            SqlDataReader reader           = cmd.ExecuteReader();
            List <string> storageLocations = new List <string>();
            List <string> Rooms            = new List <string>();

            //get shelves & rooms
            while (reader.Read())
            {
                string location = new string($"{(string)reader["shelfName"]}.{(byte)reader["shelfLevel"]}.{(byte)reader["shelfSpot"]}");
                storageLocations.Add(location);

                if (editData == null)
                {
                    //add rooms
                    string room = new string($"{(string)reader["buildingName"]}.{(byte)reader["roomNr"]}");
                    if (Rooms.Count <= 0)
                    {
                        Rooms.Add(room);
                    }
                    //add room to list of not the same
                    else
                    {
                        if (!string.Equals(room, Rooms[Rooms.Count - 1]))
                        {
                            Rooms.Add(room);
                        }
                    }
                }
            }



            EditDeviceModel data = new EditDeviceModel();

            data.Rooms  = Rooms;
            data.Shelfs = storageLocations;


            con.Close();
            return(data);
        }