Showing the changes to Source/Libraries/TimeSeriesPlatformLibrary/Samples/AverageFrequencyCalculator.cpp (1 of 32 files). Show all
Unified · Split
| @@ -27,56 +27,56 @@ · 26 unmodified lines | ||
| 27 | 27 | #include <map> |
| 28 | 28 | |
| 29 | 29 | #include "../Common/Convert.h" |
| 30 | −#include "../Common/Measurement.h" | |
| 31 | 30 | #include "../Transport/DataSubscriber.h" |
| 32 | 31 | |
| 33 | −namespace gsfts = GSF::TimeSeries; | |
| 34 | −namespace tst = gsfts::Transport; | |
| 32 | +using namespace GSF::TimeSeries; | |
| 33 | +using namespace GSF::TimeSeries::Transport; | |
| 35 | 34 | |
| 36 | −tst::DataSubscriber Subscriber; | |
| 37 | −tst::SubscriptionInfo Info; | |
| 35 | +DataSubscriber Subscriber; | |
| 36 | +SubscriptionInfo Info; | |
| 38 | 37 | |
| 39 | 38 | // Create helper objects for subscription. |
| 40 | −tst::SubscriberConnector CreateSubscriberConnector(std::string hostname, uint16_t port); | |
| 41 | −tst::SubscriptionInfo CreateSubscriptionInfo(); | |
| 39 | +SubscriberConnector CreateSubscriberConnector(string hostname, uint16_t port); | |
| 40 | +SubscriptionInfo CreateSubscriptionInfo(); | |
| 42 | 41 | |
| 43 | 42 | // Handlers for subscriber callbacks. |
| 44 | −void Resubscribe(tst::DataSubscriber* source); | |
| 45 | −void ProcessMeasurements(tst::DataSubscriber* source, std::vector<gsfts::Measurement> newMeasurements); | |
| 46 | −void DisplayStatusMessage(tst::DataSubscriber* source, std::string message); | |
| 47 | −void DisplayErrorMessage(tst::DataSubscriber* source, std::string message); | |
| 43 | +void Resubscribe(DataSubscriber* source); | |
| 44 | +void ProcessMeasurements(DataSubscriber* source, vector<Measurement> newMeasurements); | |
| 45 | +void DisplayStatusMessage(DataSubscriber* source, string message); | |
| 46 | +void DisplayErrorMessage(DataSubscriber* source, string message); | |
| 48 | 47 | |
| 49 | 48 | // Runs the subscriber. |
| 50 | −void RunSubscriber(std::string hostname, gsfts::uint16_t port); | |
| 49 | +void RunSubscriber(string hostname, uint16_t port); | |
| 51 | 50 | |
| 52 | 51 | // Sample application to demonstrate average frequency calculation using the subscriber API. |
| 53 | 52 | int main(int argc, char* argv[]) |
| 54 | 53 | { |
| 55 | − std::string hostname; | |
| 56 | − gsfts::uint16_t port; | |
| 54 | + string hostname; | |
| 55 | + uint16_t port; | |
| 57 | 56 | |
| 58 | 57 | // Ensure that the necessary |
| 59 | 58 | // command line arguments are given. |
| 60 | 59 | if (argc < 3) |
| 61 | 60 | { |
| 62 | − std::cout << "Usage:" << std::endl; | |
| 63 | − std::cout << " AverageFrequencyCalculator HOSTNAME PORT" << std::endl; | |
| 61 | + cout << "Usage:" << endl; | |
| 62 | + cout << " AverageFrequencyCalculator HOSTNAME PORT" << endl; | |
| 64 | 63 | return 0; |
| 65 | 64 | } |
| 66 | 65 | |
| 67 | 66 | // Get hostname and port. |
| 68 | 67 | hostname = argv[1]; |
| 69 | − std::stringstream(argv[2]) >> port; | |
| 68 | + stringstream(argv[2]) >> port; | |
| 70 | 69 | |
| 71 | 70 | // Run the subscriber. |
| 72 | 71 | RunSubscriber(hostname, port); |
| 73 | 72 | |
| 74 | 73 | // Wait until the user presses enter before quitting. |
| 75 | − std::string line; | |
| 76 | − std::getline(std::cin, line); | |
| 74 | + string line; | |
| 75 | + getline(cin, line); | |
| 77 | 76 | |
| 78 | 77 | // Disconnect the subscriber to stop background threads. |
| 79 | 78 | Subscriber.Disconnect(); |
| 79 | + cout << "Disconnected." << endl; | |
| 80 | 80 | |
| 81 | 81 | return 0; |
| 82 | 82 | } |
| @@ -86,11 +86,11 @@ int main(int argc, char* argv[]) · 3 unmodified lines | ||
| 86 | 86 | // - Register callbacks |
| 87 | 87 | // - Connect to publisher |
| 88 | 88 | // - Subscribe |
| 89 | −void RunSubscriber(std::string hostname, gsfts::uint16_t port) | |
| 89 | +void RunSubscriber(string hostname, uint16_t port) | |
| 90 | 90 | { |
| 91 | 91 | // The connector is declared here because it |
| 92 | 92 | // is only needed for the initial connection |
| 93 | − tst::SubscriberConnector connector; | |
| 93 | + SubscriberConnector connector; | |
| 94 | 94 | |
| 95 | 95 | // Set up helper objects |
| 96 | 96 | connector = CreateSubscriberConnector(hostname, port); |
| @@ -100,18 +100,26 @@ void RunSubscriber(std::string hostname, gsfts::uint16_t port) · 3 unmodified lines | ||
| 100 | 100 | Subscriber.RegisterStatusMessageCallback(&DisplayStatusMessage); |
| 101 | 101 | Subscriber.RegisterErrorMessageCallback(&DisplayErrorMessage); |
| 102 | 102 | |
| 103 | + // Connect and subscribe to publisher | |
| 104 | + cout << endl << "Connecting to " << hostname << ":" << port << "..." << endl << endl; | |
| 105 | + | |
| 103 | 106 | // Connect and subscribe to publisher |
| 104 | 107 | if (connector.Connect(Subscriber)) |
| 108 | + { | |
| 109 | + cout << "Connected! Subscribing to data..." << endl << endl; | |
| 105 | 110 | Subscriber.Subscribe(Info); |
| 111 | + } | |
| 106 | 112 | else |
| 107 | − std::cerr << "All connection attempts failed" << std::endl; | |
| 113 | + { | |
| 114 | + cout << "Connection attempts exceeded. Press enter to exit." << endl; | |
| 115 | + } | |
| 108 | 116 | } |
| 109 | 117 | |
| 110 | −tst::SubscriptionInfo CreateSubscriptionInfo() | |
| 118 | +SubscriptionInfo CreateSubscriptionInfo() | |
| 111 | 119 | { |
| 112 | 120 | // SubscriptionInfo is a helper object which allows the user |
| 113 | 121 | // to set up their subscription and reuse subscription settings. |
| 114 | − tst::SubscriptionInfo info; | |
| 122 | + SubscriptionInfo info; | |
| 115 | 123 | |
| 116 | 124 | info.FilterExpression = "FILTER ActiveMeasurements WHERE SignalType = 'FREQ'"; |
| 117 | 125 | info.NewMeasurementsCallback = &ProcessMeasurements; |
| @@ -131,11 +139,11 @@ tst::SubscriptionInfo CreateSubscriptionInfo() · 13 unmodified lines | ||
| 131 | 139 | return info; |
| 132 | 140 | } |
| 133 | 141 | |
| 134 | −tst::SubscriberConnector CreateSubscriberConnector(std::string hostname, gsfts::uint16_t port) | |
| 142 | +SubscriberConnector CreateSubscriberConnector(string hostname, uint16_t port) | |
| 135 | 143 | { |
| 136 | 144 | // SubscriberConnector is another helper object which allows the |
| 137 | 145 | // user to modify settings for auto-reconnects and retry cycles. |
| 138 | − tst::SubscriberConnector connector; | |
| 146 | + SubscriberConnector connector; | |
| 139 | 147 | |
| 140 | 148 | connector.RegisterErrorMessageCallback(&DisplayErrorMessage); |
| 141 | 149 | connector.RegisterReconnectCallback(&Resubscribe); |
| @@ -151,20 +159,20 @@ tst::SubscriberConnector CreateSubscriberConnector(std::string hostname, gsfts:: · 9 unmodified lines | ||
| 151 | 159 | |
| 152 | 160 | // Callback which is called when the subscriber has |
| 153 | 161 | // received a new packet of measurements from the publisher. |
| 154 | −void ProcessMeasurements(tst::DataSubscriber* source, std::vector<gsfts::Measurement> newMeasurements) | |
| 162 | +void ProcessMeasurements(DataSubscriber* source, vector<Measurement> newMeasurements) | |
| 155 | 163 | { |
| 156 | 164 | const double LoFrequency = 57.0; |
| 157 | 165 | const double HiFrequency = 62.0; |
| 158 | 166 | const double HzResolution = 1000.0; // three decimal places |
| 159 | 167 | |
| 160 | − const std::string TimestampFormat = "%Y-%m-%d %H:%M:%S.%f"; | |
| 161 | − const std::size_t MaxTimestampSize = 80; | |
| 168 | + const string TimestampFormat = "%Y-%m-%d %H:%M:%S.%f"; | |
| 169 | + const size_t MaxTimestampSize = 80; | |
| 162 | 170 | |
| 163 | − static std::map<gsfts::Guid, int> m_lastValues; | |
| 171 | + static map<Guid, int> m_lastValues; | |
| 164 | 172 | |
| 165 | − std::map<gsfts::Guid, int>::iterator lastValueIter; | |
| 166 | − gsfts::Measurement currentMeasurement; | |
| 167 | − gsfts::Guid signalID; | |
| 173 | + map<Guid, int>::iterator lastValueIter; | |
| 174 | + Measurement currentMeasurement; | |
| 175 | + Guid signalID; | |
| 168 | 176 | |
| 169 | 177 | double frequency; |
| 170 | 178 | double frequencyTotal; |
| @@ -175,24 +183,24 @@ void ProcessMeasurements(tst::DataSubscriber* source, std::vector<gsfts::Measure · 4 unmodified lines | ||
| 175 | 183 | int total; |
| 176 | 184 | |
| 177 | 185 | char timestamp[MaxTimestampSize]; |
| 178 | − std::size_t i; | |
| 186 | + size_t i; | |
| 179 | 187 | |
| 180 | 188 | frequencyTotal = 0.0; |
| 181 | 189 | total = 0; |
| 182 | 190 | |
| 183 | − std::cout << Subscriber.GetTotalMeasurementsReceived() << " measurements received so far..." << std::endl; | |
| 191 | + cout << Subscriber.GetTotalMeasurementsReceived() << " measurements received so far..." << endl; | |
| 184 | 192 | |
| 185 | 193 | if (newMeasurements.size() > 0) |
| 186 | 194 | { |
| 187 | − if (gsfts::TicksToString(timestamp, MaxTimestampSize, TimestampFormat, newMeasurements[0].Timestamp)) | |
| 188 | − std::cout << "Timestamp: " << std::string(timestamp) << std::endl; | |
| 195 | + if (TicksToString(timestamp, MaxTimestampSize, TimestampFormat, newMeasurements[0].Timestamp)) | |
| 196 | + cout << "Timestamp: " << string(timestamp) << endl; | |
| 189 | 197 | |
| 190 | − std::cout << "Point\tValue" << std::endl; | |
| 198 | + cout << "Point\tValue" << endl; | |
| 191 | 199 | |
| 192 | 200 | for (i = 0; i < newMeasurements.size(); ++i) |
| 193 | − std::cout << newMeasurements[i].ID << '\t' << newMeasurements[i].Value << std::endl; | |
| 201 | + cout << newMeasurements[i].ID << '\t' << newMeasurements[i].Value << endl; | |
| 194 | 202 | |
| 195 | − std::cout << std::endl; | |
| 203 | + cout << endl; | |
| 196 | 204 | |
| 197 | 205 | for (i = 0; i < newMeasurements.size(); ++i) |
| 198 | 206 | { |
| @@ -235,28 +243,36 @@ void ProcessMeasurements(tst::DataSubscriber* source, std::vector<gsfts::Measure · 36 unmodified lines | ||
| 235 | 243 | |
| 236 | 244 | if (total > 0) |
| 237 | 245 | { |
| 238 | − std::cout << "Avg frequency: " << (frequencyTotal / total) << std::endl; | |
| 239 | − std::cout << "Max frequency: " << maximumFrequency << std::endl; | |
| 240 | − std::cout << "Min frequency: " << minimumFrequency << std::endl << std::endl; | |
| 246 | + cout << "Avg frequency: " << (frequencyTotal / total) << endl; | |
| 247 | + cout << "Max frequency: " << maximumFrequency << endl; | |
| 248 | + cout << "Min frequency: " << minimumFrequency << endl << endl; | |
| 241 | 249 | } |
| 242 | 250 | } |
| 243 | 251 | } |
| 244 | 252 | |
| 245 | 253 | // Callback that is called when the subscriber auto-reconnects. |
| 246 | −void Resubscribe(tst::DataSubscriber* source) | |
| 254 | +void Resubscribe(DataSubscriber* source) | |
| 247 | 255 | { |
| 248 | 256 | if (source->IsConnected()) |
| 257 | + { | |
| 258 | + cout << "Reconnected! Subscribing to data..." << endl << endl; | |
| 249 | 259 | source->Subscribe(Info); |
| 260 | + } | |
| 261 | + else | |
| 262 | + { | |
| 263 | + source->Disconnect(); | |
| 264 | + cout << "Connection retry attempts exceeded. Press enter to exit." << endl; | |
| 265 | + } | |
| 250 | 266 | } |
| 251 | 267 | |
| 252 | 268 | // Callback which is called to display status messages from the subscriber. |
| 253 | −void DisplayStatusMessage(tst::DataSubscriber* source, std::string message) | |
| 269 | +void DisplayStatusMessage(DataSubscriber* source, string message) | |
| 254 | 270 | { |
| 255 | − std::cout << message << std::endl << std::endl; | |
| 271 | + cout << message << endl << endl; | |
| 256 | 272 | } |
| 257 | 273 | |
| 258 | 274 | // Callback which is called to display error messages from the connector and subscriber. |
| 259 | −void DisplayErrorMessage(tst::DataSubscriber* source, std::string message) | |
| 275 | +void DisplayErrorMessage(DataSubscriber* source, string message) | |
| 260 | 276 | { |
| 261 | − std::cerr << message << std::endl << std::endl; | |
| 277 | + cerr << message << endl << endl; | |
| 262 | 278 | } |