mirror of
https://github.com/ckaczor/SystemTemperatureStatusWindow.git
synced 2026-01-13 17:23:03 -05:00
Split into service and UI
This commit is contained in:
31
Service/App.config
Normal file
31
Service/App.config
Normal file
@@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<system.serviceModel>
|
||||
<behaviors>
|
||||
<serviceBehaviors>
|
||||
<behavior name="">
|
||||
<serviceMetadata httpGetEnabled="true"/>
|
||||
<serviceDebug includeExceptionDetailInFaults="false"/>
|
||||
</behavior>
|
||||
</serviceBehaviors>
|
||||
</behaviors>
|
||||
<services>
|
||||
<service name="SystemTemperatureService.SystemTemperatureService">
|
||||
<endpoint address="" binding="wsHttpBinding" contract="SystemTemperatureService.ISystemTemperatureService">
|
||||
<identity>
|
||||
<dns value="localhost"/>
|
||||
</identity>
|
||||
</endpoint>
|
||||
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
|
||||
<host>
|
||||
<baseAddresses>
|
||||
<add baseAddress="http://localhost/SystemTemperatureService/SystemTemperatureService.svc"/>
|
||||
</baseAddresses>
|
||||
</host>
|
||||
</service>
|
||||
</services>
|
||||
</system.serviceModel>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
|
||||
</startup>
|
||||
</configuration>
|
||||
30
Service/Device.cs
Normal file
30
Service/Device.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace SystemTemperatureService
|
||||
{
|
||||
[DataContract]
|
||||
public enum DeviceType
|
||||
{
|
||||
[EnumMember]
|
||||
Cpu,
|
||||
|
||||
[EnumMember]
|
||||
Gpu,
|
||||
|
||||
[EnumMember]
|
||||
Hdd
|
||||
}
|
||||
|
||||
[DataContract]
|
||||
public class Device
|
||||
{
|
||||
[DataMember]
|
||||
public string Id { get; set; }
|
||||
|
||||
[DataMember]
|
||||
public DeviceType Type { get; set; }
|
||||
|
||||
[DataMember]
|
||||
public double Temperature { get; set; }
|
||||
}
|
||||
}
|
||||
70
Service/Framework/ConsoleHarness.cs
Normal file
70
Service/Framework/ConsoleHarness.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
|
||||
namespace SystemTemperatureService.Framework
|
||||
{
|
||||
public static class ConsoleHarness
|
||||
{
|
||||
// Run a service from the console given a service implementation
|
||||
public static void Run(string[] args, IWindowsService service)
|
||||
{
|
||||
bool isRunning = true;
|
||||
|
||||
// simulate starting the windows service
|
||||
service.OnStart(args);
|
||||
|
||||
// let it run as long as Q is not pressed
|
||||
while (isRunning)
|
||||
{
|
||||
WriteToConsole(ConsoleColor.Yellow, "Enter either [Q]uit, [P]ause, [R]esume : ");
|
||||
isRunning = HandleConsoleInput(service, Console.ReadLine());
|
||||
}
|
||||
|
||||
// stop and shutdown
|
||||
service.OnStop();
|
||||
service.OnShutdown();
|
||||
}
|
||||
|
||||
// Private input handler for console commands.
|
||||
private static bool HandleConsoleInput(IWindowsService service, string line)
|
||||
{
|
||||
bool canContinue = true;
|
||||
|
||||
// check input
|
||||
if (line != null)
|
||||
{
|
||||
switch (line.ToUpper())
|
||||
{
|
||||
case "Q":
|
||||
canContinue = false;
|
||||
break;
|
||||
|
||||
case "P":
|
||||
service.OnPause();
|
||||
break;
|
||||
|
||||
case "R":
|
||||
service.OnContinue();
|
||||
break;
|
||||
|
||||
default:
|
||||
WriteToConsole(ConsoleColor.Red, "Did not understand that input, try again.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return canContinue;
|
||||
}
|
||||
|
||||
// Helper method to write a message to the console at the given foreground color.
|
||||
internal static void WriteToConsole(ConsoleColor foregroundColor, string format, params object[] formatArguments)
|
||||
{
|
||||
ConsoleColor originalColor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = foregroundColor;
|
||||
|
||||
Console.WriteLine(format, formatArguments);
|
||||
Console.Out.Flush();
|
||||
|
||||
Console.ForegroundColor = originalColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
46
Service/Framework/IWindowsService.cs
Normal file
46
Service/Framework/IWindowsService.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
|
||||
namespace SystemTemperatureService.Framework
|
||||
{
|
||||
/// <summary>
|
||||
/// The interface that any windows service should implement to be used
|
||||
/// with the GenericWindowsService executable.
|
||||
/// </summary>
|
||||
public interface IWindowsService : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// This method is called when the service gets a request to start.
|
||||
/// </summary>
|
||||
/// <param name="args">Any command line arguments</param>
|
||||
void OnStart(string[] args);
|
||||
|
||||
/// <summary>
|
||||
/// This method is called when the service gets a request to stop.
|
||||
/// </summary>
|
||||
void OnStop();
|
||||
|
||||
/// <summary>
|
||||
/// This method is called when a service gets a request to pause,
|
||||
/// but not stop completely.
|
||||
/// </summary>
|
||||
void OnPause();
|
||||
|
||||
/// <summary>
|
||||
/// This method is called when a service gets a request to resume
|
||||
/// after a pause is issued.
|
||||
/// </summary>
|
||||
void OnContinue();
|
||||
|
||||
/// <summary>
|
||||
/// This method is called when the machine the service is running on
|
||||
/// is being shutdown.
|
||||
/// </summary>
|
||||
void OnShutdown();
|
||||
|
||||
/// <summary>
|
||||
/// This method is called when a custom command is issued to the service.
|
||||
/// </summary>
|
||||
/// <param name="command">The command identifier to execute.</param >
|
||||
void OnCustomCommand(int command);
|
||||
}
|
||||
}
|
||||
47
Service/Framework/TypeExtensions.cs
Normal file
47
Service/Framework/TypeExtensions.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace SystemTemperatureService.Framework
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for the Type class
|
||||
/// </summary>
|
||||
public static class TypeExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Loads the configuration from assembly attributes
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the custom attribute to find.</typeparam>
|
||||
/// <param name="typeWithAttributes">The calling assembly to search.</param>
|
||||
/// <returns>The custom attribute of type T, if found.</returns>
|
||||
public static T GetAttribute<T>(this Type typeWithAttributes)
|
||||
where T : Attribute
|
||||
{
|
||||
return GetAttributes<T>(typeWithAttributes).FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads the configuration from assembly attributes
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the custom attribute to find.</typeparam>
|
||||
/// <param name="typeWithAttributes">The calling assembly to search.</param>
|
||||
/// <returns>An enumeration of attributes of type T that were found.</returns>
|
||||
public static IEnumerable<T> GetAttributes<T>(this Type typeWithAttributes)
|
||||
where T : Attribute
|
||||
{
|
||||
// Try to find the configuration attribute for the default logger if it exists
|
||||
object[] configAttributes = Attribute.GetCustomAttributes(typeWithAttributes,
|
||||
typeof(T), false);
|
||||
|
||||
// get just the first one
|
||||
if (configAttributes != null && configAttributes.Length > 0)
|
||||
{
|
||||
foreach (T attribute in configAttributes)
|
||||
{
|
||||
yield return attribute;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
90
Service/Framework/WindowsServiceAttribute.cs
Normal file
90
Service/Framework/WindowsServiceAttribute.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using System.ServiceProcess;
|
||||
|
||||
namespace SystemTemperatureService.Framework
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
|
||||
public class WindowsServiceAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// The name of the service.
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The displayable name that shows in service manager (defaults to Name).
|
||||
/// </summary>
|
||||
public string DisplayName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A textural description of the service name (defaults to Name).
|
||||
/// </summary>
|
||||
public string Description { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The user to run the service under (defaults to null). A null or empty
|
||||
/// UserName field causes the service to run as ServiceAccount.LocalService.
|
||||
/// </summary>
|
||||
public string UserName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The password to run the service under (defaults to null). Ignored
|
||||
/// if the UserName is empty or null, this property is ignored.
|
||||
/// </summary>
|
||||
public string Password { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Specifies the event log source to set the service's EventLog to. If this is
|
||||
/// empty or null (the default) no event log source is set. If set, will auto-log
|
||||
/// start and stop events.
|
||||
/// </summary>
|
||||
public string EventLogSource { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The method to start the service when the machine reboots (defaults to Manual).
|
||||
/// </summary>
|
||||
public ServiceStartMode StartMode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// True if service supports pause and continue (defaults to true).
|
||||
/// </summary>
|
||||
public bool CanPauseAndContinue { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// True if service supports shutdown event (defaults to true).
|
||||
/// </summary>
|
||||
public bool CanShutdown { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// True if service supports stop event (defaults to true).
|
||||
/// </summary>
|
||||
public bool CanStop { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The service account to use if the UserName is not specified.
|
||||
/// </summary>
|
||||
public ServiceAccount ServiceAccount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Marks an IWindowsService with configuration and installation attributes.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the windows service.</param>
|
||||
public WindowsServiceAttribute(string name)
|
||||
{
|
||||
// set name and default description and display name to name.
|
||||
Name = name;
|
||||
Description = name;
|
||||
DisplayName = name;
|
||||
|
||||
// default all other attributes.
|
||||
CanStop = true;
|
||||
CanShutdown = true;
|
||||
CanPauseAndContinue = true;
|
||||
StartMode = ServiceStartMode.Manual;
|
||||
EventLogSource = null;
|
||||
Password = null;
|
||||
UserName = null;
|
||||
ServiceAccount = ServiceAccount.LocalService;
|
||||
}
|
||||
}
|
||||
}
|
||||
37
Service/Framework/WindowsServiceHarness.Designer.cs
generated
Normal file
37
Service/Framework/WindowsServiceHarness.Designer.cs
generated
Normal file
@@ -0,0 +1,37 @@
|
||||
namespace SystemTemperatureService.Framework
|
||||
{
|
||||
public partial class WindowsServiceHarness
|
||||
{
|
||||
/// <summary>
|
||||
/// Required designer variable.
|
||||
/// </summary>
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
/// <summary>
|
||||
/// Clean up any resources being used.
|
||||
/// </summary>
|
||||
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Component Designer generated code
|
||||
|
||||
/// <summary>
|
||||
/// Required method for Designer support - do not modify
|
||||
/// the contents of this method with the code editor.
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
components = new System.ComponentModel.Container();
|
||||
this.ServiceName = "UsageService";
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
129
Service/Framework/WindowsServiceHarness.cs
Normal file
129
Service/Framework/WindowsServiceHarness.cs
Normal file
@@ -0,0 +1,129 @@
|
||||
using System;
|
||||
using System.ServiceProcess;
|
||||
|
||||
namespace SystemTemperatureService.Framework
|
||||
{
|
||||
/// <summary>
|
||||
/// A generic Windows Service that can handle any assembly that
|
||||
/// implements IWindowsService (including AbstractWindowsService)
|
||||
/// </summary>
|
||||
public partial class WindowsServiceHarness : ServiceBase
|
||||
{
|
||||
/// <summary>
|
||||
/// Get the class implementing the windows service
|
||||
/// </summary>
|
||||
public IWindowsService ServiceImplementation { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Constructor a generic windows service from the given class
|
||||
/// </summary>
|
||||
/// <param name="serviceImplementation">Service implementation.</param>
|
||||
public WindowsServiceHarness(IWindowsService serviceImplementation)
|
||||
{
|
||||
// make sure service passed in is valid
|
||||
if (serviceImplementation == null)
|
||||
{
|
||||
throw new ArgumentNullException("serviceImplementation",
|
||||
"IWindowsService cannot be null in call to GenericWindowsService");
|
||||
}
|
||||
|
||||
// set instance and backward instance
|
||||
ServiceImplementation = serviceImplementation;
|
||||
|
||||
// configure our service
|
||||
ConfigureServiceFromAttributes(serviceImplementation);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Override service control on continue
|
||||
/// </summary>
|
||||
protected override void OnContinue()
|
||||
{
|
||||
// perform class specific behavior
|
||||
ServiceImplementation.OnContinue();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when service is paused
|
||||
/// </summary>
|
||||
protected override void OnPause()
|
||||
{
|
||||
// perform class specific behavior
|
||||
ServiceImplementation.OnPause();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when a custom command is requested
|
||||
/// </summary>
|
||||
/// <param name="command">Id of custom command</param>
|
||||
protected override void OnCustomCommand(int command)
|
||||
{
|
||||
// perform class specific behavior
|
||||
ServiceImplementation.OnCustomCommand(command);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when the Operating System is shutting down
|
||||
/// </summary>
|
||||
protected override void OnShutdown()
|
||||
{
|
||||
// perform class specific behavior
|
||||
ServiceImplementation.OnShutdown();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when service is requested to start
|
||||
/// </summary>
|
||||
/// <param name="args">The startup arguments array.</param>
|
||||
protected override void OnStart(string[] args)
|
||||
{
|
||||
ServiceImplementation.OnStart(args);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when service is requested to stop
|
||||
/// </summary>
|
||||
protected override void OnStop()
|
||||
{
|
||||
ServiceImplementation.OnStop();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set configuration data
|
||||
/// </summary>
|
||||
/// <param name="serviceImplementation">The service with configuration settings.</param>
|
||||
private void ConfigureServiceFromAttributes(IWindowsService serviceImplementation)
|
||||
{
|
||||
var attribute = serviceImplementation.GetType().GetAttribute<WindowsServiceAttribute>();
|
||||
|
||||
if (attribute != null)
|
||||
{
|
||||
// wire up the event log source, if provided
|
||||
if (!string.IsNullOrWhiteSpace(attribute.EventLogSource))
|
||||
{
|
||||
// assign to the base service's EventLog property for auto-log events.
|
||||
EventLog.Source = attribute.EventLogSource;
|
||||
}
|
||||
|
||||
CanStop = attribute.CanStop;
|
||||
CanPauseAndContinue = attribute.CanPauseAndContinue;
|
||||
CanShutdown = attribute.CanShutdown;
|
||||
|
||||
// we don't handle: laptop power change event
|
||||
CanHandlePowerEvent = false;
|
||||
|
||||
// we don't handle: Term Services session event
|
||||
CanHandleSessionChangeEvent = false;
|
||||
|
||||
// always auto-event-log
|
||||
AutoLog = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
string.Format("IWindowsService implementer {0} must have a WindowsServiceAttribute.",
|
||||
serviceImplementation.GetType().FullName));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
36
Service/Framework/WindowsServiceInstaller.Designer.cs
generated
Normal file
36
Service/Framework/WindowsServiceInstaller.Designer.cs
generated
Normal file
@@ -0,0 +1,36 @@
|
||||
namespace SystemTemperatureService.Framework
|
||||
{
|
||||
public partial class WindowsServiceInstaller
|
||||
{
|
||||
/// <summary>
|
||||
/// Required designer variable.
|
||||
/// </summary>
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
/// <summary>
|
||||
/// Clean up any resources being used.
|
||||
/// </summary>
|
||||
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Component Designer generated code
|
||||
|
||||
/// <summary>
|
||||
/// Required method for Designer support - do not modify
|
||||
/// the contents of this method with the code editor.
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
components = new System.ComponentModel.Container();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
212
Service/Framework/WindowsServiceInstaller.cs
Normal file
212
Service/Framework/WindowsServiceInstaller.cs
Normal file
@@ -0,0 +1,212 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.ComponentModel;
|
||||
using System.Configuration.Install;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.ServiceProcess;
|
||||
|
||||
namespace SystemTemperatureService.Framework
|
||||
{
|
||||
/// <summary>
|
||||
/// A generic windows service installer
|
||||
/// </summary>
|
||||
[RunInstaller(true)]
|
||||
public partial class WindowsServiceInstaller : Installer
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the type of the windows service to install.
|
||||
/// </summary>
|
||||
public WindowsServiceAttribute Configuration { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Creates a blank windows service installer with configuration in ServiceImplementation
|
||||
/// </summary>
|
||||
public WindowsServiceInstaller()
|
||||
: this(typeof(ServiceImplementation))
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Creates a windows service installer using the type specified.
|
||||
/// </summary>
|
||||
/// <param name="windowsServiceType">The type of the windows service to install.</param>
|
||||
public WindowsServiceInstaller(Type windowsServiceType)
|
||||
{
|
||||
if (!windowsServiceType.GetInterfaces().Contains(typeof(IWindowsService)))
|
||||
{
|
||||
throw new ArgumentException("Type to install must implement IWindowsService.",
|
||||
"windowsServiceType");
|
||||
}
|
||||
|
||||
var attribute = windowsServiceType.GetAttribute<WindowsServiceAttribute>();
|
||||
|
||||
if (attribute == null)
|
||||
{
|
||||
throw new ArgumentException("Type to install must be marked with a WindowsServiceAttribute.",
|
||||
"windowsServiceType");
|
||||
}
|
||||
|
||||
Configuration = attribute;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Performs a transacted installation at run-time of the AutoCounterInstaller and any other listed installers.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The IWindowsService implementer to install.</typeparam>
|
||||
public static void RuntimeInstall<T>()
|
||||
where T : IWindowsService
|
||||
{
|
||||
string path = "/assemblypath=" + Assembly.GetEntryAssembly().Location;
|
||||
|
||||
using (var ti = new TransactedInstaller())
|
||||
{
|
||||
ti.Installers.Add(new WindowsServiceInstaller(typeof(T)));
|
||||
ti.Context = new InstallContext(null, new[] { path });
|
||||
ti.Install(new Hashtable());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Performs a transacted un-installation at run-time of the AutoCounterInstaller and any other listed installers.
|
||||
/// </summary>
|
||||
/// <param name="otherInstallers">The other installers to include in the transaction</param>
|
||||
/// <typeparam name="T">The IWindowsService implementer to install.</typeparam>
|
||||
public static void RuntimeUnInstall<T>(params Installer[] otherInstallers)
|
||||
where T : IWindowsService
|
||||
{
|
||||
string path = "/assemblypath=" + Assembly.GetEntryAssembly().Location;
|
||||
|
||||
using (var ti = new TransactedInstaller())
|
||||
{
|
||||
ti.Installers.Add(new WindowsServiceInstaller(typeof(T)));
|
||||
ti.Context = new InstallContext(null, new[] { path });
|
||||
ti.Uninstall(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Installer class, to use run InstallUtil against this .exe
|
||||
/// </summary>
|
||||
/// <param name="savedState">The saved state for the installation.</param>
|
||||
public override void Install(IDictionary savedState)
|
||||
{
|
||||
ConsoleHarness.WriteToConsole(ConsoleColor.White, "Installing service {0}.", Configuration.Name);
|
||||
|
||||
// install the service
|
||||
ConfigureInstallers();
|
||||
base.Install(savedState);
|
||||
|
||||
// wire up the event log source, if provided
|
||||
if (!string.IsNullOrWhiteSpace(Configuration.EventLogSource))
|
||||
{
|
||||
// create the source if it doesn't exist
|
||||
if (!EventLog.SourceExists(Configuration.EventLogSource))
|
||||
{
|
||||
EventLog.CreateEventSource(Configuration.EventLogSource, "Application");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Removes the counters, then calls the base uninstall.
|
||||
/// </summary>
|
||||
/// <param name="savedState">The saved state for the installation.</param>
|
||||
public override void Uninstall(IDictionary savedState)
|
||||
{
|
||||
ConsoleHarness.WriteToConsole(ConsoleColor.White, "Un-Installing service {0}.", Configuration.Name);
|
||||
|
||||
// load the assembly file name and the config
|
||||
ConfigureInstallers();
|
||||
base.Uninstall(savedState);
|
||||
|
||||
// wire up the event log source, if provided
|
||||
if (!string.IsNullOrWhiteSpace(Configuration.EventLogSource))
|
||||
{
|
||||
// create the source if it doesn't exist
|
||||
if (EventLog.SourceExists(Configuration.EventLogSource))
|
||||
{
|
||||
EventLog.DeleteEventSource(Configuration.EventLogSource);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Rolls back to the state of the counter, and performs the normal rollback.
|
||||
/// </summary>
|
||||
/// <param name="savedState">The saved state for the installation.</param>
|
||||
public override void Rollback(IDictionary savedState)
|
||||
{
|
||||
ConsoleHarness.WriteToConsole(ConsoleColor.White, "Rolling back service {0}.", Configuration.Name);
|
||||
|
||||
// load the assembly file name and the config
|
||||
ConfigureInstallers();
|
||||
base.Rollback(savedState);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Method to configure the installers
|
||||
/// </summary>
|
||||
private void ConfigureInstallers()
|
||||
{
|
||||
// load the assembly file name and the config
|
||||
Installers.Add(ConfigureProcessInstaller());
|
||||
Installers.Add(ConfigureServiceInstaller());
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Helper method to configure a process installer for this windows service
|
||||
/// </summary>
|
||||
/// <returns>Process installer for this service</returns>
|
||||
private ServiceProcessInstaller ConfigureProcessInstaller()
|
||||
{
|
||||
var result = new ServiceProcessInstaller();
|
||||
|
||||
// if a user name is not provided, will run under local service acct
|
||||
if (string.IsNullOrEmpty(Configuration.UserName))
|
||||
{
|
||||
result.Account = Configuration.ServiceAccount;
|
||||
result.Username = null;
|
||||
result.Password = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// otherwise, runs under the specified user authority
|
||||
result.Account = ServiceAccount.User;
|
||||
result.Username = Configuration.UserName;
|
||||
result.Password = Configuration.Password;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Helper method to configure a service installer for this windows service
|
||||
/// </summary>
|
||||
/// <returns>Process installer for this service</returns>
|
||||
private ServiceInstaller ConfigureServiceInstaller()
|
||||
{
|
||||
// create and config a service installer
|
||||
var result = new ServiceInstaller
|
||||
{
|
||||
ServiceName = Configuration.Name,
|
||||
DisplayName = Configuration.DisplayName,
|
||||
Description = Configuration.Description,
|
||||
StartType = Configuration.StartMode,
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Service/ISystemTemperatureService.cs
Normal file
12
Service/ISystemTemperatureService.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.Collections.Generic;
|
||||
using System.ServiceModel;
|
||||
|
||||
namespace SystemTemperatureService
|
||||
{
|
||||
[ServiceContract]
|
||||
interface ISystemTemperatureService
|
||||
{
|
||||
[OperationContract]
|
||||
List<Device> GetDeviceList();
|
||||
}
|
||||
}
|
||||
61
Service/Program.cs
Normal file
61
Service/Program.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using Common.Debug;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.ServiceProcess;
|
||||
using SystemTemperatureService.Framework;
|
||||
|
||||
namespace SystemTemperatureService
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Tracer.Initialize(null, null, Process.GetCurrentProcess().Id.ToString(CultureInfo.InvariantCulture), Environment.UserInteractive);
|
||||
|
||||
if (args.Contains("-install", StringComparer.InvariantCultureIgnoreCase))
|
||||
{
|
||||
Tracer.WriteLine("Starting install...");
|
||||
|
||||
try
|
||||
{
|
||||
WindowsServiceInstaller.RuntimeInstall<ServiceImplementation>();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Tracer.WriteException("Service install", exception);
|
||||
}
|
||||
|
||||
Tracer.WriteLine("Install complete");
|
||||
}
|
||||
else if (args.Contains("-uninstall", StringComparer.InvariantCultureIgnoreCase))
|
||||
{
|
||||
Tracer.WriteLine("Starting uninstall...");
|
||||
|
||||
try
|
||||
{
|
||||
WindowsServiceInstaller.RuntimeUnInstall<ServiceImplementation>();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Tracer.WriteException("Service uninstall", exception);
|
||||
}
|
||||
|
||||
Tracer.WriteLine("Uninstall complete");
|
||||
}
|
||||
else
|
||||
{
|
||||
Tracer.WriteLine("Starting service");
|
||||
|
||||
var implementation = new ServiceImplementation();
|
||||
|
||||
if (Environment.UserInteractive)
|
||||
ConsoleHarness.Run(args, implementation);
|
||||
else
|
||||
ServiceBase.Run(new WindowsServiceHarness(implementation));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
35
Service/Properties/AssemblyInfo.cs
Normal file
35
Service/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("Service")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("Service")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2014")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("9cf103fa-d59f-4abd-b1bd-797119b4843e")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
57
Service/ServiceImplementation.cs
Normal file
57
Service/ServiceImplementation.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using Common.Debug;
|
||||
using System;
|
||||
using System.ServiceModel;
|
||||
using System.ServiceProcess;
|
||||
using SystemTemperatureService.Framework;
|
||||
|
||||
namespace SystemTemperatureService
|
||||
{
|
||||
[WindowsService("SystemTemperatureStatus", DisplayName = "System Temperature Status", Description = "", StartMode = ServiceStartMode.Automatic, ServiceAccount = ServiceAccount.LocalSystem)]
|
||||
public class ServiceImplementation : IWindowsService
|
||||
{
|
||||
private ServiceHost _serviceHost;
|
||||
|
||||
public void OnStart(string[] args)
|
||||
{
|
||||
using (new BeginEndTracer(GetType().Name))
|
||||
{
|
||||
try
|
||||
{
|
||||
_serviceHost = new ServiceHost(typeof(SystemTemperatureService));
|
||||
_serviceHost.Open();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Tracer.WriteException("ServiceImplementation.OnStart", exception);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnStop()
|
||||
{
|
||||
using (new BeginEndTracer(GetType().Name))
|
||||
{
|
||||
try
|
||||
{
|
||||
_serviceHost.Close();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Tracer.WriteException("ServiceImplementation.OnStop", exception);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnPause() { }
|
||||
|
||||
public void OnContinue() { }
|
||||
|
||||
public void OnShutdown() { }
|
||||
|
||||
public void Dispose() { }
|
||||
|
||||
public void OnCustomCommand(int command) { }
|
||||
}
|
||||
}
|
||||
60
Service/SystemTemperatureService.cs
Normal file
60
Service/SystemTemperatureService.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using OpenHardwareMonitor.Hardware;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace SystemTemperatureService
|
||||
{
|
||||
public class SystemTemperatureService : ISystemTemperatureService
|
||||
{
|
||||
private static Computer _computer;
|
||||
|
||||
public List<Device> GetDeviceList()
|
||||
{
|
||||
if (_computer == null)
|
||||
{
|
||||
_computer = new Computer { HDDEnabled = true, FanControllerEnabled = false, GPUEnabled = true, MainboardEnabled = true, CPUEnabled = true };
|
||||
_computer.Open();
|
||||
}
|
||||
|
||||
var deviceList = new List<Device>();
|
||||
|
||||
foreach (var hardware in _computer.Hardware)
|
||||
{
|
||||
hardware.Update();
|
||||
|
||||
var averageValue = hardware.Sensors.Where(sensor => sensor.SensorType == SensorType.Temperature).Average(sensor => sensor.Value);
|
||||
|
||||
if (averageValue.HasValue)
|
||||
{
|
||||
Device device = null;
|
||||
|
||||
switch (hardware.HardwareType)
|
||||
{
|
||||
case HardwareType.CPU:
|
||||
device = new Device { Type = DeviceType.Cpu };
|
||||
break;
|
||||
|
||||
case HardwareType.GpuAti:
|
||||
case HardwareType.GpuNvidia:
|
||||
device = new Device { Type = DeviceType.Gpu };
|
||||
break;
|
||||
|
||||
case HardwareType.HDD:
|
||||
device = new Device { Type = DeviceType.Hdd };
|
||||
break;
|
||||
}
|
||||
|
||||
if (device != null)
|
||||
{
|
||||
device.Id = hardware.Identifier.ToString();
|
||||
device.Temperature = averageValue.Value;
|
||||
}
|
||||
|
||||
deviceList.Add(device);
|
||||
}
|
||||
}
|
||||
|
||||
return deviceList;
|
||||
}
|
||||
}
|
||||
}
|
||||
98
Service/SystemTemperatureService.csproj
Normal file
98
Service/SystemTemperatureService.csproj
Normal file
@@ -0,0 +1,98 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{94DE06A9-4F37-487B-96E9-663B9A98F05D}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>SystemTemperatureService</RootNamespace>
|
||||
<AssemblyName>SystemTemperatureService</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<ApplicationManifest>app.manifest</ApplicationManifest>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Configuration.Install" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Runtime.Serialization" />
|
||||
<Reference Include="System.ServiceModel" />
|
||||
<Reference Include="System.ServiceProcess" />
|
||||
<Reference Include="WindowsBase" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Device.cs" />
|
||||
<Compile Include="Framework\ConsoleHarness.cs" />
|
||||
<Compile Include="Framework\IWindowsService.cs" />
|
||||
<Compile Include="Framework\TypeExtensions.cs" />
|
||||
<Compile Include="Framework\WindowsServiceAttribute.cs" />
|
||||
<Compile Include="Framework\WindowsServiceHarness.cs">
|
||||
<SubType>Component</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Framework\WindowsServiceHarness.Designer.cs">
|
||||
<DependentUpon>WindowsServiceHarness.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Framework\WindowsServiceInstaller.cs">
|
||||
<SubType>Component</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Framework\WindowsServiceInstaller.Designer.cs">
|
||||
<DependentUpon>WindowsServiceInstaller.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="ISystemTemperatureService.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="SystemTemperatureService.cs" />
|
||||
<Compile Include="ServiceImplementation.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
<None Include="app.manifest" />
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\Public\OpenHardwareMonitor\OpenHardwareMonitorLib.csproj">
|
||||
<Project>{b0397530-545a-471d-bb74-027ae456df1a}</Project>
|
||||
<Name>OpenHardwareMonitorLib</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\..\Common\Common.csproj">
|
||||
<Project>{17864D82-457D-4A0A-BC10-1D07F2B3A5D6}</Project>
|
||||
<Name>Common</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<Import Project="..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />
|
||||
<Target Name="EnsureBclBuildImported" BeforeTargets="BeforeBuild" Condition="'$(BclBuildImported)' == ''">
|
||||
<Error Condition="!Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" />
|
||||
<Error Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />
|
||||
</Target>
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
||||
58
Service/app.manifest
Normal file
58
Service/app.manifest
Normal file
@@ -0,0 +1,58 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
|
||||
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
|
||||
<security>
|
||||
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
|
||||
<!-- UAC Manifest Options
|
||||
If you want to change the Windows User Account Control level replace the
|
||||
requestedExecutionLevel node with one of the following.
|
||||
|
||||
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
|
||||
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
|
||||
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
|
||||
|
||||
Specifying requestedExecutionLevel node will disable file and registry virtualization.
|
||||
If you want to utilize File and Registry Virtualization for backward
|
||||
compatibility then delete the requestedExecutionLevel node.
|
||||
-->
|
||||
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
|
||||
</requestedPrivileges>
|
||||
</security>
|
||||
</trustInfo>
|
||||
|
||||
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
|
||||
<application>
|
||||
<!-- A list of all Windows versions that this application is designed to work with.
|
||||
Windows will automatically select the most compatible environment.-->
|
||||
|
||||
<!-- If your application is designed to work with Windows Vista, uncomment the following supportedOS node-->
|
||||
<!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"></supportedOS>-->
|
||||
|
||||
<!-- If your application is designed to work with Windows 7, uncomment the following supportedOS node-->
|
||||
<!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>-->
|
||||
|
||||
<!-- If your application is designed to work with Windows 8, uncomment the following supportedOS node-->
|
||||
<!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"></supportedOS>-->
|
||||
|
||||
<!-- If your application is designed to work with Windows 8.1, uncomment the following supportedOS node-->
|
||||
<!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>-->
|
||||
|
||||
</application>
|
||||
</compatibility>
|
||||
|
||||
<!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
|
||||
<!-- <dependency>
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity
|
||||
type="win32"
|
||||
name="Microsoft.Windows.Common-Controls"
|
||||
version="6.0.0.0"
|
||||
processorArchitecture="*"
|
||||
publicKeyToken="6595b64144ccf1df"
|
||||
language="*"
|
||||
/>
|
||||
</dependentAssembly>
|
||||
</dependency>-->
|
||||
|
||||
</asmv1:assembly>
|
||||
5
Service/packages.config
Normal file
5
Service/packages.config
Normal file
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
|
||||
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net45" />
|
||||
</packages>
|
||||
Reference in New Issue
Block a user