The Free Software Media System - Server Backend & API
update power management
Luke Pulverenti24532f3e2d7d
diff --git a/Emby.Server.Implementations/Emby.Server.Implementations.csproj b/Emby.Server.Implementations/Emby.Server.Implementations.csprojindex f5c1572341..7e885b7798 100644--- a/Emby.Server.Implementations/Emby.Server.Implementations.csproj+++ b/Emby.Server.Implementations/Emby.Server.Implementations.csproj@@ -51,6 +51,7 @@<Compile Include="Devices\DeviceManager.cs" /><Compile Include="Dto\DtoService.cs" /><Compile Include="EntryPoints\AutomaticRestartEntryPoint.cs" />+ <Compile Include="EntryPoints\KeepServerAwake.cs" /><Compile Include="EntryPoints\LibraryChangedNotifier.cs" /><Compile Include="EntryPoints\LoadRegistrations.cs" /><Compile Include="EntryPoints\RecordingNotifier.cs" />diff --git a/Emby.Server.Implementations/EntryPoints/KeepServerAwake.cs b/Emby.Server.Implementations/EntryPoints/KeepServerAwake.csnew file mode 100644index 0000000000..8ae85e3909--- /dev/null+++ b/Emby.Server.Implementations/EntryPoints/KeepServerAwake.cs@@ -0,0 +1,65 @@+using MediaBrowser.Controller;+using MediaBrowser.Controller.Plugins;+using MediaBrowser.Controller.Session;+using MediaBrowser.Model.Logging;+using System;+using System.Linq;+using MediaBrowser.Model.System;+using MediaBrowser.Model.Threading;++namespace Emby.Server.Implementations.EntryPoints+{+ public class KeepServerAwake : IServerEntryPoint+ {+ private readonly ISessionManager _sessionManager;+ private readonly ILogger _logger;+ private ITimer _timer;+ private readonly IServerApplicationHost _appHost;+ private readonly ITimerFactory _timerFactory;+ private readonly IPowerManagement _powerManagement;++ public KeepServerAwake(ISessionManager sessionManager, ILogger logger, IServerApplicationHost appHost, ITimerFactory timerFactory, IPowerManagement powerManagement)+ {+ _sessionManager = sessionManager;+ _logger = logger;+ _appHost = appHost;+ _timerFactory = timerFactory;+ _powerManagement = powerManagement;+ }++ public void Run()+ {+ _timer = _timerFactory.Create(OnTimerCallback, null, TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(5));+ }++ private void OnTimerCallback(object state)+ {+ var now = DateTime.UtcNow;++ try+ {+ if (_sessionManager.Sessions.Any(i => (now - i.LastActivityDate).TotalMinutes < 15))+ {+ _powerManagement.PreventSystemStandby();+ }+ else+ {+ _powerManagement.AllowSystemStandby();+ }+ }+ catch (Exception ex)+ {+ _logger.ErrorException("Error resetting system standby timer", ex);+ }+ }++ public void Dispose()+ {+ if (_timer != null)+ {+ _timer.Dispose();+ _timer = null;+ }+ }+ }+}diff --git a/Emby.Server.Implementations/Social/SharingManager.cs b/Emby.Server.Implementations/Social/SharingManager.csnew file mode 100644index 0000000000..54614c8797--- /dev/null+++ b/Emby.Server.Implementations/Social/SharingManager.cs@@ -0,0 +1,100 @@+using MediaBrowser.Common.Extensions;+using MediaBrowser.Controller;+using MediaBrowser.Controller.Configuration;+using MediaBrowser.Controller.Entities;+using MediaBrowser.Controller.Library;+using MediaBrowser.Model.Social;+using System;+using System.Threading.Tasks;++namespace Emby.Server.Implementations.Social+{+ public class SharingManager : ISharingManager+ {+ private readonly ISharingRepository _repository;+ private readonly IServerConfigurationManager _config;+ private readonly ILibraryManager _libraryManager;+ private readonly IServerApplicationHost _appHost;++ public SharingManager(ISharingRepository repository, IServerConfigurationManager config, ILibraryManager libraryManager, IServerApplicationHost appHost)+ {+ _repository = repository;+ _config = config;+ _libraryManager = libraryManager;+ _appHost = appHost;+ }++ public async Task<SocialShareInfo> CreateShare(string itemId, string userId)+ {+ if (string.IsNullOrWhiteSpace(itemId))+ {+ throw new ArgumentNullException("itemId");+ }+ if (string.IsNullOrWhiteSpace(userId))+ {+ throw new ArgumentNullException("userId");+ }++ var item = _libraryManager.GetItemById(itemId);++ if (item == null)+ {+ throw new ResourceNotFoundException();+ }++ var externalUrl = (await _appHost.GetSystemInfo().ConfigureAwait(false)).WanAddress;++ if (string.IsNullOrWhiteSpace(externalUrl))+ {+ throw new InvalidOperationException("No external server address is currently available.");+ }++ var info = new SocialShareInfo+ {+ Id = Guid.NewGuid().ToString("N"),+ ExpirationDate = DateTime.UtcNow.AddDays(_config.Configuration.SharingExpirationDays),+ ItemId = itemId,+ UserId = userId+ };++ AddShareInfo(info, externalUrl);++ await _repository.CreateShare(info).ConfigureAwait(false);++ return info;+ }++ private string GetTitle(BaseItem item)+ {+ return item.Name;+ }++ public SocialShareInfo GetShareInfo(string id)+ {+ var info = _repository.GetShareInfo(id);++ AddShareInfo(info, _appHost.GetSystemInfo().Result.WanAddress);++ return info;+ }++ private void AddShareInfo(SocialShareInfo info, string externalUrl)+ {+ info.ImageUrl = externalUrl + "/Social/Shares/Public/" + info.Id + "/Image";+ info.Url = externalUrl + "/emby/web/shared.html?id=" + info.Id;++ var item = _libraryManager.GetItemById(info.ItemId);++ if (item != null)+ {+ info.Overview = item.Overview;+ info.Name = GetTitle(item);+ }+ }++ public Task DeleteShare(string id)+ {+ return _repository.DeleteShare(id);+ }+ }+}diff --git a/MediaBrowser.Model/Configuration/ServerConfiguration.cs b/MediaBrowser.Model/Configuration/ServerConfiguration.csindex b1e52dc7b6..9715a624fb 100644--- a/MediaBrowser.Model/Configuration/ServerConfiguration.cs+++ b/MediaBrowser.Model/Configuration/ServerConfiguration.cs@@ -8,6 +8,9 @@ namespace MediaBrowser.Model.Configuration/// </summary>public class ServerConfiguration : BaseApplicationConfiguration{+ public const int DefaultHttpPort = 8096;+ public const int DefaultHttpsPort = 8920;+/// <summary>/// Gets or sets a value indicating whether [enable u pn p]./// </summary>@@ -225,10 +228,10 @@ namespace MediaBrowser.Model.ConfigurationEnableExternalContentInSuggestions = true;ImageSavingConvention = ImageSavingConvention.Compatible;- PublicPort = 8096;- PublicHttpsPort = 8920;- HttpServerPortNumber = 8096;- HttpsPortNumber = 8920;+ PublicPort = DefaultHttpPort;+ PublicHttpsPort = DefaultHttpsPort;+ HttpServerPortNumber = DefaultHttpPort;+ HttpsPortNumber = DefaultHttpsPort;EnableHttps = false;EnableDashboardResponseCaching = true;EnableDashboardResourceMinification = true;diff --git a/MediaBrowser.Model/MediaBrowser.Model.csproj b/MediaBrowser.Model/MediaBrowser.Model.csprojindex abf07e1dce..64472ca3a2 100644--- a/MediaBrowser.Model/MediaBrowser.Model.csproj+++ b/MediaBrowser.Model/MediaBrowser.Model.csproj@@ -145,6 +145,7 @@<Compile Include="Services\IHttpResult.cs" /><Compile Include="Social\ISharingRepository.cs" /><Compile Include="System\IEnvironmentInfo.cs" />+ <Compile Include="System\IPowerManagement.cs" /><Compile Include="Text\ITextEncoding.cs" /><Compile Include="Extensions\LinqExtensions.cs" /><Compile Include="FileOrganization\SmartMatchInfo.cs" />diff --git a/MediaBrowser.Model/Social/ISharingRepository.cs b/MediaBrowser.Model/Social/ISharingRepository.csnew file mode 100644index 0000000000..069b6e1fe2--- /dev/null+++ b/MediaBrowser.Model/Social/ISharingRepository.cs@@ -0,0 +1,15 @@+using System;+using System.Collections.Generic;+using System.Linq;+using System.Text;+using System.Threading.Tasks;++namespace MediaBrowser.Model.Social+{+ public interface ISharingRepository+ {+ Task CreateShare(SocialShareInfo info);+ Task DeleteShare(string id);+ SocialShareInfo GetShareInfo(string id);+ }+}diff --git a/MediaBrowser.Model/System/IPowerManagement.cs b/MediaBrowser.Model/System/IPowerManagement.csnew file mode 100644index 0000000000..91cae0d3e4--- /dev/null+++ b/MediaBrowser.Model/System/IPowerManagement.cs@@ -0,0 +1,9 @@++namespace MediaBrowser.Model.System+{+ public interface IPowerManagement+ {+ void PreventSystemStandby();+ void AllowSystemStandby();+ }+}diff --git a/MediaBrowser.Server.Mono/MediaBrowser.Server.Mono.csproj b/MediaBrowser.Server.Mono/MediaBrowser.Server.Mono.csprojindex fcaeb035d6..b7dbe4dc74 100644--- a/MediaBrowser.Server.Mono/MediaBrowser.Server.Mono.csproj+++ b/MediaBrowser.Server.Mono/MediaBrowser.Server.Mono.csproj@@ -100,6 +100,7 @@</Compile><Compile Include="Native\BaseMonoApp.cs" /><Compile Include="Native\DbConnector.cs" />+ <Compile Include="Native\PowerManagement.cs" /><Compile Include="Networking\CertificateGenerator.cs" /><Compile Include="Program.cs" /><Compile Include="Properties\AssemblyInfo.cs" />diff --git a/MediaBrowser.Server.Mono/Native/BaseMonoApp.cs b/MediaBrowser.Server.Mono/Native/BaseMonoApp.csindex 73e2686d29..46605e19f0 100644--- a/MediaBrowser.Server.Mono/Native/BaseMonoApp.cs+++ b/MediaBrowser.Server.Mono/Native/BaseMonoApp.cs@@ -68,16 +68,6 @@ namespace MediaBrowser.Server.Mono.Nativeget { return false; }}- public void PreventSystemStandby()- {-- }-- public void AllowSystemStandby()- {-- }-public List<Assembly> GetAssembliesWithParts(){var list = new List<Assembly>();diff --git a/MediaBrowser.Server.Mono/Native/PowerManagement.cs b/MediaBrowser.Server.Mono/Native/PowerManagement.csnew file mode 100644index 0000000000..0fdd4de808--- /dev/null+++ b/MediaBrowser.Server.Mono/Native/PowerManagement.cs@@ -0,0 +1,15 @@+using MediaBrowser.Model.System;++namespace MediaBrowser.Server.Mono.Native+{+ public class PowerManagement : IPowerManagement+ {+ public void PreventSystemStandby()+ {+ }++ public void AllowSystemStandby()+ {+ }+ }+}diff --git a/MediaBrowser.Server.Mono/Program.cs b/MediaBrowser.Server.Mono/Program.csindex 215ee44689..662d82ff0a 100644--- a/MediaBrowser.Server.Mono/Program.cs+++ b/MediaBrowser.Server.Mono/Program.cs@@ -80,7 +80,7 @@ namespace MediaBrowser.Server.Monovar nativeApp = new NativeApp(options, logManager.GetLogger("App"));- _appHost = new ApplicationHost(appPaths, logManager, options, fileSystem, "emby.mono.zip", nativeApp);+ _appHost = new ApplicationHost(appPaths, logManager, options, fileSystem, nativeApp, new PowerManagement(), "emby.mono.zip");if (options.ContainsOption("-v")){diff --git a/MediaBrowser.Server.Startup.Common/ApplicationHost.cs b/MediaBrowser.Server.Startup.Common/ApplicationHost.csindex 1c811ed113..c09e028ea8 100644--- a/MediaBrowser.Server.Startup.Common/ApplicationHost.cs+++ b/MediaBrowser.Server.Startup.Common/ApplicationHost.cs@@ -119,6 +119,7 @@ using Emby.Server.Implementations.Sync;using Emby.Server.Implementations.TV;using Emby.Server.Implementations.Updates;using MediaBrowser.Model.Activity;+using MediaBrowser.Model.Configuration;using MediaBrowser.Model.Dlna;using MediaBrowser.Model.Globalization;using MediaBrowser.Model.Net;@@ -260,26 +261,24 @@ namespace MediaBrowser.Server.Startup.Commoninternal INativeApp NativeApp { get; set; }+ internal IPowerManagement PowerManagement { get; private set; }+/// <summary>/// Initializes a new instance of the <see cref="ApplicationHost" /> class./// </summary>- /// <param name="applicationPaths">The application paths.</param>- /// <param name="logManager">The log manager.</param>- /// <param name="options">The options.</param>- /// <param name="fileSystem">The file system.</param>- /// <param name="releaseAssetFilename">The release asset filename.</param>- /// <param name="nativeApp">The native application.</param>public ApplicationHost(ServerApplicationPaths applicationPaths,ILogManager logManager,StartupOptions options,IFileSystem fileSystem,- string releaseAssetFilename,- INativeApp nativeApp)+ INativeApp nativeApp,+ IPowerManagement powerManagement,+ string releaseAssetFilename): base(applicationPaths, logManager, fileSystem){_startupOptions = options;_releaseAssetFilename = releaseAssetFilename;NativeApp = nativeApp;+ PowerManagement = powerManagement;SetBaseExceptionMessage();}@@ -485,6 +484,13 @@ namespace MediaBrowser.Server.Startup.CommonHttpPort = ServerConfigurationManager.Configuration.HttpServerPortNumber;HttpsPort = ServerConfigurationManager.Configuration.HttpsPortNumber;+ // Safeguard against invalid configuration+ if (HttpPort == HttpsPort)+ {+ HttpPort = ServerConfiguration.DefaultHttpPort;+ HttpsPort = ServerConfiguration.DefaultHttpsPort;+ }+return base.Init(progress);}@@ -535,6 +541,8 @@ namespace MediaBrowser.Server.Startup.Common{await base.RegisterResources(progress).ConfigureAwait(false);+ RegisterSingleInstance(PowerManagement);+SecurityManager = new PluginSecurityManager(this, HttpClient, JsonSerializer, ApplicationPaths, LogManager, FileSystemManager, CryptographyProvider);RegisterSingleInstance(SecurityManager);@@ -999,13 +1007,13 @@ namespace MediaBrowser.Server.Startup.Common{Logger.ErrorException("Error starting http server", ex);- if (HttpPort == 8096)+ if (HttpPort == ServerConfiguration.DefaultHttpPort){throw;}}- HttpPort = 8096;+ HttpPort = ServerConfiguration.DefaultHttpPort;try{diff --git a/MediaBrowser.Server.Startup.Common/EntryPoints/KeepServerAwake.cs b/MediaBrowser.Server.Startup.Common/EntryPoints/KeepServerAwake.csdeleted file mode 100644index 95b42afbf9..0000000000--- a/MediaBrowser.Server.Startup.Common/EntryPoints/KeepServerAwake.cs+++ /dev/null@@ -1,60 +0,0 @@-using MediaBrowser.Controller;-using MediaBrowser.Controller.Plugins;-using MediaBrowser.Controller.Session;-using MediaBrowser.Model.Logging;-using System;-using System.Linq;-using MediaBrowser.Server.Startup.Common.Threading;--namespace MediaBrowser.Server.Startup.Common.EntryPoints-{- public class KeepServerAwake : IServerEntryPoint- {- private readonly ISessionManager _sessionManager;- private readonly ILogger _logger;- private PeriodicTimer _timer;- private readonly IServerApplicationHost _appHost;-- public KeepServerAwake(ISessionManager sessionManager, ILogger logger, IServerApplicationHost appHost)- {- _sessionManager = sessionManager;- _logger = logger;- _appHost = appHost;- }-- public void Run()- {- _timer = new PeriodicTimer(obj =>- {- var now = DateTime.UtcNow;- var nativeApp = ((ApplicationHost)_appHost).NativeApp;-- try- {- if (_sessionManager.Sessions.Any(i => (now - i.LastActivityDate).TotalMinutes < 15))- {- nativeApp.PreventSystemStandby();- }- else- {- nativeApp.AllowSystemStandby();- }- }- catch (Exception ex)- {- _logger.ErrorException("Error resetting system standby timer", ex);- }-- }, null, TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(5));- }-- public void Dispose()- {- if (_timer != null)- {- _timer.Dispose();- _timer = null;- }- }- }-}diff --git a/MediaBrowser.Server.Startup.Common/INativeApp.cs b/MediaBrowser.Server.Startup.Common/INativeApp.csindex c56bb9b4bc..df29dfde42 100644--- a/MediaBrowser.Server.Startup.Common/INativeApp.cs+++ b/MediaBrowser.Server.Startup.Common/INativeApp.cs@@ -88,13 +88,6 @@ namespace MediaBrowser.Server.Startup.Common/// <returns>INetworkManager.</returns>INetworkManager CreateNetworkManager(ILogger logger);- /// <summary>- /// Prevents the system stand by.- /// </summary>- void PreventSystemStandby();-- void AllowSystemStandby();-FFMpegInstallInfo GetFfmpegInstallInfo();void LaunchUrl(string url);diff --git a/MediaBrowser.Server.Startup.Common/MediaBrowser.Server.Startup.Common.csproj b/MediaBrowser.Server.Startup.Common/MediaBrowser.Server.Startup.Common.csprojindex aca30aa2c4..97cdddf26d 100644--- a/MediaBrowser.Server.Startup.Common/MediaBrowser.Server.Startup.Common.csproj+++ b/MediaBrowser.Server.Startup.Common/MediaBrowser.Server.Startup.Common.csproj@@ -75,7 +75,6 @@<Compile Include="ApplicationHost.cs" /><Compile Include="ApplicationPathHelper.cs" /><Compile Include="Browser\BrowserLauncher.cs" />- <Compile Include="EntryPoints\KeepServerAwake.cs" /><Compile Include="EntryPoints\StartupWizard.cs" /><Compile Include="FFMpeg\FFMpegLoader.cs" /><Compile Include="FFMpeg\FFMpegInstallInfo.cs" />@@ -90,7 +89,6 @@<Compile Include="StartupOptions.cs" /><Compile Include="SystemEvents.cs" /><Compile Include="TextLocalizer.cs" />- <Compile Include="Threading\PeriodicTimer.cs" /><Compile Include="UnhandledExceptionWriter.cs" /></ItemGroup><ItemGroup>diff --git a/MediaBrowser.Server.Startup.Common/Threading/PeriodicTimer.cs b/MediaBrowser.Server.Startup.Common/Threading/PeriodicTimer.csdeleted file mode 100644index 3e898adfdf..0000000000--- a/MediaBrowser.Server.Startup.Common/Threading/PeriodicTimer.cs+++ /dev/null@@ -1,72 +0,0 @@-using System;-using System.Threading;-using Microsoft.Win32;--namespace MediaBrowser.Server.Startup.Common.Threading-{- public class PeriodicTimer : IDisposable- {- public Action<object> Callback { get; set; }- private Timer _timer;- private readonly object _state;- private readonly object _timerLock = new object();- private readonly TimeSpan _period;-- public PeriodicTimer(Action<object> callback, object state, TimeSpan dueTime, TimeSpan period)- {- if (callback == null)- {- throw new ArgumentNullException("callback");- }-- Callback = callback;- _period = period;- _state = state;-- StartTimer(dueTime);- }-- void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)- {- if (e.Mode == PowerModes.Resume)- {- DisposeTimer();- StartTimer(Timeout.InfiniteTimeSpan);- }- }-- private void TimerCallback(object state)- {- Callback(state);- }-- private void StartTimer(TimeSpan dueTime)- {- lock (_timerLock)- {- _timer = new Timer(TimerCallback, _state, dueTime, _period);-- Microsoft.Win32.SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;- }- }-- private void DisposeTimer()- {- Microsoft.Win32.SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;-- lock (_timerLock)- {- if (_timer != null)- {- _timer.Dispose();- _timer = null;- }- }- }-- public void Dispose()- {- DisposeTimer();- }- }-}diff --git a/MediaBrowser.ServerApplication/MainStartup.cs b/MediaBrowser.ServerApplication/MainStartup.csindex 18fa80fe2e..a3f4096367 100644--- a/MediaBrowser.ServerApplication/MainStartup.cs+++ b/MediaBrowser.ServerApplication/MainStartup.cs@@ -320,8 +320,9 @@ namespace MediaBrowser.ServerApplicationlogManager,options,fileSystem,- "emby.windows.zip",- nativeApp);+ nativeApp,+ new PowerManagement(),+ "emby.windows.zip");var initProgress = new Progress<double>();diff --git a/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj b/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csprojindex a306274e1d..666ba640e0 100644--- a/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj+++ b/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj@@ -127,6 +127,7 @@<Compile Include="Native\LnkShortcutHandler.cs" /><Compile Include="Native\DbConnector.cs" /><Compile Include="Native\LoopbackUtil.cs" />+ <Compile Include="Native\PowerManagement.cs" /><Compile Include="Native\Standby.cs" /><Compile Include="Native\ServerAuthorization.cs" /><Compile Include="Native\WindowsApp.cs" />diff --git a/MediaBrowser.ServerApplication/Native/PowerManagement.cs b/MediaBrowser.ServerApplication/Native/PowerManagement.csnew file mode 100644index 0000000000..0bd3db1dae--- /dev/null+++ b/MediaBrowser.ServerApplication/Native/PowerManagement.cs@@ -0,0 +1,17 @@+using MediaBrowser.Model.System;++namespace MediaBrowser.ServerApplication.Native+{+ public class PowerManagement : IPowerManagement+ {+ public void PreventSystemStandby()+ {+ MainStartup.Invoke(Standby.PreventSleep);+ }++ public void AllowSystemStandby()+ {+ MainStartup.Invoke(Standby.AllowSleep);+ }+ }+}diff --git a/MediaBrowser.ServerApplication/Native/WindowsApp.cs b/MediaBrowser.ServerApplication/Native/WindowsApp.csindex cf0dbfbe33..5bed8556e2 100644--- a/MediaBrowser.ServerApplication/Native/WindowsApp.cs+++ b/MediaBrowser.ServerApplication/Native/WindowsApp.cs@@ -139,16 +139,6 @@ namespace MediaBrowser.ServerApplication.Nativereturn new NetworkManager(logger);}- public void PreventSystemStandby()- {- MainStartup.Invoke(Standby.PreventSleep);- }-- public void AllowSystemStandby()- {- MainStartup.Invoke(Standby.AllowSleep);- }-public FFMpegInstallInfo GetFfmpegInstallInfo(){var info = new FFMpegInstallInfo();