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