working notificationstate service

pull/9/head
sleevezipper 4 years ago
parent f9e15a0a13
commit de6ba209d3

@ -1,9 +1,11 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using hass_desktop_service.StateDetectors.Windows.Fullscreen;
namespace hass_desktop_service namespace hass_desktop_service
{ {
@ -11,13 +13,23 @@ namespace hass_desktop_service
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
CreateHostBuilder(args).Build().Run();
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
CreateHostBuilder(args).Build().Run();
}
else
{
// we only support MS Windows for now
throw new NotImplementedException("Your platform is not yet supported");
}
} }
public static IHostBuilder CreateHostBuilder(string[] args) => public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args) Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) => .ConfigureServices((hostContext, services) =>
{ {
services.AddSingleton<UserNotificationStateDetector>();
services.AddHostedService<Worker>(); services.AddHostedService<Worker>();
}); });
} }

@ -0,0 +1,59 @@
using System.Runtime.InteropServices;
using PInvoke;
using static PInvoke.Shell32;
namespace hass_desktop_service.StateDetectors.Windows.Fullscreen
{
public enum UserNotificationState
{
/// <summary>
/// A screen saver is displayed, the machine is locked,
/// or a nonactive Fast User Switching session is in progress.
/// </summary>
NotPresent = 1,
/// <summary>
/// A full-screen application is running or Presentation Settings are applied.
/// Presentation Settings allow a user to put their machine into a state fit
/// for an uninterrupted presentation, such as a set of PowerPoint slides, with a single click.
/// </summary>
Busy = 2,
/// <summary>
/// A full-screen (exclusive mode) Direct3D application is running.
/// </summary>
RunningDirect3dFullScreen = 3,
/// <summary>
/// The user has activated Windows presentation settings to block notifications and pop-up messages.
/// </summary>
PresentationMode = 4,
/// <summary>
/// None of the other states are found, notifications can be freely sent.
/// </summary>
AcceptsNotifications = 5,
/// <summary>
/// Introduced in Windows 7. The current user is in "quiet time", which is the first hour after
/// a new user logs into his or her account for the first time. During this time, most notifications
/// should not be sent or shown. This lets a user become accustomed to a new computer system
/// without those distractions.
/// Quiet time also occurs for each user after an operating system upgrade or clean installation.
/// </summary>
QuietTime = 6
};
public class UserNotificationStateDetector
{
[DllImport("shell32.dll")]
static extern int SHQueryUserNotificationState(out UserNotificationState state);
public UserNotificationState GetState(){
UserNotificationState state;
SHQueryUserNotificationState(out state);
return state;
}
}
}

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using hass_desktop_service.StateDetectors.Windows.Fullscreen;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -11,10 +12,12 @@ namespace hass_desktop_service
public class Worker : BackgroundService public class Worker : BackgroundService
{ {
private readonly ILogger<Worker> _logger; private readonly ILogger<Worker> _logger;
private readonly UserNotificationStateDetector _userNotificationStateDetector;
public Worker(ILogger<Worker> logger) public Worker(ILogger<Worker> logger, UserNotificationStateDetector userNotificationStateDetector)
{ {
_logger = logger; _logger = logger;
this._userNotificationStateDetector = userNotificationStateDetector;
} }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) protected override async Task ExecuteAsync(CancellationToken stoppingToken)
@ -22,6 +25,8 @@ namespace hass_desktop_service
while (!stoppingToken.IsCancellationRequested) while (!stoppingToken.IsCancellationRequested)
{ {
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
_logger.LogInformation($"Notificationstate: {this._userNotificationStateDetector.GetState()}");
await Task.Delay(1000, stoppingToken); await Task.Delay(1000, stoppingToken);
} }
} }

Loading…
Cancel
Save