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 | #include <map> | 27 | #include <map> |
| 28 | 28 | ||
| 29 | #include "../Common/Convert.h" | 29 | #include "../Common/Convert.h" |
| 30 | #include "../Common/Measurement.h" | ||
| 31 | #include "../Transport/DataSubscriber.h" | 30 | #include "../Transport/DataSubscriber.h" |
| 32 | 31 | ||
| 33 | namespace gsfts = GSF::TimeSeries; | 32 | using namespace GSF::TimeSeries; |
| 34 | namespace tst = gsfts::Transport; | 33 | using namespace GSF::TimeSeries::Transport; |
| 35 | 34 | ||
| 36 | tst::DataSubscriber Subscriber; | 35 | DataSubscriber Subscriber; |
| 37 | tst::SubscriptionInfo Info; | 36 | SubscriptionInfo Info; |
| 38 | 37 | ||
| 39 | // Create helper objects for subscription. | 38 | // Create helper objects for subscription. |
| 40 | tst::SubscriberConnector CreateSubscriberConnector(std::string hostname, uint16_t port); | 39 | SubscriberConnector CreateSubscriberConnector(string hostname, uint16_t port); |
| 41 | tst::SubscriptionInfo CreateSubscriptionInfo(); | 40 | SubscriptionInfo CreateSubscriptionInfo(); |
| 42 | 41 | ||
| 43 | // Handlers for subscriber callbacks. | 42 | // Handlers for subscriber callbacks. |
| 44 | void Resubscribe(tst::DataSubscriber* source); | 43 | void Resubscribe(DataSubscriber* source); |
| 45 | void ProcessMeasurements(tst::DataSubscriber* source, std::vector<gsfts::Measurement> newMeasurements); | 44 | void ProcessMeasurements(DataSubscriber* source, vector<Measurement> newMeasurements); |
| 46 | void DisplayStatusMessage(tst::DataSubscriber* source, std::string message); | 45 | void DisplayStatusMessage(DataSubscriber* source, string message); |
| 47 | void DisplayErrorMessage(tst::DataSubscriber* source, std::string message); | 46 | void DisplayErrorMessage(DataSubscriber* source, string message); |
| 48 | 47 | ||
| 49 | // Runs the subscriber. | 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 | // Sample application to demonstrate average frequency calculation using the subscriber API. | 51 | // Sample application to demonstrate average frequency calculation using the subscriber API. |
| 53 | int main(int argc, char* argv[]) | 52 | int main(int argc, char* argv[]) |
| 54 | { | 53 | { |
| 55 | std::string hostname; | 54 | string hostname; |
| 56 | gsfts::uint16_t port; | 55 | uint16_t port; |
| 57 | 56 | ||
| 58 | // Ensure that the necessary | 57 | // Ensure that the necessary |
| 59 | // command line arguments are given. | 58 | // command line arguments are given. |
| 60 | if (argc < 3) | 59 | if (argc < 3) |
| 61 | { | 60 | { |
| 62 | std::cout << "Usage:" << std::endl; | 61 | cout << "Usage:" << endl; |
| 63 | std::cout << " AverageFrequencyCalculator HOSTNAME PORT" << std::endl; | 62 | cout << " AverageFrequencyCalculator HOSTNAME PORT" << endl; |
| 64 | return 0; | 63 | return 0; |
| 65 | } | 64 | } |
| 66 | 65 | ||
| 67 | // Get hostname and port. | 66 | // Get hostname and port. |
| 68 | hostname = argv[1]; | 67 | hostname = argv[1]; |
| 69 | std::stringstream(argv[2]) >> port; | 68 | stringstream(argv[2]) >> port; |
| 70 | 69 | ||
| 71 | // Run the subscriber. | 70 | // Run the subscriber. |
| 72 | RunSubscriber(hostname, port); | 71 | RunSubscriber(hostname, port); |
| 73 | 72 | ||
| 74 | // Wait until the user presses enter before quitting. | 73 | // Wait until the user presses enter before quitting. |
| 75 | std::string line; | 74 | string line; |
| 76 | std::getline(std::cin, line); | 75 | getline(cin, line); |
| 77 | 76 | ||
| 78 | // Disconnect the subscriber to stop background threads. | 77 | // Disconnect the subscriber to stop background threads. |
| 79 | Subscriber.Disconnect(); | 78 | Subscriber.Disconnect(); |
| 79 | cout << "Disconnected." << endl; | ||
| 80 | 80 | ||
| 81 | return 0; | 81 | return 0; |
| 82 | } | 82 | } |
| @@ -86,11 +86,11 @@ int main(int argc, char* argv[]) · 3 unmodified lines | |||
| 86 | // - Register callbacks | 86 | // - Register callbacks |
| 87 | // - Connect to publisher | 87 | // - Connect to publisher |
| 88 | // - Subscribe | 88 | // - Subscribe |
| 89 | void RunSubscriber(std::string hostname, gsfts::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 | tst::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); |
| @@ -100,18 +100,26 @@ void RunSubscriber(std::string hostname, gsfts::uint16_t port) · 3 unmodified lines | |||
| 100 | Subscriber.RegisterStatusMessageCallback(&DisplayStatusMessage); | 100 | Subscriber.RegisterStatusMessageCallback(&DisplayStatusMessage); |
| 101 | Subscriber.RegisterErrorMessageCallback(&DisplayErrorMessage); | 101 | Subscriber.RegisterErrorMessageCallback(&DisplayErrorMessage); |
| 102 | 102 | ||
| 103 | // Connect and subscribe to publisher | ||
| 104 | cout << endl << "Connecting to " << hostname << ":" << port << "..." << endl << endl; | ||
| 105 | |||
| 103 | // Connect and subscribe to publisher | 106 | // Connect and subscribe to publisher |
| 104 | if (connector.Connect(Subscriber)) | 107 | if (connector.Connect(Subscriber)) |
| 108 | { | ||
| 109 | cout << "Connected! Subscribing to data..." << endl << endl; | ||
| 105 | Subscriber.Subscribe(Info); | 110 | Subscriber.Subscribe(Info); |
| 111 | } | ||
| 106 | else | 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 | // SubscriptionInfo is a helper object which allows the user | 120 | // SubscriptionInfo is a helper object which allows the user |
| 113 | // to set up their subscription and reuse subscription settings. | 121 | // to set up their subscription and reuse subscription settings. |
| 114 | tst::SubscriptionInfo info; | 122 | SubscriptionInfo info; |
| 115 | 123 | ||
| 116 | info.FilterExpression = "FILTER ActiveMeasurements WHERE SignalType = 'FREQ'"; | 124 | info.FilterExpression = "FILTER ActiveMeasurements WHERE SignalType = 'FREQ'"; |
| 117 | info.NewMeasurementsCallback = &ProcessMeasurements; | 125 | info.NewMeasurementsCallback = &ProcessMeasurements; |
| @@ -131,11 +139,11 @@ tst::SubscriptionInfo CreateSubscriptionInfo() · 13 unmodified lines | |||
| 131 | return info; | 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 | // SubscriberConnector is another helper object which allows the | 144 | // SubscriberConnector is another helper object which allows the |
| 137 | // user to modify settings for auto-reconnects and retry cycles. | 145 | // user to modify settings for auto-reconnects and retry cycles. |
| 138 | tst::SubscriberConnector connector; | 146 | SubscriberConnector connector; |
| 139 | 147 | ||
| 140 | connector.RegisterErrorMessageCallback(&DisplayErrorMessage); | 148 | connector.RegisterErrorMessageCallback(&DisplayErrorMessage); |
| 141 | connector.RegisterReconnectCallback(&Resubscribe); | 149 | connector.RegisterReconnectCallback(&Resubscribe); |
| @@ -151,20 +159,20 @@ tst::SubscriberConnector CreateSubscriberConnector(std::string hostname, gsfts:: · 9 unmodified lines | |||
| 151 | 159 | ||
| 152 | // Callback which is called when the subscriber has | 160 | // Callback which is called when the subscriber has |
| 153 | // received a new packet of measurements from the publisher. | 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 | const double LoFrequency = 57.0; | 164 | const double LoFrequency = 57.0; |
| 157 | const double HiFrequency = 62.0; | 165 | const double HiFrequency = 62.0; |
| 158 | const double HzResolution = 1000.0; // three decimal places | 166 | const double HzResolution = 1000.0; // three decimal places |
| 159 | 167 | ||
| 160 | const std::string TimestampFormat = "%Y-%m-%d %H:%M:%S.%f"; | 168 | const string TimestampFormat = "%Y-%m-%d %H:%M:%S.%f"; |
| 161 | const std::size_t MaxTimestampSize = 80; | 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; | 173 | map<Guid, int>::iterator lastValueIter; |
| 166 | gsfts::Measurement currentMeasurement; | 174 | Measurement currentMeasurement; |
| 167 | gsfts::Guid signalID; | 175 | Guid signalID; |
| 168 | 176 | ||
| 169 | double frequency; | 177 | double frequency; |
| 170 | double frequencyTotal; | 178 | double frequencyTotal; |
| @@ -175,24 +183,24 @@ void ProcessMeasurements(tst::DataSubscriber* source, std::vector<gsfts::Measure · 4 unmodified lines | |||
| 175 | int total; | 183 | int total; |
| 176 | 184 | ||
| 177 | char timestamp[MaxTimestampSize]; | 185 | char timestamp[MaxTimestampSize]; |
| 178 | std::size_t i; | 186 | size_t i; |
| 179 | 187 | ||
| 180 | frequencyTotal = 0.0; | 188 | frequencyTotal = 0.0; |
| 181 | total = 0; | 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 | if (newMeasurements.size() > 0) | 193 | if (newMeasurements.size() > 0) |
| 186 | { | 194 | { |
| 187 | if (gsfts::TicksToString(timestamp, MaxTimestampSize, TimestampFormat, newMeasurements[0].Timestamp)) | 195 | if (TicksToString(timestamp, MaxTimestampSize, TimestampFormat, newMeasurements[0].Timestamp)) |
| 188 | std::cout << "Timestamp: " << std::string(timestamp) << std::endl; | 196 | cout << "Timestamp: " << string(timestamp) << endl; |
| 189 | 197 | ||
| 190 | std::cout << "Point\tValue" << std::endl; | 198 | cout << "Point\tValue" << endl; |
| 191 | 199 | ||
| 192 | for (i = 0; i < newMeasurements.size(); ++i) | 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 | for (i = 0; i < newMeasurements.size(); ++i) | 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 | if (total > 0) | 244 | if (total > 0) |
| 237 | { | 245 | { |
| 238 | std::cout << "Avg frequency: " << (frequencyTotal / total) << std::endl; | 246 | cout << "Avg frequency: " << (frequencyTotal / total) << endl; |
| 239 | std::cout << "Max frequency: " << maximumFrequency << std::endl; | 247 | cout << "Max frequency: " << maximumFrequency << endl; |
| 240 | std::cout << "Min frequency: " << minimumFrequency << std::endl << std::endl; | 248 | cout << "Min frequency: " << minimumFrequency << endl << endl; |
| 241 | } | 249 | } |
| 242 | } | 250 | } |
| 243 | } | 251 | } |
| 244 | 252 | ||
| 245 | // Callback that is called when the subscriber auto-reconnects. | 253 | // Callback that is called when the subscriber auto-reconnects. |
| 246 | void Resubscribe(tst::DataSubscriber* source) | 254 | void Resubscribe(DataSubscriber* source) |
| 247 | { | 255 | { |
| 248 | if (source->IsConnected()) | 256 | if (source->IsConnected()) |
| 257 | { | ||
| 258 | cout << "Reconnected! Subscribing to data..." << endl << endl; | ||
| 249 | source->Subscribe(Info); | 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 | // Callback which is called to display status messages from the subscriber. | 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 | // 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. |
| 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 | } |