Exemplo n.º 1
0
 /// <summary>
 /// Clears the configurations.
 /// </summary>
 private void ClearConfig()
 {
     Positions.Clear();
     Relays.Clear();
     Sections.Clear();
     Sensors.Clear();
     Switchs.Clear();
 }
Exemplo n.º 2
0
        /// <summary>
        /// Reads the configuration file.
        /// </summary>
        /// <param name="path">File name and path.</param>
        /// <returns>True on success, false otherwise.</returns>
        public virtual bool ReadConf(string path)
        {
            bool bReturn = true;
            //string myLine, myMotionLine;
            int iLine = 0;
            var nfi   = new System.Globalization.NumberFormatInfo();

            ScanBus();
            ClearConfig();

            var fi = new FileInfo(ICCConstants.SERVER_CONF_PATH);

            if (fi.Exists)
            {
                using (var sr = new StreamReader(ICCConstants.SERVER_CONF_PATH))
                {
                    bool bHeaderFound = false;
                    while (!sr.EndOfStream && !bHeaderFound)
                    {
                        var line = sr.ReadLine().Trim();
                        iLine++;
                        if (line.Length > 0 && line[0] != '#')
                        {
                            if (line.Substring(0, 17) == "ICHOOCHOO_CONF V1")
                            {
                                bHeaderFound = true;
                            }
                        }
                    }
                    if (bHeaderFound)
                    {
                        while (!sr.EndOfStream && bReturn)
                        {
                            var line = sr.ReadLine().Trim();
                            iLine++;
                            if (line.Length > 0 && line[0] != '#')
                            {
                                var terms = line.Split(' ');
                                if (terms.Length > 0)
                                {
                                    if (terms[0] == "POSITION")
                                    {
                                        if (terms.Length == 3)
                                        {
                                            byte bID = 0xFF;
                                            if (byte.TryParse(terms[1], NumberStyles.HexNumber, nfi, out bID))
                                            {
                                                if (!Positions.ContainsKey(bID))
                                                {
                                                    Positions.Add(bID, new ConfPosition()
                                                    {
                                                        ID = bID, Description = terms[2]
                                                    });
                                                }
                                                else
                                                {
                                                    bReturn = false;
                                                }
                                            }
                                            else
                                            {
                                                bReturn = false;
                                            }
                                        }
                                        else
                                        {
                                            bReturn = false;
                                        }
                                    }
                                    else if (terms[0] == "SECTION")
                                    {
                                        if (terms.Length == 5)
                                        {
                                            int iID         = -1;
                                            int iModuleAddr = -1;
                                            int iOutput     = -1;
                                            if (int.TryParse(terms[1], NumberStyles.HexNumber, nfi, out iID))
                                            {
                                                if (int.TryParse(terms[2], NumberStyles.HexNumber, nfi, out iModuleAddr))
                                                {
                                                    if (int.TryParse(terms[3], NumberStyles.HexNumber, nfi, out iOutput))
                                                    {
                                                        if (!Sections.ContainsKey(iID))
                                                        {
                                                            Sections.Add(iID, new ConfSection()
                                                            {
                                                                ID = iID, ModuleID = iModuleAddr, IOPort = iOutput, Description = terms[4]
                                                            });
                                                        }
                                                        else
                                                        {
                                                            bReturn = false;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        bReturn = false;
                                                    }
                                                }
                                                else
                                                {
                                                    bReturn = false;
                                                }
                                            }
                                            else
                                            {
                                                bReturn = false;
                                            }
                                        }
                                        else
                                        {
                                            bReturn = false;
                                        }
                                    }
                                    else if (terms[0] == "SWITCH")
                                    {
                                        if (terms.Length == 6)
                                        {
                                            int iID            = -1;
                                            int iModuleAddr    = -1;
                                            int iOutput        = -1;
                                            int iStraightValue = -1;
                                            if (int.TryParse(terms[1], NumberStyles.HexNumber, nfi, out iID))
                                            {
                                                if (int.TryParse(terms[2], NumberStyles.HexNumber, nfi, out iModuleAddr))
                                                {
                                                    if (int.TryParse(terms[3], NumberStyles.HexNumber, nfi, out iOutput))
                                                    {
                                                        if (int.TryParse(terms[4], NumberStyles.Integer, nfi, out iStraightValue))
                                                        {
                                                            if (!Switchs.ContainsKey(iID))
                                                            {
                                                                Switchs.Add(iID, new ConfSwitch()
                                                                {
                                                                    ID = iID, ModuleID = iModuleAddr, IOPort = iOutput, StraightValue = iStraightValue, Description = terms[5]
                                                                });
                                                            }
                                                            else
                                                            {
                                                                bReturn = false;
                                                            }
                                                        }
                                                        else
                                                        {
                                                            bReturn = false;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        bReturn = false;
                                                    }
                                                }
                                                else
                                                {
                                                    bReturn = false;
                                                }
                                            }
                                            else
                                            {
                                                bReturn = false;
                                            }
                                        }
                                        else
                                        {
                                            bReturn = false;
                                        }
                                    }
                                    else if (terms[0] == "RELAY")
                                    {
                                        if (terms.Length == 5)
                                        {
                                            int iID         = -1;
                                            int iModuleAddr = -1;
                                            int iOutput     = -1;
                                            if (int.TryParse(terms[1], NumberStyles.HexNumber, nfi, out iID))
                                            {
                                                if (int.TryParse(terms[2], NumberStyles.HexNumber, nfi, out iModuleAddr))
                                                {
                                                    if (int.TryParse(terms[3], NumberStyles.HexNumber, nfi, out iOutput))
                                                    {
                                                        if (!Relays.ContainsKey(iID))
                                                        {
                                                            Relays.Add(iID, new ConfRelay()
                                                            {
                                                                ID = iID, ModuleID = iModuleAddr, IOPort = iOutput, Description = terms[4]
                                                            });
                                                        }
                                                        else
                                                        {
                                                            bReturn = false;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        bReturn = false;
                                                    }
                                                }
                                                else
                                                {
                                                    bReturn = false;
                                                }
                                            }
                                            else
                                            {
                                                bReturn = false;
                                            }
                                        }
                                        else
                                        {
                                            bReturn = false;
                                        }
                                    }
                                    else if (terms[0] == "SENSOR")
                                    {
                                        if (terms.Length == 6)
                                        {
                                            int iID         = -1;
                                            int iModuleAddr = -1;
                                            int iOutput     = -1;
                                            int iType       = -1;
                                            if (int.TryParse(terms[1], NumberStyles.HexNumber, nfi, out iID))
                                            {
                                                if (int.TryParse(terms[2], NumberStyles.HexNumber, nfi, out iModuleAddr))
                                                {
                                                    if (int.TryParse(terms[3], NumberStyles.HexNumber, nfi, out iOutput))
                                                    {
                                                        if (int.TryParse(terms[4], NumberStyles.HexNumber, nfi, out iType))
                                                        {
                                                            if (!Sensors.ContainsKey(iID))
                                                            {
                                                                Sensors.Add(iID, new ConfSensor()
                                                                {
                                                                    ID = iID, ModuleID = iModuleAddr, IOPort = iOutput, Type = iType, Description = terms[5]
                                                                });
                                                            }
                                                            else
                                                            {
                                                                bReturn = false;
                                                            }
                                                        }
                                                        else
                                                        {
                                                            bReturn = false;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        bReturn = false;
                                                    }
                                                }
                                                else
                                                {
                                                    bReturn = false;
                                                }
                                            }
                                            else
                                            {
                                                bReturn = false;
                                            }
                                        }
                                        else
                                        {
                                            bReturn = false;
                                        }
                                    }
                                    else if (terms[0] == "MOTION")
                                    {
                                        bool bInMotion = true;
                                        while (!sr.EndOfStream && bInMotion && bReturn)
                                        {
                                            var motionLine = sr.ReadLine().Trim();
                                            iLine++;
                                            if (line.Length > 0 && line[0] != '#')
                                            {
                                                var motionTerms = line.Split(' ');
                                                if (motionTerms.Length > 0)
                                                {
                                                    if (motionTerms[0] == "ENDMOTION")
                                                    {
                                                        bInMotion = false;
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    if (!bReturn)
                                    {
                                        Log.LogText(string.Format("Configuration error: syntax error at line {0} '{1}'", iLine, line));
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        bReturn = false;
                    }

                    sr.Close();
                }
            }
            else
            {
                Log.LogText("No configuration file found.", LogLevel.Information);
            }

            return(bReturn);
        }