Korero – Rev 1
?pathlinks?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Korero.Communication;
using Korero.Properties;
using Korero.Utilities;
using Serilog;
namespace Korero.Economy
{
public partial class EconomyForm : Form
{
#region Private Delegates, Events, Enums, Properties, Indexers and Fields
private readonly CancellationToken _cancellationToken;
private readonly CancellationTokenSource _cancellationTokenSource;
private readonly MainForm _mainForm;
private readonly MqttCommunication _mqttCommunication;
private PaymentForm _paymentForm;
#endregion
#region Constructors, Destructors and Finalizers
public EconomyForm()
{
InitializeComponent();
Utilities.WindowState.FormTracker.Track(this);
_cancellationTokenSource = new CancellationTokenSource();
_cancellationToken = _cancellationTokenSource.Token;
}
public EconomyForm(MainForm mainForm, MqttCommunication mqttCommunication) : this()
{
_mainForm = mainForm;
_mqttCommunication = mqttCommunication;
_mqttCommunication.NotificationReceived += MqttCommunication_NotificationReceived;
}
/// <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();
}
_mqttCommunication.NotificationReceived -= MqttCommunication_NotificationReceived;
base.Dispose(disposing);
}
#endregion
#region Event Handlers
private void MqttCommunication_NotificationReceived(object sender, MqttNotificationEventArgs e)
{
switch (e.Notification["notification"])
{
case "balance":
textBox1.InvokeIfRequired(textBox => { textBox.Text = e.Notification["balance"]; });
break;
}
}
private void Button1_Click(object sender, EventArgs e)
{
if (_paymentForm != null)
{
return;
}
_paymentForm = new PaymentForm(_mainForm, _mainForm.MqttCommunication);
_paymentForm.Closing += PaymentForm_Closing;
_paymentForm.Show();
}
private void PaymentForm_Closing(object sender, CancelEventArgs e)
{
if (_paymentForm == null)
{
return;
}
_paymentForm.Closing -= PaymentForm_Closing;
_paymentForm.Dispose();
_paymentForm = null;
}
private async void EconomyForm_Shown(object sender, EventArgs e)
{
await GetBalance();
}
private void EconomyForm_FormClosing(object sender, FormClosingEventArgs e)
{
_cancellationTokenSource.Cancel();
}
#endregion
#region Private Methods
private async Task GetBalance()
{
var data = new Dictionary<string, string>
{
{"command", "getbalance"},
{"group", Settings.Default.Group},
{"password", Settings.Default.Password}
};
var callback = await _mqttCommunication.SendCommand(new Command(data), _cancellationToken);
if (callback == null || !callback.Success)
{
if (callback != null)
{
Log.Warning("Command {Command} has failed with {Error}.",
callback.Command, callback.Error);
}
return;
}
textBox1.InvokeIfRequired(textBox =>
{
var balance = callback.Data.FirstOrDefault();
textBox.Text = balance;
});
}
#endregion
}
}
Generated by GNU Enscript 1.6.5.90.