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.

626 lines
26 KiB

using Container.Common;
using Container.Model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Transitionals;
namespace Container.Business
{
class OutLine
{
App app = ((App)Application.Current);
private string outLinePath = Environment.CurrentDirectory + "/OutlineFiles";
//private string localIP = ConfigurationManager.AppSettings["LocalIP"];
//private string remoteAddress = ConfigurationManager.AppSettings["RemoteAddress"];
private List<string> needDel = new List<string>();
private List<string> needDownLoad = new List<string>();
Class_Log log = new Class_Log();
private static readonly object lockHelper = new object();
private static OutLine OutLineInstance = null;
private OutLine()
{
}
public static OutLine CreateInstance()
{
if (OutLineInstance == null)
{
lock (lockHelper)
{
if (OutLineInstance == null)
{
OutLineInstance = new OutLine();
}
}
}
return OutLineInstance;
}
/// <summary>
/// 通过对比本地和网络数据来看是否需要下载或更改播单
/// </summary>
/// <returns></returns>
public void DownOutLineFiles(ScreenSavers resource)
{
try
{
if (resource == null)
return;
DirectoryInfo dir = new DirectoryInfo(outLinePath);
List<string> localFilesPath = new List<string>();
List<string> remoteFilesPath = new List<string>();
needDownLoad.Clear();
needDel.Clear();
if (dir.Exists)
{
FileInfo[] fiList = dir.GetFiles();
foreach (var item in fiList)
{
if (!Path.GetExtension(item.FullName).Contains("temp"))
{
localFilesPath.Add(item.FullName);
}
}
}
else
Directory.CreateDirectory(outLinePath);
if (resource.ResList.Count > 0)
{
foreach (var item in resource.ResList)
remoteFilesPath.Add(item.img);
}
//else
// return;
foreach (var item in remoteFilesPath)
{
var httpPath = item.Substring(0, item.IndexOf("://") + 3);
var fileBackPath = item.Substring(item.IndexOf("://") + 3).Replace("//", "/").Replace("\\", "/");
var file = httpPath + fileBackPath;
var names = file.Split('/');
var fileName = file;
if (names.Length > 0)
{
fileName = names[names.Length - 2];
}
if (localFilesPath.FindAll(p => p.Contains(fileName)).Count <= 0)
{
//if (!localFilesPath.Contains(fileName)) {
needDownLoad.Add(item);
}
//if (localFilesPath.FindAll(p => p.EndsWith(Path.GetFileName(item))).Count <= 0)
// needDownLoad.Add(item);
}
foreach (var item in localFilesPath)
{
if (remoteFilesPath.FindAll(p => p.Contains(Path.GetFileNameWithoutExtension(item))).Count <= 0)
{
needDel.Add(item);
}
//if (remoteFilesPath.FindAll(p => p.EndsWith(Path.GetFileName(item))).Count <= 0)
// needDel.Add(item);
}
if (needDel.Count > 1 || needDownLoad.Count > 0 || !File.Exists(Environment.CurrentDirectory + "/OutlineFiles" + "/temp.xml"))
DownLoadFiles(resource);
else
{
if (resource.ResList.Count < 0)
return;
foreach (var item in resource.ResList)
{
var httpPath = item.img.Substring(0, item.img.IndexOf("://") + 3);
var fileBackPath = item.img.Substring(item.img.IndexOf("://") + 3).Replace("//", "/").Replace("\\", "/");
item.img = httpPath + fileBackPath;
log.WriteLogFile("原图片地址" + item.img, "image");
if (item.img.Contains("/"))
{
var names = item.img.Split('/');
log.WriteLogFile("names个数" + names.Length, "image");
if (names.Length > 0)
{
item.img = outLinePath + "/" + names[names.Length - 2] + names[names.Length - 1].Substring(names[names.Length - 1].LastIndexOf('.'));
}
else
{
item.img = outLinePath + item.img.Substring(item.img.LastIndexOf("/"));
}
log.WriteLogFile("下载后地址" + item.img, "image");
}
else
item.img = outLinePath + item.img;
}
//string xmlPath = outLinePath + "/temp.xml";
//if (File.Exists(xmlPath))
// File.Delete(xmlPath);
////写入离线配置文件
//using (Stream fs = new FileStream(xmlPath, FileMode.Create))
//{
// StreamWriter sw = new StreamWriter(fs);
// sw.Write(CommonMethod.SerializeXML<ScreenSavers>(resource));
// sw.Close();
// fs.Close();
//}
ScreenSavers result = new ScreenSavers();
if (File.Exists(outLinePath + "/temp.xml"))
{
using (StreamReader sr = new StreamReader(outLinePath + "/temp.xml"))
{
result = CommonMethod.DeserializeXML<ScreenSavers>(sr.ReadToEnd());
sr.Close();
}
if (result != null)
{
ScreenSavers result1 = new ScreenSavers();
if (!result.Equals(resource))
{
string xmlPath = outLinePath + "/temp.xml";
//if (File.Exists(xmlPath))
// File.Delete(xmlPath);
//写入离线配置文件
using (Stream fs = new FileStream(xmlPath, FileMode.Create))
{
StreamWriter sw = new StreamWriter(fs);
sw.Write(CommonMethod.SerializeXML<ScreenSavers>(resource));
sw.Close();
fs.Close();
}
App.Downloaddone = true;
}
}
else
{
string xmlPath = outLinePath + "/temp.xml";
//if (File.Exists(xmlPath))
// File.Delete(xmlPath);
//写入离线配置文件
using (Stream fs = new FileStream(xmlPath, FileMode.Create))
{
StreamWriter sw = new StreamWriter(fs);
sw.Write(CommonMethod.SerializeXML<ScreenSavers>(resource));
sw.Close();
fs.Close();
}
App.Downloaddone = true;
}
}
else
{
string xmlPath = outLinePath + "/temp.xml";
//if (File.Exists(xmlPath))
// File.Delete(xmlPath);
//写入离线配置文件
using (Stream fs = new FileStream(xmlPath, FileMode.Create))
{
StreamWriter sw = new StreamWriter(fs);
sw.Write(CommonMethod.SerializeXML<ScreenSavers>(resource));
sw.Close();
fs.Close();
}
App.Downloaddone = true;
}
return;
}
}
catch (Exception ex)
{
log.WriteLogFile(ex.ToString(), "OutLineLog");
}
}
/// <summary>
/// 通过对比本地和网络数据来看是否需要删除过期文件
/// </summary>
/// <param name="resource"></param>
public void DeleteOutTimeFiles(ScreenSavers resource)
{
try
{
if (resource == null)
return;
DirectoryInfo dir = new DirectoryInfo(outLinePath);
List<string> localFilesPath = new List<string>();
List<string> remoteFilesPath = new List<string>();
needDel.Clear();
if (dir.Exists)
{
FileInfo[] fiList = dir.GetFiles();
foreach (var item in fiList)
{
if (!Path.GetExtension(item.FullName).Contains("temp"))
{
localFilesPath.Add(item.FullName);
}
}
}
else
return;
if (resource.ResList.Count > 0)
{
foreach (var item in resource.ResList)
remoteFilesPath.Add(item.img);
}
else
return;
foreach (var item in localFilesPath)
{
if (remoteFilesPath.FindAll(p => p.Contains(Path.GetFileNameWithoutExtension(item))).Count <= 0)
{
needDel.Add(item);
}
//if (remoteFilesPath.FindAll(p => p.EndsWith(Path.GetFileName(item))).Count <= 0)
// needDel.Add(item);
}
//删除多余的文件
if (needDel.Count > 1)
{
for (int i = 0; i < needDel.Count; i++)
{
try
{
if (File.Exists(needDel[i]) && Path.GetFileName(needDel[i]) != "temp.xml")
File.Delete(needDel[i]);
}
catch (Exception ex)
{
log.WriteLogFile(ex.ToString(), "OutLineLog");
}
}
}
}
catch (Exception ex)
{
log.WriteLogFile(ex.ToString(), "OutLineLog");
}
}
/// <summary>
/// 获取后台图片或视频资源地址
/// </summary>
/// <returns></returns>
public List<Tuple<ResInfo, Transition>> GetResource()
{
try
{
List<Tuple<ResInfo, Transition>> tpList = new List<Tuple<ResInfo, Transition>>();
while (!File.Exists(outLinePath + "/temp.xml"))//等待更新播放单
{
Thread.Sleep(200);
}
ScreenSavers result = new ScreenSavers();
using (StreamReader sr = new StreamReader(outLinePath + "/temp.xml"))
{
//MessageBox.Show(sr.ReadToEnd());
result = CommonMethod.DeserializeXML<ScreenSavers>(sr.ReadToEnd());
sr.Close();
}
var curDate = DateTime.Now;
var list = result.ResList.Where(i => !string.IsNullOrEmpty(i.img) && Convert.ToDateTime(i.launchtime) < curDate && Convert.ToDateTime(i.expirydate) > curDate.AddDays(-1)).ToList();
if (list == null || list.Count == 0)
return tpList;
foreach (var item in list)
{
if (!string.IsNullOrEmpty(item.img))
{
Transition t = CommonMethod.GetTransition(item.effect);
Tuple<ResInfo, Transition> tp = new Tuple<ResInfo, Transition>(item, t);
tpList.Add(tp);
}
}
return tpList;
}
catch (Exception ex)
{
log.WriteLogFile(ex.ToString(), "OutLineLog");
return null;
}
}
public List<ProgModel> LoadScreenConfig()
{
string outLinePath = AppDomain.CurrentDomain.BaseDirectory + "/OutlineFiles";
if (!Directory.Exists(outLinePath + "/Main"))
{
return null;
}
else
{
if (File.Exists(outLinePath + "/Main/screenconfig.txt"))
{
string rev = DownLoadMethod.ReadContext(outLinePath + "/Main/screenconfig.txt");
if (!string.IsNullOrEmpty(rev))
{
var Jo = JObject.Parse(rev);
string code = Jo.Value<string>("code");
if (code == "200")
{
List<ScheduleModel> itemList = Jo.Value<JArray>("data").ToObject<List<ScheduleModel>>();
var Schedule = itemList.Where(i => i.date == DateTime.Now.ToString("yyyy-MM-dd")).FirstOrDefault();
if (Schedule != null)
{
return Schedule.schedule;
}
else
{
return null;
}
}
else
{
return null;
}
}
else
{
return null;
}
}
else
{
return null;
}
}
}
/// <summary>
/// 下载离线数据
/// </summary>
/// <param name="viewModel"></param>
/// <returns></returns>
bool done = false;
private bool DownLoadFiles(ScreenSavers resource)
{
if (!done)
{
done = true;
log.WriteLogFile("开始下载", "exelog");
try
{
if (needDownLoad.Count > 0)
{
//下载缺少的文件
for (int i = 0; i < needDownLoad.Count; i++)
{
log.WriteLogFile(needDownLoad[i], "downfile");
try
{
var httpPath = needDownLoad[i].Substring(0, needDownLoad[i].IndexOf("://") + 3);
var fileBackPath = needDownLoad[i].Substring(needDownLoad[i].IndexOf("://") + 3).Replace("//", "/").Replace("\\", "/");
needDownLoad[i] = httpPath + fileBackPath;
string localFile = string.Empty;
if (needDownLoad[i].Contains("/"))
{
var names = needDownLoad[i].Split('/');
if (names.Length > 0)
{
localFile = outLinePath + "/" + names[names.Length - 2] + names[names.Length - 1].Substring(names[names.Length - 1].LastIndexOf('.'));
}
else
{
localFile = outLinePath + needDownLoad[i].Substring(needDownLoad[i].LastIndexOf("/"));
}
}
else
localFile = outLinePath + needDownLoad[i];
if (!File.Exists(localFile))//&& !File.Exists(localFile + ".temp")
{
log.WriteLogFile("开始下载" + localFile, "exelog");
if (File.Exists(localFile + ".temp"))
File.Delete(localFile + ".temp");
using (Stream fileStream = new FileStream(localFile + ".temp", FileMode.OpenOrCreate))
{
try
{
//fileStream.Seek(fileStream.Length, SeekOrigin.Current);
//HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://" + app.HttpUrl + needDownLoad[i]);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(needDownLoad[i]);
request.AddRange(fileStream.Length);
HttpWebResponse respone = (HttpWebResponse)request.GetResponse();
using (Stream netStream = respone.GetResponseStream())
{
long totalDownloadedByte = 0;
byte[] read = new byte[1024];
int realReadLen = netStream.Read(read, 0, read.Length);
while (realReadLen > 0)
{
totalDownloadedByte = realReadLen + totalDownloadedByte;
fileStream.Write(read, 0, realReadLen);
realReadLen = netStream.Read(read, 0, read.Length);
System.Windows.Forms.Application.DoEvents();
}
netStream.Close();
}
if (fileStream.Length != respone.ContentLength)
{
log.WriteLogFile("文件未下载完毕", "文件");
}
fileStream.Close();
log.WriteLogFile("下载完成" + localFile, "exelog");
File.Move(localFile + ".temp", localFile);
}
catch (Exception ex)
{
log.WriteLogFile("下载失败" + localFile + ex.ToString(), "exelog");
File.Delete(localFile + ".temp");
log.WriteLogFile(ex.ToString(), "OutLineLog");
done = false;
return false;
}
}
}
}
catch (Exception ex)
{
log.WriteLogFile(ex.ToString(), "OutLineLog");
done = false;
//MessageBox.Show("http://" + remoteAddress + needDownLoad[i]+"文件下载失败"+ex.ToString());
return false;
}
}
}
foreach (var item in resource.ResList)
{
item.img = item.img.Replace("\\", "/");
if (item.img.Contains("/"))
{
var names = item.img.Split('/');
if (names.Length > 0)
{
item.img = outLinePath + "/" + names[names.Length - 2] + names[names.Length - 1].Substring(names[names.Length - 1].LastIndexOf('.'));
}
else
{
item.img = outLinePath + item.img.Substring(item.img.LastIndexOf("/"));
}
}
else
item.img = outLinePath + item.img;
//item.img = item.img.Replace("\\", "/");
//if (item.img.Contains("/"))
// item.img = outLinePath + item.img.Substring(item.img.LastIndexOf("/"));
//else
// item.img = outLinePath + item.img;
}
if (resource.ResList.Count < 0)
{
done = false;
return false;
}
//string xmlPath = outLinePath + "/temp.xml";
//if (File.Exists(xmlPath))
// File.Delete(xmlPath);
////写入离线配置文件
//using (Stream fs = new FileStream(xmlPath, FileMode.Create))
//{
// StreamWriter sw = new StreamWriter(fs);
// sw.Write(CommonMethod.SerializeXML<ScreenSavers>(resource));
// sw.Close();
// fs.Close();
//}
ScreenSavers result = new ScreenSavers();
if (File.Exists(outLinePath + "/temp.xml"))
{
using (StreamReader sr = new StreamReader(outLinePath + "/temp.xml"))
{
result = CommonMethod.DeserializeXML<ScreenSavers>(sr.ReadToEnd());
sr.Close();
}
ScreenSavers result1 = new ScreenSavers();
if (result != null)
{
if (!result.Equals(resource))
{
string xmlPath = outLinePath + "/temp.xml";
//if (File.Exists(xmlPath))
// File.Delete(xmlPath);
//写入离线配置文件
using (Stream fs = new FileStream(xmlPath, FileMode.Create))
{
StreamWriter sw = new StreamWriter(fs);
sw.Write(CommonMethod.SerializeXML<ScreenSavers>(resource));
sw.Close();
fs.Close();
}
App.Downloaddone = true;
}
}
else
{
string xmlPath = outLinePath + "/temp.xml";
//if (File.Exists(xmlPath))
// File.Delete(xmlPath);
//写入离线配置文件
using (Stream fs = new FileStream(xmlPath, FileMode.Create))
{
StreamWriter sw = new StreamWriter(fs);
sw.Write(CommonMethod.SerializeXML<ScreenSavers>(resource));
sw.Close();
fs.Close();
}
App.Downloaddone = true;
}
}
else
{
string xmlPath = outLinePath + "/temp.xml";
//if (File.Exists(xmlPath))
// File.Delete(xmlPath);
//写入离线配置文件
using (Stream fs = new FileStream(xmlPath, FileMode.Create))
{
StreamWriter sw = new StreamWriter(fs);
sw.Write(CommonMethod.SerializeXML<ScreenSavers>(resource));
sw.Close();
fs.Close();
}
App.Downloaddone = true;
}
done = false;
return true;
}
catch (Exception ex)
{
log.WriteLogFile(ex.ToString(), "OutLineLog");
done = false;
return false;
}
finally
{
done = false;
}
}
else
{
return false;
}
}
}
}