📄 frmmain.cs
字号:
#region Copyright ArtfulBits Inc. 2005 - 2008
//
// Copyright ArtfulBits Inc. 2005 - 2008. All rights reserved.
//
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// info@artfulbits.com. Re-distribution in any form is strictly
// prohibited. Any infringement will be prosecuted under applicable laws.
//
#endregion
#region file using
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using System.Diagnostics;
using Artfulbits.Utilities.Diagnostics;
#endregion
namespace OpenBoxUpdater
{
/// <summary>
///
/// </summary>
public partial class frmMain : Form
{
#region Class members
/// <summary>
///
/// </summary>
private QueueTraceListener m_listener = new QueueTraceListener();
/// <summary></summary>
private int m_secondsWait;
/// <summary></summary>
private readonly WebClient m_webClient = new WebClient();
#endregion
#region Class properties
/// <summary></summary>
public bool HasFileName
{
get
{
return ( !string.IsNullOrEmpty( txtFilePath.Text ) && File.Exists( txtFilePath.Text ) );
}
}
/// <summary></summary>
public int BaudRate
{
get
{
int result;
if( !int.TryParse( txtBaudeRate.Text, out result ) )
return 115200;
return result;
}
}
/// <summary></summary>
public string PortName
{
get
{
return txtComPort.Text;
}
}
/// <summary></summary>
public bool HasProxy
{
get
{
return ( mnuProxyUrl.Text != null &&
mnuProxyUrl.Text.IndexOf( "Example:" ) < 0 );
}
}
#endregion
#region Class initialize/finalize methods
/// <summary></summary>
public frmMain()
{
InitializeComponent();
m_listener.OnWriteLine += Listener_OnWriteLine;
Trace.Listeners.Add( m_listener );
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose( bool disposing )
{
if( disposing && ( components != null ) )
{
components.Dispose();
Trace.Listeners.Remove( m_listener );
m_listener.Close();
m_listener = null;
}
base.Dispose( disposing );
}
#endregion
#region Event handlers
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmMain_Load( object sender, EventArgs e )
{
CreateDynamicComPorts();
UpdateGuiControls();
}
/// <summary></summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void mnuOpen_Click( object sender, EventArgs e )
{
if( this.HasFileName )
{
openFileDlg.InitialDirectory = Path.GetDirectoryName( txtFilePath.Text );
}
if( openFileDlg.ShowDialog() == DialogResult.OK )
{
txtFilePath.Text = openFileDlg.FileName;
UpdateGuiControls();
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void mnuClose_Click( object sender, EventArgs e )
{
if( threadUpdate.IsBusy )
{
threadUpdate.CancelAsync();
}
Close();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void mnuComs_Click( object sender, EventArgs e )
{
ToolStripMenuItem strip = sender as ToolStripMenuItem;
ToolStripItemCollection items = mnuComPorts.DropDownItems;
foreach( ToolStripItem item in items )
{
if( item is ToolStripSeparator )
continue;
ToolStripMenuItem menu = item as ToolStripMenuItem;
if( menu != null )
{
menu.Checked = ( menu == strip );
if( menu == strip )
{
txtComPort.Text = ( string )strip.Tag;
}
}
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void mnuBaudRate_Click( object sender, EventArgs e )
{
ToolStripMenuItem strip = sender as ToolStripMenuItem;
ToolStripMenuItem[] items = new ToolStripMenuItem[] { mnu009600, mnu019200, mnu115200, mnu128000 };
foreach( ToolStripMenuItem item in items )
{
item.Checked = ( item == strip );
if( item == strip )
{
txtBaudeRate.Text = ( string )strip.Tag;
}
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSearch_Click( object sender, EventArgs e )
{
mnuOpen_Click( sender, e );
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void stripBtnUpdate_Click( object sender, EventArgs e )
{
// cancel thread if it works
if( threadUpdate.IsBusy )
{
threadUpdate.CancelAsync();
}
threadUpdate.RunWorkerAsync( txtFilePath.Text );
UpdateGuiControls();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void stripBtnCancel_Click( object sender, EventArgs e )
{
if( threadUpdate.IsBusy )
{
threadUpdate.CancelAsync();
}
UpdateGuiControls();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void mnuResetLog_Click( object sender, EventArgs e )
{
lstLogViewer.BeginUpdate();
lstLogViewer.Items.Clear();
lstLogViewer.EndUpdate();
lblCounter.Text = string.Format( "Quantity of records: {0}", lstLogViewer.Items.Count );
}
/// <summary></summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void mnuAbout_Click( object sender, EventArgs e )
{
using( frmAboutDialog form = new frmAboutDialog() )
{
form.ShowDialog( this );
}
}
/// <summary></summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void mnuCheckFirmware_Click( object sender, EventArgs e )
{
mnuCheckFirmware.Enabled = false;
mnuFoundFirmwares.DropDownItems.Clear();
if( this.HasProxy )
{
m_webClient.Proxy = new WebProxy( mnuProxyUrl.Text );
}
CredentialCache cache = new CredentialCache();
cache.Add( new Uri( mnuUrl.Text ), "Basic", new NetworkCredential( mnuLogin.Text, mnuPassword.Text ) );
m_webClient.Credentials = cache;
Stream stream = m_webClient.OpenRead( mnuUrl.Text );
StreamReader reader = new StreamReader( stream );
string body = reader.ReadToEnd();
Regex search = new Regex( @"(?<url>http[a-z0-9.:/\-]+/(?<filename>[a-z0-9_.]+\.zip))", RegexOptions.IgnoreCase );
MatchCollection matches = search.Matches( body );
// make found url's unique
Dictionary<string, Match> unique = new Dictionary<string, Match>();
foreach( Match match in matches )
{
unique[ match.Value ] = match;
}
// create on each unique item - menuitem
foreach( Match match in unique.Values )
{
ToolStripMenuItem menuItem = new ToolStripMenuItem();
menuItem.Text = match.Groups[ "filename" ].Value;
menuItem.Tag = match.Groups[ "url" ].Value;
menuItem.Click += DownloadFirmwareZip_Click;
mnuFoundFirmwares.DropDownItems.Add( menuItem );
}
}
/// <summary></summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DownloadFirmwareZip_Click( object sender, EventArgs e )
{
ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
if( menuItem != null )
{
CredentialCache cache = ( CredentialCache )m_webClient.Credentials;
cache.Add( new Uri( ( string )menuItem.Tag ), "Basic", new NetworkCredential( mnuLogin.Text, mnuPassword.Text ) );
string path = Path.Combine( Path.GetTempPath(), menuItem.Text );
_tracer.Info( "Start downloading file from web site..." );
_tracer.Info( "Download URL is: " + ( string )menuItem.Tag );
m_webClient.DownloadFile( ( string )menuItem.Tag, path );
_tracer.Info( "GOOD! Archive downloaded into temporary folder: " + Path.GetTempPath() );
_tracer.Info( "Start unzipping firmware..." );
List<string> files = ZipHelper.UnZipFirmwareArchive( path );
_tracer.Info( "GOOD! unzipping done. Found files quantity: " + files.Count );
if( files.Count > 0 )
{
txtFilePath.Text = files[ 0 ];
_tracer.Info( "GOOD! Set firmware file path: " + files[ 0 ] );
}
}
}
#endregion
#region Helper methods
/// <summary></summary>
private void CreateDynamicComPorts()
{
string[] _coms = new string[] { "COM1", "COM2" };
string[] ports = SerialPort.GetPortNames();
mnuSeparator2.Visible = false;
foreach( string port in ports )
{
if( Array.IndexOf( _coms, port.ToUpper() ) < 0 )
{
ToolStripMenuItem menuItem = new ToolStripMenuItem();
menuItem.Tag = port;
menuItem.Text = port;
menuItem.Click += mnuComs_Click;
mnuComPorts.DropDownItems.Add( menuItem );
mnuSeparator2.Visible = true;
}
}
}
/// <summary></summary>
private void UpdateGuiControls()
{
stripBtnCancel.Enabled = threadUpdate.IsBusy;
stripBtnUpdate.Enabled = ( !stripBtnCancel.Enabled && this.HasFileName );
// lock menu's
if( this.HasFileName )
{
btnSearch.Enabled = mnuOpen.Enabled = mnuSerialPort.Enabled = stripBtnUpdate.Enabled;
}
}
#endregion
#region Log file
/// <summary></summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Listener_OnWriteLine( object sender, EventArgs<string> e )
{
if( this.InvokeRequired )
{
this.BeginInvoke( new EventHandler<EventArgs<string>>( Listener_OnWriteLine ), sender, e );
}
else
{
int index = this.lstLogViewer.Items.Add( e.Data );
if( this.lstLogViewer.Items.Count > QueueTraceListener.MaxMessages )
{
int temp = this.lstLogViewer.SelectedIndex;
this.lstLogViewer.Items.RemoveAt( 0 );
this.lstLogViewer.SelectedIndex = Math.Max( 0, temp - 1 ); // be polite to user selection
index--;
}
if( chkAutoScroll.Checked )
{
this.lstLogViewer.SelectedIndex = index;
}
lblCounter.Text = string.Format( "Quantity of records: {0}", this.lstLogViewer.Items.Count );
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lstLogViewer_DrawItem( object sender, DrawItemEventArgs e )
{
if( e.Index >= 0 )
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -