using CefSharp.Wpf;
using IOTContainer.Common;
using IOTContainer.Model;
using LottieSharp;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Wpf;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Vlc.DotNet.Wpf;
namespace IOTContainer.View
{
///
/// ProgControl.xaml 的交互逻辑
///
public partial class ProgControl : UserControl
{
string outLinePath;
bool isStop = false;
bool isSingle = false;
private int _maxTryCount = 5;
List components;
List VlcLists = new List();
List imageControls = new List();
List mediaElements = new List();
List canvasWebBrowsers = new List();
List lottieView = new List();
List cancellationTokens = new List();
List marqueeControlNews = new List();
List clockControls = new List();
List weatherControls = new List();
List labels = new List();
List mediaControls = new List();
List chromiumWebBrowsers = new List();
List webView2 = new List();
List tasks = new List();
string[] options = new string[]{
};
string _httpUrl = string.IsNullOrEmpty(ComParameters.Parameters.httpPort) ? ComParameters.Parameters.httpServer : ComParameters.Parameters.httpServer + ":" + ComParameters.Parameters.httpPort;
public ProgControl()
{
InitializeComponent();
}
public void LoadProg(double width, double height, string backgroundmaterial, string backgroundcolor, List componentList)
{
this.Width = width;
this.Height = height;
canvas.Width = width;
canvas.Height = height;
components = componentList;
try
{
outLinePath = AppDomain.CurrentDomain.BaseDirectory + "OutlineFiles";
string urlBg = outLinePath + "/Main/" + System.IO.Path.GetFileName(backgroundmaterial);
if (backgroundmaterial != null)
{
if (File.Exists(urlBg))
{
Image image = new Image();//新建图片对象
image.Source = new BitmapImage(new Uri(urlBg));//读取图片
ImageBrush ib = new ImageBrush();//新建图片对象
ib.ImageSource = image.Source;//赋值为背景图片
canvas.Background = ib;//将图片对象赋值给Canvas的Background
ib.Freeze();
}
}
if (!string.IsNullOrEmpty(backgroundcolor) && !File.Exists(urlBg))
{
Color color = (Color)ColorConverter.ConvertFromString(backgroundcolor);
canvas.Background = new SolidColorBrush(color);
}
isStop = false;
if(components != null)
{
//续播功能
if (components.Count == 1)
{
isSingle = true;
}
foreach (var item in components)
{
if ((item.materials == null || item.materials.Count == 0) && (item.typeCode == "text" || item.typeCode == "image" || item.typeCode == "video" || item.typeCode == "audio" || item.typeCode == "stream" || item.typeCode == "url" || item.typeCode == "lottie"))
{
continue;
}
if (item.typeCode == "stream")
{
VlcControl Vlc = new VlcControl();
var currentAssembly = Assembly.GetEntryAssembly();
var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
DirectoryInfo vlcLibDirectory = new DirectoryInfo(currentDirectory);
//初始化配置,指定引用库
Vlc.SourceProvider.CreatePlayer(vlcLibDirectory, options);
Canvas canvasM = new Canvas();
canvasM.Width = item.width;
canvasM.Height = item.height;
canvasM.Background = Brushes.Transparent;
Vlc.Width = item.width;
Vlc.Height = item.height;
var viewBox = Vlc.Content as Viewbox;
viewBox.Stretch = System.Windows.Media.Stretch.Uniform;
Canvas.SetLeft(canvasM, item.offsetX);
Canvas.SetZIndex(canvasM, item.zindex);
Canvas.SetTop(canvasM, item.offsetY);
canvasM.Children.Add(Vlc);
canvas.Children.Add(canvasM);
Vlc.SourceProvider.MediaPlayer.Play(item.materials[0].outerChain);
VlcLists.Add(Vlc);
}
if (item.typeCode == "video" && item.materials.Count > 0)
{
string filefolder = AppDomain.CurrentDomain.BaseDirectory + "\\Video";
try
{
if (!Directory.Exists(filefolder))
{
DirectoryInfo directoryInfo = new DirectoryInfo(filefolder);
directoryInfo.Create();
}
}
catch (Exception ex)
{
}
if (item.materials.Count > 1)
{
MediaElement media = new MediaElement();
media.LoadedBehavior = MediaState.Manual;
Canvas canvasM = new Canvas();
canvasM.Width = item.width;
canvasM.Height = item.height;
canvasM.Background = Brushes.Black;
media.Width = item.width;
media.Height = item.height;
media.Stretch = Stretch.Fill;
canvasM.Children.Add(media);
Canvas.SetLeft(canvasM, item.offsetX);
Canvas.SetZIndex(canvasM, item.zindex);
Canvas.SetTop(canvasM, item.offsetY);
canvas.Children.Add(canvasM);
canvasWebBrowsers.Add(canvasM);
mediaElements.Add(media);
Image image = new Image();
image.Width = item.width;
image.Height = item.height;
imageControls.Add(image);
canvas.Children.Add(image);
Canvas.SetLeft(image, item.offsetX);
Canvas.SetZIndex(image, item.zindex);
Canvas.SetTop(image, item.offsetY);
image.Stretch = Stretch.Fill;
PlayVideo(media, image, item.materials, item.config, isSingle);
}
else if (item.materials.Count == 1)
{
string fileExtension = string.Empty;
string url = string.Empty;
url = outLinePath + "/Main/" + System.IO.Path.GetFileName(item.materials[0].fileUrl);
var duration = item.materials[0].duration * 1000;
if (duration > 0)
{
string imgVideo = AppDomain.CurrentDomain.BaseDirectory + @"Video\" + System.IO.Path.GetFileNameWithoutExtension(item.materials[0].fileUrl) + ".jpg";
if (File.Exists(imgVideo))
{
Image image = new Image();
image.Width = item.width;
image.Height = item.height;
imageControls.Add(image);
canvas.Children.Add(image);
Canvas.SetLeft(image, item.offsetX);
Canvas.SetZIndex(image, item.zindex);
Canvas.SetTop(image, item.offsetY);
image.Stretch = Stretch.Fill;
BindImage(image, imgVideo);
}
else
{
VideoStart(url, imgVideo);
Image image = new Image();
image.Width = item.width;
image.Height = item.height;
imageControls.Add(image);
canvas.Children.Add(image);
Canvas.SetLeft(image, item.offsetX);
Canvas.SetZIndex(image, item.zindex);
Canvas.SetTop(image, item.offsetY);
image.Stretch = Stretch.Fill;
BindImage(image, imgVideo);
}
var playAudio = item.config.Value("playAudio");
MediaElement media = new MediaElement();
media.MediaEnded += new RoutedEventHandler((send, e) =>
{
media.Position = new TimeSpan(0, 0, 0, 0, 100);
});
media.LoadedBehavior = MediaState.Manual;
media.Stretch = Stretch.Fill;
media.Source = new Uri(url);
Canvas canvasM = new Canvas();
canvasM.Width = item.width;
canvasM.Height = item.height;
canvasM.Background = Brushes.Transparent;
media.Width = item.width;
media.Height = item.height;
canvasM.Children.Add(media);
Canvas.SetLeft(canvasM, item.offsetX);
Canvas.SetZIndex(canvasM, item.zindex);
Canvas.SetTop(canvasM, item.offsetY);
canvas.Children.Add(canvasM);
canvasWebBrowsers.Add(canvasM);
mediaElements.Add(media);
media.Volume = playAudio ? 1 : 0;
media.Play();
}
else
{
Image image = new Image();
image.Width = item.width;
image.Height = item.height;
imageControls.Add(image);
canvas.Children.Add(image);
Canvas.SetLeft(image, item.offsetX);
Canvas.SetZIndex(image, item.zindex);
Canvas.SetTop(image, item.offsetY);
image.Stretch = Stretch.Fill;
PlayImage(image, item.materials, item.config,isSingle);
}
}
}
if (item.typeCode == "image")
{
Image image = new Image();
image.Width = item.width;
image.Height = item.height;
imageControls.Add(image);
canvas.Children.Add(image);
Canvas.SetLeft(image, item.offsetX);
Canvas.SetZIndex(image, item.zindex);
Canvas.SetTop(image, item.offsetY);
image.Stretch = Stretch.Fill;
PlayImage(image, item.materials, item.config, isSingle);
}
if (item.typeCode == "lottie")
{
LottieAnimationView lottie = new LottieAnimationView();
lottie.Width = item.width;
lottie.Height = item.height;
lottieView.Add(lottie);
canvas.Children.Add(lottie);
Canvas.SetLeft(lottie, item.offsetX);
Canvas.SetZIndex(lottie, item.zindex);
Canvas.SetTop(lottie, item.offsetY);
//自动播放
//lottie.AutoPlay = true;
//开启硬件加速
lottie.UseHardwareAcceleration(true);
lottie.FileName = outLinePath + "/Main/" + System.IO.Path.GetFileName(item.materials[0].fileUrl);
lottie.PlayAnimation();
}
if (item.typeCode == "text" && item.materials != null && item.materials.Count > 0)
{
var textBackgroundColor = item.config.Value("backgroundColor");
var textBackgroundOpacity = item.config.Value("backgroundOpacity");
var textFontColor = item.config.Value("fontColor");
var textFontSize = item.config.Value("fontSize");
var textFontStyle = item.config.Value("fontStyle");
var textAnimationSpeed = item.config.Value("animationSpeed");
var animation = item.config.Value("animation");
string textContent = item.materials[0].text;
textContent = textContent.Replace("\r\n", " ");
if (!animation.Equals("固定"))
{
MarqueeControl marqueeControlNew = new MarqueeControl();
marqueeControlNew.Load(textContent, item.width, item.height, textFontSize, textBackgroundColor, textBackgroundOpacity, textFontColor, textFontStyle, textAnimationSpeed);
canvas.Children.Add(marqueeControlNew);
marqueeControlNews.Add(marqueeControlNew);
Canvas.SetLeft(marqueeControlNew, item.offsetX);
Canvas.SetZIndex(marqueeControlNew, item.zindex);
Canvas.SetTop(marqueeControlNew, item.offsetY);
}
else
{
Label label = new Label();
label.FontSize = double.Parse(textFontSize);
Brush brush;
BrushConverter brushConverter = new BrushConverter();
brush = (Brush)brushConverter.ConvertFromString(textBackgroundColor);//"#17acae");
label.Background = brush;
label.Opacity = Convert.ToDouble(textBackgroundOpacity);
Brush brush1;
BrushConverter brushConverter1 = new BrushConverter();
brush1 = (Brush)brushConverter1.ConvertFromString(textFontColor);
label.Foreground = brush1;
//加粗 Label1.FontBold = True
//斜体 Label1.FontItalic = true
if (textFontStyle.Contains("加粗"))
{
label.FontWeight = FontWeights.Bold;
}
if (textFontStyle.Contains("斜体"))
{
label.FontStyle = FontStyles.Italic;
}
label.Height = item.height;
label.Width = item.width;
label.HorizontalContentAlignment = HorizontalAlignment.Center;
label.VerticalContentAlignment = VerticalAlignment.Center;
label.Content = textContent;
canvas.Children.Add(label);
labels.Add(label);
Canvas.SetLeft(label, item.offsetX);
Canvas.SetZIndex(label, item.zindex);
Canvas.SetTop(label, item.offsetY);
}
}
if (item.typeCode == "clock")
{
var backgroundColor = item.config.Value("backgroundColor");
var fontColor = item.config.Value("fontColor");
var style = item.config.Value("style");
ClockControl clock = new ClockControl();
clock.BindClock(style.Value, item.width, item.height, backgroundColor, fontColor);
canvas.Children.Add(clock);
clockControls.Add(clock);
Canvas.SetLeft(clock, item.offsetX);
Canvas.SetZIndex(clock, item.zindex);
Canvas.SetTop(clock, item.offsetY);
}
if (item.typeCode == "weather")
{
var backgroundColor = item.config.Value("backgroundColor");
var fontColor = item.config.Value("fontColor");
var style = item.config.Value("style");
var city = item.config.Value("cityName");
if (string.IsNullOrEmpty(city))
{
city = "南京";
}
if (!string.IsNullOrEmpty(city))
{
WeatherControl weather = new WeatherControl();
weather.LoadWeather(city, style.Value, item.width, item.height, backgroundColor, fontColor);
canvas.Children.Add(weather);
weatherControls.Add(weather);
Canvas.SetLeft(weather, item.offsetX);
Canvas.SetZIndex(weather, item.zindex);
Canvas.SetTop(weather, item.offsetY);
}
}
if (item.typeCode == "audio" && item.materials != null && item.materials.Count > 0)
{
var playAudio = item.config.Value("playAudio");
MediaPlayer media = new MediaPlayer();
string url = string.Empty;
url = outLinePath + "/Main/" + System.IO.Path.GetFileName(item.materials[0].fileUrl);
media.Open(new Uri(url, UriKind.Relative));
media.MediaEnded += (sender, e) =>
{//播放结束后 又重新播放
media.Position = new TimeSpan(0);
};
media.Volume = playAudio ? 1 : 0;
media.Play();
mediaControls.Add(media);
}
if (item.typeCode == "url" && item.materials != null && item.materials.Count > 0)
{
ChromiumWebBrowser webBrowser = new ChromiumWebBrowser();
webBrowser.Width = item.width;
webBrowser.Height = item.height;
webBrowser.Address = item.materials[0].outerChain;
webBrowser.Background = Brushes.Black;
Canvas canvas1 = new Canvas();
canvas1.Width = item.width;
canvas1.Height = item.height;
canvas1.Background = Brushes.White;
canvas1.Children.Add(webBrowser);
canvas.Children.Add(canvas1);
canvasWebBrowsers.Add(canvas1);
chromiumWebBrowsers.Add(webBrowser);
Canvas.SetLeft(canvas1, item.offsetX);
Canvas.SetZIndex(canvas1, item.zindex);
Canvas.SetTop(canvas1, item.offsetY);
}
if (item.typeCode == "facility" && item.subComponents != null && item.subComponents.Count > 0)
{
var transitionPeriod = item.config.Value("transitionPeriod");
if (transitionPeriod <= 0)
{
transitionPeriod = 15;
}
foreach (var subComponents in item.subComponents)
{
if (subComponents.typeCode == "signImage_logo")
{
var imageMove = subComponents.config.Value("imageMove");
if (imageMove == "1")
{
LottieAnimationView lottie = new LottieAnimationView();
lottie.Width = subComponents.width;
lottie.Height = subComponents.height;
lottieView.Add(lottie);
canvas.Children.Add(lottie);
Canvas.SetLeft(lottie, subComponents.offsetX);
Canvas.SetZIndex(lottie, subComponents.offsetY);
Canvas.SetTop(lottie, subComponents.offsetY);
PlayLottie(lottie, subComponents.materials, transitionPeriod);
}
else
{
Image image = new Image();
image.Width = subComponents.width;
image.Height = subComponents.height;
imageControls.Add(image);
canvas.Children.Add(image);
Canvas.SetLeft(image, subComponents.offsetX);
Canvas.SetZIndex(image, subComponents.zindex);
Canvas.SetTop(image, subComponents.offsetY);
image.Stretch = Stretch.Fill;
PlayImage(image, subComponents.materials, transitionPeriod);
}
}
if (subComponents.typeCode == "signImage_direction")
{
var imageMove = subComponents.config.Value("imageMove");
if (imageMove == "1")
{
LottieAnimationView lottie = new LottieAnimationView();
lottie.Width = subComponents.width;
lottie.Height = subComponents.height;
lottieView.Add(lottie);
canvas.Children.Add(lottie);
Canvas.SetLeft(lottie, subComponents.offsetX);
Canvas.SetZIndex(lottie, subComponents.offsetY);
Canvas.SetTop(lottie, subComponents.offsetY);
PlayLottie(lottie, subComponents.materials, transitionPeriod);
}
else
{
Image image = new Image();
image.Width = subComponents.width;
image.Height = subComponents.height;
imageControls.Add(image);
canvas.Children.Add(image);
Canvas.SetLeft(image, subComponents.offsetX);
Canvas.SetZIndex(image, subComponents.zindex);
Canvas.SetTop(image, subComponents.offsetY);
image.Stretch = Stretch.Fill;
PlayImage(image, subComponents.materials, transitionPeriod);
}
}
if (subComponents.typeCode == "signTxt_distance")
{
PlayText(subComponents.materials, subComponents.config, subComponents.height, subComponents.width, subComponents.offsetX, subComponents.offsetY, subComponents.zindex, transitionPeriod);
}
if (subComponents.typeCode == "signTxt_time")
{
PlayText(subComponents.materials, subComponents.config, subComponents.height, subComponents.width, subComponents.offsetX, subComponents.offsetY, subComponents.zindex, transitionPeriod);
}
if (subComponents.typeCode == "signTxt_name")
{
PlayText(subComponents.materials, subComponents.config, subComponents.height, subComponents.width, subComponents.offsetX, subComponents.offsetY, subComponents.zindex, transitionPeriod);
}
}
}
if (item.typeCode == "brand" && item.subComponents != null && item.subComponents.Count > 0)
{
var transitionPeriod = item.config.Value("transitionPeriod");
if (transitionPeriod <= 0)
{
transitionPeriod = 15;
}
foreach (var subComponents in item.subComponents)
{
if (subComponents.typeCode == "signImage_logo")
{
Image image = new Image();
image.Width = subComponents.width;
image.Height = subComponents.height;
imageControls.Add(image);
canvas.Children.Add(image);
Canvas.SetLeft(image, subComponents.offsetX);
Canvas.SetZIndex(image, subComponents.zindex);
Canvas.SetTop(image, subComponents.offsetY);
image.Stretch = Stretch.Fill;
PlayImage(image, subComponents.materials, transitionPeriod);
}
if (subComponents.typeCode == "signImage_direction")
{
var imageMove = subComponents.config.Value("imageMove");
if (imageMove == "1")
{
LottieAnimationView lottie = new LottieAnimationView();
lottie.Width = subComponents.width;
lottie.Height = subComponents.height;
lottieView.Add(lottie);
canvas.Children.Add(lottie);
Canvas.SetLeft(lottie, subComponents.offsetX);
Canvas.SetZIndex(lottie, subComponents.offsetY);
Canvas.SetTop(lottie, subComponents.offsetY);
PlayLottie(lottie, subComponents.materials, transitionPeriod);
}
else
{
Image image = new Image();
image.Width = subComponents.width;
image.Height = subComponents.height;
imageControls.Add(image);
canvas.Children.Add(image);
Canvas.SetLeft(image, subComponents.offsetX);
Canvas.SetZIndex(image, subComponents.zindex);
Canvas.SetTop(image, subComponents.offsetY);
image.Stretch = Stretch.Fill;
PlayImage(image, subComponents.materials, transitionPeriod);
}
}
if (subComponents.typeCode == "signTxt_distance")
{
PlayText(subComponents.materials, subComponents.config, subComponents.height, subComponents.width, subComponents.offsetX, subComponents.offsetY, subComponents.zindex, transitionPeriod);
}
if (subComponents.typeCode == "signTxt_time")
{
PlayText(subComponents.materials, subComponents.config, subComponents.height, subComponents.width, subComponents.offsetX, subComponents.offsetY, subComponents.zindex, transitionPeriod);
}
if (subComponents.typeCode == "signTxt_name")
{
PlayText(subComponents.materials, subComponents.config, subComponents.height, subComponents.width, subComponents.offsetX, subComponents.offsetY, subComponents.zindex, transitionPeriod);
}
}
}
if (item.typeCode == "position" && item.subComponents != null && item.subComponents.Count > 0)
{
var transitionPeriod = item.config.Value("transitionPeriod");
if (transitionPeriod <= 0)
{
transitionPeriod = 15;
}
foreach (var subComponents in item.subComponents)
{
if (subComponents.typeCode == "signImage_logo_pt")
{
Image image = new Image();
image.Width = subComponents.width;
image.Height = subComponents.height;
imageControls.Add(image);
canvas.Children.Add(image);
Canvas.SetLeft(image, subComponents.offsetX);
Canvas.SetZIndex(image, subComponents.zindex);
Canvas.SetTop(image, subComponents.offsetY);
image.Stretch = Stretch.Fill;
PlayImage(image, subComponents.materials, transitionPeriod);
}
if (subComponents.typeCode == "signImage_direction")
{
var imageMove = subComponents.config.Value("imageMove");
if(imageMove =="1")
{
LottieAnimationView lottie = new LottieAnimationView();
lottie.Width = subComponents.width;
lottie.Height = subComponents.height;
lottieView.Add(lottie);
canvas.Children.Add(lottie);
Canvas.SetLeft(lottie, subComponents.offsetX);
Canvas.SetZIndex(lottie, subComponents.offsetY);
Canvas.SetTop(lottie, subComponents.offsetY);
PlayLottie(lottie, subComponents.materials, transitionPeriod);
}
else
{
Image image = new Image();
image.Width = subComponents.width;
image.Height = subComponents.height;
imageControls.Add(image);
canvas.Children.Add(image);
Canvas.SetLeft(image, subComponents.offsetX);
Canvas.SetZIndex(image, subComponents.zindex);
Canvas.SetTop(image, subComponents.offsetY);
image.Stretch = Stretch.Fill;
PlayImage(image, subComponents.materials, transitionPeriod);
}
}
if (subComponents.typeCode == "signTxt_distance")
{
PlayText(subComponents.materials, subComponents.config, subComponents.height, subComponents.width, subComponents.offsetX, subComponents.offsetY, subComponents.zindex, transitionPeriod);
}
if (subComponents.typeCode == "signTxt_time")
{
PlayText(subComponents.materials, subComponents.config, subComponents.height, subComponents.width, subComponents.offsetX, subComponents.offsetY, subComponents.zindex, transitionPeriod);
}
if (subComponents.typeCode == "signTxt_name_pt")
{
PlayText(subComponents.materials, subComponents.config, subComponents.height, subComponents.width, subComponents.offsetX, subComponents.offsetY, subComponents.zindex, transitionPeriod);
}
}
}
if (item.typeCode == "flow" && item.materials != null && item.materials.Count > 0 && ComParameters.Parameters.devType == "信发")
{
string localFileName = outLinePath + "\\Main\\" + System.IO.Path.GetFileName(item.materials[0].fileUrl);
string url = string.Empty;
url = outLinePath + "\\Main\\" + System.IO.Path.GetFileNameWithoutExtension(item.materials[0].fileUrl);
if (!Directory.Exists(url))
ZipFile.ExtractToDirectory(localFileName, url);
string interfaceJson;
try
{
interfaceJson = FileManage.ReadContext(url + "\\InterfaceList.json");
}
catch (Exception)
{
interfaceJson = null;
}
if (interfaceJson != null)
{
try
{
JObject jsons = (JObject)JsonConvert.DeserializeObject(interfaceJson);
if (jsons["code"].ToString() == "200")
{
JArray data = (JArray)jsons["data"];
if (data.Count > 0)
{
string jsonText = null;
foreach (JObject list in data)
{
string fileName = (string)list.GetValue("interfaceName");
string interfaceUrl = (string)list.GetValue("interfaceUrl");
if (interfaceUrl.Contains("code="))
interfaceUrl = interfaceUrl.Replace("code=", "code=" + ComParameters.Parameters.devCode);
if (interfaceUrl.Contains(_httpUrl))
{
jsonText = HttpComm.Http.Request("GET", interfaceUrl);
}
else
{
jsonText = HttpComm.Http.Request("GET", _httpUrl + interfaceUrl);
}
var baseDir = System.IO.Path.Combine(url, "static", "offline", "JSON");
if (!Directory.Exists(baseDir))
{
Directory.CreateDirectory(baseDir);
}
string jsonPath = $"{baseDir}\\{fileName}.json";
if (File.Exists(jsonPath))
{
File.Delete(jsonPath);
}
FileManage.WriteJsonFile(jsonPath, jsonText);
}
}
}
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile("LoadProg:" + ex.ToString(), "ProgControl");
}
}
//下载模型资源
HttpComm.Http.GetModelFileResource(url + "\\static\\offline");
WebView2 webBrowser = new WebView2();
webBrowser.Width = item.width;
webBrowser.Height = item.height;
CreateWebView(webBrowser, url);
Canvas canvas = new Canvas();
canvas.Width = item.width;
canvas.Height = item.height;
canvas.Background = Brushes.White;
canvas.Children.Add(webBrowser);
canvas.Children.Add(canvas);
canvasWebBrowsers.Add(canvas);
webView2.Add(webBrowser);
Canvas.SetLeft(canvas, item.offsetX);
Canvas.SetZIndex(canvas, item.zindex);
Canvas.SetTop(canvas, item.offsetY);
}
if (item.typeCode == "brand_list" && item.materials != null && item.materials.Count > 0 && ComParameters.Parameters.devType == "信发")
{
try
{
string localFileName = outLinePath + "\\Main\\" + System.IO.Path.GetFileName(item.materials[0].fileUrl);
string url = string.Empty;
url = outLinePath + "\\Main\\" + System.IO.Path.GetFileNameWithoutExtension(item.materials[0].fileUrl);
if (!Directory.Exists(url))
ZipFile.ExtractToDirectory(localFileName, url);
//下载Json
HttpComm.Http.Getbrand_listJson(System.IO.Path.Combine(url, "static", "offline", "JSON"), item.code);
//下载模型资源
HttpComm.Http.Getbrand_listResource(System.IO.Path.Combine(url, "static", "offline"), item.code);
WebView2 webBrowser = new WebView2();
webBrowser.Width = item.width;
webBrowser.Height = item.height;
CreateWebView(webBrowser, url);
Canvas canvas1 = new Canvas();
canvas1.Width = item.width;
canvas1.Height = item.height;
canvas1.Background = Brushes.White;
canvas1.Children.Add(webBrowser);
canvas.Children.Add(canvas1);
canvasWebBrowsers.Add(canvas1);
webView2.Add(webBrowser);
Canvas.SetLeft(canvas1, item.offsetX);
Canvas.SetZIndex(canvas1, item.zindex);
Canvas.SetTop(canvas1, item.offsetY);
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile("LoadProg:" + ex.ToString(), "ProgControl");
}
}
if (item.typeCode == "map" && item.materials != null && item.materials.Count > 0 && ComParameters.Parameters.devType == "信发")
{
try
{
string localFileName = outLinePath + "\\Main\\" + System.IO.Path.GetFileName(item.materials[0].fileUrl);
string url = string.Empty;
url = outLinePath + "\\Main\\" + System.IO.Path.GetFileNameWithoutExtension(item.materials[0].fileUrl);
if (!Directory.Exists(url))
ZipFile.ExtractToDirectory(localFileName, url);
//下载Json
HttpComm.Http.Getbrand_listJson(System.IO.Path.Combine(url, "static", "offline", "JSON"), item.code);
//下载模型资源
HttpComm.Http.Getbrand_listResource(System.IO.Path.Combine(url, "static", "offline"), item.code);
WebView2 webBrowser = new WebView2();
webBrowser.Width = item.width;
webBrowser.Height = item.height;
CreateWebView(webBrowser, url);
Canvas canvas1 = new Canvas();
canvas1.Width = item.width;
canvas1.Height = item.height;
canvas1.Background = Brushes.White;
canvas1.Children.Add(webBrowser);
canvas.Children.Add(canvas1);
canvasWebBrowsers.Add(canvas1);
webView2.Add(webBrowser);
Canvas.SetLeft(canvas1, item.offsetX);
Canvas.SetZIndex(canvas1, item.zindex);
Canvas.SetTop(canvas1, item.offsetY);
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile("LoadProg:" + ex.ToString(), "ProgControl");
}
}
}
}
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile("LoadProg:" + ex.ToString(), "ProgControl");
}
}
public async void CreateWebView(WebView2 webView, string endPath)
{
try
{
if (webView.CoreWebView2 == null)
{
var env = await CoreWebView2Environment.CreateAsync(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "WebView2"), null, new CoreWebView2EnvironmentOptions(additionalBrowserArguments: "--remote-debugging-port=9222 --incognito --disable-pinch"));
await webView.EnsureCoreWebView2Async(env);
webView.CoreWebView2.ProcessFailed += CoreWebView2_ProcessFailed;
}
webView.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;//取消右键
webView.CoreWebView2.Settings.IsZoomControlEnabled = false;//取消缩放
webView.CoreWebView2.Settings.IsPinchZoomEnabled = false;//取消捏合缩放
webView.CoreWebView2.Settings.IsGeneralAutofillEnabled = false;//取消自动填充
webView.CoreWebView2.Settings.IsPasswordAutosaveEnabled = false;//取消密码自动保存
webView.CoreWebView2.SetVirtualHostNameToFolderMapping("brandlist", endPath, CoreWebView2HostResourceAccessKind.Allow);
webView.CoreWebView2.Navigate("http://brandlist/index.html");
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile("CreateWebView:" + ex.ToString(), "ProgControl");
ExternalCall.StartExplorer();
Log.MyLog.WriteLogFile($"打开WebView2浏览器出错:{ex}", "ProgControl");
Environment.Exit(0);
}
}
private void CoreWebView2_ProcessFailed(object sender, CoreWebView2ProcessFailedEventArgs e)
{
Log.MyLog.WriteLogFile("CoreWebView2_ProcessFailed:" + e.ToString(), "ProgControl");
ExternalCall.StartExplorer();
Log.MyLog.WriteLogFile($"打开WebView2浏览器出错:{e}", "ProgControl");
Environment.Exit(0);
}
private void PlayText(List materials, JObject config, int height, int width, int offsetX, int offsetY, int zIndex, int transitionPeriod)
{
try
{
MarqueeControl marqueeControlNew = new MarqueeControl();
Label label = new Label();
var textBackgroundColor = config.Value("backgroundColor");
if (textBackgroundColor == null)
textBackgroundColor = "#FFFFFF";
var textBackgroundOpacity = config.Value("backgroundOpacity");
var textFontColor = config.Value("fontColor");
var textFontSize = config.Value("fontSize");
var textFontStyle = config.Value("fontStyle");
var textAnimationSpeed = config.Value("animationSpeed");
var animation = config.Value("animation");
if (!animation.Equals("固定"))
{
canvas.Children.Add(marqueeControlNew);
marqueeControlNews.Add(marqueeControlNew);
Canvas.SetLeft(marqueeControlNew, offsetX);
Canvas.SetZIndex(marqueeControlNew, zIndex);
Canvas.SetTop(marqueeControlNew, offsetY);
}
else
{
label.FontSize = double.Parse(textFontSize);
Brush brush;
BrushConverter brushConverter = new BrushConverter();
brush = (Brush)brushConverter.ConvertFromString(textBackgroundColor);
label.Background = brush;
label.Opacity = Convert.ToDouble(textBackgroundOpacity);
Brush brush1;
BrushConverter brushConverter1 = new BrushConverter();
brush1 = (Brush)brushConverter1.ConvertFromString(textFontColor);
label.Foreground = brush1;
if (textFontStyle.Contains("加粗"))
{
label.FontWeight = FontWeights.Bold;
}
if (textFontStyle.Contains("斜体"))
{
label.FontStyle = FontStyles.Italic;
}
label.Height = height;
label.Width = width;
label.HorizontalContentAlignment = HorizontalAlignment.Center;
label.VerticalContentAlignment = VerticalAlignment.Center;
canvas.Children.Add(label);
labels.Add(label);
Canvas.SetLeft(label, offsetX);
Canvas.SetZIndex(label, zIndex);
Canvas.SetTop(label, offsetY);
}
CancellationTokenSource source1 = new CancellationTokenSource();
cancellationTokens.Add(source1);
CancellationToken token = source1.Token;
var threads = System.Diagnostics.Process.GetCurrentProcess().Threads;
var count = threads.Count;
var actived = threads.Cast().Where(t => t.ThreadState == System.Diagnostics.ThreadState.Running).ToList();
Task tsk1 = Task.Run(async () =>
{
var curPlayIndex = 0;
DateTime? curDate = null;
while (!isStop)
{
if (token.IsCancellationRequested)
{
//isStop = true;
// 释放资源操作等等...
break;
}
string textContent = materials[curPlayIndex].text;
textContent = textContent.Replace("\r\n", " ");
Dispatcher.Invoke(new Action(() =>
{
if (!animation.Equals("固定"))
{
marqueeControlNew.Load(textContent, width, height, textFontSize, textBackgroundColor, textBackgroundOpacity, textFontColor, textFontStyle, textAnimationSpeed);
}
else
{
label.Content = textContent;
}
}));
curPlayIndex++;
if (curPlayIndex >= materials.Count)
{
curPlayIndex = 0;
}
if (token.IsCancellationRequested)
{
break;
}
else
{
await Task.Delay(transitionPeriod * 1000, token);
}
}
}, token);
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile("PlayText:" + ex.ToString(), "ProgControl");
}
}
private void PlayLottie(LottieAnimationView lottie, List materials, int transitionPeriod)
{
try
{
CancellationTokenSource source1 = new CancellationTokenSource();
cancellationTokens.Add(source1);
CancellationToken token = source1.Token;
var threads = System.Diagnostics.Process.GetCurrentProcess().Threads;
var count = threads.Count;
var actived = threads.Cast().Where(t => t.ThreadState == System.Diagnostics.ThreadState.Running).ToList();
Task tsk1 = Task.Run(async () =>
{
var curPlayIndex = 0;
string url = string.Empty;
DateTime? curDate = null;
while (!isStop)
{
if (token.IsCancellationRequested)
{
//isStop = true;
// 释放资源操作等等...
break;
}
curDate = DateTime.Now;
url = url = outLinePath + "/Main/" + System.IO.Path.GetFileName(materials[curPlayIndex].fileUrl);
BindLottie(lottie, url);
curPlayIndex++;
if (curPlayIndex >= materials.Count)
{
curPlayIndex = 0;
}
if (token.IsCancellationRequested)
{
//isStop = true;
// 释放资源操作等等...
break;
}
else
{
await Task.Delay(transitionPeriod * 1000, token);
}
}
}, token);
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile("PlayLottie:" + ex.ToString(), "ProgControl");
}
}
private void PlayImage(Image image, List materials, int transitionPeriod)
{
try
{
CancellationTokenSource source1 = new CancellationTokenSource();
cancellationTokens.Add(source1);
CancellationToken token = source1.Token;
var threads = System.Diagnostics.Process.GetCurrentProcess().Threads;
var count = threads.Count;
var actived = threads.Cast().Where(t => t.ThreadState == System.Diagnostics.ThreadState.Running).ToList();
Task tsk1 = Task.Run(async () =>
{
var curPlayIndex = 0;
string url = string.Empty;
DateTime? curDate = null;
while (!isStop)
{
if (token.IsCancellationRequested)
{
//isStop = true;
// 释放资源操作等等...
break;
}
curDate = DateTime.Now;
url = url = outLinePath + "/Main/" + System.IO.Path.GetFileName(materials[curPlayIndex].fileUrl);
BindImage(image, url);
curPlayIndex++;
if (curPlayIndex >= materials.Count)
{
curPlayIndex = 0;
}
if (token.IsCancellationRequested)
{
//isStop = true;
// 释放资源操作等等...
break;
}
else
{
await Task.Delay(transitionPeriod * 1000, token);
}
}
image.Source.Freeze();
}, token);
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile("PlayImage:" + ex.ToString(), "ProgControl");
}
}
private void PlayImage(Image image, List materials, JObject config, bool isCountine)
{
try
{
CancellationTokenSource source1 = new CancellationTokenSource();
cancellationTokens.Add(source1);
CancellationToken token = source1.Token;
var threads = System.Diagnostics.Process.GetCurrentProcess().Threads;
var count = threads.Count;
var actived = threads.Cast().Where(t => t.ThreadState == System.Diagnostics.ThreadState.Running).ToList();
Task tsk1 = Task.Run(async () =>
{
var curPlayIndex = 0;
var transitionPeriod = config.Value("transitionPeriod");
if (transitionPeriod <= 0)
{
transitionPeriod = 15;
}
string fileExtension = string.Empty;
string url = string.Empty;
DateTime? curDate = null;
if (isCountine)
{
if (ComParameters.Parameters.CurrentMaterialIndex.HasValue )
{
if (ComParameters.Parameters.CurrentMaterialIndex.Value <= materials.Count - 1)
{
curPlayIndex = ComParameters.Parameters.CurrentMaterialIndex.Value;
}
}
}
while (!isStop)
{
if (token.IsCancellationRequested)
{
//isStop = true;
// 释放资源操作等等...
break;
}
curDate = DateTime.Now;
url = outLinePath + "/Main/" + System.IO.Path.GetFileName(materials[curPlayIndex].fileUrl);
BindImage(image, url);
ComParameters.Parameters.CurrentMaterialIndex = curPlayIndex;
curPlayIndex++;
if (curPlayIndex >= materials.Count)
{
curPlayIndex = 0;
}
if (token.IsCancellationRequested)
{
//isStop = true;
// 释放资源操作等等...
break;
}
else
{
await Task.Delay(transitionPeriod * 1000, token);
}
}
}, token);
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile("PlayImage:" + ex.ToString(), "ProgControl");
}
}
private void PlayVideo(MediaElement media, Image image, List materials, JObject config, bool isCountine)
{
Action action = new Action(async delegate
{
try
{
var playAudio = config.Value("playAudio");
var transitionPeriod = config.Value("transitionPeriod");
if (transitionPeriod <= 0)
{
transitionPeriod = 15;
}
var curPlayIndex = 0;
string url = string.Empty;
var duration = 0;
if (isCountine)
{
if (ComParameters.Parameters.CurrentMaterialIndex.HasValue)
{
if(ComParameters.Parameters.CurrentMaterialIndex.Value <= materials.Count -1)
{
curPlayIndex = ComParameters.Parameters.CurrentMaterialIndex.Value;
}
}
}
CancellationTokenSource source1 = new CancellationTokenSource();
cancellationTokens.Add(source1);
CancellationToken token = source1.Token;
TaskFactory factory = new TaskFactory(token);
Task tsk1 = factory.StartNew(async () =>
{
while (!isStop)
{
if (token.IsCancellationRequested)
{
//isStop = true;
// 释放资源操作等等...
break;
}
url = outLinePath + "/Main/" + System.IO.Path.GetFileName(materials[curPlayIndex].fileUrl);
duration = materials[curPlayIndex].duration * 1000;
//var Videolist = materials.Where(i => i.duration > 0).ToList();
if (duration > 0)
{
string imgVideo = AppDomain.CurrentDomain.BaseDirectory + @"Video\" + System.IO.Path.GetFileNameWithoutExtension(materials[curPlayIndex].fileUrl) + ".jpg";
if (File.Exists(imgVideo))
{
BindImage(image, imgVideo);
}
else
{
VideoStart(url, imgVideo);
BindImage(image, imgVideo);
}
media.Dispatcher.Invoke(new Action(() =>
{
media.Visibility = Visibility.Visible;
}));
image.Dispatcher.Invoke(new Action(() =>
{
image.Visibility = Visibility.Visible;
}));
StopVideo(media);
StartVideo(media, url, image, playAudio);
}
else
{
media.Dispatcher.Invoke(new Action(() =>
{
media.Visibility = Visibility.Hidden;
}));
image.Dispatcher.Invoke(new Action(() =>
{
image.Visibility = Visibility.Visible;
}));
duration = transitionPeriod * 1000;
BindImage(image, url);
}
ComParameters.Parameters.CurrentMaterialIndex = curPlayIndex;
curPlayIndex++;
if (curPlayIndex >= materials.Count)
{
curPlayIndex = 0;
}
await Task.Delay(duration, token);
//image.Source.Freeze();
}
});
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile("PlayVideo:" + ex.ToString(), "ProgControl");
}
});
action.BeginInvoke(null, null);
}
private void BindLottie(LottieAnimationView lottie, string img)
{
try
{
Task action = new Task(() =>
{
lottie.Dispatcher.Invoke(new Action(() =>
{
if (img.ToLower().EndsWith(".json"))
{
//自动播放
//lottie.AutoPlay = true;
//开启硬件加速
lottie.UseHardwareAcceleration(true);
lottie.FileName = img;
lottie.PlayAnimation();
}
}));
});
action.Start();
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile("imgPath:" + img + "------" + "BindImage:" + ex.ToString(), "ProgControl");
}
}
private void BindImage(Image image, string img)
{
try
{
Task action = new Task(() =>
{
image.Dispatcher.Invoke(new Action(() =>
{
if (img.ToLower().EndsWith(".gif"))
{
XamlAnimatedGif.AnimationBehavior.SetSourceUri(image, new Uri(img));
XamlAnimatedGif.AnimationBehavior.SetRepeatBehavior(image, RepeatBehavior.Forever);
XamlAnimatedGif.AnimationBehavior.SetAutoStart(image, true);
}
else
{
image.Source = new BitmapImage(new Uri(img));
image.Source.Freeze();
}
}));
});
action.Start();
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile("imgPath:" + img + "------" + "BindImage:" + ex.ToString(), "ProgControl");
}
}
private void StartVideo(MediaElement media, string videoPath, Image image, bool playAudio)
{
try
{
media.Dispatcher.Invoke(new Action(() =>
{
media.Source = new Uri(videoPath);
media.Position = ComParameters.Parameters.IsSync ? new TimeSpan(0, 0, 0, 0, 1000) : new TimeSpan(0, 0, 0, 0, 100);
media.Play();
media.Volume = playAudio ? 1 : 0;
}));
Thread.Sleep(800);
image.Dispatcher.Invoke(new Action(() =>
{
image.Visibility = Visibility.Hidden;
}));
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile("StartVideo:" + ex.ToString(), "ProgControl");
}
}
private void StopVideo(MediaElement media)
{
try
{
media.Dispatcher.Invoke(new Action(() =>
{
media.Stop();
}));
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile("StopVideo:" + ex.ToString(), "ProgControl");
}
}
private void VideoStart(string urlVideo1, string imgVideo)
{
try
{
string urlVideo = urlVideo1.Replace("/", @"\");
using (System.Diagnostics.Process process = new System.Diagnostics.Process())
{
process.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + "ffmpeg.exe";
process.StartInfo.Arguments = @"-i" + " " + urlVideo + " " + "-ss 0.1 -f image2 " + " " + imgVideo;
process.StartInfo.CreateNoWindow = true;
process.Start();
}
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile("VideoStart:" + ex.ToString(), "ProgControl");
}
}
public void Dispose()
{
try
{
isStop = true;
foreach (var item in labels)
{
canvas.Children.Remove(item);
}
labels.Clear();
foreach (var item in VlcLists)
{
item.SourceProvider.MediaPlayer.Pause();
item.Dispose();
canvas.Children.Remove(item);
}
VlcLists.Clear();
foreach (var item in tasks)
{
if (item != null && (item.IsCanceled || item.IsCompleted || item.IsFaulted))
{
item.Dispose();
}
}
tasks.Clear();
foreach (var item in marqueeControlNews)
{
item.Dispose();
canvas.Children.Remove(item);
}
marqueeControlNews.Clear();
foreach (var item in clockControls)
{
item.Dispose();
canvas.Children.Remove(item);
}
clockControls.Clear();
foreach (var item in weatherControls)
{
item.Dispose();
canvas.Children.Remove(item);
}
weatherControls.Clear();
foreach (var item in cancellationTokens)
{
item.Cancel();
}
cancellationTokens.Clear();
foreach (var item in mediaElements)
{
item.Stop();
}
mediaElements.Clear();
foreach (var item in imageControls)
{
canvas.Children.Remove(item);
}
imageControls.Clear();
foreach (var item in lottieView)
{
item.Dispose();
canvas.Children.Remove(item);
}
lottieView.Clear();
foreach (var item in mediaControls)
{
item.Stop();
item.Close();
}
mediaControls.Clear();
foreach (var item in chromiumWebBrowsers)
{
item.Dispose();
canvas.Children.Remove(item);
}
chromiumWebBrowsers.Clear();
foreach (var item in webView2)
{
item.Dispose();
canvas.Children.Remove(item);
}
foreach (var item in canvasWebBrowsers)
{
canvas.Children.Remove(item);
}
canvasWebBrowsers.Clear();
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile("Dispose:" + ex.ToString(), "ProgControl");
}
}
}
}