forked from yanw/App_win_iot_V2.0
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
305 lines
13 KiB
305 lines
13 KiB
using IOTContainer.Common;
|
|
using IOTContainer.Model;
|
|
using IOTContainer.MvvmBase;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Media;
|
|
|
|
namespace IOTContainer.ViewModel
|
|
{
|
|
public class TouchMeViewModel: ViewModelBase
|
|
{
|
|
/// <summary>
|
|
/// 顶部字幕
|
|
/// </summary>
|
|
private string _topText;
|
|
public string TopText
|
|
{
|
|
get { return _topText; }
|
|
set { base.SetProperty(ref _topText, value, "TopText"); }
|
|
}
|
|
/// <summary>
|
|
/// 顶部字幕颜色
|
|
/// </summary>
|
|
private SolidColorBrush _topColor;
|
|
public SolidColorBrush TopColor
|
|
{
|
|
get { return _topColor; }
|
|
set { base.SetProperty(ref _topColor, value, "TopColor"); }
|
|
}
|
|
/// <summary>
|
|
/// 顶部字幕大小
|
|
/// </summary>
|
|
private int _topSize;
|
|
public int TopSize
|
|
{
|
|
get { return _topSize; }
|
|
set { base.SetProperty(ref _topSize, value, "TopSize"); }
|
|
}
|
|
/// <summary>
|
|
/// 底部字幕
|
|
/// </summary>
|
|
private string _bottomText;
|
|
public string BottomText
|
|
{
|
|
get { return _bottomText; }
|
|
set { base.SetProperty(ref _bottomText, value, "BottomText"); }
|
|
}
|
|
/// <summary>
|
|
/// 底部字幕颜色
|
|
/// </summary>
|
|
private SolidColorBrush _bottomColor;
|
|
public SolidColorBrush BottomColor
|
|
{
|
|
get { return _bottomColor; }
|
|
set { base.SetProperty(ref _bottomColor, value, "BottomColor"); }
|
|
}
|
|
/// <summary>
|
|
/// 底部字幕大小
|
|
/// </summary>
|
|
private int _bottomSize;
|
|
public int BottomSize
|
|
{
|
|
get { return _bottomSize; }
|
|
set { base.SetProperty(ref _bottomSize, value, "BottomSize"); }
|
|
}
|
|
|
|
private bool _topIsEmpty;
|
|
private bool _bottomIsEmpty;
|
|
|
|
public TouchMeViewModel()
|
|
{
|
|
try
|
|
{
|
|
#region 字幕相关
|
|
TopText = "";
|
|
TopColor = new SolidColorBrush(Colors.White);
|
|
TopSize = 16;
|
|
BottomText = "";
|
|
BottomColor = new SolidColorBrush(Colors.White);
|
|
BottomSize = 16;
|
|
Task.Run(() => { PlayTopSubtitle(); });
|
|
Task.Run(() => { PlayBottomSubtitle(); });
|
|
#endregion
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.MyLog.WriteLogFile("字幕播放出错:" + e.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 播放顶部字幕
|
|
/// </summary>
|
|
public void PlayTopSubtitle()
|
|
{
|
|
while (ComParameters.Parameters.IsPlay)
|
|
{
|
|
try
|
|
{
|
|
if (ComParameters.Parameters.PlaySubtitleToken == null || !ComParameters.Parameters.PlaySubtitleToken.Token.IsCancellationRequested)
|
|
{
|
|
Task.Run(() => HttpComm.Http.GetSubtitle());
|
|
}
|
|
var basePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Programme");
|
|
var filePath = Path.Combine(basePath, "SubtitleList.txt");
|
|
ComParameters.srwl.EnterReadLock();
|
|
var subcontent = FileManage.ReadJsonFile(filePath);
|
|
ComParameters.srwl.ExitReadLock();
|
|
if (!string.IsNullOrEmpty(subcontent) && subcontent != "[]")//列表不为空
|
|
{
|
|
var subList = JsonConvert.DeserializeObject<List<SubtitleModel>>(subcontent);
|
|
var now = DateTime.Now;
|
|
subList = subList.Where(p => p.startTime <= now && p.endTime >= now && p.newsLoca == 1).ToList();
|
|
if (!subList.Any())//列表为空
|
|
{
|
|
_topIsEmpty = true;
|
|
if (_bottomIsEmpty)
|
|
{
|
|
ComParameters.Parameters.SubListIsEmpty = true;
|
|
}
|
|
Task.Delay(ComParameters.Parameters.waitMilliseconds).Wait();
|
|
}
|
|
else
|
|
{
|
|
if (ComParameters.Parameters.PlaySubtitleToken != null)
|
|
{
|
|
ComParameters.Parameters.PlaySubtitleToken.Dispose();
|
|
}
|
|
ComParameters.Parameters.PlaySubtitleToken = new CancellationTokenSource();
|
|
foreach (var item in subList)
|
|
{
|
|
try
|
|
{
|
|
//设置顶部字幕
|
|
if (null != ComParameters.Parameters.ScreenWindow)
|
|
{
|
|
ComParameters.Parameters.ScreenWindow.Dispatcher.Invoke(() =>
|
|
{
|
|
TopText = item.newsText;
|
|
TopColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString(item.fontColor));
|
|
TopSize = item.fontSize;
|
|
});
|
|
}
|
|
|
|
if (!ComParameters.Parameters.PlaySubtitleToken.Token.IsCancellationRequested)
|
|
{
|
|
try
|
|
{
|
|
Task.Delay(200).Wait();
|
|
//通过可取消标记 通知是否取消当前方法
|
|
Task.Delay(TimeSpan.FromSeconds(ComParameters.Parameters.TopTotalWidth / (double)ComParameters.Parameters.subtitleSpeed), ComParameters.Parameters.PlaySubtitleToken.Token).Wait();
|
|
TopText = "";
|
|
}
|
|
catch (Exception)//收到取消信号
|
|
{
|
|
var task = Task.Run(() =>
|
|
{
|
|
HttpComm.Http.GetSubtitle();
|
|
});
|
|
task.Wait();
|
|
TopText = "";
|
|
break;
|
|
}
|
|
}
|
|
else//收到取消信号
|
|
{
|
|
var task = Task.Run(() =>
|
|
{
|
|
HttpComm.Http.GetSubtitle();
|
|
});
|
|
task.Wait();
|
|
TopText = "";
|
|
break;
|
|
}
|
|
}
|
|
catch (Exception suex)
|
|
{
|
|
Log.MyLog.WriteLogFile("顶部字幕播放失败:" + suex);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_topIsEmpty = true;
|
|
_bottomIsEmpty = true;
|
|
ComParameters.Parameters.SubListIsEmpty = true;
|
|
Task.Delay(ComParameters.Parameters.waitMilliseconds).Wait();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.MyLog.WriteLogFile("顶部字幕播放失败:" + ex);
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 播放底部字幕
|
|
/// </summary>
|
|
public void PlayBottomSubtitle()
|
|
{
|
|
while (ComParameters.Parameters.IsPlay)
|
|
{
|
|
try
|
|
{
|
|
var basePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Programme");
|
|
var filePath = Path.Combine(basePath, "SubtitleList.txt");
|
|
ComParameters.srwl.EnterReadLock();
|
|
var subcontent = FileManage.ReadJsonFile(filePath);
|
|
ComParameters.srwl.ExitReadLock();
|
|
if (!string.IsNullOrEmpty(subcontent) && subcontent != "[]")//列表为空
|
|
{
|
|
var subList = JsonConvert.DeserializeObject<List<SubtitleModel>>(subcontent);
|
|
var now = DateTime.Now;
|
|
subList = subList.Where(p => p.startTime <= now && p.endTime >= now && p.newsLoca == 2).ToList();
|
|
if (!subList.Any())//列表为空
|
|
{
|
|
_bottomIsEmpty = true;
|
|
if (_topIsEmpty)
|
|
{
|
|
ComParameters.Parameters.SubListIsEmpty = true;
|
|
}
|
|
Task.Delay(ComParameters.Parameters.waitMilliseconds).Wait();
|
|
}
|
|
else
|
|
{
|
|
if (ComParameters.Parameters.PlaySubtitleToken != null)
|
|
{
|
|
ComParameters.Parameters.PlaySubtitleToken.Dispose();
|
|
}
|
|
ComParameters.Parameters.PlaySubtitleToken = new CancellationTokenSource();
|
|
foreach (var item in subList)
|
|
{
|
|
try
|
|
{
|
|
if (null != ComParameters.Parameters.ScreenWindow)
|
|
{
|
|
ComParameters.Parameters.ScreenWindow.Dispatcher.Invoke(() =>
|
|
{
|
|
BottomText = item.newsText;
|
|
BottomColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString(item.fontColor));
|
|
BottomSize = item.fontSize;
|
|
});
|
|
}
|
|
|
|
if (!ComParameters.Parameters.PlaySubtitleToken.Token.IsCancellationRequested)
|
|
{
|
|
try
|
|
{
|
|
Task.Delay(200).Wait();
|
|
//通过可取消标记 通知是否取消当前方法
|
|
Task.Delay(TimeSpan.FromSeconds(ComParameters.Parameters.BottomTotalWidth / (double)ComParameters.Parameters.subtitleSpeed), ComParameters.Parameters.PlaySubtitleToken.Token).Wait();
|
|
BottomText = "";
|
|
}
|
|
catch (Exception)//收到取消信号
|
|
{
|
|
var task = Task.Run(() =>
|
|
{
|
|
HttpComm.Http.GetSubtitle();
|
|
});
|
|
task.Wait();
|
|
BottomText = "";
|
|
break;
|
|
}
|
|
}
|
|
else//收到取消信号
|
|
{
|
|
var task = Task.Run(() =>
|
|
{
|
|
HttpComm.Http.GetSubtitle();
|
|
});
|
|
task.Wait();
|
|
BottomText = "";
|
|
break;
|
|
}
|
|
}
|
|
catch (Exception suex)
|
|
{
|
|
Log.MyLog.WriteLogFile("底部字幕播放失败:" + suex);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_topIsEmpty = true;
|
|
_bottomIsEmpty = true;
|
|
ComParameters.Parameters.SubListIsEmpty = true;
|
|
Task.Delay(ComParameters.Parameters.waitMilliseconds).Wait();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.MyLog.WriteLogFile("底部字幕播放失败:" + ex);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|