using BreakpointTransmission.Client;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
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.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CL.IO.Zip;
using Lib = ESSupport.Lib;
using System.Threading.Tasks;
using System.Web;
using System.Diagnostics;
using System.Configuration;
namespace EShangUpdateRelease
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
private enum UpdateType
{
ServiceSite,
PosRelease
}
string UpLoadPack { get; set; }
//CreateRequestHeadersHandler _Handler = new CreateRequestHeadersHandler();
private BreakpointUploader uploader;
private BackgroundWorker _ReleaseBackgroundWorker;
private IAsyncResult asyncResult;
private string UploadUri;
public MainWindow()
{
InitializeComponent();
try
{
UploadUri = ConfigurationManager.AppSettings["DefaultUploadUrl"];
}
catch
{
UploadUri = "http://192.168.11.192:11000";
}
uploader = new BreakpointUploader(1024 * 1024 * 1, UploadType.Append);
uploader.OnUploading += Uploader_OnUploading;
_ReleaseBackgroundWorker = new BackgroundWorker();
_ReleaseBackgroundWorker.DoWork += BackgroundWorker_DoWork;
_ReleaseBackgroundWorker.WorkerReportsProgress = true;
_ReleaseBackgroundWorker.WorkerSupportsCancellation = true;
cboServerpark.Items.Add("ServiceInstall");
cboServerpark.Items.Add("ServiceUpdate");
}
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
string[] _EventArgs = e.Argument as string[];
if (_EventArgs.Count() == 0)
{
return;
}
string _FolderPath = _EventArgs[0];
int _Count = 0;
Lib.FileHelper.DeleteFiles(_FolderPath, new string[] { ".pw" }, false, false, ref _Count);
ZipHandler _ZipHandler = ZipHandler.GetInstance();
string _ZipPath = AppDomain.CurrentDomain.BaseDirectory + _FolderPath.Substring(_FolderPath.LastIndexOf("\\") + 1) + ".zip";
string _PassWord = DateTime.Now.ToString("yyyyMMddHHmmss");
FileStream _FileStream = File.Create(_FolderPath + "\\" + _PassWord + ".pw");
_FileStream.Close();
_ZipHandler.PackDirectory(_FolderPath, _ZipPath, _PassWord, (ProgressPercentage) =>
{
MessageBeginInvoke(ProgressPercentage.ToString(), "downProgress", "ProgressBar");
});
//CreateRequestHeadersHandler _Handler = new CreateRequestHeadersHandler("ServiceSite");
Upload(_ZipPath, UploadUri, "ServiceSite");
}
void Uploader_OnUploading(object sender, UploadEventArgs e)
{
if (e.Length == 0L)
{
MessageBeginInvoke("0", "downProgress", "ProgressBar");
}
else
{
MessageBeginInvoke(((decimal)e.Position / e.Length * 100).ToString("F2"), "downProgress", "ProgressBar");
}
}
private void Upload(string filePath, string uploadUrl, string uploadType, string updateContent = "")
{
try
{
UploadStatus status = uploader.Upload(filePath, uploadUrl);
if (status == UploadStatus.Completed)
{
System.Threading.Thread.Sleep(1000);
MessageBeginInvoke("100", "downProgress", "ProgressBar");
if (uploadType == "PosRelease")
{
string _MD5 = Lib.FileHelper.GetMD5ByMD5CryptoService(filePath);
string _FileName = System.IO.Path.GetFileNameWithoutExtension(filePath);
object[] args = { _FileName, _MD5, uploadType, updateContent };
string _Result = Lib.WSHelper.InvokeWebService(UploadUri + "/Update.asmx", "UpdateRelease", args).ToString();
MessageBox.Show(_Result);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Button _Button = sender as Button;
System.Windows.Forms.FolderBrowserDialog _FolderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog
{
Description = "请选择更新文件所在的文件夹"
};
OpenFileDialog _OpenFileDialog = new OpenFileDialog
{
InitialDirectory = AppDomain.CurrentDomain.BaseDirectory
};
switch (_Button.Name)
{
case "BtFolderBrowser":
if (_FolderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
TxtPath.Text = _FolderBrowserDialog.SelectedPath;
if (File.Exists(System.IO.Path.Combine(_FolderBrowserDialog.SelectedPath, "TouchCashier.exe")))
{
TxtSoftVersion.Text = FileVersionInfo.GetVersionInfo(System.IO.Path.Combine(_FolderBrowserDialog.SelectedPath, "TouchCashier.exe")).ProductVersion;
}
else if (File.Exists(System.IO.Path.Combine(_FolderBrowserDialog.SelectedPath, "cashier.exe")))
{
TxtSoftVersion.Text = FileVersionInfo.GetVersionInfo(System.IO.Path.Combine(_FolderBrowserDialog.SelectedPath, "cashier.exe")).ProductVersion;
}
}
break;
case "BtFilePack":
if (RBtService.IsChecked == true)
{
string _FolderPath = TxtPath.Text.Trim();
string _FileType = cboServerpark.Text;
if (String.IsNullOrWhiteSpace(_FileType))
{
MessageBox.Show("请选择安装包类型");
return;
}
if (String.IsNullOrWhiteSpace(_FolderPath))
{
return;
}
string _FilePath = AppDomain.CurrentDomain.BaseDirectory + "UpdatePack\\" + _FileType;
TaskFactory _Task = new TaskFactory();
_Task.StartNew(() =>
{
int _Count = 0;
Lib.FileHelper.CopyOldFilesToNewFiles(_FolderPath, _FilePath);
Lib.FileHelper.DeleteFiles(_FilePath, new string[] { ".pw" }, false, false, ref _Count);
ZipHandler _ZipHandler = ZipHandler.GetInstance();
string _ZipPath = AppDomain.CurrentDomain.BaseDirectory + _FilePath.Substring(_FilePath.LastIndexOf("\\") + 1) + ".zip";
UpLoadPack = _ZipPath;
string _PassWord = DateTime.Now.ToString("yyyyMMddHHmmss");
FileStream _FileStream = File.Create(_FilePath + "\\" + _PassWord + ".pw");
_FileStream.Close();
_ZipHandler.PackDirectory(_FilePath, _ZipPath, _PassWord, (ProgressPercentage) =>
{
MessageBeginInvoke(ProgressPercentage.ToString(), "downProgress", "ProgressBar");
});
MessageBox.Show("打包完成");
});
}
else if (RBtPos.IsChecked == true)
{
string _FolderPath = TxtPath.Text.Trim();
string _SoftVersion = TxtSoftVersion.Text.Trim();
if (String.IsNullOrWhiteSpace(_SoftVersion))
{
MessageBox.Show("请输入版本号");
return;
}
if (String.IsNullOrWhiteSpace(_FolderPath))
{
return;
}
string _FilePath = AppDomain.CurrentDomain.BaseDirectory + "UpdatePack\\" + _SoftVersion;
//if (!Directory.Exists(_FilePath + "\\UpdateFiles\\"))
//{
// Directory.CreateDirectory(_FilePath + "\\UpdateFiles\\");
//}
TaskFactory _Task = new TaskFactory();
_Task.StartNew(() =>
{
int _Count = 0;
Lib.FileHelper.CopyOldFilesToNewFiles(_FolderPath, _FilePath + "\\UpdateFiles\\", ".rar");
Lib.FileHelper.DeleteFiles(_FilePath, new string[] { ".pw" }, false, false, ref _Count);
System.Threading.Thread.Sleep(1000);
ZipHandler _ZipHandler = ZipHandler.GetInstance();
string _ZipPath = AppDomain.CurrentDomain.BaseDirectory + _FilePath.Substring(_FilePath.LastIndexOf("\\") + 1) + ".zip";
UpLoadPack = _ZipPath;
string _PassWord = DateTime.Now.ToString("yyyyMMddHHmmss");
FileStream _FileStream = File.Create(_FilePath + "\\" + _PassWord + ".pw");
_FileStream.Close();
_ZipHandler.PackDirectory(_FilePath, _ZipPath, _PassWord, (ProgressPercentage) =>
{
MessageBeginInvoke(ProgressPercentage.ToString(), "downProgress", "ProgressBar");
});
MessageBox.Show("打包完成");
});
}
break;
case "BtFileUpload":
if (asyncResult == null || asyncResult.IsCompleted)
{
if (RBtService.IsChecked == true)
{
if (_OpenFileDialog.ShowDialog() == true)
{
asyncResult = new Action((filePath, uploadUrl, uploadType) =>
{
Header.M_Headers = new KeyValuePair("x-file-type", HttpUtility.UrlEncode(uploadType));
Upload(filePath, uploadUrl, uploadType);
//_ReleaseBackgroundWorker.RunWorkerAsync(new string[] { _FolderBrowserDialog.SelectedPath, "ServiceSite" });
}).BeginInvoke(_OpenFileDialog.FileName, UploadUri + "/UploadRelease", "ServiceSite", null, null);
}
}
else if (RBtPos.IsChecked == true)
{
if (_OpenFileDialog.ShowDialog() == true)
{
asyncResult = new Action((filePath, uploadUrl, uploadType, updateContent) =>
{
Header.M_Headers = new KeyValuePair("x-file-type", HttpUtility.UrlEncode(uploadType));
//CreateRequestHeadersHandler _Handler = new CreateRequestHeadersHandler(uploadType);
Upload(filePath, uploadUrl, uploadType, updateContent);
}).BeginInvoke(_OpenFileDialog.FileName, UploadUri + "/UploadRelease", "PosRelease", txtUpdateContent.Text.Trim(), null, null);
}
//_ReleaseBackgroundWorker.RunWorkerAsync(new string[] { _FolderBrowserDialog.SelectedPath, "PosRelease" });
}
else
{
MessageBox.Show("请选择上传类型");
}
}
break;
}
}
#region 控件操作委托
///
/// 控件线程委托
///
/// 消息内容
/// 控件名称
/// 控件类型
delegate void DelegateMessage(string Msg, string ControlName, string ControlType);
///
/// 线程委托操作事件
///
/// 消息内容
/// 控件名称
/// 控件类型
private void MessageBeginInvoke(string Msg, string ControlName, string ControlType)
{
Dispatcher.BeginInvoke(new DelegateMessage(MessageHandle), new object[] { Msg, ControlName, ControlType });
}
///
/// 线程消息操作
///
/// 消息内容
/// 控件名称
/// 控件类型
private void MessageHandle(string Msg, string ControlName, string ControlsType)
{
switch (ControlsType)
{
case "ProgressBar":
if (double.TryParse(Msg, out double _Value))
{
ProgressBar _ProgressBar = (ProgressBar)FindName(ControlName);
_ProgressBar.Value = _Value;
}
break;
case "Label":
Label _Label = (Label)FindName(ControlName);
_Label.Content = Msg;
break;
case "Image":
Image _Image = (Image)FindName(ControlName);
_Image.Source = new BitmapImage(new Uri(Msg, UriKind.Relative));
break;
}
}
#endregion
}
}