Showing the changes to Source/Libraries/TimeSeriesPlatformLibrary/Samples/AverageFrequencyCalculator.cpp (1 of 24 files). Show all
Unified · Split
| @@ -51,34 +51,34 @@ void RunSubscriber(string hostname, uint16_t port); · 50 unmodified lines | |||
| 51 | // Sample application to demonstrate average frequency calculation using the subscriber API. | 51 | // Sample application to demonstrate average frequency calculation using the subscriber API. |
| 52 | int main(int argc, char* argv[]) | 52 | int main(int argc, char* argv[]) |
| 53 | { | 53 | { |
| 54 | string hostname; | 54 | string hostname; |
| 55 | uint16_t port; | 55 | uint16_t port; |
| 56 | 56 | ||
| 57 | // Ensure that the necessary | 57 | // Ensure that the necessary |
| 58 | // command line arguments are given. | 58 | // command line arguments are given. |
| 59 | if (argc < 3) | 59 | if (argc < 3) |
| 60 | { | 60 | { |
| 61 | cout << "Usage:" << endl; | 61 | cout << "Usage:" << endl; |
| 62 | cout << " AverageFrequencyCalculator HOSTNAME PORT" << endl; | 62 | cout << " AverageFrequencyCalculator HOSTNAME PORT" << endl; |
| 63 | return 0; | 63 | return 0; |
| 64 | } | 64 | } |
| 65 | 65 | ||
| 66 | // Get hostname and port. | 66 | // Get hostname and port. |
| 67 | hostname = argv[1]; | 67 | hostname = argv[1]; |
| 68 | stringstream(argv[2]) >> port; | 68 | stringstream(argv[2]) >> port; |
| 69 | 69 | ||
| 70 | // Run the subscriber. | 70 | // Run the subscriber. |
| 71 | RunSubscriber(hostname, port); | 71 | RunSubscriber(hostname, port); |
| 72 | 72 | ||
| 73 | // Wait until the user presses enter before quitting. | 73 | // Wait until the user presses enter before quitting. |
| 74 | string line; | 74 | string line; |
| 75 | getline(cin, line); | 75 | getline(cin, line); |
| 76 | 76 | ||
| 77 | // Disconnect the subscriber to stop background threads. | 77 | // Disconnect the subscriber to stop background threads. |
| 78 | Subscriber.Disconnect(); | 78 | Subscriber.Disconnect(); |
| 79 | cout << "Disconnected." << endl; | 79 | cout << "Disconnected." << endl; |
| 80 | 80 | ||
| 81 | return 0; | 81 | return 0; |
| 82 | } | 82 | } |
| 83 | 83 | ||
| 84 | // The proper procedure when creating and running a subscriber is: | 84 | // The proper procedure when creating and running a subscriber is: |
| @@ -88,191 +88,191 @@ int main(int argc, char* argv[]) · 3 unmodified lines | |||
| 88 | // - Subscribe | 88 | // - Subscribe |
| 89 | void RunSubscriber(string hostname, uint16_t port) | 89 | void RunSubscriber(string hostname, uint16_t port) |
| 90 | { | 90 | { |
| 91 | // The connector is declared here because it | 91 | // The connector is declared here because it |
| 92 | // is only needed for the initial connection | 92 | // is only needed for the initial connection |
| 93 | SubscriberConnector connector; | 93 | SubscriberConnector connector; |
| 94 | 94 | ||
| 95 | // Set up helper objects | 95 | // Set up helper objects |
| 96 | connector = CreateSubscriberConnector(hostname, port); | 96 | connector = CreateSubscriberConnector(hostname, port); |
| 97 | Info = CreateSubscriptionInfo(); | 97 | Info = CreateSubscriptionInfo(); |
| 98 | 98 | ||
| 99 | // Register callbacks | 99 | // Register callbacks |
| 100 | Subscriber.RegisterStatusMessageCallback(&DisplayStatusMessage); | 100 | Subscriber.RegisterStatusMessageCallback(&DisplayStatusMessage); |
| 101 | Subscriber.RegisterErrorMessageCallback(&DisplayErrorMessage); | 101 | Subscriber.RegisterErrorMessageCallback(&DisplayErrorMessage); |
| 102 | 102 | ||
| 103 | // Connect and subscribe to publisher | 103 | // Connect and subscribe to publisher |
| 104 | cout << endl << "Connecting to " << hostname << ":" << port << "..." << endl << endl; | 104 | cout << endl << "Connecting to " << hostname << ":" << port << "..." << endl << endl; |
| 105 | 105 | ||
| 106 | // Connect and subscribe to publisher | 106 | // Connect and subscribe to publisher |
| 107 | if (connector.Connect(Subscriber)) | 107 | if (connector.Connect(Subscriber)) |
| 108 | { | 108 | { |
| 109 | cout << "Connected! Subscribing to data..." << endl << endl; | 109 | cout << "Connected! Subscribing to data..." << endl << endl; |
| 110 | Subscriber.Subscribe(Info); | 110 | Subscriber.Subscribe(Info); |
| 111 | } | 111 | } |
| 112 | else | 112 | else |
| 113 | { | 113 | { |
| 114 | cout << "Connection attempts exceeded. Press enter to exit." << endl; | 114 | cout << "Connection attempts exceeded. Press enter to exit." << endl; |
| 115 | } | 115 | } |
| 116 | } | 116 | } |
| 117 | 117 | ||
| 118 | SubscriptionInfo CreateSubscriptionInfo() | 118 | SubscriptionInfo CreateSubscriptionInfo() |
| 119 | { | 119 | { |
| 120 | // SubscriptionInfo is a helper object which allows the user | 120 | // SubscriptionInfo is a helper object which allows the user |
| 121 | // to set up their subscription and reuse subscription settings. | 121 | // to set up their subscription and reuse subscription settings. |
| 122 | SubscriptionInfo info; | 122 | SubscriptionInfo info; |
| 123 | 123 | ||
| 124 | info.FilterExpression = "FILTER ActiveMeasurements WHERE SignalType = 'FREQ'"; | 124 | info.FilterExpression = "FILTER ActiveMeasurements WHERE SignalType = 'FREQ'"; |
| 125 | info.NewMeasurementsCallback = &ProcessMeasurements; | 125 | info.NewMeasurementsCallback = &ProcessMeasurements; |
| 126 | 126 | ||
| 127 | // Uncomment to enable optional UDP data channel | 127 | // Uncomment to enable optional UDP data channel |
| 128 | //info.UdpDataChannel = true; | 128 | //info.UdpDataChannel = true; |
| 129 | //info.DataChannelLocalPort = 9600; | 129 | //info.DataChannelLocalPort = 9600; |
| 130 | 130 | ||
| 131 | info.IncludeTime = true; | 131 | info.IncludeTime = true; |
| 132 | info.UseLocalClockAsRealTime = false; | 132 | info.UseLocalClockAsRealTime = false; |
| 133 | info.UseMillisecondResolution = true; | 133 | info.UseMillisecondResolution = true; |
| 134 | 134 | ||
| 135 | // This controls the downsampling time, in seconds | 135 | // This controls the downsampling time, in seconds |
| 136 | info.Throttled = true; | 136 | info.Throttled = true; |
| 137 | info.LagTime = 1.0; | 137 | info.LagTime = 1.0; |
| 138 | 138 | ||
| 139 | return info; | 139 | return info; |
| 140 | } | 140 | } |
| 141 | 141 | ||
| 142 | SubscriberConnector CreateSubscriberConnector(string hostname, uint16_t port) | 142 | SubscriberConnector CreateSubscriberConnector(string hostname, uint16_t port) |
| 143 | { | 143 | { |
| 144 | // SubscriberConnector is another helper object which allows the | 144 | // SubscriberConnector is another helper object which allows the |
| 145 | // user to modify settings for auto-reconnects and retry cycles. | 145 | // user to modify settings for auto-reconnects and retry cycles. |
| 146 | SubscriberConnector connector; | 146 | SubscriberConnector connector; |
| 147 | 147 | ||
| 148 | connector.RegisterErrorMessageCallback(&DisplayErrorMessage); | 148 | connector.RegisterErrorMessageCallback(&DisplayErrorMessage); |
| 149 | connector.RegisterReconnectCallback(&Resubscribe); | 149 | connector.RegisterReconnectCallback(&Resubscribe); |
| 150 | 150 | ||
| 151 | connector.SetHostname(hostname); | 151 | connector.SetHostname(hostname); |
| 152 | connector.SetPort(port); | 152 | connector.SetPort(port); |
| 153 | connector.SetMaxRetries(-1); | 153 | connector.SetMaxRetries(-1); |
| 154 | connector.SetRetryInterval(2000); | 154 | connector.SetRetryInterval(2000); |
| 155 | connector.SetAutoReconnect(true); | 155 | connector.SetAutoReconnect(true); |
| 156 | 156 | ||
| 157 | return connector; | 157 | return connector; |
| 158 | } | 158 | } |
| 159 | 159 | ||
| 160 | // Callback which is called when the subscriber has | 160 | // Callback which is called when the subscriber has |
| 161 | // received a new packet of measurements from the publisher. | 161 | // received a new packet of measurements from the publisher. |
| 162 | void ProcessMeasurements(DataSubscriber* source, const vector<MeasurementPtr>& newMeasurements) | 162 | void ProcessMeasurements(DataSubscriber* source, const vector<MeasurementPtr>& newMeasurements) |
| 163 | { | 163 | { |
| 164 | const double LoFrequency = 57.0; | 164 | const double LoFrequency = 57.0; |
| 165 | const double HiFrequency = 62.0; | 165 | const double HiFrequency = 62.0; |
| 166 | const double HzResolution = 1000.0; // three decimal places | 166 | const double HzResolution = 1000.0; // three decimal places |
| 167 | 167 | ||
| 168 | const string TimestampFormat = "%Y-%m-%d %H:%M:%S.%f"; | 168 | const string TimestampFormat = "%Y-%m-%d %H:%M:%S.%f"; |
| 169 | const size_t MaxTimestampSize = 80; | 169 | const size_t MaxTimestampSize = 80; |
| 170 | 170 | ||
| 171 | static map<Guid, int> m_lastValues; | 171 | static map<Guid, int> m_lastValues; |
| 172 | 172 | ||
| 173 | map<Guid, int>::iterator lastValueIter; | 173 | map<Guid, int>::iterator lastValueIter; |
| 174 | MeasurementPtr currentMeasurement; | 174 | MeasurementPtr currentMeasurement; |
| 175 | Guid signalID; | 175 | Guid signalID; |
| 176 | 176 | ||
| 177 | double frequency; | 177 | double frequency; |
| 178 | double frequencyTotal; | 178 | double frequencyTotal; |
| 179 | double maximumFrequency = LoFrequency; | 179 | double maximumFrequency = LoFrequency; |
| 180 | double minimumFrequency = HiFrequency; | 180 | double minimumFrequency = HiFrequency; |
| 181 | int adjustedFrequency; | 181 | int adjustedFrequency; |
| 182 | int lastValue; | 182 | int lastValue; |
| 183 | int total; | 183 | int total; |
| 184 | 184 | ||
| 185 | char timestamp[MaxTimestampSize]; | 185 | char timestamp[MaxTimestampSize]; |
| 186 | size_t i; | 186 | size_t i; |
| 187 | 187 | ||
| 188 | frequencyTotal = 0.0; | 188 | frequencyTotal = 0.0; |
| 189 | total = 0; | 189 | total = 0; |
| 190 | 190 | ||
| 191 | cout << source->GetTotalMeasurementsReceived() << " measurements received so far..." << endl; | 191 | cout << source->GetTotalMeasurementsReceived() << " measurements received so far..." << endl; |
| 192 | 192 | ||
| 193 | if (!newMeasurements.empty()) | 193 | if (!newMeasurements.empty()) |
| 194 | { | 194 | { |
| 195 | if (TicksToString(timestamp, MaxTimestampSize, TimestampFormat, newMeasurements[0]->Timestamp)) | 195 | if (TicksToString(timestamp, MaxTimestampSize, TimestampFormat, newMeasurements[0]->Timestamp)) |
| 196 | cout << "Timestamp: " << string(timestamp) << endl; | 196 | cout << "Timestamp: " << string(timestamp) << endl; |
| 197 | 197 | ||
| 198 | cout << "Point\tValue" << endl; | 198 | cout << "Point\tValue" << endl; |
| 199 | 199 | ||
| 200 | for (i = 0; i < newMeasurements.size(); ++i) | 200 | for (i = 0; i < newMeasurements.size(); ++i) |
| 201 | cout << newMeasurements[i]->ID << '\t' << newMeasurements[i]->Value << endl; | 201 | cout << newMeasurements[i]->ID << '\t' << newMeasurements[i]->Value << endl; |
| 202 | 202 | ||
| 203 | cout << endl; | 203 | cout << endl; |
| 204 | 204 | ||
| 205 | for (i = 0; i < newMeasurements.size(); ++i) | 205 | for (i = 0; i < newMeasurements.size(); ++i) |
| 206 | { | 206 | { |
| 207 | currentMeasurement = newMeasurements[i]; | 207 | currentMeasurement = newMeasurements[i]; |
| 208 | frequency = currentMeasurement->Value; | 208 | frequency = currentMeasurement->Value; |
| 209 | signalID = currentMeasurement->SignalID; | 209 | signalID = currentMeasurement->SignalID; |
| 210 | adjustedFrequency = static_cast<int>(frequency * HzResolution); | 210 | adjustedFrequency = static_cast<int>(frequency * HzResolution); |
| 211 | 211 | ||
| 212 | // Do some simple flat line avoidance... | 212 | // Do some simple flat line avoidance... |
| 213 | lastValueIter = m_lastValues.find(signalID); | 213 | lastValueIter = m_lastValues.find(signalID); |
| 214 | 214 | ||
| 215 | if (lastValueIter != m_lastValues.end()) | 215 | if (lastValueIter != m_lastValues.end()) |
| 216 | { | 216 | { |
| 217 | lastValue = lastValueIter->second; | 217 | lastValue = lastValueIter->second; |
| 218 | 218 | ||
| 219 | if (lastValue == adjustedFrequency) | 219 | if (lastValue == adjustedFrequency) |
| 220 | frequency = 0.0; | 220 | frequency = 0.0; |
| 221 | else | 221 | else |
| 222 | m_lastValues[signalID] = adjustedFrequency; | 222 | m_lastValues[signalID] = adjustedFrequency; |
| 223 | } | 223 | } |
| 224 | else | 224 | else |
| 225 | { | 225 | { |
| 226 | m_lastValues[signalID] = adjustedFrequency; | 226 | m_lastValues[signalID] = adjustedFrequency; |
| 227 | } | 227 | } |
| 228 | 228 | ||
| 229 | // Validate frequency | 229 | // Validate frequency |
| 230 | if (frequency > LoFrequency && frequency < HiFrequency) | 230 | if (frequency > LoFrequency && frequency < HiFrequency) |
| 231 | { | 231 | { |
| 232 | frequencyTotal += frequency; | 232 | frequencyTotal += frequency; |
| 233 | 233 | ||
| 234 | if (frequency > maximumFrequency) | 234 | if (frequency > maximumFrequency) |
| 235 | maximumFrequency = frequency; | 235 | maximumFrequency = frequency; |
| 236 | 236 | ||
| 237 | if (frequency < minimumFrequency) | 237 | if (frequency < minimumFrequency) |
| 238 | minimumFrequency = frequency; | 238 | minimumFrequency = frequency; |
| 239 | 239 | ||
| 240 | total++; | 240 | total++; |
| 241 | } | 241 | } |
| 242 | } | 242 | } |
| 243 | 243 | ||
| 244 | if (total > 0) | 244 | if (total > 0) |
| 245 | { | 245 | { |
| 246 | cout << "Avg frequency: " << (frequencyTotal / total) << endl; | 246 | cout << "Avg frequency: " << (frequencyTotal / total) << endl; |
| 247 | cout << "Max frequency: " << maximumFrequency << endl; | 247 | cout << "Max frequency: " << maximumFrequency << endl; |
| 248 | cout << "Min frequency: " << minimumFrequency << endl << endl; | 248 | cout << "Min frequency: " << minimumFrequency << endl << endl; |
| 249 | } | 249 | } |
| 250 | } | 250 | } |
| 251 | } | 251 | } |
| 252 | 252 | ||
| 253 | // Callback that is called when the subscriber auto-reconnects. | 253 | // Callback that is called when the subscriber auto-reconnects. |
| 254 | void Resubscribe(DataSubscriber* source) | 254 | void Resubscribe(DataSubscriber* source) |
| 255 | { | 255 | { |
| 256 | if (source->IsConnected()) | 256 | if (source->IsConnected()) |
| 257 | { | 257 | { |
| 258 | cout << "Reconnected! Subscribing to data..." << endl << endl; | 258 | cout << "Reconnected! Subscribing to data..." << endl << endl; |
| 259 | source->Subscribe(Info); | 259 | source->Subscribe(Info); |
| 260 | } | 260 | } |
| 261 | else | 261 | else |
| 262 | { | 262 | { |
| 263 | source->Disconnect(); | 263 | source->Disconnect(); |
| 264 | cout << "Connection retry attempts exceeded. Press enter to exit." << endl; | 264 | cout << "Connection retry attempts exceeded. Press enter to exit." << endl; |
| 265 | } | 265 | } |
| 266 | } | 266 | } |
| 267 | 267 | ||
| 268 | // Callback which is called to display status messages from the subscriber. | 268 | // Callback which is called to display status messages from the subscriber. |
| 269 | void DisplayStatusMessage(DataSubscriber* source, const string& message) | 269 | void DisplayStatusMessage(DataSubscriber* source, const string& message) |
| 270 | { | 270 | { |
| 271 | cout << message << endl << endl; | 271 | cout << message << endl << endl; |
| 272 | } | 272 | } |
| 273 | 273 | ||
| 274 | // Callback which is called to display error messages from the connector and subscriber. | 274 | // Callback which is called to display error messages from the connector and subscriber. |
| 275 | void DisplayErrorMessage(DataSubscriber* source, const string& message) | 275 | void DisplayErrorMessage(DataSubscriber* source, const string& message) |
| 276 | { | 276 | { |
| 277 | cerr << message << endl << endl; | 277 | cerr << message << endl << endl; |
| 278 | } | 278 | } |