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