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