|
1 | 1 | using System.Windows.Input; |
2 | 2 | using System.Diagnostics; |
| 3 | +using System.Net.Http.Headers; |
| 4 | +using System.Text; |
3 | 5 | using HttpGamepadInput.BaseClasses; |
4 | 6 |
|
5 | 7 | namespace HttpGamepadInput.ViewModels; |
@@ -36,8 +38,24 @@ public class GamepadViewModel : BindableBase |
36 | 38 |
|
37 | 39 | public ICommand BButtonPressedCommand { get; } |
38 | 40 | public ICommand BButtonReleasedCommand { get; } |
| 41 | + |
39 | 42 | #endregion |
| 43 | + |
| 44 | + private HttpClient _client = new HttpClient(); |
40 | 45 |
|
| 46 | + private string _serverUrl = "http://127.0.0.1:8080/"; |
| 47 | + public string ServerUrl |
| 48 | + { |
| 49 | + get => _serverUrl; |
| 50 | + set |
| 51 | + { |
| 52 | + if (SetProperty(ref _serverUrl, value)) |
| 53 | + { |
| 54 | + UpdateHttpClient(); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
41 | 59 | #region ~ Constructor ~ |
42 | 60 | public GamepadViewModel() |
43 | 61 | { |
@@ -70,96 +88,141 @@ public GamepadViewModel() |
70 | 88 | } |
71 | 89 | #endregion |
72 | 90 |
|
| 91 | + private void UpdateHttpClient() |
| 92 | + { |
| 93 | + _client = new HttpClient |
| 94 | + { |
| 95 | + BaseAddress = new Uri(ServerUrl) |
| 96 | + }; |
| 97 | + _client.DefaultRequestHeaders.Accept.Clear(); |
| 98 | + _client.DefaultRequestHeaders.Accept.Add( |
| 99 | + new MediaTypeWithQualityHeaderValue("application/json")); |
| 100 | + } |
| 101 | + |
| 102 | + public async Task PostButtonStatus(string relativeUrl) |
| 103 | + { |
| 104 | + var content = new StringContent("{}", Encoding.UTF8, "application/json"); |
| 105 | + |
| 106 | + HttpResponseMessage response = await _client.PostAsync(relativeUrl, content); |
| 107 | + if (response.IsSuccessStatusCode) |
| 108 | + { |
| 109 | + string responseBody = await response.Content.ReadAsStringAsync(); |
| 110 | + Debug.WriteLine($"Response from {relativeUrl}: {responseBody}"); |
| 111 | + } |
| 112 | + else |
| 113 | + { |
| 114 | + Debug.WriteLine($"Error posting to {relativeUrl}: {response.StatusCode}"); |
| 115 | + } |
| 116 | + } |
| 117 | + |
73 | 118 | #region ~ Commands implementstions ~ |
74 | 119 |
|
75 | | - private void OnUpButtonPressed() |
| 120 | + private async void OnUpButtonPressed() |
76 | 121 | { |
77 | 122 | Debug.WriteLine("[INFO] UP pressed"); |
| 123 | + await PostButtonStatus("up/pressed"); |
78 | 124 | } |
79 | 125 |
|
80 | | - private void OnUpButtonReleased() |
| 126 | + private async void OnUpButtonReleased() |
81 | 127 | { |
82 | 128 | Debug.WriteLine("[INFO] UP released"); |
| 129 | + await PostButtonStatus("up/released"); |
83 | 130 | } |
84 | 131 |
|
85 | | - private void OnDownButtonPressed() |
| 132 | + private async void OnDownButtonPressed() |
86 | 133 | { |
87 | 134 | Debug.WriteLine("[INFO] DOWN pressed"); |
| 135 | + await PostButtonStatus("down/pressed"); |
88 | 136 | } |
89 | 137 |
|
90 | | - private void OnDownButtonReleased() |
| 138 | + private async void OnDownButtonReleased() |
91 | 139 | { |
92 | 140 | Debug.WriteLine("[INFO] DOWN released"); |
| 141 | + await PostButtonStatus("down/released"); |
93 | 142 | } |
94 | 143 |
|
95 | | - private void OnLeftButtonPressed() |
| 144 | + private async void OnLeftButtonPressed() |
96 | 145 | { |
97 | 146 | Debug.WriteLine("[INFO] LEFT pressed"); |
| 147 | + await PostButtonStatus("left/pressed"); |
98 | 148 | } |
99 | 149 |
|
100 | | - private void OnLeftButtonReleased() |
| 150 | + private async void OnLeftButtonReleased() |
101 | 151 | { |
102 | 152 | Debug.WriteLine("[INFO] LEFT released"); |
| 153 | + await PostButtonStatus("left/released"); |
103 | 154 | } |
104 | 155 |
|
105 | | - private void OnRightButtonPressed() |
| 156 | + private async void OnRightButtonPressed() |
106 | 157 | { |
107 | 158 | Debug.WriteLine("[INFO] RIGHT pressed"); |
| 159 | + await PostButtonStatus("right/pressed"); |
108 | 160 | } |
109 | 161 |
|
110 | | - private void OnRightButtonReleased() |
| 162 | + private async void OnRightButtonReleased() |
111 | 163 | { |
112 | 164 | Debug.WriteLine("[INFO] RIGHT released"); |
| 165 | + await PostButtonStatus("right/released"); |
113 | 166 | } |
114 | 167 |
|
115 | | - private void OnCenterButtonPressed() |
| 168 | + private async void OnCenterButtonPressed() |
116 | 169 | { |
117 | 170 | Debug.WriteLine("[INFO] MIDDLE pressed"); |
| 171 | + await PostButtonStatus("center/pressed"); |
118 | 172 | } |
119 | 173 |
|
120 | | - private void OnCenterButtonReleased() |
| 174 | + private async void OnCenterButtonReleased() |
121 | 175 | { |
122 | 176 | Debug.WriteLine("[INFO] MIDDLE released"); |
| 177 | + await PostButtonStatus("center/released"); |
123 | 178 | } |
124 | 179 |
|
125 | | - private void OnStartButtonPressed() |
| 180 | + private async void OnStartButtonPressed() |
126 | 181 | { |
127 | 182 | Debug.WriteLine("[INFO] START pressed"); |
| 183 | + await PostButtonStatus("start/pressed"); |
128 | 184 | } |
129 | 185 |
|
130 | | - private void OnStartButtonReleased() |
| 186 | + private async void OnStartButtonReleased() |
131 | 187 | { |
132 | 188 | Debug.WriteLine("[INFO] START released"); |
| 189 | + await PostButtonStatus("start/released"); |
133 | 190 | } |
134 | 191 |
|
135 | | - private void OnSelectButtonPressed() |
| 192 | + private async void OnSelectButtonPressed() |
136 | 193 | { |
137 | 194 | Debug.WriteLine("[INFO] SELECT pressed"); |
| 195 | + await PostButtonStatus("select/pressed"); |
138 | 196 | } |
139 | 197 |
|
140 | | - private void OnSelectButtonReleased() |
| 198 | + private async void OnSelectButtonReleased() |
141 | 199 | { |
142 | 200 | Debug.WriteLine("[INFO] SELECT released"); |
| 201 | + await PostButtonStatus("select/released"); |
143 | 202 | } |
144 | 203 |
|
145 | | - private void OnAButtonPressed() |
| 204 | + private async void OnAButtonPressed() |
146 | 205 | { |
147 | 206 | Debug.WriteLine("[INFO] A pressed"); |
| 207 | + await PostButtonStatus("a-button/pressed"); |
148 | 208 | } |
149 | 209 |
|
150 | | - private void OnAButtonReleased() |
| 210 | + private async void OnAButtonReleased() |
151 | 211 | { |
152 | 212 | Debug.WriteLine("[INFO] A released"); |
| 213 | + await PostButtonStatus("a-button/released"); |
153 | 214 | } |
154 | 215 |
|
155 | | - private void OnBButtonPressed() |
| 216 | + private async void OnBButtonPressed() |
156 | 217 | { |
157 | 218 | Debug.WriteLine("[INFO] B pressed"); |
| 219 | + await PostButtonStatus("b-button/pressed"); |
158 | 220 | } |
159 | 221 |
|
160 | | - private void OnBButtonReleased() |
| 222 | + private async void OnBButtonReleased() |
161 | 223 | { |
162 | 224 | Debug.WriteLine("[INFO] B released"); |
| 225 | + await PostButtonStatus("b-button/released"); |
163 | 226 | } |
164 | 227 |
|
165 | 228 | #endregion |
|
0 commit comments