Commit e0d96526 by Joona Romppanen

Changed app exit to global hotkey hook. Cleaned up form1 and added loggr

1 parent f78ff2de
......@@ -20,7 +20,7 @@
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
......
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using WebCam;
using ImageFilters;
using System.IO;
using MoyaAdminLib;
using System.Diagnostics;
using MoyaSignup.Properties;
using ThermoPrinterLibrary;
namespace MoyaSignup
{
public partial class Form1 : Form
{
ThermoPrinter thermoPrinter;
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private const int ShutdownHotkey = 1;
private ThermoPrinter _thermoPrinter;
public bool AllowShutdown { get; set; }
//public delegate void ThermoPrinterEvent(ThermoPrinter.Models model);
//public event ThermoPrinterEvent SetPrinter;
public Form1()
{
RestClient.ApiApplicationKey = Properties.Settings.Default.ApiApplicationKey;
RestClient.ApiPass = Properties.Settings.Default.ApiPass;
RestClient.ApiUser = Properties.Settings.Default.ApiUser;
RestClient.ApiURL = Properties.Settings.Default.ApiURL;
RestClient.ApiApplicationKey = Settings.Default.ApiApplicationKey;
RestClient.ApiPass = Settings.Default.ApiPass;
RestClient.ApiUser = Settings.Default.ApiUser;
RestClient.ApiURL = Settings.Default.ApiURL;
InitializeComponent();
RegisterHotKey(Handle, ShutdownHotkey, 3, (int) Keys.S);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0312 && m.WParam.ToInt32() == ShutdownHotkey)
{
AllowShutdown = true;
Application.Exit();
}
base.WndProc(ref m);
}
enum Language
{
Finnish = 0,
English = 1,
English = 1
}
// Language currentLanguage = Language.English;
......@@ -62,18 +76,18 @@ namespace MoyaSignup
public void SetPrinter(ThermoPrinter.Models model)
{
thermoPrinter = new ThermoPrinter(model);
_thermoPrinter = new ThermoPrinter(model);
try
{
thermoPrinter.Start(Properties.Settings.Default.printerPort);
thermoPrinter.SetBarcodeWidth(ThermoPrinter.BarcodeWidths.Small);
thermoPrinter.SetCharacterAlignment(ThermoPrinter.CharacterAlignments.Center);
thermoPrinter.SetPositionHRI(ThermoPrinter.HRIPrintingPositions.Below);
_thermoPrinter.Start(Settings.Default.printerPort);
_thermoPrinter.SetBarcodeWidth(ThermoPrinter.BarcodeWidths.Small);
_thermoPrinter.SetCharacterAlignment(ThermoPrinter.CharacterAlignments.Center);
_thermoPrinter.SetPositionHRI(ThermoPrinter.HRIPrintingPositions.Below);
}
catch
{
thermoPrinter = null;
_thermoPrinter = null;
}
}
......@@ -86,40 +100,20 @@ namespace MoyaSignup
}*/
if (Debugger.IsAttached)
this.WindowState = FormWindowState.Normal;
WindowState = FormWindowState.Normal;
ApiSettings form = new ApiSettings();
var form = new ApiSettings();
form.ShowDialog();
if(Properties.Settings.Default.printerPort != null && Properties.Settings.Default.ThermoPrinter > 0)
if (Settings.Default.printerPort != null && Settings.Default.ThermoPrinter > 0)
{
ThermoPrinter.Models model = (ThermoPrinter.Models)Properties.Settings.Default.ThermoPrinter;
var model = (ThermoPrinter.Models) Settings.Default.ThermoPrinter;
SetPrinter(model);
}
userLoginControl1.SetPrinter += SetPrinter;
}
bool pictureTaken;
private void btnFinnish_Click(object sender, EventArgs e)
{
}
private void btnEnglish_Click(object sender, EventArgs e)
{
}
bool checkSaveSafety()
{
return true;
}
/*
private void btnSave_Click(object sender, EventArgs e)
{
......@@ -145,12 +139,6 @@ namespace MoyaSignup
this.Cursor = Cursors.Default;
}*/
bool allowShutdown;
public bool AllowShutdown {
get { return allowShutdown; }
set { allowShutdown = value; }
}
/*
private void txtName_TextChanged(object sender, EventArgs e)
{
......@@ -159,54 +147,57 @@ namespace MoyaSignup
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (!allowShutdown)
if (!AllowShutdown)
{
e.Cancel = true;
}
else
{
if (thermoPrinter != null)
thermoPrinter.Stop();
_thermoPrinter?.Stop();
}
}
private void Form1_Deactivate(object sender, EventArgs e)
{
if (!allowShutdown)
this.Activate();
if (!AllowShutdown)
{
Activate();
}
else
{
UnregisterHotKey(Handle, ShutdownHotkey);
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.Alt && e.Control && e.KeyCode == Keys.S)
/* if (e.Alt && e.Control && e.KeyCode == Keys.S)
{
allowShutdown = true;
AllowShutdown = true;
Application.Exit();
return;
} else if (e.Alt && e.KeyCode == Keys.F5)
}
if (e.Alt && e.KeyCode == Keys.F5)
{
//userDetailsEditor1.SetPrinter(ThermoPrinterLibrary.ThermoPrinter.Models.IDP3210);
//MessageBox.Show("Printteri: IDP3210");
}
}*/
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && e.Control && e.KeyCode == Keys.S)
{
allowShutdown = true;
this.Close();
return;
AllowShutdown = true;
Close();
}
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{/*
{
/*
if (e.Button == MouseButtons.Right)
{
allowShutdown = true;
......@@ -214,12 +205,11 @@ namespace MoyaSignup
return;
}*/
}
private void button1_Click(object sender, EventArgs e)
{
allowShutdown = true;
AllowShutdown = true;
Application.Exit();
}
......@@ -227,7 +217,7 @@ namespace MoyaSignup
{
userLoginControl1.Visible = false;
userDetailsEditor1.LoadUser(userLoginControl1.CurrentUser);
userDetailsEditor1.ThermoPrinter = thermoPrinter;
userDetailsEditor1.ThermoPrinter = _thermoPrinter;
userLoginControl1.Clear();
userDetailsEditor1.Visible = true;
//userLoginControl1.CurrentUser;
......@@ -235,7 +225,6 @@ namespace MoyaSignup
private void userDetailsEditor1_Load(object sender, EventArgs e)
{
}
private void userDetailsEditor1_CloseView(object sender, EventArgs e)
......@@ -243,7 +232,5 @@ namespace MoyaSignup
userDetailsEditor1.Visible = false;
userLoginControl1.Visible = true;
}
}
}
}
\ No newline at end of file
using System;
using System.IO;
namespace MoyaSignup
{
public enum LogLevel
{
Debug,
Info,
Error
}
public static class Logger
{
private static readonly StreamWriter LogWriter;
static Logger()
{
LogWriter = File.AppendText("log.txt");
}
public static void Log(LogLevel level, string message)
{
LogWriter.Write("[{0}] [{1}] {2}",
string.Format("{0} {1}", DateTime.UtcNow.ToLongTimeString(), DateTime.UtcNow.ToLongDateString()),
level,
message + '\n'
);
LogWriter.Flush();
}
public static void Debug(string message) => Log(LogLevel.Debug, message);
public static void Info(string message) => Log(LogLevel.Info, message);
public static void Error(string message) => Log(LogLevel.Error, message);
public static void Error(string message, Exception ex) => Log(LogLevel.Error, message + ":\n" + ex);
}
}
\ No newline at end of file
......@@ -50,11 +50,12 @@
</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<DebugType>none</DebugType>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
<UseVSHostingProcess>false</UseVSHostingProcess>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<Reference Include="AutoUpdateLib">
......@@ -114,6 +115,7 @@
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="HeartBeatMonitor.cs" />
<Compile Include="Logger.cs" />
<Compile Include="mjpeg\ByteArrayUtils.cs" />
<Compile Include="mjpeg\MJPEGConfiguration.cs" />
<Compile Include="mjpeg\MJPEGSource.cs" />
......
......@@ -61,27 +61,19 @@ namespace MoyaSignup
private static bool IsValidEmail(string email)
{
if (!email.Contains("@") || !email.Contains("."))
{
return false;
}
var parts = email.Split(new[] {"@"}, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 2)
{
if (!parts[1].Contains("."))
{
return false;
}
var domainparts = parts[1].Split(new[] {"."}, StringSplitOptions.RemoveEmptyEntries);
if (domainparts.Length < 2)
{
return false;
}
var ltd = domainparts[domainparts.Length - 1];
if (ltd.Length < 2)
{
return false;
}
}
else
{
......@@ -104,6 +96,7 @@ namespace MoyaSignup
var client = new RestClient();
try
{
throw new MoyaApiException("test", HttpStatusCode.Ambiguous, new WebException());
var json = client.MakeRequest("user", "email=" + txtEmail.Text);
var ser = new JavaScriptSerializer();
var euser = ser.Deserialize<Eventuser>(json);
......@@ -126,6 +119,9 @@ namespace MoyaSignup
}
else
{
timer1.Stop();
Logger.Error("Connection to MoyaApi failed", ex);
MessageBox.Show(
"Yhteys taustajärjestelmään epäonnistui. Ota yhteys infoon.",
"Järjestelmävirhe",
......@@ -136,11 +132,8 @@ namespace MoyaSignup
}
catch (Exception ex)
{
using (var eventLog = new EventLog("Application"))
{
eventLog.Source = "MoyaSignup";
eventLog.WriteEntry(string.Format("Odottamaton virhe:\n{0}", ex.StackTrace), EventLogEntryType.Error, 666);
}
timer1.Stop();
Logger.Error("Unexpected exception", ex);
MessageBox.Show(
"Odottamaton virhe. Ota yhteys infoon.",
......@@ -149,6 +142,7 @@ namespace MoyaSignup
MessageBoxIcon.Error
);
}
timer1.Stop();
}
......@@ -181,7 +175,9 @@ namespace MoyaSignup
LoginOk?.Invoke(this, null);
}
else
{
WrongPass();
}
}
catch (Exception)
{
......@@ -230,7 +226,7 @@ namespace MoyaSignup
private void UserLoginControl_KeyUp(object sender, KeyEventArgs e)
{
if (e.Alt && e.Control && e.KeyCode == Keys.S)
/*if (e.Alt && e.Control && e.KeyCode == Keys.S)
{
Application.Exit();
return;
......@@ -240,12 +236,12 @@ namespace MoyaSignup
{
//userDetailsEditor1.SetPrinter(ThermoPrinterLibrary.ThermoPrinter.Models.IDP3210);
//MessageBox.Show("Printteri: IDP3210");
}
}*/
}
private void TxtEmail_KeyUp(object sender, KeyEventArgs e)
{
if (e.Alt && e.Control && e.KeyCode == Keys.S)
/*if (e.Alt && e.Control && e.KeyCode == Keys.S)
{
Program.form1.AllowShutdown = true;
Application.Exit();
......@@ -255,7 +251,7 @@ namespace MoyaSignup
{
//userDetailsEditor1.SetPrinter(ThermoPrinterLibrary.ThermoPrinter.Models.IDP3210);
//MessageBox.Show("Printteri: IDP3210");
}
}*/
}
}
}
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!