Korero – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Collections.Generic; |
||
3 | using System.ComponentModel; |
||
4 | using System.Linq; |
||
5 | using System.Threading; |
||
6 | using System.Threading.Tasks; |
||
7 | using System.Windows.Forms; |
||
8 | using Korero.Communication; |
||
9 | using Korero.Properties; |
||
10 | using Korero.Utilities; |
||
11 | using Serilog; |
||
12 | |||
13 | namespace Korero.Economy |
||
14 | { |
||
15 | public partial class EconomyForm : Form |
||
16 | { |
||
17 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
||
18 | |||
19 | private readonly CancellationToken _cancellationToken; |
||
20 | |||
21 | private readonly CancellationTokenSource _cancellationTokenSource; |
||
22 | |||
23 | private readonly MainForm _mainForm; |
||
24 | |||
25 | private readonly MqttCommunication _mqttCommunication; |
||
26 | |||
27 | private PaymentForm _paymentForm; |
||
28 | |||
29 | #endregion |
||
30 | |||
31 | #region Constructors, Destructors and Finalizers |
||
32 | |||
33 | public EconomyForm() |
||
34 | { |
||
35 | InitializeComponent(); |
||
36 | Utilities.WindowState.FormTracker.Track(this); |
||
37 | |||
38 | _cancellationTokenSource = new CancellationTokenSource(); |
||
39 | _cancellationToken = _cancellationTokenSource.Token; |
||
40 | } |
||
41 | |||
42 | public EconomyForm(MainForm mainForm, MqttCommunication mqttCommunication) : this() |
||
43 | { |
||
44 | _mainForm = mainForm; |
||
45 | _mqttCommunication = mqttCommunication; |
||
46 | |||
47 | _mqttCommunication.NotificationReceived += MqttCommunication_NotificationReceived; |
||
48 | } |
||
49 | |||
50 | /// <summary> |
||
51 | /// Clean up any resources being used. |
||
52 | /// </summary> |
||
53 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
||
54 | protected override void Dispose(bool disposing) |
||
55 | { |
||
56 | if (disposing && components != null) |
||
57 | { |
||
58 | components.Dispose(); |
||
59 | } |
||
60 | |||
61 | _mqttCommunication.NotificationReceived -= MqttCommunication_NotificationReceived; |
||
62 | base.Dispose(disposing); |
||
63 | } |
||
64 | |||
65 | #endregion |
||
66 | |||
67 | #region Event Handlers |
||
68 | |||
69 | private void MqttCommunication_NotificationReceived(object sender, MqttNotificationEventArgs e) |
||
70 | { |
||
71 | switch (e.Notification["notification"]) |
||
72 | { |
||
73 | case "balance": |
||
74 | textBox1.InvokeIfRequired(textBox => { textBox.Text = e.Notification["balance"]; }); |
||
75 | break; |
||
76 | } |
||
77 | } |
||
78 | |||
79 | private void Button1_Click(object sender, EventArgs e) |
||
80 | { |
||
81 | if (_paymentForm != null) |
||
82 | { |
||
83 | return; |
||
84 | } |
||
85 | |||
86 | _paymentForm = new PaymentForm(_mainForm, _mainForm.MqttCommunication); |
||
87 | _paymentForm.Closing += PaymentForm_Closing; |
||
88 | _paymentForm.Show(); |
||
89 | } |
||
90 | |||
91 | private void PaymentForm_Closing(object sender, CancelEventArgs e) |
||
92 | { |
||
93 | if (_paymentForm == null) |
||
94 | { |
||
95 | return; |
||
96 | } |
||
97 | |||
98 | _paymentForm.Closing -= PaymentForm_Closing; |
||
99 | _paymentForm.Dispose(); |
||
100 | _paymentForm = null; |
||
101 | } |
||
102 | |||
103 | private async void EconomyForm_Shown(object sender, EventArgs e) |
||
104 | { |
||
105 | await GetBalance(); |
||
106 | } |
||
107 | |||
108 | private void EconomyForm_FormClosing(object sender, FormClosingEventArgs e) |
||
109 | { |
||
110 | _cancellationTokenSource.Cancel(); |
||
111 | } |
||
112 | |||
113 | #endregion |
||
114 | |||
115 | #region Private Methods |
||
116 | |||
117 | private async Task GetBalance() |
||
118 | { |
||
119 | var data = new Dictionary<string, string> |
||
120 | { |
||
121 | {"command", "getbalance"}, |
||
122 | {"group", Settings.Default.Group}, |
||
123 | {"password", Settings.Default.Password} |
||
124 | }; |
||
125 | |||
126 | var callback = await _mqttCommunication.SendCommand(new Command(data), _cancellationToken); |
||
127 | |||
128 | if (callback == null || !callback.Success) |
||
129 | { |
||
130 | if (callback != null) |
||
131 | { |
||
132 | Log.Warning("Command {Command} has failed with {Error}.", |
||
133 | callback.Command, callback.Error); |
||
134 | } |
||
135 | |||
136 | return; |
||
137 | } |
||
138 | |||
139 | textBox1.InvokeIfRequired(textBox => |
||
140 | { |
||
141 | var balance = callback.Data.FirstOrDefault(); |
||
142 | |||
143 | textBox.Text = balance; |
||
144 | }); |
||
145 | } |
||
146 | |||
147 | #endregion |
||
148 | } |
||
149 | } |