Skip to content

Commit

Permalink
More code reformat
Browse files Browse the repository at this point in the history
Remove unused test class
  • Loading branch information
epinter committed Mar 29, 2024
1 parent d6d0fc3 commit cc6dc42
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 58 deletions.
12 changes: 6 additions & 6 deletions service/src/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ Task IHostedLifecycleService.StoppingAsync(CancellationToken cancellationToken)
return Task.CompletedTask;
}
private void onStopped() {
Log.Info(hostEnvironment.ApplicationName + " stopped");
Log.info(hostEnvironment.ApplicationName + " stopped");
if (isDebug) {
Debug.WriteLine("{0}: Stopping LibreHardwareService", hostEnvironment.ApplicationName);
}

sensorsManager.Close();
sensorsManager.close();
timer.Stop();
timer.Dispose();
worker?.CancelAsync();
Expand All @@ -73,7 +73,7 @@ private void onStopped() {

private void onStarted() {
startService();
Log.Info(hostEnvironment.ApplicationName + " started");
Log.info(hostEnvironment.ApplicationName + " started");
}

public void onTimeInterval(object? sender, ElapsedEventArgs args) {
Expand All @@ -87,7 +87,7 @@ public void onTimeInterval(object? sender, ElapsedEventArgs args) {
private void updateSensors(object? sender, DoWorkEventArgs e) {
Debug.WriteLine("{0}: UpdateSensors", hostEnvironment.ApplicationName);

sensorsManager.UpdateHardwareSensors();
sensorsManager.updateHardwareSensors();
}

public void startService() {
Expand All @@ -103,8 +103,8 @@ public void startService() {
timer.Interval = interval;
timer.Elapsed += new ElapsedEventHandler(onTimeInterval);
timer.Enabled = true;
Log.Info(String.Format("Starting '{0}' with interval set to {1}ms, sensors time-window to {2} minutes",
hostEnvironment.ApplicationName, interval, sensorsManager.GetSensorsTimeWindow()));
Log.info(String.Format("Starting '{0}' with interval set to {1}ms, sensors time-window to {2} minutes",
hostEnvironment.ApplicationName, interval, sensorsManager.getSensorsTimeWindow()));
}

private int readUpdateIntervalSetting() {
Expand Down
18 changes: 0 additions & 18 deletions service/src/Worker.cs

This file was deleted.

14 changes: 7 additions & 7 deletions service/src/core/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@

namespace LibreHardwareService {
internal class Log {
internal static ILoggerFactory LoggerFactory = new LoggerFactory();
internal static ILogger CreateLogger<T>() => LoggerFactory.CreateLogger<T>();
internal static ILogger CreateLogger(string categoryName) => LoggerFactory.CreateLogger(categoryName);
private static ILogger logger = CreateLogger("LibreHardwareService");
internal static ILoggerFactory loggerFactory = new LoggerFactory();
internal static ILogger createLogger<T>() => loggerFactory.CreateLogger<T>();
internal static ILogger createLogger(string categoryName) => loggerFactory.CreateLogger(categoryName);
private static ILogger logger = createLogger("LibreHardwareService");

public static string eventLogSource = "LibreHardwareService";

#pragma warning disable CA1416 // Validate platform compatibility
public static void Info(string message, params object[] args) {
public static void info(string message, params object[] args) {
logger.LogInformation(String.Format(message, args));
EventLog.WriteEntry(eventLogSource, string.Format(message, args), EventLogEntryType.Information);
}

public static void Error(string message, params object[] args) {
public static void error(string message, params object[] args) {
logger.LogError(String.Format(message, args));
EventLog.WriteEntry(eventLogSource, string.Format(message, args), EventLogEntryType.Error);
}

public static void Warning(string message, params object[] args) {
public static void warning(string message, params object[] args) {
logger.LogWarning(String.Format(message, args));
EventLog.WriteEntry(eventLogSource, string.Format(message, args), EventLogEntryType.Warning);
}
Expand Down
32 changes: 16 additions & 16 deletions service/src/core/MemoryMappedSensors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ private MemoryMappedSensors()
new MutexAccessRule(everyone, MutexRights.Synchronize | MutexRights.Modify, AccessControlType.Allow));
mutexAllHardware = new Mutex(false, Constants.MUTEXNAME_ALLHARDWARE);
mutexAllHardware.SetAccessControl(mtxSecAllHardware);
Log.Info("Memory Mapped Files (non-persisted) created:\n{0}\n{1}\n{2}\nMutex:\n{3}\n{4}\n{5}\n",
Log.info("Memory Mapped Files (non-persisted) created:\n{0}\n{1}\n{2}\nMutex:\n{3}\n{4}\n{5}\n",
Constants.FILENAME_SENSORS, Constants.FILENAME_ALLHARDWARE, Constants.FILENAME_STATUS,
Constants.MUTEXNAME_SENSORS, Constants.MUTEXNAME_ALLHARDWARE, Constants.MUTEXNAME_STATUS);
} else {
Log.Info("Memory Mapped Files (non-persisted) created:\n{0}\n{1}\nMutex:\n{2}\n{3}\n",
Log.info("Memory Mapped Files (non-persisted) created:\n{0}\n{1}\nMutex:\n{2}\n{3}\n",
Constants.FILENAME_SENSORS, Constants.FILENAME_STATUS, Constants.MUTEXNAME_SENSORS,
Constants.MUTEXNAME_STATUS);
}
Expand All @@ -107,15 +107,15 @@ private MemoryMappedSensors()
} catch (Exception ex) {
string errorMessage =
"Error creating memory mapped files, please check if the service is running with administrative privileges (LocalSystem), or if the service is already running.";
Log.Error(errorMessage);
Log.error(errorMessage);
Debug.WriteLine(errorMessage);
Log.Error(ex.Message);
Log.error(ex.Message);
System.Environment.Exit(2);
throw;
}
}

public static MemoryMappedSensors Instance {
public static MemoryMappedSensors instance {
get { return Nested.instance; }
}

Expand All @@ -133,7 +133,7 @@ public static MemoryMappedSensors Instance {
mmfAllHardware?.Dispose();
}

public void WriteSensors(byte[] index, byte[] data, Metadata metadata) {
public void writeSensors(byte[] index, byte[] data, Metadata metadata) {
try {
// Acquire mutex ownership with timeout, the client should open the mutex before read the content and release it
// afterwards.
Expand All @@ -146,7 +146,7 @@ public void WriteSensors(byte[] index, byte[] data, Metadata metadata) {
int length = index.Length + data.Length;
if ((length / 1024) > MMAP_SIZE &&
lastLogLimit < DateTime.Now.AddMinutes(-1 * Config.MemoryMapLimitLogIntervalMinutes)) {
Log.Error("Data being written to memory map is {0} bytes, larger than the limit {1} kb", (length / 1024),
Log.error("Data being written to memory map is {0} bytes, larger than the limit {1} kb", (length / 1024),
MMAP_SIZE);
lastLogLimit = DateTime.Now;
}
Expand Down Expand Up @@ -185,7 +185,7 @@ public void WriteSensors(byte[] index, byte[] data, Metadata metadata) {
acessorSensors.Write(indexOffset + index.Length, (int)0); // 4-bytes padding
acessorSensors.WriteArray(dataOffset, data, 0, data.Length);
} catch (Exception ex) {
Log.Error("Error writing to memory mapped file, service stopping: " + ex.Message);
Log.error("Error writing to memory mapped file, service stopping: " + ex.Message);
System.Environment.Exit(1);
}
try {
Expand All @@ -196,7 +196,7 @@ public void WriteSensors(byte[] index, byte[] data, Metadata metadata) {
}
}

public void WriteHardware(byte[] data, Metadata metadata) {
public void writeHardware(byte[] data, Metadata metadata) {
if (Config.FeatureEnableMemoryMapAllHardwareData) {
try {
// Acquire mutex ownership with timeout, the client should open the mutex before read the content and release
Expand All @@ -208,9 +208,9 @@ public void WriteHardware(byte[] data, Metadata metadata) {
}

try {
Write(data, acessorAllHardware, metadata);
write(data, acessorAllHardware, metadata);
} catch (Exception ex) {
Log.Error("Error writing to memory mapped file, service stopping: " + ex.Message);
Log.error("Error writing to memory mapped file, service stopping: " + ex.Message);
System.Environment.Exit(1);
}

Expand All @@ -223,7 +223,7 @@ public void WriteHardware(byte[] data, Metadata metadata) {
}
}

public void WriteStatus(byte[] data, Metadata metadata) {
public void writeStatus(byte[] data, Metadata metadata) {
try {
// Acquire mutex ownership with timeout, the client should open the mutex before read the content and release it
// afterwards.
Expand All @@ -234,9 +234,9 @@ public void WriteStatus(byte[] data, Metadata metadata) {
}

try {
Write(data, acessorStatus, metadata);
write(data, acessorStatus, metadata);
} catch (Exception ex) {
Log.Error("Error writing to memory mapped file, service stopping: " + ex.Message);
Log.error("Error writing to memory mapped file, service stopping: " + ex.Message);
System.Environment.Exit(1);
}

Expand All @@ -248,10 +248,10 @@ public void WriteStatus(byte[] data, Metadata metadata) {
}
}

private void Write(byte[] data, MemoryMappedViewAccessor acessor, Metadata metadata) {
private void write(byte[] data, MemoryMappedViewAccessor acessor, Metadata metadata) {
if ((data.Length / 1024) > MMAP_SIZE &&
lastLogLimit < DateTime.Now.AddMinutes(-1 * Config.MemoryMapLimitLogIntervalMinutes)) {
Log.Error("Data being written to memory map is {0} bytes, larger than the limit {1} kb", (data.Length / 1024),
Log.error("Data being written to memory map is {0} bytes, larger than the limit {1} kb", (data.Length / 1024),
MMAP_SIZE);
lastLogLimit = DateTime.Now;
}
Expand Down
22 changes: 11 additions & 11 deletions service/src/core/SensorsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ public SensorsManager() {
recyclableMemoryStreamManager = new RecyclableMemoryStreamManager();
}

public int GetSensorsTimeWindow() {
public int getSensorsTimeWindow() {
return sensorsTimeWindow.Minutes;
}

public void UpdateHardwareSensors() {
public void updateHardwareSensors() {
computer.Accept(updateVisitor);

computer.Accept(new SensorVisitor(delegate(ISensor sensor) { sensor.ValuesTimeWindow = sensorsTimeWindow; }));

if (lastHwStatusUpdate < DateTime.Now.AddMinutes(-1 * Config.HwStatusUpdateIntervalMinutes)) {
UpdateHardwareStatus();
updateHardwareStatus();
lastHwStatusUpdate = DateTime.Now;
}

Expand Down Expand Up @@ -101,7 +101,7 @@ public void UpdateHardwareSensors() {
Max = (float)s.Max,
Min = (float)s.Min,
ValuesTimeWindow = s.ValuesTimeWindow.TotalSeconds,
Values = FromHardwareSensorValue(s.Values) };
Values = fromHardwareSensorValue(s.Values) };
#pragma warning restore CS8629 // Nullable value type may be null.
byte[] sensorData = new byte[0];
if (Config.FeatureEnableMemoryMapAllHardwareData) {
Expand Down Expand Up @@ -129,7 +129,7 @@ public void UpdateHardwareSensors() {
Max = s.Max != null ? (float)s.Max : 0.0f,
Min = s.Min != null ? (float)s.Min : 0.0f,
ValuesTimeWindow = s.ValuesTimeWindow.TotalSeconds,
Values = FromHardwareSensorValue(s.Values) };
Values = fromHardwareSensorValue(s.Values) };
byte[] sensorData = new byte[0];

if (Config.FeatureEnableMemoryMapAllHardwareData) {
Expand Down Expand Up @@ -168,11 +168,11 @@ public void UpdateHardwareSensors() {
UpdateInterval = Config.UpdateIntervalSeconds,
LastUpdate = lastUpdate,
};
MemoryMappedSensors.Instance.WriteSensors(indexBytes, dataBytes, m);
MemoryMappedSensors.instance.writeSensors(indexBytes, dataBytes, m);

if (Config.FeatureEnableMemoryMapAllHardwareData) {
byte[] rootBytes = Utf8Json.JsonSerializer.Serialize(root);
MemoryMappedSensors.Instance.WriteHardware(rootBytes, m);
MemoryMappedSensors.instance.writeHardware(rootBytes, m);
if (isDebug) {
Console.WriteLine(" WRITING ROOT ---------- {0} bytes -- {1} ", rootBytes.Length,
Encoding.UTF8.GetString(rootBytes));
Expand All @@ -181,7 +181,7 @@ public void UpdateHardwareSensors() {
}
}

List<DataSensorValue> FromHardwareSensorValue(IEnumerable<LibreHardwareMonitor.Hardware.SensorValue> from) {
List<DataSensorValue> fromHardwareSensorValue(IEnumerable<LibreHardwareMonitor.Hardware.SensorValue> from) {
List<DataSensorValue> ret = new List<DataSensorValue>();
foreach (LibreHardwareMonitor.Hardware.SensorValue fh in from) {
ret.Add(new DataSensorValue {
Expand All @@ -193,7 +193,7 @@ List<DataSensorValue> FromHardwareSensorValue(IEnumerable<LibreHardwareMonitor.H
return ret;
}

public void UpdateHardwareStatus() {
public void updateHardwareStatus() {
long lastUpdate = DateTimeOffset.UtcNow.ToUnixTimeSeconds();

using (MemoryStream stream = recyclableMemoryStreamManager.GetStream()) {
Expand Down Expand Up @@ -297,12 +297,12 @@ public void UpdateHardwareStatus() {
LastUpdate = lastUpdate,
};

MemoryMappedSensors.Instance.WriteStatus(stream.ToArray(), m);
MemoryMappedSensors.instance.writeStatus(stream.ToArray(), m);
}
}
}

public void Close() {
public void close() {
computer.Close();
}
}
Expand Down

0 comments on commit cc6dc42

Please sign in to comment.