Hardware/LPC/LMSensors.cs
author moel.mich
Tue, 24 Aug 2010 19:06:49 +0000
changeset 173 fb96c0ca3c2d
parent 166 fa9dfbfc4145
child 182 4801e9eaf979
permissions -rwxr-xr-x
Added another registry path for getting the Heatmaster serial port.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2009-2010
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System;
    39 using System.Collections.Generic;
    40 using System.Globalization;
    41 using System.IO;
    42 
    43 namespace OpenHardwareMonitor.Hardware.LPC {
    44 
    45   internal class LMSensors {
    46 
    47     private List<LMChip> lmChips = new List<LMChip>();
    48 
    49     public LMSensors() {
    50       string[] devicePaths = Directory.GetDirectories("/sys/class/hwmon/");
    51       foreach (string path in devicePaths) {
    52         string name = null;
    53         try {
    54           using (StreamReader reader = new StreamReader(path + "/device/name")) 
    55             name = reader.ReadLine();
    56         } catch (IOException) { }
    57         switch (name) {
    58           case "f71858fg":
    59             lmChips.Add(new LMChip(Chip.F71858, path + "/device")); break;
    60           case "f71862fg":
    61             lmChips.Add(new LMChip(Chip.F71862, path + "/device")); break;
    62           case "f71882fg":
    63             lmChips.Add(new LMChip(Chip.F71882, path + "/device")); break;
    64           case "f71889fg":
    65             lmChips.Add(new LMChip(Chip.F71889F, path + "/device")); break;
    66 
    67           case "it8712":
    68             lmChips.Add(new LMChip(Chip.IT8712F, path + "/device")); break;
    69           case "it8716":
    70             lmChips.Add(new LMChip(Chip.IT8716F, path + "/device")); break;
    71           case "it8718":
    72             lmChips.Add(new LMChip(Chip.IT8718F, path + "/device")); break;
    73           case "it8720":
    74             lmChips.Add(new LMChip(Chip.IT8720F, path + "/device")); break;
    75 
    76           case "w83627ehf":
    77             lmChips.Add(new LMChip(Chip.W83627EHF, path + "/device")); break;
    78           case "w83627dhg":
    79             lmChips.Add(new LMChip(Chip.W83627DHG, path + "/device")); break;
    80           case "w83667hg":
    81             lmChips.Add(new LMChip(Chip.W83667HG, path + "/device")); break;
    82           case "w83627hf":
    83             lmChips.Add(new LMChip(Chip.W83627HF, path + "/device")); break;
    84           case "w83627thf":
    85             lmChips.Add(new LMChip(Chip.W83627THF, path + "/device")); break;
    86           case "w83687thf":
    87             lmChips.Add(new LMChip(Chip.W83687THF, path + "/device")); break;
    88         }
    89       }
    90     }
    91 
    92     public void Close() {
    93       foreach (LMChip lmChip in lmChips)
    94         lmChip.Close();
    95     }
    96 
    97     public ISuperIO[] SuperIO {
    98       get {
    99         return lmChips.ToArray();
   100       }
   101     }
   102 
   103     private class LMChip : ISuperIO {
   104 
   105       private string path;
   106       private Chip chip;
   107 
   108       private float?[] voltages;
   109       private float?[] temperatures;
   110       private float?[] fans;
   111 
   112       private StreamReader[] voltageReaders;
   113       private StreamReader[] temperatureReaders;
   114       private StreamReader[] fanReaders;
   115 
   116       public Chip Chip { get { return chip; } }
   117       public float?[] Voltages { get { return voltages; } }
   118       public float?[] Temperatures { get { return temperatures; } }
   119       public float?[] Fans { get { return fans; } }
   120 
   121 
   122       public LMChip(Chip chip, string path) {
   123         this.path = path;
   124         this.chip = chip;
   125 
   126         string[] voltagePaths = Directory.GetFiles(path, "in*_input");
   127         this.voltages = new float?[voltagePaths.Length];
   128         this.voltageReaders = new StreamReader[voltagePaths.Length];
   129         for (int i = 0; i < voltagePaths.Length; i++)
   130           voltageReaders[i] = new StreamReader(voltagePaths[i]);
   131 
   132         string[] temperaturePaths = Directory.GetFiles(path, "temp*_input");
   133         this.temperatures = new float?[temperaturePaths.Length];
   134         this.temperatureReaders = new StreamReader[temperaturePaths.Length];
   135         for (int i = 0; i < temperaturePaths.Length; i++)
   136           temperatureReaders[i] = new StreamReader(temperaturePaths[i]);
   137 
   138         string[] fanPaths = Directory.GetFiles(path, "fan*_input");
   139         this.fans = new float?[fanPaths.Length];
   140         this.fanReaders = new StreamReader[fanPaths.Length];
   141         for (int i = 0; i < fanPaths.Length; i++)
   142           fanReaders[i] = new StreamReader(fanPaths[i]);
   143       }
   144 
   145       public string GetReport() {
   146         return null;
   147       }
   148 
   149       public void Update() {
   150         for (int i = 0; i < voltages.Length; i++) {
   151           voltageReaders[i].BaseStream.Seek(0, SeekOrigin.Begin);
   152           string s = voltageReaders[i].ReadLine();
   153           try {
   154             voltages[i] = 0.001f * 
   155               long.Parse(s, CultureInfo.InvariantCulture);
   156           } catch {
   157             voltages[i] = null;
   158           }
   159         }
   160 
   161         for (int i = 0; i < temperatures.Length; i++) {
   162           temperatureReaders[i].BaseStream.Seek(0, SeekOrigin.Begin);
   163           string s = temperatureReaders[i].ReadLine();
   164           try {
   165             temperatures[i] = 0.001f * 
   166               long.Parse(s, CultureInfo.InvariantCulture);
   167           } catch {
   168             temperatures[i] = null;
   169           }
   170         }
   171 
   172         for (int i = 0; i < fans.Length; i++) {
   173           fanReaders[i].BaseStream.Seek(0, SeekOrigin.Begin);
   174           string s = fanReaders[i].ReadLine();
   175           try {
   176             fans[i] = long.Parse(s, CultureInfo.InvariantCulture);
   177           } catch {
   178             fans[i] = null;
   179           }
   180         }
   181       }
   182 
   183       public void Close() {
   184         foreach (StreamReader reader in voltageReaders)
   185           reader.Close();
   186         foreach (StreamReader reader in temperatureReaders)
   187           reader.Close();
   188         foreach (StreamReader reader in fanReaders)
   189           reader.Close();
   190       }
   191     }
   192   }
   193 }