Skip to content

Commit 68593d8

Browse files
committed
Fixed sessions
1 parent d0d19c2 commit 68593d8

37 files changed

Lines changed: 1887 additions & 909 deletions

Mpv.JsonIpc/Api.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ public async Task<TimeSpan> GetCurrentPosition()
4040
public async Task<TimeSpan> GetDuration()
4141
{
4242
var result = await _ipc.GetProperty<double>(Property.Duration);
43+
44+
int counter = 0;
45+
while (Math.Abs(result.Data) < 1)
46+
{
47+
await Task.Delay(100);
48+
result = await _ipc.GetProperty<double>(Property.Duration);
49+
50+
counter++;
51+
if (counter > 20)
52+
{
53+
break;
54+
}
55+
}
56+
4357
return TimeSpan.FromSeconds(result.Data);
4458
}
4559
}

SnackTime.Core/DependencyModule.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ protected override void Load(ContainerBuilder builder)
1818

1919
builder.RegisterType<SettingsService>().AsSelf();
2020
builder.RegisterType<SettingsRepo>().AsSelf();
21+
22+
builder.RegisterType<LocalSessionRepo>().As<ILocalSessionRepo>();
2123

2224
builder.RegisterType<TimeService>().AsSelf();
23-
builder.RegisterType<SessionService>().AsSelf();
25+
builder.RegisterType<SessionFactory>().AsSelf();
2426
builder.RegisterType<Queue<Item>>().AsSelf().SingleInstance();
2527

2628
builder.RegisterType<EpisodeFileLookupProvider>().AsSelf();
@@ -29,13 +31,6 @@ protected override void Load(ContainerBuilder builder)
2931
builder.RegisterType<ProcessManager>().AsSelf();
3032
builder.RegisterType<EpisodeBuilder>().AsSelf();
3133
}
32-
33-
public class SonarrConfig
34-
{
35-
public string Host { get; set; }
36-
public int Port { get; set; }
37-
public string ApiKey { get; set; }
38-
}
3934
}
4035

4136
public class SonarrFactory
@@ -55,9 +50,9 @@ public SonarrClient GetClient()
5550

5651
var settings = _settingsService.Get();
5752

58-
if (!string.IsNullOrWhiteSpace(settings.MediaServerAddress))
53+
if (!string.IsNullOrWhiteSpace(settings.Remote.MediaServerAddress))
5954
{
60-
host = settings.MediaServerAddress;
55+
host = settings.Remote.MediaServerAddress;
6156
}
6257

6358

SnackTime.Core/LocalSessionRepo.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using SnackTime.Core.Database;
4+
using SnackTime.Core.Session;
5+
6+
namespace SnackTime.Core
7+
{
8+
public class LocalSessionRepo : ILocalSessionRepo, IRemoteSessionRepo
9+
{
10+
private readonly DatabaseFactory _databaseFactory;
11+
12+
public LocalSessionRepo(DatabaseFactory databaseFactory)
13+
{
14+
_databaseFactory = databaseFactory;
15+
}
16+
17+
public Task UpsertSession(MediaServer.Storage.ProtoGenerated.Session session)
18+
{
19+
using (var db = _databaseFactory.GetRepository())
20+
{
21+
db.Upsert(session);
22+
}
23+
24+
return Task.CompletedTask;
25+
}
26+
27+
public Task<List<MediaServer.Storage.ProtoGenerated.Session>> GetAll()
28+
{
29+
using (var db = _databaseFactory.GetRepository())
30+
{
31+
var sessions = db.Fetch<MediaServer.Storage.ProtoGenerated.Session>();
32+
return Task.FromResult(sessions);
33+
}
34+
}
35+
}
36+
}

SnackTime.Core/Media/Episodes/EpisodeBuilder.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Linq;
3+
using System.Threading.Tasks;
34
using SnackTime.Core.Session;
45
using SnackTime.MediaServer.Models.ProtoGenerated;
56
using SnackTime.MediaServer.Storage.ProtoGenerated;
@@ -8,34 +9,38 @@ namespace SnackTime.Core.Media.Episodes
89
{
910
public class EpisodeBuilder
1011
{
11-
private readonly SessionService _sessionService;
12+
private readonly ISessionRepoFactory _sessionRepoFactory;
1213

13-
public EpisodeBuilder(SessionService sessionService)
14+
public EpisodeBuilder(ISessionRepoFactory sessionRepoFactory)
1415
{
15-
_sessionService = sessionService;
16+
_sessionRepoFactory = sessionRepoFactory;
1617
}
1718

18-
public List<Episode> Build(IEnumerable<SonarrSharp.Models.Episode> episodes)
19+
public async Task<List<Episode>> Build(IEnumerable<SonarrSharp.Models.Episode> episodes)
1920
{
20-
return episodes.Select(Build)
21+
var tasks = episodes.Select(Build).ToList();
22+
await Task.WhenAll(tasks);
23+
24+
return tasks.Select(task => task.Result)
2125
.OrderByDescending(episode => episode.SeasonNumber)
2226
.ToList();
2327
}
2428

25-
public Episode Build(SonarrSharp.Models.Episode episode)
29+
public async Task<Episode> Build(SonarrSharp.Models.Episode episode)
2630
{
2731
var mediaFileId = new MediaFileId
2832
{
2933
Provider = Providers.Sonarr,
3034
MediaId = episode.SeriesId,
3135
FileId = episode.EpisodeFileId,
3236
};
37+
var sessionRepo = await _sessionRepoFactory.GetRepo();
3338

34-
var allSessionsForCurrentEpisode = _sessionService.GetAll()
39+
var allSessionsForCurrentEpisode = (await sessionRepo.GetAll())
3540
.Where(session => session.MediaId == mediaFileId.ToString())
3641
.ToList();
3742

38-
var lastSession = allSessionsForCurrentEpisode.OrderBy(session => session.EndUTC).FirstOrDefault();
43+
var lastSession = allSessionsForCurrentEpisode.OrderByDescending(session => session.EndUTC).FirstOrDefault();
3944

4045
return new Episode
4146
{

SnackTime.Core/Media/Episodes/EpisodeProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ public EpisodeProvider(SonarrFactory sonarrFactory, EpisodeBuilder episodeBuilde
1919
public async Task<List<Episode>> GetEpisodesForSeriesById(int seriesId)
2020
{
2121
var episodes = await _client.Episode.GetEpisodes(seriesId);
22-
return _seriesBuilder.Build(episodes);
22+
return await _seriesBuilder.Build(episodes);
2323
}
2424

2525
public async Task<Episode> GetEpisodeById(int id)
2626
{
2727
var episodes = await _client.Episode.GetEpisode(id);
28-
return _seriesBuilder.Build(episodes);
28+
return await _seriesBuilder.Build(episodes);
2929
}
3030
}
3131
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
4+
namespace SnackTime.Core.Session
5+
{
6+
public interface ISessionRepo
7+
{
8+
Task UpsertSession(MediaServer.Storage.ProtoGenerated.Session session);
9+
Task<List<MediaServer.Storage.ProtoGenerated.Session>> GetAll();
10+
}
11+
12+
public interface ILocalSessionRepo : ISessionRepo
13+
{
14+
}
15+
16+
public interface IRemoteSessionRepo : ISessionRepo
17+
{
18+
}
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Threading.Tasks;
2+
3+
namespace SnackTime.Core.Session
4+
{
5+
public interface ISessionRepoFactory
6+
{
7+
Task<ISessionRepo> GetRepo();
8+
}
9+
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
using System;
2-
using System.Collections.Generic;
2+
using System.Linq;
33
using SnackTime.Core.Database;
4+
using SnackTime.MediaServer.Service.Session;
45
using SnackTime.MediaServer.Storage.ProtoGenerated;
56

67
namespace SnackTime.Core.Session
78
{
8-
public class SessionService
9+
public class SessionFactory
910
{
10-
private readonly DatabaseFactory _databaseFactory;
11-
private readonly TimeService _timeService;
11+
private readonly TimeService _timeService;
1212

13-
public SessionService(DatabaseFactory databaseFactory, TimeService timeService)
13+
public SessionFactory(TimeService timeService)
1414
{
15-
_databaseFactory = databaseFactory;
1615
_timeService = timeService;
1716
}
1817

@@ -30,32 +29,16 @@ public MediaServer.Storage.ProtoGenerated.Session CreateNewSession
3029
timeWatched.EndPostionInSec = startPosition.Value.TotalSeconds;
3130
}
3231

33-
var sessionId = Guid.NewGuid().ToString("N");
34-
return new MediaServer.Storage.ProtoGenerated.Session
32+
var session = new MediaServer.Storage.ProtoGenerated.Session
3533
{
36-
Id = sessionId,
34+
Id = Guid.NewGuid().ToString("N"),
3735
MediaId = mediaFileId.ToString(),
3836
Duration = timeWatched,
3937
StartUTC = _timeService.GetCurrentTimeAsUnixSeconds(),
4038
EndUTC = _timeService.GetCurrentTimeAsUnixSeconds(),
4139
MediaLenghtInSec = mediaDuration.TotalSeconds,
4240
};
43-
}
44-
45-
public void UpsertSession(MediaServer.Storage.ProtoGenerated.Session session)
46-
{
47-
using (var db = _databaseFactory.GetRepository())
48-
{
49-
db.Upsert(session);
50-
}
51-
}
52-
53-
public IList<MediaServer.Storage.ProtoGenerated.Session> GetAll()
54-
{
55-
using (var db = _databaseFactory.GetRepository())
56-
{
57-
return db.Fetch<MediaServer.Storage.ProtoGenerated.Session>();
58-
}
41+
return session;
5942
}
6043
}
6144
}

SnackTime.Core/Settings/SettingsService.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using Microsoft.Extensions.Logging;
2+
using SnackTime.App.Settings.ProtoGenerated;
3+
using SonarrSharp.Models;
24

35
namespace SnackTime.Core.Settings
46
{
@@ -25,7 +27,19 @@ public void Save(App.Settings.ProtoGenerated.Settings settings)
2527

2628
public App.Settings.ProtoGenerated.Settings Get()
2729
{
28-
return _settingsRepo.Get()?.Value ?? new App.Settings.ProtoGenerated.Settings();
30+
var setting = _settingsRepo.Get()?.Value ?? new App.Settings.ProtoGenerated.Settings();
31+
32+
if (setting.Remote == null)
33+
{
34+
setting.Remote = new Remote();
35+
}
36+
37+
if (setting.System == null)
38+
{
39+
setting.System = new LocalSystem();
40+
}
41+
42+
return setting;
2943
}
3044
}
3145
}

SnackTime.MediaServer.Proto/csharp-grpc/File-serviceGrpc.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010
namespace SnackTime.MediaServer.Service.File {
1111
public static partial class File
1212
{
13-
static readonly string __ServiceName = "snacktime.series.file.File";
13+
static readonly string __ServiceName = "snacktime.file.service.File";
1414

15-
static readonly grpc::Marshaller<global::SnackTime.MediaServer.Service.File.DownloadFileRequest> __Marshaller_snacktime_series_file_DownloadFileRequest = grpc::Marshallers.Create((arg) => global::Google.Protobuf.MessageExtensions.ToByteArray(arg), global::SnackTime.MediaServer.Service.File.DownloadFileRequest.Parser.ParseFrom);
16-
static readonly grpc::Marshaller<global::SnackTime.MediaServer.Service.File.ResponseDownloadFile> __Marshaller_snacktime_series_file_ResponseDownloadFile = grpc::Marshallers.Create((arg) => global::Google.Protobuf.MessageExtensions.ToByteArray(arg), global::SnackTime.MediaServer.Service.File.ResponseDownloadFile.Parser.ParseFrom);
15+
static readonly grpc::Marshaller<global::SnackTime.MediaServer.Service.File.DownloadFileRequest> __Marshaller_snacktime_file_service_DownloadFileRequest = grpc::Marshallers.Create((arg) => global::Google.Protobuf.MessageExtensions.ToByteArray(arg), global::SnackTime.MediaServer.Service.File.DownloadFileRequest.Parser.ParseFrom);
16+
static readonly grpc::Marshaller<global::SnackTime.MediaServer.Service.File.ResponseDownloadFile> __Marshaller_snacktime_file_service_ResponseDownloadFile = grpc::Marshallers.Create((arg) => global::Google.Protobuf.MessageExtensions.ToByteArray(arg), global::SnackTime.MediaServer.Service.File.ResponseDownloadFile.Parser.ParseFrom);
1717

1818
static readonly grpc::Method<global::SnackTime.MediaServer.Service.File.DownloadFileRequest, global::SnackTime.MediaServer.Service.File.ResponseDownloadFile> __Method_Download = new grpc::Method<global::SnackTime.MediaServer.Service.File.DownloadFileRequest, global::SnackTime.MediaServer.Service.File.ResponseDownloadFile>(
1919
grpc::MethodType.ServerStreaming,
2020
__ServiceName,
2121
"Download",
22-
__Marshaller_snacktime_series_file_DownloadFileRequest,
23-
__Marshaller_snacktime_series_file_ResponseDownloadFile);
22+
__Marshaller_snacktime_file_service_DownloadFileRequest,
23+
__Marshaller_snacktime_file_service_ResponseDownloadFile);
2424

2525
/// <summary>Service descriptor</summary>
2626
public static global::Google.Protobuf.Reflection.ServiceDescriptor Descriptor

0 commit comments

Comments
 (0)