-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainPage.xaml.cs
More file actions
331 lines (287 loc) · 9.46 KB
/
MainPage.xaml.cs
File metadata and controls
331 lines (287 loc) · 9.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using CommunityToolkit.Maui.Storage;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;
namespace yTorqueChecker
{
public partial class MainPage : ContentPage
{
private readonly MainPageViewModel _mainPageViewModel;
private readonly IDispatcherTimer _timer;
public MainPage()
{
InitializeComponent();
_timer = Application.Current.Dispatcher.CreateTimer();
_timer.Interval = TimeSpan.FromMilliseconds(500);
_timer.Tick += (sender, e) => PeriodicHandler();
_mainPageViewModel = new MainPageViewModel();
BindingContext = _mainPageViewModel;
}
private List<string> GetSensors()
{
List<string> res = new List<string>();
YWeighScale scale = YWeighScale.FirstWeighScale();
while (scale != null) {
res.Add(scale.get_friendlyName());
scale = scale.nextWeighScale();
}
return res;
}
protected async override void OnAppearing()
{
base.OnAppearing();
List<string> sensors = GetSensors();
await Task.Delay(500);
string action = null;
while (action == null) {
action = await DisplayActionSheetAsync("Select WeighScale sensor?", "Exit", null, sensors.ToArray());
}
if (action == "Cancel") {
Application.Current.Quit();
}
_mainPageViewModel.Hwid = action;
_timer.Start();
}
protected override void OnDisappearing()
{
base.OnDisappearing();
_timer.Stop();
}
private void PeriodicHandler()
{
string errmsg = "";
int err = YAPI.HandleEvents(ref errmsg);
if (err != 0) {
Trace.WriteLine(errmsg);
}
}
private async void StartClicked(object? sender, EventArgs e)
{
_mainPageViewModel.Start();
FillResultsGrid();
}
private void ResetClicked(object? sender, EventArgs e)
{
_mainPageViewModel.Reset();
}
private void ValidateClicked(object? sender, EventArgs e)
{
_mainPageViewModel.Validate();
FillResultsGrid();
}
private void FillResultsGrid()
{
ResultsGrid.ColumnDefinitions.Clear();
if (_mainPageViewModel.RecordedValues.Count == 0)
return;
int columnCount = _mainPageViewModel.RecordedValues.Count;
for (int col = 0; col < columnCount; col++) {
ResultsGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
}
for (int col = 0; col < columnCount; col++) {
var targetLabel = new Label
{
Text = _mainPageViewModel.RecordedValues[col][0].ToString("0.0"),
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center
};
ResultsGrid.Add(targetLabel, col, 0);
var measureLabel = new Label
{
Text = _mainPageViewModel.RecordedValues[col][1].ToString("0.0"),
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center
};
ResultsGrid.Add(measureLabel, col, 1);
}
}
private async void ExportClicked(object? sender, EventArgs e)
{
string data = _mainPageViewModel.GetDataAsCSV();
using var stream = new MemoryStream(Encoding.Default.GetBytes(data));
var fileSaverResult = await FileSaver.Default.SaveAsync("result.csv", stream, null);
if (fileSaverResult.IsSuccessful) {
} else {
}
}
}
class MainPageViewModel : INotifyPropertyChanged
{
private float _currentTorque;
private float _maxTorque;
private string _hwid;
private YWeighScale _scale;
private int _step;
private int _targetTorque;
private List<float[]> _recoredValues = new List<float[]>();
private int _start;
private int _stop;
private int _stepsize;
private static List<int> _all_targets = new List<int>();
public MainPageViewModel()
{
_currentTorque = 0;
_maxTorque = 0;
_hwid = "";
_step = -1;
_start = 2;
_stop = 20;
_stepsize = 2;
_targetTorque = 0;
_scale = null;
}
private void ValueChangeCallBack(YWeighScale func, string value)
{
float torque;
if (float.TryParse(value, out torque)) {
Trace.WriteLine(func.get_friendlyName() + "->" + value);
_currentTorque = torque;
if (_maxTorque < torque) {
_maxTorque = torque;
OnPropertyChanged(nameof(MaxNm));
}
OnPropertyChanged(nameof(CurrentNm));
} else {
Debug.WriteLine("invalid " + value);
}
}
public float Current => _currentTorque;
public float Max => _maxTorque;
public bool IsReady
{
get => _hwid != "";
}
public string Hwid
{
get => _hwid;
set
{
if (value == _hwid)
return;
_hwid = value;
_scale = YWeighScale.FindWeighScale(value);
_scale.registerValueCallback(ValueChangeCallBack);
OnPropertyChanged();
OnPropertyChanged(nameof(IsReady));
}
}
public string StepString
{
get
{
if (_step < 0) {
return "Step 0/0";
}
int cur = _step + 1;
return $"Step {cur:0}/{_all_targets.Count:0}";
}
}
public string TargetNm
{
get => $"{_targetTorque:0}Nm";
}
public string MaxNm
{
get => $"{_maxTorque:0.0}Nm";
}
public string CurrentNm
{
get => $"{_currentTorque:0.0}Nm";
}
public int StartNm
{
get => _start;
set
{
if (value == _start)
return;
_start = value;
OnPropertyChanged();
}
}
public int EndNm
{
get => _stop;
set
{
if (value == _stop)
return;
_stop = value;
OnPropertyChanged();
}
}
public bool IsTesting
{
get { return _step >= 0; }
}
public bool CanExport
{
get { return _step > 0 && _step >= _all_targets.Count; }
}
public int StepSize
{
get => _stepsize;
set
{
if (value == _stepsize)
return;
_stepsize = value;
OnPropertyChanged();
}
}
public IReadOnlyList<float[]> RecordedValues => _recoredValues.AsReadOnly();
public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
Trace.WriteLine($"PropertyChanged(MainPageViewModel): {propertyName}");
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public void Reset()
{
_maxTorque = 0;
OnPropertyChanged(nameof(MaxNm));
}
public void Validate()
{
if (_step >= 0 && _step < _all_targets.Count) {
float[] res = new float[2];
res[0] = _targetTorque;
res[1] = _maxTorque;
_recoredValues.Add(res);
_step++;
if (_step < _all_targets.Count) {
_targetTorque = _all_targets[_step];
}
_maxTorque = 0;
OnPropertyChanged(nameof(StepString));
OnPropertyChanged(nameof(TargetNm));
OnPropertyChanged(nameof(MaxNm));
OnPropertyChanged(nameof(CanExport));
}
}
public void Start()
{
_recoredValues.Clear();
_all_targets.Clear();
for (int i = _start; i <= _stop; i += _stepsize) {
_all_targets.Add(i);
}
_step = 0;
_targetTorque = _all_targets[0];
OnPropertyChanged(nameof(StepString));
OnPropertyChanged(nameof(CanExport));
OnPropertyChanged(nameof(TargetNm));
OnPropertyChanged(nameof(MaxNm));
OnPropertyChanged(nameof(IsTesting));
}
public string GetDataAsCSV()
{
string res = "Target;Measure;\n";
for (int i = 0; i < _recoredValues.Count; i++) {
res += $"{_recoredValues[i][0]:0.0},{_recoredValues[i][1]:0.0}\n";
}
return res;
}
}
}