Showing the changes to Source/Libraries/TimeSeriesPlatformLibrary/Samples/SubscriberHandler.cpp (1 of 39 files). Show all
Unified · Split
| @@ -0,0 +1,194 @@ | |||
| 1 | //****************************************************************************************************** | ||
| 2 | // SubscriberHandler.cpp - Gbtc | ||
| 3 | // | ||
| 4 | // Copyright � 2018, Grid Protection Alliance. All Rights Reserved. | ||
| 5 | // | ||
| 6 | // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See | ||
| 7 | // the NOTICE file distributed with this work for additional information regarding copyright ownership. | ||
| 8 | // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this | ||
| 9 | // file except in compliance with the License. You may obtain a copy of the License at: | ||
| 10 | // | ||
| 11 | // http://opensource.org/licenses/MIT | ||
| 12 | // | ||
| 13 | // Unless agreed to in writing, the subject software distributed under the License is distributed on an | ||
| 14 | // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the | ||
| 15 | // License for the specific language governing permissions and limitations. | ||
| 16 | // | ||
| 17 | // Code Modification History: | ||
| 18 | // ---------------------------------------------------------------------------------------------------- | ||
| 19 | // 03/27/2018 - J. Ritchie Carroll | ||
| 20 | // Generated original version of source code. | ||
| 21 | // | ||
| 22 | //****************************************************************************************************** | ||
| 23 | |||
| 24 | #include "SubscriberHandler.h" | ||
| 25 | #include "../Common/Convert.h" | ||
| 26 | |||
| 27 | using namespace std; | ||
| 28 | using namespace GSF::TimeSeries; | ||
| 29 | |||
| 30 | SubscriberHandler::SubscriberHandler(const string& name) | ||
| 31 | { | ||
| 32 | m_name = name; | ||
| 33 | } | ||
| 34 | |||
| 35 | SubscriptionInfo SubscriberHandler::CreateSubscriptionInfo() | ||
| 36 | { | ||
| 37 | SubscriptionInfo info = SubscriberInstance::CreateSubscriptionInfo(); | ||
| 38 | |||
| 39 | // TODO: Modify subscription info properties as desired... | ||
| 40 | |||
| 41 | // To set up a remotely synchronized subscription, set this flag | ||
| 42 | // to true and add the framesPerSecond parameter to the | ||
| 43 | // ExtraConnectionStringParameters. Additionally, the following | ||
| 44 | // example demonstrates the use of some other useful parameters | ||
| 45 | // when setting up remotely synchronized subscriptions. | ||
| 46 | |||
| 47 | //info.RemotelySynchronized = true; | ||
| 48 | //info.ExtraConnectionStringParameters = "framesPerSecond=30;timeResolution=10000;downsamplingMethod=Closest"; | ||
| 49 | //info.LagTime = 3.0; | ||
| 50 | //info.LeadTime = 1.0; | ||
| 51 | //info.UseLocalClockAsRealTime = false; | ||
| 52 | |||
| 53 | // Other example properties (see SubscriptionInfo.h for all properties) | ||
| 54 | //info.Throttled = false; | ||
| 55 | //info.IncludeTime = true; | ||
| 56 | //info.UseMillisecondResolution = true; | ||
| 57 | |||
| 58 | return info; | ||
| 59 | } | ||
| 60 | |||
| 61 | void SubscriberHandler::StatusMessage(const string& message) | ||
| 62 | { | ||
| 63 | // TODO: Make sure these messages get logged to an appropriate location | ||
| 64 | // For now, the base class just displays to console: | ||
| 65 | stringstream status; | ||
| 66 | |||
| 67 | status << "[" << m_name << "] " << message; | ||
| 68 | |||
| 69 | SubscriberInstance::StatusMessage(status.str()); | ||
| 70 | } | ||
| 71 | |||
| 72 | void SubscriberHandler::ErrorMessage(const string& message) | ||
| 73 | { | ||
| 74 | // TODO: Make sure these messages get logged to an appropriate location | ||
| 75 | // For now, the base class just displays to console: | ||
| 76 | stringstream status; | ||
| 77 | |||
| 78 | status << "[" << m_name << "] " << message; | ||
| 79 | |||
| 80 | SubscriberInstance::ErrorMessage(status.str()); | ||
| 81 | } | ||
| 82 | |||
| 83 | void SubscriberHandler::DataStartTime(time_t unixSOC, int milliseconds) | ||
| 84 | { | ||
| 85 | // TODO: This reports timestamp of very first received measurement (if useful) | ||
| 86 | } | ||
| 87 | |||
| 88 | void SubscriberHandler::ReceivedMetadata(const vector<uint8_t>& payload) | ||
| 89 | { | ||
| 90 | stringstream message; | ||
| 91 | message << "Received " << payload.size() << " bytes of metadata, parsing..."; | ||
| 92 | StatusMessage(message.str()); | ||
| 93 | |||
| 94 | SubscriberInstance::ReceivedMetadata(payload); | ||
| 95 | } | ||
| 96 | |||
| 97 | void SubscriberHandler::ParsedMetadata() | ||
| 98 | { | ||
| 99 | StatusMessage("Metadata successfully parsed."); | ||
| 100 | } | ||
| 101 | |||
| 102 | void SubscriberHandler::ReceivedNewMeasurements(const vector<MeasurementPtr>& measurements) | ||
| 103 | { | ||
| 104 | // Start processing measurements | ||
| 105 | for (auto &measurement: measurements) | ||
| 106 | { | ||
| 107 | time_t soc; | ||
| 108 | int16_t milliseconds; | ||
| 109 | |||
| 110 | // Get adjusted value | ||
| 111 | float64_t value = measurement->AdjustedValue(); | ||
| 112 | |||
| 113 | // Get time converted to UNIX second of century plus milliseconds | ||
| 114 | measurement->GetUnixTime(soc, milliseconds); | ||
| 115 | |||
| 116 | // Handle per measurement quality flags | ||
| 117 | int32_t qualityFlags = measurement->Flags; | ||
| 118 | |||
| 119 | ConfigurationFramePtr configurationFrame; | ||
| 120 | |||
| 121 | // Find associated configuration for measurement | ||
| 122 | if (TryFindTargetConfigurationFrame(measurement->SignalID, configurationFrame)) | ||
| 123 | { | ||
| 124 | MeasurementMetadataPtr measurementMetadata; | ||
| 125 | |||
| 126 | // Lookup measurement metadata - it's faster to find metadata from within configuration frame | ||
| 127 | if (TryGetMeasurementMetdataFromConfigurationFrame(measurement->SignalID, configurationFrame, measurementMetadata)) | ||
| 128 | { | ||
| 129 | const SignalReference& reference = measurementMetadata->Reference; | ||
| 130 | |||
| 131 | // reference.Acronym << target device acronym | ||
| 132 | // reference.Kind << kind of signal (see SignalKind in "Types.h"), like Frequency, Angle, etc | ||
| 133 | // reference.Index << for Phasors, Analogs and Digitals - this is the ordered "index" | ||
| 134 | |||
| 135 | // TODO: Handle measurement processing here... | ||
| 136 | } | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | // TODO: *** Temporary Testing Code Below *** -- REMOVE BEFOR USE | ||
| 141 | const string TimestampFormat = "%Y-%m-%d %H:%M:%S.%f"; | ||
| 142 | const size_t MaxTimestampSize = 80; | ||
| 143 | |||
| 144 | static int processCount = 0; | ||
| 145 | size_t i; | ||
| 146 | |||
| 147 | char timestamp[MaxTimestampSize]; | ||
| 148 | |||
| 149 | // Only display messages every five | ||
| 150 | // seconds (assuming 30 calls per second). | ||
| 151 | if (processCount % 150 == 0) | ||
| 152 | { | ||
| 153 | stringstream message; | ||
| 154 | |||
| 155 | message << GetTotalMeasurementsReceived() << " measurements received so far..." << endl; | ||
| 156 | |||
| 157 | if (!measurements.empty()) | ||
| 158 | { | ||
| 159 | if (TicksToString(timestamp, MaxTimestampSize, TimestampFormat, measurements[0]->Timestamp)) | ||
| 160 | message << "Timestamp: " << string(timestamp) << endl; | ||
| 161 | |||
| 162 | message << "Point\tValue" << endl; | ||
| 163 | |||
| 164 | for (i = 0; i < measurements.size(); ++i) | ||
| 165 | message << measurements[i]->ID << '\t' << measurements[i]->Value << endl; | ||
| 166 | |||
| 167 | message << endl; | ||
| 168 | } | ||
| 169 | |||
| 170 | StatusMessage(message.str()); | ||
| 171 | } | ||
| 172 | |||
| 173 | ++processCount; | ||
| 174 | } | ||
| 175 | |||
| 176 | void SubscriberHandler::ConfigurationChanged() | ||
| 177 | { | ||
| 178 | StatusMessage("Configuration change detected. Metadata refresh requested."); | ||
| 179 | } | ||
| 180 | |||
| 181 | void SubscriberHandler::HistoricalReadComplete() | ||
| 182 | { | ||
| 183 | StatusMessage("Historical data read complete."); | ||
| 184 | } | ||
| 185 | |||
| 186 | void SubscriberHandler::ConnectionEstablished() | ||
| 187 | { | ||
| 188 | StatusMessage("Connection established."); | ||
| 189 | } | ||
| 190 | |||
| 191 | void SubscriberHandler::ConnectionTerminated() | ||
| 192 | { | ||
| 193 | StatusMessage("Connection terminated."); | ||
| 194 | } | ||