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
|
#include "pch.h"
#include "ServiceViewModel.h"
#include "MainPage.xaml.h"
using namespace Concurrency;
using namespace Platform;
using namespace SDKTemplate;
using namespace Windows::System::Threading;
using namespace Windows::UI::Xaml::Data;
ServiceViewModel::ServiceViewModel()
{
// Set default rate
samplePeriod = 100;
// Initialize UI state
startButtonEnabled = true;
stopButtonEnabled = false;
sliderEnabled = true;
sliderValue = samplePeriod;
actualRpcCallbackRate = "0";
expectedRpcCallbackRate = "0";
meteringOn = false;
// Get the dispatcher for notifying property change
dispatcher = Windows::UI::Core::CoreWindow::GetForCurrentThread()->Dispatcher;
sampleArray = ref new Platform::Array<__int64>(1000);
}
//
// if errCode represents an error, then display a message and return true.
//
inline bool ServiceViewModel::NotifyIfAnyError(__int64 errCode)
{
if (errCode != 0)
{
meteringOn = false;
NotifyStatusMessage("Error occured while communicating with RPC server: " + errCode.ToString(), NotifyType::ErrorMessage);
// Reset button state
StartButtonEnabled = true;
StopButtonEnabled = false;
return true;
}
return false;
}
// Bindable properties
#pragma region BindableProperties
String^ ServiceViewModel::ExpectedRpcCallbackRate::get() { return expectedRpcCallbackRate; }
void ServiceViewModel::ExpectedRpcCallbackRate::set(String^ value)
{
if (expectedRpcCallbackRate != value)
{
expectedRpcCallbackRate = value;
NotifyPropertyChanged("ExpectedRpcCallbackRate");
}
}
String^ ServiceViewModel::ActualRpcCallbackRate::get() { return actualRpcCallbackRate; }
void ServiceViewModel::ActualRpcCallbackRate::set(String^ value)
{
if (actualRpcCallbackRate != value)
{
actualRpcCallbackRate = value;
NotifyPropertyChanged("ActualRpcCallbackRate");
}
}
String^ ServiceViewModel::SampleMessage::get() { return sampleMessage; }
void ServiceViewModel::SampleMessage::set(String^ value)
{
if (sampleMessage != value)
{
sampleMessage = value;
NotifyPropertyChanged("SampleMessage");
}
}
bool ServiceViewModel::StartButtonEnabled::get() { return startButtonEnabled; }
void ServiceViewModel::StartButtonEnabled::set(bool value)
{
if (startButtonEnabled != value)
{
startButtonEnabled = value;
NotifyPropertyChanged("StartButtonEnabled");
}
}
bool ServiceViewModel::StopButtonEnabled::get() { return stopButtonEnabled; }
void ServiceViewModel::StopButtonEnabled::set(bool value)
{
if (stopButtonEnabled != value)
{
stopButtonEnabled = value;
NotifyPropertyChanged("StopButtonEnabled");
}
}
bool ServiceViewModel::SliderEnabled::get() { return sliderEnabled; }
void ServiceViewModel::SliderEnabled::set(bool value)
{
if (sliderEnabled != value)
{
sliderEnabled = value;
NotifyPropertyChanged("SliderEnabled");
}
}
double ServiceViewModel::SliderValue::get() { return sliderValue; }
void ServiceViewModel::SliderValue::set(double value)
{
if (sliderValue != value)
{
sliderValue = value;
NotifyPropertyChanged("SliderValue");
}
}
#pragma endregion BindableProperties
void SDKTemplate::ServiceViewModel::NotifyPropertyChanged(Platform::String^ prop)
{
if (dispatcher != nullptr)
{
dispatcher->RunAsync(
Windows::UI::Core::CoreDispatcherPriority::Normal,
ref new Windows::UI::Core::DispatchedHandler([this, prop]()
{
PropertyChangedEventArgs^ args =
ref new PropertyChangedEventArgs(prop);
PropertyChanged(this, args);
}));
}
// else log error
}
//
// Initializes rpcclient and metering
//
void ServiceViewModel::StartMetering(int sampleRate)
{
create_task([this, sampleRate]
{
SampleMessage = "";
StartButtonEnabled = false;
StopButtonEnabled = true;
stopMeteringRequested = false;
rpcclient = std::make_unique<RpcClient>();
if (NotifyIfAnyError(rpcclient->Initialize())) return;
meteringOn = true;
sampleRefreshCount = 0;
sampleArray[0] = 0;
// Set the sample rate on server
this->samplePeriod = sampleRate;
__int64 retCode = rpcclient->SetSampleRate(sampleRate);
FILETIME initialSystemTime;
GetSystemTimeAsFileTime(&initialSystemTime);
lastUpdateTime.LowPart = initialSystemTime.dwLowDateTime;
lastUpdateTime.HighPart = initialSystemTime.dwHighDateTime;
if (!NotifyIfAnyError(retCode))
{
// Set up worker for UI update.
Windows::Foundation::TimeSpan span{ 100 * 10000 }; // 100ms refresh rate
auto timerHandler = ref new TimerElapsedHandler([this](ThreadPoolTimer^)
{
__int64 meteringData = rpcclient->MeteringData;
// If there is no new data, return
if (sampleArray[0] == meteringData)
{
return;
}
// Arithmetic subtraction of time
// https://msdn.microsoft.com/en-us/library/ms724950%28VS.85%29.aspx?f=255&MSPPError=-2147217396
FILETIME currentSystemTime;
ULARGE_INTEGER currentTimeUi;
GetSystemTimeAsFileTime(¤tSystemTime);
currentTimeUi.LowPart = currentSystemTime.dwLowDateTime;
currentTimeUi.HighPart = currentSystemTime.dwHighDateTime;
// Calculate number of samples received since last callback
__int64 now = rpcclient->CallbackCount;
__int64 diff = now - rpcDataCountOld;
rpcDataCountOld = now;
// Calculate incoming sample rate
double rate = (double)diff*1E7 / (currentTimeUi.QuadPart - lastUpdateTime.QuadPart);
ExpectedRpcCallbackRate = (1E3 / this->samplePeriod).ToString() + " /sec";
lastUpdateTime = currentTimeUi;
ActualRpcCallbackRate = rate.ToString() + " /sec";
// Construct the sample data string
String^ sampleMessage = meteringData + "\n";
unsigned int length = sampleArray->Length;
if (sampleRefreshCount < length)
{
++sampleRefreshCount;
length = sampleRefreshCount;
}
// Insert the new data point at the top of the array.
// Old data falls off the end of the array.
for (unsigned int i = 1; i < length; ++i)
{
sampleArray[i] = sampleArray[i - 1];
sampleMessage += sampleArray[i] + "\n";
}
sampleArray[0] = meteringData;
SampleMessage = sampleMessage;
});
ThreadPoolTimer^ periodicTimer = ThreadPoolTimer::CreatePeriodicTimer(timerHandler, span);
NotifyStatusMessage("Metering start command sent successfully", NotifyType::StatusMessage);
retCode = this->rpcclient->StartMeteringAndWaitForStop(sampleRate);
meteringOn = false;
periodicTimer->Cancel();
if (!NotifyIfAnyError(retCode) && !stopMeteringRequested)
{
NotifyStatusMessage("Rpc server connection closed without stop metering being requested",
NotifyType::ErrorMessage);
StartButtonEnabled = true;
StopButtonEnabled = false;
}
}
});
}
//
// Stop metering
//
void ServiceViewModel::StopMetering()
{
create_task([this]
{
StopButtonEnabled = false;
SliderEnabled = false;
stopMeteringRequested = true;
__int64 retCode = this->rpcclient->StopMetering();
if (!NotifyIfAnyError(retCode))
{
NotifyStatusMessage("Metering stop command sent successfully",
NotifyType::StatusMessage);
}
SliderEnabled = true;
StartButtonEnabled = true;
});
}
//
// Set sample period
//
void ServiceViewModel::SetSamplePeriod(int samplePeriod)
{
create_task([this, samplePeriod]
{
this->samplePeriod = samplePeriod;
if (meteringOn)
{
_int64 retCode = rpcclient->SetSampleRate(samplePeriod);
if (!NotifyIfAnyError(retCode))
{
NotifyStatusMessage(
"Sample period set to: " + samplePeriod,
NotifyType::StatusMessage);
}
}
});
}
|