using IOTContainer.Common; using IOTContainer.Communication; using IOTContainer.Model; using IOTContainer.MvvmBase; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Imaging; namespace IOTContainer.ViewModel { public class ScreenWindowViewModel : ViewModelBase { #region 控件 /// /// 主页显示的用户控件 /// private UserControl _screenUserControl; public UserControl ScreenUserControl { get { return _screenUserControl; } set { base.SetProperty(ref _screenUserControl, value, "ScreenUserControl"); } } /// /// 节目播放的背景色 /// private System.Windows.Media.Brush _background; public System.Windows.Media.Brush Background { get { return _background; } set { base.SetProperty(ref _background, value, "Background"); } } /// /// 屏保图片组件 /// private ImageBrush _imageBrush; private ProgrammeControl _programmeControl; private SolidColorBrush _colorBrush; #endregion public ScreenWindowViewModel() { try { _programmeControl = new ProgrammeControl(); this.ScreenUserControl = _programmeControl; _colorBrush = new SolidColorBrush(Colors.Black); #region 查询是否为同屏设备 if (ComParameters.Parameters.devType == "信发") { TimeJobs.Jobs.QuerySyncMachine(); } #endregion #region 屏保组件 if (!string.IsNullOrEmpty(ComParameters.Parameters.screenSaver)) { var bitmap = new BitmapImage(); using (var stream = new MemoryStream(File.ReadAllBytes(ComParameters.Parameters.screenSaver))) { bitmap.BeginInit(); bitmap.CacheOption = BitmapCacheOption.OnLoad; bitmap.StreamSource = stream; bitmap.EndInit(); bitmap.Freeze(); _imageBrush = new ImageBrush(bitmap); } } #endregion #region 节目相关 Task.Run(() => { PlayProgramme(); }); #endregion } catch (Exception e) { Log.MyLog.WriteLogFile("节目播放出错:" + e.Message); } } /// /// 节目播放 /// public void PlayProgramme() { while (ComParameters.Parameters.IsPlay) { try { var now = DateTime.Now; if (ComParameters.Parameters.PlayToken == null || !ComParameters.Parameters.PlayToken.Token.IsCancellationRequested) { Task.Run(() => HttpComm.Http.GetProgram()); } #region 节目 //获取节目单 ComParameters.rwl.EnterReadLock(); var basePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Programme"); var content = FileManage.ReadJsonFile(Path.Combine(basePath, "ProgrammeList.txt")); ComParameters.rwl.ExitReadLock(); List list = null; if (!string.IsNullOrEmpty(content) && content != "[]") { list = JsonConvert.DeserializeObject>(content); #region 插播>普通 list = list.Where(p => p.LaunchTime <= now && p.ExpiryDate >= now).ToList(); list = list.Any(p => p.ProType == 2) ? list.Where(p => p.ProType == 2).ToList() : list.Where(p => p.ProType == 1).ToList(); #endregion } if (list!=null&&list.Any())//可播放列表不为空 { //如果播放列表不为空 Background = _colorBrush; for (int i = 0; i < list.Count; i++) { try { if (ComParameters.Parameters.PlayToken != null) { ComParameters.Parameters.PlayToken.Dispose(); } ComParameters.Parameters.PlayToken = null; ComParameters.Parameters.PlayToken = new CancellationTokenSource(); //同屏设备 if (ComParameters.Parameters.IsSync) { if (ComParameters.Parameters.IsMainMachine)//主设备 { WebSocketCom.Socket.ProgrammePlay(list.IndexOf(list[i])); } else { if (ComParameters.Parameters.ProgramIndex.HasValue) { i = ComParameters.Parameters.ProgramIndex.Value; } } } var filePath = basePath + list[i].Img.Substring(list[i].Img.LastIndexOf("/")); if (!File.Exists(filePath)) { //无文件时继续下一个 i = i + 1 < list.Count ? i + 1 : 0; } #region 设备上传节目播放记录 //Task.Run(() => //{ // HttpComm.Http.UploadPlayDetail(list[i].Id, list[i].ProType); //}); #endregion var fileExtension = list[i].Img.Substring(list[i].Img.LastIndexOf(".")); switch (list[i].FileType) { case 1://图片 { switch (fileExtension) { case ".gif": { ScreenUserControl.Dispatcher.Invoke(new Action(() => { _programmeControl.PlayGif(filePath); })); break; } default: { ScreenUserControl.Dispatcher.Invoke(new Action(() => { _programmeControl.PlayPic(filePath); })); break; } } if (!ComParameters.Parameters.PlayToken.Token.IsCancellationRequested) { try { //通过可取消标记 通知是否取消当前方法 Task.Delay(list[i].Time * 1000, ComParameters.Parameters.PlayToken.Token).Wait(); ScreenUserControl.Dispatcher.Invoke(new Action(() => { _programmeControl.StopPic(); })); } catch (Exception)//收到取消信号 { //正常情况需要下载新素材 但是同屏设备不需要每次都下载 if (!ComParameters.Parameters.ProgramIndex.HasValue || ComParameters.Parameters.ProgramIndex == list.Count - 1) { var task = Task.Run(async () => { await HttpComm.Http.GetProgram(); }); task.Wait(); } ScreenUserControl.Dispatcher.Invoke(new Action(() => { _programmeControl.StopPic(); })); break; } } else//收到取消信号 { //正常情况需要下载新素材 但是同屏设备不需要每次都下载 if (!ComParameters.Parameters.ProgramIndex.HasValue || ComParameters.Parameters.ProgramIndex == list.Count - 1) { var task = Task.Run(async () => { await HttpComm.Http.GetProgram(); }); task.Wait(); } ScreenUserControl.Dispatcher.Invoke(new Action(() => { _programmeControl.StopPic(); })); break; } break; } case 2://视频 { ScreenUserControl.Dispatcher.Invoke(new Action(() => { _programmeControl.PlayVideo(filePath); })); if (!ComParameters.Parameters.PlayToken.Token.IsCancellationRequested) { try { //通过可取消标记 通知是否取消当前方法 Task.Delay(list[i].Time * 1000, ComParameters.Parameters.PlayToken.Token).Wait(); ScreenUserControl.Dispatcher.Invoke(new Action(() => { _programmeControl.StopVideo(); })); } catch (Exception)//收到取消信号 { //正常情况需要下载新素材 但是同屏设备不需要每次都下载 if (!ComParameters.Parameters.ProgramIndex.HasValue || ComParameters.Parameters.ProgramIndex == list.Count - 1) { var task = Task.Run(async () => { await HttpComm.Http.GetProgram(); }); task.Wait(); } ScreenUserControl.Dispatcher.Invoke(new Action(() => { _programmeControl.StopVideo(); })); break; } } else//收到取消信号 { //正常情况需要下载新素材 但是同屏设备不需要每次都下载 if (!ComParameters.Parameters.ProgramIndex.HasValue || ComParameters.Parameters.ProgramIndex == list.Count - 1) { var task = Task.Run(async () => { await HttpComm.Http.GetProgram(); }); task.Wait(); } ScreenUserControl.Dispatcher.Invoke(new Action(() => { _programmeControl.StopVideo(); })); break; } break; } } } catch (Exception ex) { Log.MyLog.WriteLogFile("节目播放失败:" + ex); } } } else { ComParameters.Parameters.ProListIsEmpty = true; if (ComParameters.Parameters.SubListIsEmpty)//如果当前字幕、节目都没有要播放的,则(容器为导视)关闭信发或者(容器为信发)设置默认壁纸 { //如果播放列表为空 switch (ComParameters.Parameters.devType) { case "导视": { if (ComParameters.Parameters.ScreenWindowNew != null) { ComParameters.Parameters.ScreenWindowNew.Dispatcher.Invoke(() => { ComParameters.Parameters.ScreenWindowNew.Close(); }); } break; } case "信发": { Background = _imageBrush; Task.Delay(ComParameters.Parameters.waitMilliseconds).Wait();//等待*s(节目列表为空,则等待*s后再看节目列表是否为空) break; } } } else { Background = _imageBrush; Task.Delay(ComParameters.Parameters.waitMilliseconds).Wait();//等待*s(节目列表为空,则等待*s后再看节目列表是否为空) } } #endregion } catch (Exception ex) { Log.MyLog.WriteLogFile("节目播放失败:" + ex); } } } } }