LiveTvManager.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Controller;
  4. using MediaBrowser.Controller.Drawing;
  5. using MediaBrowser.Controller.Dto;
  6. using MediaBrowser.Controller.Entities;
  7. using MediaBrowser.Controller.Library;
  8. using MediaBrowser.Controller.LiveTv;
  9. using MediaBrowser.Controller.Localization;
  10. using MediaBrowser.Controller.Persistence;
  11. using MediaBrowser.Model.LiveTv;
  12. using MediaBrowser.Model.Logging;
  13. using MediaBrowser.Model.Querying;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.IO;
  17. using System.Linq;
  18. using System.Threading;
  19. using System.Threading.Tasks;
  20. namespace MediaBrowser.Server.Implementations.LiveTv
  21. {
  22. /// <summary>
  23. /// Class LiveTvManager
  24. /// </summary>
  25. public class LiveTvManager : ILiveTvManager
  26. {
  27. private readonly IServerApplicationPaths _appPaths;
  28. private readonly IFileSystem _fileSystem;
  29. private readonly ILogger _logger;
  30. private readonly IItemRepository _itemRepo;
  31. private readonly IUserManager _userManager;
  32. private readonly ILocalizationManager _localization;
  33. private readonly LiveTvDtoService _tvDtoService;
  34. private readonly List<ILiveTvService> _services = new List<ILiveTvService>();
  35. private Dictionary<Guid, LiveTvChannel> _channels = new Dictionary<Guid, LiveTvChannel>();
  36. private Dictionary<Guid, LiveTvProgram> _programs = new Dictionary<Guid, LiveTvProgram>();
  37. public LiveTvManager(IServerApplicationPaths appPaths, IFileSystem fileSystem, ILogger logger, IItemRepository itemRepo, IImageProcessor imageProcessor, ILocalizationManager localization, IUserDataManager userDataManager, IDtoService dtoService, IUserManager userManager)
  38. {
  39. _appPaths = appPaths;
  40. _fileSystem = fileSystem;
  41. _logger = logger;
  42. _itemRepo = itemRepo;
  43. _localization = localization;
  44. _userManager = userManager;
  45. _tvDtoService = new LiveTvDtoService(dtoService, userDataManager, imageProcessor, logger);
  46. }
  47. /// <summary>
  48. /// Gets the services.
  49. /// </summary>
  50. /// <value>The services.</value>
  51. public IReadOnlyList<ILiveTvService> Services
  52. {
  53. get { return _services; }
  54. }
  55. public ILiveTvService ActiveService { get; private set; }
  56. /// <summary>
  57. /// Adds the parts.
  58. /// </summary>
  59. /// <param name="services">The services.</param>
  60. public void AddParts(IEnumerable<ILiveTvService> services)
  61. {
  62. _services.AddRange(services);
  63. ActiveService = _services.FirstOrDefault();
  64. }
  65. public Task<QueryResult<ChannelInfoDto>> GetChannels(ChannelQuery query, CancellationToken cancellationToken)
  66. {
  67. var user = string.IsNullOrEmpty(query.UserId) ? null : _userManager.GetUserById(new Guid(query.UserId));
  68. IEnumerable<LiveTvChannel> channels = _channels.Values;
  69. if (user != null)
  70. {
  71. channels = channels
  72. .Where(i => i.IsParentalAllowed(user, _localization))
  73. .OrderBy(i =>
  74. {
  75. double number = 0;
  76. if (!string.IsNullOrEmpty(i.ChannelInfo.Number))
  77. {
  78. double.TryParse(i.ChannelInfo.Number, out number);
  79. }
  80. return number;
  81. });
  82. }
  83. var returnChannels = channels.OrderBy(i =>
  84. {
  85. double number = 0;
  86. if (!string.IsNullOrEmpty(i.ChannelInfo.Number))
  87. {
  88. double.TryParse(i.ChannelInfo.Number, out number);
  89. }
  90. return number;
  91. }).ThenBy(i => i.Name)
  92. .Select(i => _tvDtoService.GetChannelInfoDto(i, user))
  93. .ToArray();
  94. var result = new QueryResult<ChannelInfoDto>
  95. {
  96. Items = returnChannels,
  97. TotalRecordCount = returnChannels.Length
  98. };
  99. return Task.FromResult(result);
  100. }
  101. public LiveTvChannel GetInternalChannel(string id)
  102. {
  103. var guid = new Guid(id);
  104. LiveTvChannel channel = null;
  105. _channels.TryGetValue(guid, out channel);
  106. return channel;
  107. }
  108. public LiveTvProgram GetInternalProgram(string id)
  109. {
  110. var guid = new Guid(id);
  111. LiveTvProgram obj = null;
  112. _programs.TryGetValue(guid, out obj);
  113. return obj;
  114. }
  115. public async Task<LiveTvRecording> GetInternalRecording(string id, CancellationToken cancellationToken)
  116. {
  117. var service = ActiveService;
  118. var recordings = await service.GetRecordingsAsync(cancellationToken).ConfigureAwait(false);
  119. var recording = recordings.FirstOrDefault(i => _tvDtoService.GetInternalRecordingId(service.Name, i.Id) == new Guid(id));
  120. return await GetRecording(recording, service.Name, cancellationToken).ConfigureAwait(false);
  121. }
  122. public async Task<StreamResponseInfo> GetRecordingStream(string id, CancellationToken cancellationToken)
  123. {
  124. var service = ActiveService;
  125. var recordings = await service.GetRecordingsAsync(cancellationToken).ConfigureAwait(false);
  126. var recording = recordings.FirstOrDefault(i => _tvDtoService.GetInternalRecordingId(service.Name, i.Id) == new Guid(id));
  127. return await service.GetRecordingStream(recording.Id, cancellationToken).ConfigureAwait(false);
  128. }
  129. private async Task<LiveTvChannel> GetChannel(ChannelInfo channelInfo, string serviceName, CancellationToken cancellationToken)
  130. {
  131. var path = Path.Combine(_appPaths.ItemsByNamePath, "channels", _fileSystem.GetValidFilename(serviceName), _fileSystem.GetValidFilename(channelInfo.Name));
  132. var fileInfo = new DirectoryInfo(path);
  133. var isNew = false;
  134. if (!fileInfo.Exists)
  135. {
  136. Directory.CreateDirectory(path);
  137. fileInfo = new DirectoryInfo(path);
  138. if (!fileInfo.Exists)
  139. {
  140. throw new IOException("Path not created: " + path);
  141. }
  142. isNew = true;
  143. }
  144. var id = _tvDtoService.GetInternalChannelId(serviceName, channelInfo.Id);
  145. var item = _itemRepo.RetrieveItem(id) as LiveTvChannel;
  146. if (item == null)
  147. {
  148. item = new LiveTvChannel
  149. {
  150. Name = channelInfo.Name,
  151. Id = id,
  152. DateCreated = _fileSystem.GetCreationTimeUtc(fileInfo),
  153. DateModified = _fileSystem.GetLastWriteTimeUtc(fileInfo),
  154. Path = path
  155. };
  156. isNew = true;
  157. }
  158. item.ChannelInfo = channelInfo;
  159. item.ServiceName = serviceName;
  160. // Set this now so we don't cause additional file system access during provider executions
  161. item.ResetResolveArgs(fileInfo);
  162. await item.RefreshMetadata(cancellationToken, forceSave: isNew, resetResolveArgs: false);
  163. return item;
  164. }
  165. private async Task<LiveTvProgram> GetProgram(ProgramInfo info, ChannelType channelType, string serviceName, CancellationToken cancellationToken)
  166. {
  167. var isNew = false;
  168. var id = _tvDtoService.GetInternalProgramId(serviceName, info.Id);
  169. var item = _itemRepo.RetrieveItem(id) as LiveTvProgram;
  170. if (item == null)
  171. {
  172. item = new LiveTvProgram
  173. {
  174. Name = info.Name,
  175. Id = id,
  176. DateCreated = DateTime.UtcNow,
  177. DateModified = DateTime.UtcNow
  178. };
  179. isNew = true;
  180. }
  181. item.ChannelType = channelType;
  182. item.ProgramInfo = info;
  183. item.ServiceName = serviceName;
  184. await item.RefreshMetadata(cancellationToken, forceSave: isNew, resetResolveArgs: false);
  185. return item;
  186. }
  187. private async Task<LiveTvRecording> GetRecording(RecordingInfo info, string serviceName, CancellationToken cancellationToken)
  188. {
  189. var isNew = false;
  190. var id = _tvDtoService.GetInternalRecordingId(serviceName, info.Id);
  191. var item = _itemRepo.RetrieveItem(id) as LiveTvRecording;
  192. if (item == null)
  193. {
  194. item = new LiveTvRecording
  195. {
  196. Name = info.Name,
  197. Id = id,
  198. DateCreated = DateTime.UtcNow,
  199. DateModified = DateTime.UtcNow
  200. };
  201. isNew = true;
  202. }
  203. item.RecordingInfo = info;
  204. item.ServiceName = serviceName;
  205. await item.RefreshMetadata(cancellationToken, forceSave: isNew, resetResolveArgs: false);
  206. return item;
  207. }
  208. public async Task<ProgramInfoDto> GetProgram(string id, CancellationToken cancellationToken, User user = null)
  209. {
  210. var program = GetInternalProgram(id);
  211. var dto = _tvDtoService.GetProgramInfoDto(program, user);
  212. await AddRecordingInfo(new[] { dto }, cancellationToken).ConfigureAwait(false);
  213. return dto;
  214. }
  215. public async Task<QueryResult<ProgramInfoDto>> GetPrograms(ProgramQuery query, CancellationToken cancellationToken)
  216. {
  217. IEnumerable<LiveTvProgram> programs = _programs.Values;
  218. if (query.ChannelIdList.Length > 0)
  219. {
  220. var guids = query.ChannelIdList.Select(i => new Guid(i)).ToList();
  221. var serviceName = ActiveService.Name;
  222. programs = programs.Where(i =>
  223. {
  224. var programChannelId = i.ProgramInfo.ChannelId;
  225. var internalProgramChannelId = _tvDtoService.GetInternalChannelId(serviceName, programChannelId);
  226. return guids.Contains(internalProgramChannelId);
  227. });
  228. }
  229. var user = string.IsNullOrEmpty(query.UserId) ? null : _userManager.GetUserById(new Guid(query.UserId));
  230. if (user != null)
  231. {
  232. programs = programs.Where(i => i.IsParentalAllowed(user, _localization));
  233. }
  234. var returnArray = programs
  235. .OrderBy(i => i.ProgramInfo.StartDate)
  236. .Select(i => _tvDtoService.GetProgramInfoDto(i, user))
  237. .ToArray();
  238. await AddRecordingInfo(returnArray, cancellationToken).ConfigureAwait(false);
  239. var result = new QueryResult<ProgramInfoDto>
  240. {
  241. Items = returnArray,
  242. TotalRecordCount = returnArray.Length
  243. };
  244. return result;
  245. }
  246. private async Task AddRecordingInfo(IEnumerable<ProgramInfoDto> programs, CancellationToken cancellationToken)
  247. {
  248. var timers = await ActiveService.GetTimersAsync(cancellationToken).ConfigureAwait(false);
  249. var timerList = timers.ToList();
  250. foreach (var program in programs)
  251. {
  252. var timer = timerList.FirstOrDefault(i => string.Equals(i.ProgramId, program.ExternalId, StringComparison.OrdinalIgnoreCase));
  253. if (timer != null)
  254. {
  255. program.TimerId = _tvDtoService.GetInternalTimerId(program.ServiceName, timer.Id)
  256. .ToString("N");
  257. if (!string.IsNullOrEmpty(timer.SeriesTimerId))
  258. {
  259. program.SeriesTimerId = _tvDtoService.GetInternalSeriesTimerId(program.ServiceName, timer.SeriesTimerId)
  260. .ToString("N");
  261. }
  262. }
  263. }
  264. }
  265. internal async Task RefreshChannels(IProgress<double> progress, CancellationToken cancellationToken)
  266. {
  267. // Avoid implicitly captured closure
  268. var service = ActiveService;
  269. if (service == null)
  270. {
  271. progress.Report(100);
  272. return;
  273. }
  274. progress.Report(10);
  275. var allChannels = await GetChannels(service, cancellationToken).ConfigureAwait(false);
  276. var allChannelsList = allChannels.ToList();
  277. var list = new List<LiveTvChannel>();
  278. var programs = new List<LiveTvProgram>();
  279. var numComplete = 0;
  280. foreach (var channelInfo in allChannelsList)
  281. {
  282. try
  283. {
  284. var item = await GetChannel(channelInfo.Item2, channelInfo.Item1, cancellationToken).ConfigureAwait(false);
  285. var channelPrograms = await service.GetProgramsAsync(channelInfo.Item2.Id, cancellationToken).ConfigureAwait(false);
  286. var programTasks = channelPrograms.Select(program => GetProgram(program, item.ChannelInfo.ChannelType, service.Name, cancellationToken));
  287. var programEntities = await Task.WhenAll(programTasks).ConfigureAwait(false);
  288. programs.AddRange(programEntities);
  289. list.Add(item);
  290. }
  291. catch (OperationCanceledException)
  292. {
  293. throw;
  294. }
  295. catch (Exception ex)
  296. {
  297. _logger.ErrorException("Error getting channel information for {0}", ex, channelInfo.Item2.Name);
  298. }
  299. numComplete++;
  300. double percent = numComplete;
  301. percent /= allChannelsList.Count;
  302. progress.Report(90 * percent + 10);
  303. }
  304. _programs = programs.ToDictionary(i => i.Id);
  305. _channels = list.ToDictionary(i => i.Id);
  306. }
  307. private async Task<IEnumerable<Tuple<string, ChannelInfo>>> GetChannels(ILiveTvService service, CancellationToken cancellationToken)
  308. {
  309. var channels = await service.GetChannelsAsync(cancellationToken).ConfigureAwait(false);
  310. return channels.Select(i => new Tuple<string, ChannelInfo>(service.Name, i));
  311. }
  312. public async Task<QueryResult<RecordingInfoDto>> GetRecordings(RecordingQuery query, CancellationToken cancellationToken)
  313. {
  314. var service = ActiveService;
  315. var user = string.IsNullOrEmpty(query.UserId) ? null : _userManager.GetUserById(new Guid(query.UserId));
  316. var list = new List<RecordingInfo>();
  317. var recordings = await service.GetRecordingsAsync(cancellationToken).ConfigureAwait(false);
  318. list.AddRange(recordings);
  319. if (!string.IsNullOrEmpty(query.ChannelId))
  320. {
  321. list = list
  322. .Where(i => _tvDtoService.GetInternalChannelId(service.Name, i.ChannelId) == new Guid(query.ChannelId))
  323. .ToList();
  324. }
  325. if (!string.IsNullOrEmpty(query.Id))
  326. {
  327. list = list
  328. .Where(i => _tvDtoService.GetInternalRecordingId(service.Name, i.Id) == new Guid(query.Id))
  329. .ToList();
  330. }
  331. var entities = await GetEntities(list, service.Name, cancellationToken).ConfigureAwait(false);
  332. if (user != null)
  333. {
  334. entities = entities.Where(i => i.IsParentalAllowed(user, _localization)).ToArray();
  335. }
  336. var returnArray = entities
  337. .Select(i =>
  338. {
  339. var channel = string.IsNullOrEmpty(i.RecordingInfo.ChannelId) ? null : GetInternalChannel(_tvDtoService.GetInternalChannelId(service.Name, i.RecordingInfo.ChannelId).ToString("N"));
  340. return _tvDtoService.GetRecordingInfoDto(i, channel, service, user);
  341. })
  342. .OrderByDescending(i => i.StartDate)
  343. .ToArray();
  344. return new QueryResult<RecordingInfoDto>
  345. {
  346. Items = returnArray,
  347. TotalRecordCount = returnArray.Length
  348. };
  349. }
  350. private Task<LiveTvRecording[]> GetEntities(IEnumerable<RecordingInfo> recordings, string serviceName, CancellationToken cancellationToken)
  351. {
  352. var tasks = recordings.Select(i => GetRecording(i, serviceName, cancellationToken));
  353. return Task.WhenAll(tasks);
  354. }
  355. private IEnumerable<ILiveTvService> GetServices(string serviceName, string channelId)
  356. {
  357. IEnumerable<ILiveTvService> services = _services;
  358. if (string.IsNullOrEmpty(serviceName) && !string.IsNullOrEmpty(channelId))
  359. {
  360. var channel = GetInternalChannel(channelId);
  361. if (channel != null)
  362. {
  363. serviceName = channel.ServiceName;
  364. }
  365. }
  366. if (!string.IsNullOrEmpty(serviceName))
  367. {
  368. services = services.Where(i => string.Equals(i.Name, serviceName, StringComparison.OrdinalIgnoreCase));
  369. }
  370. return services;
  371. }
  372. public async Task<QueryResult<TimerInfoDto>> GetTimers(TimerQuery query, CancellationToken cancellationToken)
  373. {
  374. var service = ActiveService;
  375. var timers = await service.GetTimersAsync(cancellationToken).ConfigureAwait(false);
  376. if (!string.IsNullOrEmpty(query.ChannelId))
  377. {
  378. var guid = new Guid(query.ChannelId);
  379. timers = timers.Where(i => guid == _tvDtoService.GetInternalChannelId(service.Name, i.ChannelId));
  380. }
  381. var returnArray = timers
  382. .Select(i =>
  383. {
  384. var program = string.IsNullOrEmpty(i.ProgramId) ? null : GetInternalProgram(_tvDtoService.GetInternalProgramId(service.Name, i.ProgramId).ToString("N"));
  385. var channel = string.IsNullOrEmpty(i.ChannelId) ? null : GetInternalChannel(_tvDtoService.GetInternalChannelId(service.Name, i.ChannelId).ToString("N"));
  386. return _tvDtoService.GetTimerInfoDto(i, service, program, channel);
  387. })
  388. .OrderBy(i => i.StartDate)
  389. .ToArray();
  390. return new QueryResult<TimerInfoDto>
  391. {
  392. Items = returnArray,
  393. TotalRecordCount = returnArray.Length
  394. };
  395. }
  396. public async Task DeleteRecording(string recordingId)
  397. {
  398. var recording = await GetRecording(recordingId, CancellationToken.None).ConfigureAwait(false);
  399. if (recording == null)
  400. {
  401. throw new ResourceNotFoundException(string.Format("Recording with Id {0} not found", recordingId));
  402. }
  403. var service = GetServices(recording.ServiceName, null)
  404. .First();
  405. await service.DeleteRecordingAsync(recording.ExternalId, CancellationToken.None).ConfigureAwait(false);
  406. }
  407. public async Task CancelTimer(string id)
  408. {
  409. var timer = await GetTimer(id, CancellationToken.None).ConfigureAwait(false);
  410. if (timer == null)
  411. {
  412. throw new ResourceNotFoundException(string.Format("Timer with Id {0} not found", id));
  413. }
  414. var service = GetServices(timer.ServiceName, null)
  415. .First();
  416. await service.CancelTimerAsync(timer.ExternalId, CancellationToken.None).ConfigureAwait(false);
  417. }
  418. public async Task CancelSeriesTimer(string id)
  419. {
  420. var timer = await GetSeriesTimer(id, CancellationToken.None).ConfigureAwait(false);
  421. if (timer == null)
  422. {
  423. throw new ResourceNotFoundException(string.Format("Timer with Id {0} not found", id));
  424. }
  425. var service = GetServices(timer.ServiceName, null)
  426. .First();
  427. await service.CancelSeriesTimerAsync(timer.ExternalId, CancellationToken.None).ConfigureAwait(false);
  428. }
  429. public async Task<RecordingInfoDto> GetRecording(string id, CancellationToken cancellationToken, User user = null)
  430. {
  431. var results = await GetRecordings(new RecordingQuery
  432. {
  433. UserId = user == null ? null : user.Id.ToString("N"),
  434. Id = id
  435. }, cancellationToken).ConfigureAwait(false);
  436. return results.Items.FirstOrDefault();
  437. }
  438. public async Task<TimerInfoDto> GetTimer(string id, CancellationToken cancellationToken)
  439. {
  440. var results = await GetTimers(new TimerQuery(), cancellationToken).ConfigureAwait(false);
  441. return results.Items.FirstOrDefault(i => string.Equals(i.Id, id, StringComparison.CurrentCulture));
  442. }
  443. public async Task<SeriesTimerInfoDto> GetSeriesTimer(string id, CancellationToken cancellationToken)
  444. {
  445. var results = await GetSeriesTimers(new SeriesTimerQuery(), cancellationToken).ConfigureAwait(false);
  446. return results.Items.FirstOrDefault(i => string.Equals(i.Id, id, StringComparison.CurrentCulture));
  447. }
  448. public async Task<QueryResult<SeriesTimerInfoDto>> GetSeriesTimers(SeriesTimerQuery query, CancellationToken cancellationToken)
  449. {
  450. var service = ActiveService;
  451. var timers = await service.GetSeriesTimersAsync(cancellationToken).ConfigureAwait(false);
  452. var returnArray = timers
  453. .Select(i =>
  454. {
  455. string channelName = null;
  456. if (!string.IsNullOrEmpty(i.ChannelId))
  457. {
  458. var internalChannelId = _tvDtoService.GetInternalChannelId(service.Name, i.ChannelId);
  459. var channel = GetInternalChannel(internalChannelId.ToString("N"));
  460. channelName = channel == null ? null : channel.ChannelInfo.Name;
  461. }
  462. return _tvDtoService.GetSeriesTimerInfoDto(i, service, channelName);
  463. })
  464. .OrderByDescending(i => i.StartDate)
  465. .ToArray();
  466. return new QueryResult<SeriesTimerInfoDto>
  467. {
  468. Items = returnArray,
  469. TotalRecordCount = returnArray.Length
  470. };
  471. }
  472. public Task<ChannelInfoDto> GetChannel(string id, CancellationToken cancellationToken, User user = null)
  473. {
  474. var channel = GetInternalChannel(id);
  475. var dto = _tvDtoService.GetChannelInfoDto(channel, user);
  476. return Task.FromResult(dto);
  477. }
  478. public async Task<SeriesTimerInfoDto> GetNewTimerDefaults(CancellationToken cancellationToken)
  479. {
  480. var service = ActiveService;
  481. var info = await service.GetNewTimerDefaultsAsync(cancellationToken).ConfigureAwait(false);
  482. var obj = _tvDtoService.GetSeriesTimerInfoDto(info, service, null);
  483. obj.Id = obj.ExternalId = string.Empty;
  484. return obj;
  485. }
  486. public async Task<SeriesTimerInfoDto> GetNewTimerDefaults(string programId, CancellationToken cancellationToken)
  487. {
  488. var info = await GetNewTimerDefaults(cancellationToken).ConfigureAwait(false);
  489. var program = await GetProgram(programId, cancellationToken).ConfigureAwait(false);
  490. info.Days = new List<DayOfWeek>
  491. {
  492. program.StartDate.ToLocalTime().DayOfWeek
  493. };
  494. info.DayPattern = _tvDtoService.GetDayPattern(info.Days);
  495. info.Name = program.Name;
  496. info.ChannelId = program.ChannelId;
  497. info.ChannelName = program.ChannelName;
  498. info.EndDate = program.EndDate;
  499. info.StartDate = program.StartDate;
  500. info.Name = program.Name;
  501. info.Overview = program.Overview;
  502. info.ProgramId = program.Id;
  503. info.ExternalProgramId = program.ExternalId;
  504. return info;
  505. }
  506. public async Task CreateTimer(TimerInfoDto timer, CancellationToken cancellationToken)
  507. {
  508. var service = string.IsNullOrEmpty(timer.ServiceName) ? ActiveService : GetServices(timer.ServiceName, null).First();
  509. var info = await _tvDtoService.GetTimerInfo(timer, true, this, cancellationToken).ConfigureAwait(false);
  510. // Set priority from default values
  511. var defaultValues = await service.GetNewTimerDefaultsAsync(cancellationToken).ConfigureAwait(false);
  512. info.Priority = defaultValues.Priority;
  513. await service.CreateTimerAsync(info, cancellationToken).ConfigureAwait(false);
  514. }
  515. public async Task CreateSeriesTimer(SeriesTimerInfoDto timer, CancellationToken cancellationToken)
  516. {
  517. var service = string.IsNullOrEmpty(timer.ServiceName) ? ActiveService : GetServices(timer.ServiceName, null).First();
  518. var info = await _tvDtoService.GetSeriesTimerInfo(timer, true, this, cancellationToken).ConfigureAwait(false);
  519. // Set priority from default values
  520. var defaultValues = await service.GetNewTimerDefaultsAsync(cancellationToken).ConfigureAwait(false);
  521. info.Priority = defaultValues.Priority;
  522. await service.CreateSeriesTimerAsync(info, cancellationToken).ConfigureAwait(false);
  523. }
  524. public async Task UpdateTimer(TimerInfoDto timer, CancellationToken cancellationToken)
  525. {
  526. var info = await _tvDtoService.GetTimerInfo(timer, false, this, cancellationToken).ConfigureAwait(false);
  527. var service = string.IsNullOrEmpty(timer.ServiceName) ? ActiveService : GetServices(timer.ServiceName, null).First();
  528. await service.UpdateTimerAsync(info, cancellationToken).ConfigureAwait(false);
  529. }
  530. public async Task UpdateSeriesTimer(SeriesTimerInfoDto timer, CancellationToken cancellationToken)
  531. {
  532. var info = await _tvDtoService.GetSeriesTimerInfo(timer, false, this, cancellationToken).ConfigureAwait(false);
  533. var service = string.IsNullOrEmpty(timer.ServiceName) ? ActiveService : GetServices(timer.ServiceName, null).First();
  534. await service.UpdateSeriesTimerAsync(info, cancellationToken).ConfigureAwait(false);
  535. }
  536. }
  537. }