Skip to content

Commit

Permalink
Merge pull request #1 from Rem0o/feature/refactor_and_optimize
Browse files Browse the repository at this point in the history
Refactor + cleanup
  • Loading branch information
SasaKaranovic authored Jan 30, 2024
2 parents e0ad133 + ebf671d commit c988b61
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 122 deletions.
32 changes: 14 additions & 18 deletions OpenFanManagementControlSensor.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,44 @@
using FanControl.OpenFanPlugin;
using FanControl.Plugins;
using System;
using System.Net;
using FanControl.Plugins;

namespace FanControl.OpenFanPlugin
{
public class OpenFanManagementControlSensor: IPluginControlSensor
{
private readonly int _fanIndex;
private float? _val;
private float? _lastSetValue;


public OpenFanManagementControlSensor(int fanIndex) => _fanIndex = fanIndex;

public float? Value { get; private set; }

public string Name => $"OpenFAN Fan #{(int)_fanIndex + 1}";
public string Name => $"OpenFAN Fan #{_fanIndex + 1}";

public string Origin => $"OpenFAN";

public string Id => "Control_" + _fanIndex.ToString();
public string Id => "OpenFan/Control/" + _fanIndex.ToString();

public void Reset()
{
//DellSmbiosBzh.EnableAutomaticFanControl(_fanIndex == BzhFanIndex.Fan1 ? false : true);
// set back the original control value, is there a command to get the current value we could use
// to get the original value at the start?
}

public void Set(float val)
{
if (val != _val)
{
SetFanSpeed(val);
_val = val;
}
_lastSetValue = val;
}

public void Update() => Value = _val;
public void Update(){ }


private void SetFanSpeed(float speed)
public void SetFanSpeed(OpenFan_Serial serial)
{
OpenFan_Serial OpenFan = new OpenFan_Serial();
OpenFan.SetPercent(_fanIndex, (int)speed);
OpenFan.Close();
if (Value != _lastSetValue)
{
serial.SetPercent(_fanIndex, (int)_lastSetValue);
Value = _lastSetValue;
}
}
}
}
25 changes: 11 additions & 14 deletions OpenFanManagementFanSensor.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
using FanControl.Plugins;
using System;
using System.Net;
using FanControl.OpenFanPlugin;
using System.Collections.Generic;

namespace FanControl.OpenFanPlugin
{
public class OpenFanManagementFanSensor : IPluginSensor
{
private readonly int _fanIndex;

public OpenFanManagementFanSensor(int fanIndex) => _fanIndex = fanIndex;
public OpenFanManagementFanSensor(int fanIndex)
{
_fanIndex = fanIndex;
}

public string Identifier => $"OpenFan/Fan{(int)_fanIndex}";
public string Identifier => $"OpenFan/Fan/{_fanIndex}";

public float? Value { get; private set; }

public string Name => $"OpenFan FAN #{(int)_fanIndex + 1}";
public string Name => $"OpenFan FAN #{_fanIndex + 1}";

public string Origin => $"OpenFan";

public string Id => "Fan_" + _fanIndex.ToString();

public void Update() => Value = GetFanRPM();
public void Update() { }

private int GetFanRPM()
public void UpdateFanRPM(SerialResponse response)
{
int rpm = 0;
OpenFan_Serial OpenFan = new OpenFan_Serial();
rpm = OpenFan.ReadRPM(_fanIndex);
OpenFan.Close();

return rpm;
int rpm = response.Data[_fanIndex];
Value = rpm;
}
}
}
86 changes: 47 additions & 39 deletions OpenFanPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
using FanControl.Plugins;
using System;
using System.Collections.Generic;
using System.Linq;

namespace FanControl.OpenFanPlugin
{
public class OpenFanPlugin : IPlugin2, IDisposable
public class OpenFanPlugin : IPlugin2
{
private bool _OpenFanInitialized;
private Boolean m_DisposedValue;
private object _serialLock = new object();
private OpenFan_Serial _serial;
private OpenFanManagementControlSensor[] _fanControls;
private OpenFanManagementFanSensor[] _fanSensors;

private static readonly int[] _fanIndexes = Enumerable.Range(0, 10).ToArray();

public string Name => "OpenFAN";
private readonly IPluginLogger _logger;
Expand All @@ -25,65 +29,69 @@ public void Close()
if (_OpenFanInitialized)
{
_OpenFanInitialized = false;
lock (_serialLock)
{
_serial.Dispose();
}

_fanControls = null;
_fanSensors = null;
}
}

public void Initialize()
{
_OpenFanInitialized = true;
_logger.Log("OpenFAN plugin loaded.");

lock(_serialLock)
{
_serial = new OpenFan_Serial();
}
}

public void Load(IPluginSensorsContainer _container)
public void Load(IPluginSensorsContainer container)
{
if (_OpenFanInitialized)
{
IEnumerable<OpenFanManagementControlSensor> fanControls = new[] {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
}.Select(i => new OpenFanManagementControlSensor(i)).ToArray();
_fanControls = _fanIndexes
.Select(i => new OpenFanManagementControlSensor(i)).ToArray();

IEnumerable<OpenFanManagementFanSensor> fanSensors = new[] {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
}.Select(i => new OpenFanManagementFanSensor(i)).ToArray();
_fanSensors = _fanIndexes
.Select(i => new OpenFanManagementFanSensor(i)).ToArray();

_container.ControlSensors.AddRange(fanControls);
_container.FanSensors.AddRange(fanSensors);
container.ControlSensors.AddRange(_fanControls);
container.FanSensors.AddRange(_fanSensors);
}
}

public void Update()
{

}

protected virtual void Dispose(Boolean disposing)
{
if (!m_DisposedValue)
lock (_serialLock)
{
if (disposing)
try
{
// TODO: dispose managed state (managed objects)
}
_serial.Open();

// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
Close();
m_DisposedValue = true;
var rpms = _serial.ReadRPM();
foreach(OpenFanManagementFanSensor fan in _fanSensors)
{
fan.UpdateFanRPM(rpms);
}
foreach(OpenFanManagementControlSensor control in _fanControls)
{
control.SetFanSpeed(_serial);
}
}
catch (Exception exception)
{
// do something
}
finally
{
_serial.Close();
}
}
}

// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
~OpenFanPlugin()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: false);
}

public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
Loading

0 comments on commit c988b61

Please # to comment.