Example #1
0
        public ActionResult EditDevice(string txtName, short selType, string txtIP, byte selLocation, string selUplinkIP, string txtUplinkPort, bool mainDevice, HttpPostedFileBase uplConfig, byte returnId, string[] NIP, string[] A, string[] B)
        {
            try
            {
                using (var scope = new TransactionScope())
                {
                    // Device
                    var device = db.TD_Device.Find(txtIP);
                    device.Type       = selType;
                    device.Name       = txtName;
                    device.UplinkIP   = selUplinkIP;
                    device.UplinkPort = txtUplinkPort;
                    device.EntryDate  = DateTime.Now;

                    if (uplConfig != null && uplConfig.ContentLength > 0)
                    {
                        var serverPath = dir.SaveFile(uplConfig, configFilePath, txtIP + "-" + uplConfig.FileName);
                        if (device.ConfigFile != null && System.IO.File.Exists(device.ConfigFile))
                        {
                            System.IO.File.Delete(Server.MapPath(root + device.ConfigFile));
                        }

                        device.ConfigFile = serverPath;
                    }

                    // Nieghbors
                    var oldNieghbor = db.TD_Neighbor.Where(w => w.IP == txtIP || w.NIP == txtIP);
                    foreach (var item in oldNieghbor)
                    {
                        db.TD_Neighbor.Remove(item);
                    }

                    if (NIP != null)
                    {
                        for (int i = 0; i < NIP.Length; i++)
                        {
                            var neighbor = new TD_Neighbor();
                            neighbor.IP      = txtIP;
                            neighbor.NIP     = NIP[i];
                            neighbor.AnchorA = A[i];
                            neighbor.AnchorB = B[i];

                            db.TD_Neighbor.Add(neighbor);
                        }
                    }

                    db.SaveChanges();
                    scope.Complete();
                }

                TempData["Msg"] = "Update Device " + txtIP + " - " + txtName + " Success";
            }
            catch (Exception ex)
            {
                TempData["Msg"] = "Update Device Error: " + ex.Message;
            }

            if (mainDevice)
            {
                return(RedirectToAction("GetMap", "Home", new { plantid = returnId, mode = "admin" }));
            }
            else
            {
                return(RedirectToAction("GetMap", "Home", new { facid = returnId, mode = "admin" }));
            }
        }
Example #2
0
        public ActionResult AddDevice(string txtName, short selType, string txtIP, byte selLocation, string selUplinkIP, string txtUplinkPort, HttpPostedFileBase uplConfig, bool mainDevice, byte returnId, string[] NIP, string[] A, string[] B)
        {
            try
            {
                using (var scope = new TransactionScope())
                {
                    var device = new TD_Device();
                    device.IP         = txtIP;
                    device.Type       = selType;
                    device.FactoryId  = selLocation;
                    device.Name       = txtName;
                    device.UplinkIP   = selUplinkIP;
                    device.UplinkPort = txtUplinkPort;
                    device.MainDevice = mainDevice;
                    device.EntryDate  = DateTime.Now;
                    if (mainDevice == true)
                    {
                        device.XMain = 0;
                        device.YMain = 0;
                    }

                    if (uplConfig != null && uplConfig.ContentLength > 0)
                    {
                        var serverPath = dir.SaveFile(uplConfig, configFilePath, txtIP + "-" + uplConfig.FileName);
                        device.ConfigFile = serverPath;
                    }

                    db.TD_Device.Add(device);

                    if (NIP != null)
                    {
                        for (int i = 0; i < NIP.Length; i++)
                        {
                            var neighbor = new TD_Neighbor();
                            neighbor.IP      = txtIP;
                            neighbor.NIP     = NIP[i];
                            neighbor.AnchorA = A[i];
                            neighbor.AnchorB = B[i];

                            db.TD_Neighbor.Add(neighbor);
                        }
                    }

                    db.SaveChanges();
                    scope.Complete();
                }

                TempData["Msg"] = "Add Device " + txtIP + " - " + txtName + " Success";
            }
            catch (Exception ex)
            {
                TempData["Msg"] = "Add Device Error: " + ex.Message;
            }

            if (mainDevice)
            {
                return(RedirectToAction("GetMap", "Home", new { plantid = returnId, mode = "admin" }));
            }
            else
            {
                return(RedirectToAction("GetMap", "Home", new { facid = returnId, mode = "admin" }));
            }
        }