コード例 #1
0
ファイル: SystemInfo.cs プロジェクト: weedkiller/raspberryio
        private void ExtractOS()
        {
            try
            {
                Standard.Uname(out var unameInfo);

                OperatingSystem = new OsInfo
                {
                    DomainName = unameInfo.DomainName,
                    Machine    = unameInfo.Machine,
                    NodeName   = unameInfo.NodeName,
                    Release    = unameInfo.Release,
                    SysName    = unameInfo.SysName,
                    Version    = unameInfo.Version,
                };
            }
            catch
            {
                OperatingSystem = new OsInfo();
            }
        }
コード例 #2
0
ファイル: SystemInfo.cs プロジェクト: Gravicode/BMC.Aquascape
        /// <summary>
        /// Prevents a default instance of the <see cref="SystemInfo"/> class from being created.
        /// </summary>
        /// <exception cref="System.NotSupportedException">Could not initialize the GPIO controller</exception>
        private SystemInfo()
        {
            #region Obtain and format a property dictionary

            var properties =
                typeof(SystemInfo).GetTypeInfo()
                .GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                .Where(
                    p =>
                    p.CanWrite && p.CanRead &&
                    (p.PropertyType == typeof(string) || p.PropertyType == typeof(string[])))
                .ToArray();
            var propDictionary = new Dictionary <string, PropertyInfo>(stringComparer);

            foreach (var prop in properties)
            {
                propDictionary[prop.Name.Replace(" ", string.Empty).ToLowerInvariant().Trim()] = prop;
            }

            #endregion

            #region Extract CPU information

            if (File.Exists(CpuInfoFilePath))
            {
                var cpuInfoLines = File.ReadAllLines(CpuInfoFilePath);

                foreach (var line in cpuInfoLines)
                {
                    var lineParts = line.Split(new[] { ':' }, 2);
                    if (lineParts.Length != 2)
                    {
                        continue;
                    }

                    var propertyKey         = lineParts[0].Trim().Replace(" ", string.Empty);
                    var propertyStringValue = lineParts[1].Trim();

                    if (!propDictionary.ContainsKey(propertyKey))
                    {
                        continue;
                    }

                    var property = propDictionary[propertyKey];
                    if (property.PropertyType == typeof(string))
                    {
                        property.SetValue(this, propertyStringValue);
                    }
                    else if (property.PropertyType == typeof(string[]))
                    {
                        var propertyArrayAvalue = propertyStringValue.Split(' ');
                        property.SetValue(this, propertyArrayAvalue);
                    }
                }
            }

            #endregion

            #region Extract Memory Information

            if (File.Exists(MemInfoFilePath))
            {
                var memInfoLines = File.ReadAllLines(MemInfoFilePath);
                foreach (var line in memInfoLines)
                {
                    var lineParts = line.Split(new[] { ':' }, 2);
                    if (lineParts.Length != 2)
                    {
                        continue;
                    }

                    if (lineParts[0].ToLowerInvariant().Trim().Equals("memtotal") == false)
                    {
                        continue;
                    }

                    var memKb = lineParts[1].ToLowerInvariant().Trim().Replace("kb", string.Empty).Trim();

                    if (int.TryParse(memKb, out var parsedMem))
                    {
                        InstalledRam = parsedMem * 1024;
                        break;
                    }
                }
            }

            #endregion

            #region Board Version and Form Factor

            try
            {
                int boardVersion;
                if (string.IsNullOrWhiteSpace(Revision) == false &&
                    int.TryParse(
                        Revision.ToUpperInvariant(),
                        NumberStyles.HexNumber,
                        CultureInfo.InvariantCulture,
                        out boardVersion))
                {
                    RaspberryPiVersion = PiVersion.Unknown;
                    if (Enum.GetValues(typeof(PiVersion)).Cast <int>().Contains(boardVersion))
                    {
                        RaspberryPiVersion = (PiVersion)boardVersion;
                    }
                }

                WiringPiBoardRevision = WiringPi.piBoardRev();
            }
            catch
            {
                /* Ignore */
            }

            #endregion

            #region Version Information

            {
                var libParts = WiringPi.WiringPiLibrary.Split('.');
                var major    = int.Parse(libParts[libParts.Length - 2]);
                var minor    = int.Parse(libParts[libParts.Length - 1]);
                var version  = new Version(major, minor);
                WiringPiVersion = version;
            }

            #endregion

            #region Extract OS Info

            try
            {
                Standard.uname(out var unameInfo);
                OperatingSystem = new OsInfo
                {
                    DomainName = unameInfo.domainname,
                    Machine    = unameInfo.machine,
                    NodeName   = unameInfo.nodename,
                    Release    = unameInfo.release,
                    SysName    = unameInfo.sysname,
                    Version    = unameInfo.version
                };
            }
            catch
            {
                OperatingSystem = new OsInfo();
            }

            #endregion
        }