using System; using System.Collections.Generic; using System.Linq; using System.Text; using PCScreenSavers.Model; using Transitionals; using System.IO; using System.Net; using PCScreenSavers.Win; using PCScreenSavers.ViewModel; using System.Configuration; using PCScreenSavers.Common; using System.Threading; using System.Windows; using Newtonsoft.Json; namespace PCScreenSavers.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 needDel = new List(); private List needDownLoad = new List(); 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; } /// /// 通过对比本地和网络数据来看是否需要下载或更改播单 /// /// public void DownOutLineFiles(ScreenSavers resource) { try { if (resource == null) return; DirectoryInfo dir = new DirectoryInfo(outLinePath); List localFilesPath = new List(); List remoteFilesPath = new List(); needDownLoad.Clear(); needDel.Clear(); if (dir.Exists) { FileInfo[] fiList = dir.GetFiles(); foreach (var item in fiList) { if (!Path.GetExtension(item.FullName).Contains("temp")) { log.WriteLogFile(item.FullName,"12111"); 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 file= item.Replace("\\", "/"); 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) { log.WriteLogFile(Path.GetFileNameWithoutExtension(item), "12111"); log.WriteLogFile(remoteFilesPath.FindAll(p => p.Contains(Path.GetFileNameWithoutExtension(item))).Count.ToString(), "12111"); 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 > 0) { //删除多余的文件 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"); } } needDel.Clear(); //} } if (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) { 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; } //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(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(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(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(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(resource)); sw.Close(); fs.Close(); } App.Downloaddone = true; } return; } } catch (Exception ex) { log.WriteLogFile(ex.ToString(), "OutLineLog"); } } /// /// 通过对比本地和网络数据来看是否需要删除过期文件 /// /// public void DeleteOutTimeFiles(ScreenSavers resource) { try { if (resource == null) return; DirectoryInfo dir = new DirectoryInfo(outLinePath); List localFilesPath = new List(); List remoteFilesPath = new List(); 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"); } } /// /// 获取后台图片或视频资源地址 /// /// public List> GetResource() { try { List> tpList = new List>(); while (!File.Exists(outLinePath + "/temp.xml"))//等待更新播放单 { //Thread.Sleep(200); return null; } ScreenSavers result = new ScreenSavers(); using (StreamReader sr = new StreamReader(outLinePath + "/temp.xml")) { //MessageBox.Show(sr.ReadToEnd()); result = CommonMethod.DeserializeXML(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 tp = new Tuple(item, t); tpList.Add(tp); } } return tpList; } catch (Exception ex) { log.WriteLogFile(ex.ToString(), "OutLineLog"); return null; } } /// /// 下载离线数据 /// /// /// 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++) { try { needDownLoad[i] = needDownLoad[i].Replace("\\", "/"); 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); log.WriteLogFile("http://" + app.HttpUrl + needDownLoad[i], "OutLineLog"); HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://" + app.HttpUrl + 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("文件未下载完毕", "文件1111111"); } 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(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(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(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(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(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; } } } }