Showing the changes to Source/Libraries/TimeSeriesPlatformLibrary/Samples/TemporalSubscriber.cpp (1 of 14 files). Show all
Unified · Split
| @@ -0,0 +1,112 @@ | |||
| 1 | //****************************************************************************************************** | ||
| 2 | // TemporalSubscriber.cpp - Gbtc | ||
| 3 | // | ||
| 4 | // Copyright � 2019, 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/01/2019 - J. Ritchie Carroll | ||
| 20 | // Generated original version of source code. | ||
| 21 | // | ||
| 22 | //****************************************************************************************************** | ||
| 23 | |||
| 24 | #include "TemporalSubscriber.h" | ||
| 25 | |||
| 26 | using namespace std; | ||
| 27 | using namespace GSF; | ||
| 28 | using namespace GSF::Data; | ||
| 29 | using namespace GSF::TimeSeries; | ||
| 30 | |||
| 31 | GSF::Data::DataSetPtr TemporalSubscriber::s_historyDataSet = nullptr; | ||
| 32 | GSF::Data::DataTablePtr TemporalSubscriber::s_history = nullptr; | ||
| 33 | |||
| 34 | TemporalSubscriber::TemporalSubscriber(const SubscriberConnectionPtr& connection, std::function<void(const GSF::Guid&)> removeHandler) : | ||
| 35 | m_connection(connection), | ||
| 36 | m_removeHandler(removeHandler), | ||
| 37 | m_startTimestamp(ToTicks(m_connection->GetStartTimeConstraint())), | ||
| 38 | m_stopTimestamp(ToTicks(m_connection->GetStopTimeConstraint())), | ||
| 39 | m_currentTimestamp(m_startTimestamp), | ||
| 40 | m_currentRow(0), | ||
| 41 | m_stopped(false) | ||
| 42 | { | ||
| 43 | if (s_historyDataSet == nullptr) | ||
| 44 | { | ||
| 45 | s_historyDataSet = DataSet::FromXml("History.xml"); | ||
| 46 | s_history = s_historyDataSet->Table("History"); | ||
| 47 | } | ||
| 48 | |||
| 49 | m_lastRow = s_history->RowCount() - 1; | ||
| 50 | |||
| 51 | if (m_lastRow < 0) | ||
| 52 | throw runtime_error("No history available - run with \"GenHistory\" argument."); | ||
| 53 | |||
| 54 | m_processTimer = NewSharedPtr<Timer>(33, [this](Timer*, void*) { SendTemporalData(); }, true); | ||
| 55 | m_processTimer->Start(); | ||
| 56 | } | ||
| 57 | |||
| 58 | TemporalSubscriber::~TemporalSubscriber() | ||
| 59 | { | ||
| 60 | CompleteTemporalSubscription(); | ||
| 61 | } | ||
| 62 | |||
| 63 | void TemporalSubscriber::SendTemporalData() | ||
| 64 | { | ||
| 65 | static DataTable& history = *s_history; | ||
| 66 | static const int32_t signalIDColumn = history["SignalID"]->Index(); | ||
| 67 | static const int32_t timestampColumn = history["Timestamp"]->Index(); | ||
| 68 | static const int32_t valueColumn = history["Value"]->Index(); | ||
| 69 | |||
| 70 | vector<MeasurementPtr> measurements; | ||
| 71 | DataRow& row = *history.Row(m_currentRow); | ||
| 72 | int64_t historyTimestamp = row.ValueAsInt64(timestampColumn).GetValueOrDefault(); | ||
| 73 | const int64_t groupTimestamp = historyTimestamp; | ||
| 74 | |||
| 75 | while (historyTimestamp == groupTimestamp) | ||
| 76 | { | ||
| 77 | MeasurementPtr measurement = NewSharedPtr<Measurement>(); | ||
| 78 | |||
| 79 | measurement->Timestamp = m_currentTimestamp; | ||
| 80 | measurement->SignalID = row.ValueAsGuid(signalIDColumn).GetValueOrDefault(); | ||
| 81 | measurement->Value = row.ValueAsDouble(valueColumn).GetValueOrDefault(); | ||
| 82 | |||
| 83 | measurements.push_back(measurement); | ||
| 84 | |||
| 85 | if (m_currentRow > m_lastRow) | ||
| 86 | m_currentRow = 0; | ||
| 87 | else | ||
| 88 | m_currentRow++; | ||
| 89 | |||
| 90 | row = *history.Row(m_currentRow); | ||
| 91 | historyTimestamp = row.ValueAsInt64(timestampColumn).GetValueOrDefault(); | ||
| 92 | } | ||
| 93 | |||
| 94 | m_connection->PublishMeasurements(measurements); | ||
| 95 | |||
| 96 | // Setup next publication timestamp | ||
| 97 | m_currentTimestamp += HistoryInterval; | ||
| 98 | |||
| 99 | if (m_currentTimestamp > m_stopTimestamp) | ||
| 100 | CompleteTemporalSubscription(); | ||
| 101 | } | ||
| 102 | |||
| 103 | void TemporalSubscriber::CompleteTemporalSubscription() | ||
| 104 | { | ||
| 105 | if (m_stopped) | ||
| 106 | return; | ||
| 107 | |||
| 108 | m_stopped = true; | ||
| 109 | m_processTimer->Stop(); | ||
| 110 | m_connection->CompleteTemporalSubscription(); | ||
| 111 | m_removeHandler(m_connection->GetSubscriberID()); | ||
| 112 | } | ||