Пример #1
0
        public ActionResult SaveLocation(ComponentLocation location)
        {
            if (!ModelState.IsValid && location.Id != 0)
            {
                var viewModel = new LocationFormViewModel
                {
                    Location = location,
                };
                return(View("LocationForm", viewModel));
            }

            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            //Choose between add and update
            if (location.Id == 0)
            {
                xmlDB.Locations.Add(location);
            }
            else
            {
                var locationInDB = xmlDB.Locations.Single(c => c.Id == location.Id);
                locationInDB.Name   = location.Name;
                locationInDB.Prefix = location.Prefix;
            }

            xmlDB.Save();

            return(RedirectToAction("Index", "HWConf"));
        }
Пример #2
0
        // GET /api/locations
        public IEnumerable <ComponentLocation> GetLocations()
        {
            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            return(xmlDB.Locations);
        }
Пример #3
0
        //==========================================================================================================
        //Call form add/edit io
        public ActionResult IOForm(int id)
        {
            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            String pageName;

            if (String.IsNullOrEmpty(userName))
            {
                pageName = "";
            }
            else
            {
                pageName = _context.Users.First(c => c.Name == userName).ActProject;
            }

            var io = xmlDB.IOs.SingleOrDefault(c => c.Id == id);

            if (io == null)
            {
                return(HttpNotFound());
            }

            var viewModel = new EditIOIOListViewModel
            {
                IO          = io,
                IOLocations = xmlDB.Locations,
                Parents     = xmlDB.Components.Where(c => c.Dependancy == Enums.Dependancy.Parent),
                PageName    = pageName
            };

            return(View("IOForm", viewModel));
        }
Пример #4
0
        // GET /api/IOs
        public IEnumerable <IO> GetIOs()
        {
            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            return(xmlDB.IOs);
        }
Пример #5
0
        //==========================================================================================================
        //MAIN PAGE
        public ActionResult Index()
        {
            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            String pageName;

            if (String.IsNullOrEmpty(userName))
            {
                pageName = "";
            }
            else
            {
                pageName = _context.Users.First(c => c.Name == userName).ActProject;
            }

            var viewModel = new IOListViewModel
            {
                IOs        = xmlDB.IOs,
                Components = xmlDB.Components,
                Standards  = xmlDB.Standards,
                PageName   = pageName
            };

            return(View(viewModel));
        }
Пример #6
0
        public ActionResult SaveModule(Module module)
        {
            if (!ModelState.IsValid && module.Id != 0)
            {
                var viewModel = new ModuleFormViewModel
                {
                    Module = module,
                };
                return(View("ModuleForm", viewModel));
            }



            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            // Create new or update
            if (module.Id == 0)
            {
                //new
                module.Address = xmlDB.Modules.Count();
                xmlDB.Modules.Add(module);
            }
            else
            {
                //Update
                var moduleInDB = xmlDB.Modules.Single(c => c.Id == module.Id);
                moduleInDB.Name          = module.Name;
                moduleInDB.IOModulesType = module.IOModulesType;
            }

            xmlDB.Save();

            return(RedirectToAction("Index", "HWConf"));
        }
Пример #7
0
        public IO UpdateIO(int id, IO IO)
        {
            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            if (!ModelState.IsValid)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            var IOInDb = xmlDB.IOs.SingleOrDefault(c => c.Id == id);

            if (IOInDb == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            IOInDb.Comment        = IO.Comment;
            IOInDb.ComponentId    = IO.ComponentId;
            IOInDb.ConnectionType = IO.ConnectionType;
            IOInDb.Location       = IO.Location;
            IOInDb.MatchStatus    = IO.MatchStatus;
            IOInDb.Name           = IO.Name;
            IOInDb.ParentName     = IO.ParentName;
            IOInDb.IOAddress      = IO.IOAddress;

            xmlDB.Save();

            return(IO);
        }
Пример #8
0
        //====================================================================================================================
        //MAIN PAGE
        public ActionResult Index()
        {
            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            String pageName;

            if (String.IsNullOrEmpty(userName))
            {
                pageName = "";
            }
            else
            {
                pageName = _context.Users.First(c => c.Name == userName).ActProject;
            }

            var viewModel = new HWConfViewModel
            {
                Modules   = xmlDB.Modules,
                Locations = xmlDB.Locations,
                PLC       = xmlDB.PLC,
                PageName  = pageName
            };

            return(View(viewModel));
        }
Пример #9
0
        private void btnTestDbConnection_Click(object sender, EventArgs e)
        {
            AbstractDatabase tmpDb;

            tmpDb = null;
            if (rdoJson.Checked)
            {
                tmpDb = new JsonDatabase();
            }
            if (rdoMSSQL.Checked)
            {
                tmpDb = new SqlServerDatabase();
            }
            if (rdoMySQL.Checked)
            {
                tmpDb = new MySqlDatabase();
            }
            if (rdoSQLite.Checked)
            {
                tmpDb = new SQLiteDatabase();
            }
            if (rdoXML.Checked)
            {
                tmpDb = new XMLDatabase();
            }

            tmpDb.ConnectionString = BuildConnectionString().EncryptTextToBase64String();
            if (tmpDb.TestConnection())
            {
                MessageBox.Show("Success!");
            }
        }
Пример #10
0
        internal static AbstractDatabase GetDatabase()
        {
            AbstractDatabase db = null;

            switch (settings.dbType)
            {
            case 0:
                db = new JsonDatabase();
                db.ConnectionString = settings.dbServer;
                break;

            case 1:
                db = new SqlServerDatabase();
                db.ConnectionString = settings.dbConnStr;
                break;

            case 2:
                db = new MySqlDatabase();
                db.ConnectionString = settings.dbConnStr;
                break;

            case 3:
                db = new SQLiteDatabase();
                db.ConnectionString = settings.dbConnStr;
                break;

            case 4:
                db = new XMLDatabase();
                db.ConnectionString = settings.dbServer;
                break;
            }
            db.CreateTable();

            return(db);
        }
Пример #11
0
        //====================================================================================================================
        // ADD COMPONENT
        public ActionResult AddComponent()
        {
            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            //Load pagename
            String pageName;

            if (String.IsNullOrEmpty(userName))
            {
                pageName = "";
            }
            else
            {
                pageName = _context.Users.First(c => c.Name == userName).ActProject;
            }

            var addComponentFactoryViewModel = new AddComponentFactoryViewModel()
            {
                ComponentLocations = xmlDB.Locations.ToList(),
                Standards          = xmlDB.Standards.ToList(),
                PageName           = pageName
            };

            return(View("ComponentForm", addComponentFactoryViewModel));
        }
Пример #12
0
        //====================================================================================================================
        // MAIN COMPONENT SETUP PAGE
        public ActionResult Index()
        {
            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            //Load pagename
            String pageName;

            if (String.IsNullOrEmpty(userName))
            {
                pageName = "";
            }
            else
            {
                pageName = _context.Users.First(c => c.Name == userName).ActProject;
            }

            var viewModel = new FactoryViewModel
            {
                Components = xmlDB.Components.ToList(),
                IOs        = xmlDB.IOs.ToList(),
                //AIAlarms = xmlDB.AIAlarms.ToList(),
                //DIAlarms = xmlDB.DIAlarms.ToList(),
                Standnards = xmlDB.Standards.ToList(),
                PageName   = pageName
            };

            return(View(viewModel));
        }
Пример #13
0
        long receive2 = 0;                                              //若干时间差后获得的数据

        /// <summary>
        /// 构造函数
        /// </summary>
        public HunterDownload(Hunter _p)
        {
            hunterProject = _p;
            hunterConsole = _p.mHunterConsole;
            database      = _p.xmlDatabase;
            pInfo         = _p.projectInfo;
            strategy      = _p.projectInfo.strategy;
        }
Пример #14
0
        // GET /api/IO
        public IHttpActionResult GetIO(int id)
        {
            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            var IO = xmlDB.IOs.SingleOrDefault(c => c.Id == id);

            if (IO == null)
            {
                return(BadRequest());
            }

            return(Ok(IO));
        }
Пример #15
0
        public ActionResult Details(int id)
        {
            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            var standard = xmlDB.Standards.SingleOrDefault(c => c.Id == id);

            if (standard == null)
            {
                return(HttpNotFound());
            }

            return(View(standard));
        }
Пример #16
0
        public IHttpActionResult CreateIO(IO IO)
        {
            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            xmlDB.IOs.Add(IO);
            xmlDB.Save();

            return(Created(new Uri(Request.RequestUri + "/" + IO.Id), IO));
        }
Пример #17
0
        public IHttpActionResult CreateLocation(ComponentLocation location)
        {
            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            xmlDB.Locations.Add(location);
            xmlDB.Save();

            return(Created(new Uri(Request.RequestUri + "/" + location.Id), location));
        }
Пример #18
0
        public ActionResult Approve(int stdId, int IOId)
        {
            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            var ioInDb        = xmlDB.IOs.First(c => c.Id == IOId);
            var componentInDb = xmlDB.Components.First(c => c.Id == ioInDb.ComponentId);

            componentInDb.StandardId  = stdId;
            ioInDb.MatchStatus        = Enums.MatchStatus.Match;
            componentInDb.MatchStatus = Enums.MatchStatus.Match;

            xmlDB.Save();

            return(RedirectToAction("Index", "IOList"));
        }
Пример #19
0
        public void DeleteIO(int id)
        {
            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            var IOInDb = xmlDB.IOs.SingleOrDefault(c => c.Id == id);

            if (IOInDb == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            xmlDB.IOs.Remove(IOInDb);
            xmlDB.Save();
            return;
        }
Пример #20
0
        //====================================================================================================================
        //ADD LOCATIONS
        private void DetectLocations()
        {
            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            foreach (IO io in xmlDB.IOs.ToList())
            {
                //Add location if not duplicate
                if (xmlDB.Locations.SingleOrDefault(c => c.Name == io.Location) == null) //Add location if it doesnt exist and if it is not empty or null
                {
                    xmlDB.Locations.Add(new ComponentLocation {
                        Name = io.Location
                    });
                    xmlDB.Save();
                }
            }

            return;
        }
Пример #21
0
        public ActionResult SavePLC(PLC PLC)
        {
            if (!ModelState.IsValid && PLC.Id != 0)
            {
                var viewModel = new PLCFormViewModel
                {
                    PLC = PLC,
                };
                return(View("PLCForm", viewModel));
            }
            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            xmlDB.PLC = PLC;
            xmlDB.Save();
            //------------------------------------------------------------------------
            //Add embedded IO modules

            if (PLC.ProductType == Enums.ControllerType.L16ER || PLC.ProductType == Enums.ControllerType.L18ER &&
                (xmlDB.Modules.Where(c => ((c.IOModulesType == Enums.IOModulesType.EmbDIx16) || (c.IOModulesType == Enums.IOModulesType.EmbDOx16))).Count() == 0))
            {
                var embDI = new Module
                {
                    Name          = "PLC_Emb_DI",
                    IOModulesType = Enums.IOModulesType.EmbDIx16,
                    Address       = 1
                };
                var embDO = new Module
                {
                    Name          = "PLC_Emb_DO",
                    IOModulesType = Enums.IOModulesType.EmbDOx16,
                    Address       = 1
                };

                xmlDB.Modules.Add(embDI);
                xmlDB.Modules.Add(embDO);
                xmlDB.Save();
            }



            return(RedirectToAction("Index", "HWConf"));
        }
Пример #22
0
        //====================================================================================================================
        //PLC HANDLING
        //Call PLC form
        public ActionResult AddPLC()
        {
            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            if (xmlDB.PLC == null)
            {
                return(View("PLCForm"));
            }
            else
            {
                var viewModel = new PLCFormViewModel
                {
                    PLC = xmlDB.PLC
                };

                return(View("PLCForm", viewModel));
            }
        }
Пример #23
0
        //====================================================================================================================
        //LOCATION HANDLING
        //Call location form
        public ActionResult LocationForm(int id)
        {
            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            var location = xmlDB.Locations.SingleOrDefault(c => c.Id == id);

            if (location == null)
            {
                return(HttpNotFound());
            }

            var viewModel = new LocationFormViewModel
            {
                Location = location,
            };

            return(View("LocationForm", viewModel));
        }
Пример #24
0
        //====================================================================================================================
        //MODULE HANDLING
        //Call module form
        public ActionResult ModuleForm(int id)
        {
            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            var module = xmlDB.Modules.SingleOrDefault(c => c.Id == id);

            if (module == null)
            {
                return(HttpNotFound());
            }

            var viewModel = new ModuleFormViewModel
            {
                Module = module,
            };

            return(View("ModuleForm", viewModel));
        }
Пример #25
0
        public IHttpActionResult UpdateComponent(int id, Component component)
        {
            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            if (!ModelState.IsValid)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            var componentInDb = xmlDB.Components.SingleOrDefault(c => c.Id == id);

            if (componentInDb == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }



            componentInDb.MatchStatus = Enums.MatchStatus.Match;


            componentInDb.Name           = component.Name;
            componentInDb.Comment        = component.Comment;
            componentInDb.Location       = component.Location;
            componentInDb.StandardId     = component.StandardId;
            componentInDb.ConnectionType = componentInDb.ConnectionType;
            componentInDb.Dependancy     = component.Dependancy;

            var io = xmlDB.IOs.FirstOrDefault(c => c.Id == componentInDb.IOId);

            if (io != null)
            {
                io.MatchStatus = Enums.MatchStatus.Match;
                io.ComponentId = componentInDb.Id;
            }

            xmlDB.Save();
            return(Ok());
        }
Пример #26
0
        public ComponentLocation UpdateLocation(int id, ComponentLocation location)
        {
            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            if (!ModelState.IsValid)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            var locationInDb = xmlDB.Locations.SingleOrDefault(c => c.Id == id);

            if (locationInDb == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            locationInDb.Prefix = location.Prefix;
            locationInDb.Name   = location.Name;

            xmlDB.Save();

            return(location);
        }
Пример #27
0
        public Module UpdateModule(int id, Module module)
        {
            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            if (!ModelState.IsValid)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            var moduleInDb = xmlDB.Modules.SingleOrDefault(c => c.Id == id);

            if (moduleInDb == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            moduleInDb.Name          = module.Name;
            moduleInDb.IOModulesType = module.IOModulesType;

            xmlDB.Save();

            return(module);
        }
Пример #28
0
        public ActionResult Save(IO io)
        {
            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            //Load page name
            String pageName;

            if (String.IsNullOrEmpty(userName))
            {
                pageName = "";
            }
            else
            {
                pageName = _context.Users.First(c => c.Name == userName).ActProject;
            }

            if (!ModelState.IsValid)
            {
                var viewModel = new EditIOIOListViewModel
                {
                    IO          = io,
                    IOLocations = xmlDB.Locations.ToList(),
                    Parents     = xmlDB.Components.Where(c => c.Dependancy == Enums.Dependancy.Parent),
                    PageName    = pageName
                };

                return(View("IOForm", viewModel));
            }

            //Component created by user -> set to match
            io.MatchStatus = Enums.MatchStatus.Match;

            if (String.IsNullOrWhiteSpace(io.ParentName) || String.IsNullOrEmpty(io.ParentName))
            {
                // UPDATE/ADD Single IOs Component
                var componentInDb = xmlDB.Components.SingleOrDefault(c => c.IOId == io.Id);

                if (componentInDb == null)
                {
                    // Create single component

                    var component = new Component();
                    component.IOId       = io.Id;
                    component.Name       = io.Name;
                    component.Comment    = io.Comment;
                    component.Location   = io.Location;
                    component.Dependancy = Enums.Dependancy.Single;

                    component.MatchStatus = Enums.MatchStatus.No_Match;


                    xmlDB.Components.Add(component);
                }
                else
                {
                    //Update single component
                    componentInDb.Comment  = io.Comment;
                    componentInDb.Location = io.Location;
                    componentInDb.Name     = io.Name;
                }
            }
            else
            {
                // Update/Add parent component
                var componentInDb = xmlDB.Components.SingleOrDefault(c => c.Name == io.ParentName);

                if (componentInDb == null)
                {
                    // Create parent component

                    var component = new Component();
                    component.IOId       = io.Id;
                    component.Name       = io.Name;
                    component.Comment    = io.Comment;
                    component.Location   = io.Location;
                    component.Dependancy = Enums.Dependancy.Parent;

                    component.MatchStatus = Enums.MatchStatus.No_Match;


                    xmlDB.Components.Add(component);
                }
            }

            xmlDB.Save();

            //UPDATE/ADD IO
            var IOInDb = xmlDB.IOs.SingleOrDefault(c => c.Id == io.Id);

            if (IOInDb == null)
            {
                xmlDB.IOs.Add(io);
            }
            else
            {
                IOInDb.Name           = io.Name;
                IOInDb.Comment        = io.Comment;
                IOInDb.Location       = io.Location;
                IOInDb.IOAddress      = io.IOAddress;
                IOInDb.ComponentId    = io.ComponentId;
                IOInDb.ConnectionType = io.ConnectionType;
                IOInDb.MatchStatus    = io.MatchStatus;
            }
            xmlDB.Save();

            //UPDATE IDs
            var ioIDUpdate        = xmlDB.IOs.First(c => c.Name == io.Name);
            var ComponentIDUpdate = new Component();

            // Search for component when it is single/parent
            if (String.IsNullOrEmpty(io.ParentName))
            {
                ComponentIDUpdate = xmlDB.Components.First(c => c.Name == io.Name);
                //Update single components IO ID
                ComponentIDUpdate.IOId = ioIDUpdate.Id;
            }
            else
            {
                ComponentIDUpdate = xmlDB.Components.First(c => c.Name == io.ParentName);
                //Parent component has on IO ID because they are multiple
            }


            ioIDUpdate.ComponentId = ComponentIDUpdate.Id;


            xmlDB.Save();

            return(RedirectToAction("Index", "IOList"));
        }
Пример #29
0
        //==========================================================================================================
        //Match IO to Standard
        private void CreateComponents()
        {
            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            foreach (var io in xmlDB.IOs.ToList())
            {
                if (!String.IsNullOrWhiteSpace(io.ParentName))
                {
                    //IO is part of parent component
                    io.MatchStatus = Enums.MatchStatus.Check;

                    //If parent component doesnt exist create it
                    if (!xmlDB.Components.Any(c => c.Name == io.ParentName))
                    {
                        var component = new Component();
                        component.Name       = io.ParentName;
                        component.Comment    = io.Comment;
                        component.Location   = io.Location;
                        component.Dependancy = Enums.Dependancy.Parent;

                        component.MatchStatus = Enums.MatchStatus.No_Match;

                        if (io.IOAddress.Type == Enums.IOType.IO)
                        {
                            component.ConnectionType = Enums.ConnectionType.DIO;
                        }
                        else
                        {
                            component.ConnectionType = Enums.ConnectionType.ETH;
                        }

                        xmlDB.Components.Add(component);
                    }
                    else
                    {
                        //if it exist check connection type - if any child IO is IP(ETH) type -> switch components connection type
                        if (io.IOAddress.Type == Enums.IOType.IP)
                        {
                            var componentDB = xmlDB.Components.Single(c => c.Name == io.ParentName);
                            componentDB.ConnectionType = Enums.ConnectionType.ETH;
                        }
                    }
                }
                else
                {
                    switch (io.ConnectionType)
                    {
                    case Enums.ConnectionType.AI:
                        // Create single component

                        var componentAI = new Component();
                        componentAI.IOId           = io.Id;
                        componentAI.Name           = io.Name;
                        componentAI.Comment        = io.Comment;
                        componentAI.Location       = io.Location;
                        componentAI.Dependancy     = Enums.Dependancy.Single;
                        componentAI.ConnectionType = io.ConnectionType;

                        //Matching
                        var possibleStdAIAOI = xmlDB.Standards.Where(c => c.ConnectionType == io.ConnectionType).ToList();

                        switch (possibleStdAIAOI.Count())
                        {
                        case (0):
                            componentAI.StandardId  = null;
                            componentAI.MatchStatus = Enums.MatchStatus.No_Match;
                            io.MatchStatus          = Enums.MatchStatus.No_Match;
                            break;

                        case (1):
                            componentAI.StandardId  = possibleStdAIAOI.ElementAt(0).Id;
                            componentAI.MatchStatus = Enums.MatchStatus.Match;
                            io.MatchStatus          = Enums.MatchStatus.Match;
                            break;

                        // Case more than one
                        default:
                            // take first one - standards should be sorted from most used to least used
                            componentAI.StandardId  = possibleStdAIAOI.ElementAt(0).Id;
                            componentAI.MatchStatus = Enums.MatchStatus.Check;
                            io.MatchStatus          = Enums.MatchStatus.Check;
                            break;
                        }

                        xmlDB.Components.Add(componentAI);

                        break;

                    //=====================================================================================
                    case Enums.ConnectionType.AO:
                        //Always part of parent component
                        throw new Exception("Analog output must be part of parent component");

                    //=====================================================================================
                    case Enums.ConnectionType.DI:
                        // Create single component

                        var componentDI = new Component();
                        componentDI.IOId           = io.Id;
                        componentDI.Name           = io.Name;
                        componentDI.Comment        = io.Comment;
                        componentDI.Location       = io.Location;
                        componentDI.Dependancy     = Enums.Dependancy.Single;
                        componentDI.ConnectionType = io.ConnectionType;

                        //Matching
                        var possibleStdDIAOI = xmlDB.Standards.Where(c => c.ConnectionType == io.ConnectionType).ToList();

                        switch (possibleStdDIAOI.Count())
                        {
                        case (0):
                            componentDI.StandardId  = null;
                            componentDI.MatchStatus = Enums.MatchStatus.No_Match;
                            io.MatchStatus          = Enums.MatchStatus.No_Match;
                            break;

                        case (1):
                            componentDI.StandardId  = possibleStdDIAOI.ElementAt(0).Id;
                            componentDI.MatchStatus = Enums.MatchStatus.Match;
                            io.MatchStatus          = Enums.MatchStatus.Match;
                            break;

                        // Case more than one
                        default:
                            // take first one - standards should be sorted from most used to least used
                            componentDI.StandardId  = possibleStdDIAOI.ElementAt(0).Id;
                            componentDI.MatchStatus = Enums.MatchStatus.Check;
                            io.MatchStatus          = Enums.MatchStatus.Check;
                            break;
                        }


                        xmlDB.Components.Add(componentDI);

                        break;

                    //=====================================================================================
                    case Enums.ConnectionType.DO:
                        // Create single component

                        var componentDO = new Component();
                        componentDO.IOId           = io.Id;
                        componentDO.Name           = io.Name;
                        componentDO.Comment        = io.Comment;
                        componentDO.Location       = io.Location;
                        componentDO.Dependancy     = Enums.Dependancy.Single;
                        componentDO.ConnectionType = io.ConnectionType;

                        //Matching
                        var possibleStdDOAOI = xmlDB.Standards.Where(c => c.ConnectionType == io.ConnectionType).ToList();

                        switch (possibleStdDOAOI.Count())
                        {
                        case (0):
                            componentDO.StandardId  = null;
                            componentDO.MatchStatus = Enums.MatchStatus.No_Match;
                            io.MatchStatus          = Enums.MatchStatus.No_Match;
                            break;

                        case (1):
                            componentDO.StandardId  = possibleStdDOAOI.ElementAt(0).Id;
                            componentDO.MatchStatus = Enums.MatchStatus.Match;
                            io.MatchStatus          = Enums.MatchStatus.Match;
                            break;

                        // Case more than one
                        default:
                            // take first one - standards should be sorted from most used to least used
                            componentDO.StandardId  = possibleStdDOAOI.ElementAt(0).Id;
                            componentDO.MatchStatus = Enums.MatchStatus.Check;
                            io.MatchStatus          = Enums.MatchStatus.Check;
                            break;
                        }


                        xmlDB.Components.Add(componentDO);

                        break;

                    //=====================================================================================
                    case Enums.ConnectionType.ETH:
                        //ETH and DIO reserved for parent components
                        throw new Exception("ETH and DIO connection types reserved for parent components");

                    case Enums.ConnectionType.DIO:
                        //ETH and DIO reserved for parent components
                        throw new Exception("ETH and DIO connection types reserved for parent components");

                    default:
                        throw new Exception("Matching: Unknown connection type");
                    }
                }
            }

            xmlDB.Save();

            //Fill up io.ComponentId
            foreach (var io in xmlDB.IOs)
            {
                var component = xmlDB.Components.FirstOrDefault(c => c.IOId == io.Id);
                //If component exist link with IO
                if (component != null)
                {
                    io.ComponentId = component.Id;
                }
                else
                {
                    //For child components
                    io.ComponentId = xmlDB.Components.First(c => c.Name == io.ParentName).Id;
                }
            }

            xmlDB.Save();
        }
Пример #30
0
        public ActionResult Index(HttpPostedFileBase file)
        {
            var userName = User.Identity.GetUserName();
            var xmlDB    = new XMLDatabase(userName, _context.Users.First(c => c.Name == userName).ActProject);

            var ioList = new List <IO>();

            // Verify that the user selected a file
            if (file != null && file.ContentLength > 0)
            {
                // extract only the filename
                //var fileName = Path.GetFileName(file.FileName);
                // store the file inside ~/App_Data/uploads folder
                //var path = Path.Combine(Server.MapPath("C:/Users/Ivan/Desktop/OP generator PLC koda/uploads"), fileName);
                //file.SaveAs(path);

                //Parsing file
                var ms = new MemoryStream();
                file.InputStream.CopyTo(ms);
                byte[] array        = ms.GetBuffer();
                string fileAsString = Encoding.UTF8.GetString(array);
                //Split by lines
                String[] CSVrows = fileAsString.Split(new[] { Environment.NewLine }, StringSplitOptions.None).Skip(1).ToArray();


                //Each line split by ';' -- parse values to database
                foreach (string str in CSVrows)
                {
                    if (str.Length < 10)
                    {
                        continue;
                    }

                    var IOmembers = str.Split(';');

                    var io = new IO();

                    io.Location = IOmembers[0];

                    switch (IOmembers[1])
                    {
                    case ("AI"):
                        io.ConnectionType = Enums.ConnectionType.AI;
                        break;

                    case ("AO"):
                        io.ConnectionType = Enums.ConnectionType.AO;
                        break;

                    case ("DI"):
                        io.ConnectionType = Enums.ConnectionType.DI;
                        break;

                    case ("DO"):
                        io.ConnectionType = Enums.ConnectionType.DO;
                        break;

                    case ("DIO"):
                        io.ConnectionType = Enums.ConnectionType.DIO;
                        break;

                    default:
                        throw new Exception("Error reading PLC IO type");
                    }

                    io.IOAddress  = new IOAddress(IOmembers[2]);
                    io.Name       = IOmembers[3];
                    io.ParentName = IOmembers[4];
                    io.Comment    = IOmembers[5];


                    //Add io
                    ioList.Add(io);
                }
            }

            foreach (var io in ioList)
            {
                xmlDB.IOs.Add(io);
            }

            xmlDB.Save();
            //Add locations to database
            DetectLocations();
            CreateComponents();



            return(RedirectToAction("Index"));
        }