Showing the changes to Source/Libraries/TimeSeriesPlatformLibrary/Samples/GenHistory.cpp (1 of 14 files). Show all
Unified · Split
| @@ -0,0 +1,101 @@ | |||
| 1 | //****************************************************************************************************** | ||
| 2 | // GenHistory.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 - Ritchie Carroll | ||
| 20 | // Generated original version of source code. | ||
| 21 | // | ||
| 22 | //****************************************************************************************************** | ||
| 23 | |||
| 24 | #include "GenHistory.h" | ||
| 25 | #include "../Transport/SubscriberInstance.h" | ||
| 26 | #include <iostream> | ||
| 27 | |||
| 28 | using namespace std; | ||
| 29 | using namespace GSF; | ||
| 30 | using namespace GSF::Data; | ||
| 31 | using namespace GSF::TimeSeries; | ||
| 32 | using namespace GSF::TimeSeries::Transport; | ||
| 33 | |||
| 34 | GenHistory::GenHistory(uint16_t port) : | ||
| 35 | m_history(nullptr), | ||
| 36 | m_port(port) | ||
| 37 | { | ||
| 38 | m_subscriber = NewSharedPtr<DataSubscriber>(); | ||
| 39 | m_subscriber->SetUserData(this); | ||
| 40 | } | ||
| 41 | |||
| 42 | void GenHistory::StartArchive() | ||
| 43 | { | ||
| 44 | m_history = NewSharedPtr<DataSet>(); | ||
| 45 | DataTablePtr history = m_history->CreateTable("History"); | ||
| 46 | |||
| 47 | history->AddColumn(history->CreateColumn("SignalID", DataType::Guid)); | ||
| 48 | history->AddColumn(history->CreateColumn("Timestamp", DataType::Int64)); | ||
| 49 | history->AddColumn(history->CreateColumn("Value", DataType::Double)); | ||
| 50 | |||
| 51 | m_history->AddOrUpdateTable(history); | ||
| 52 | |||
| 53 | m_subscriber->RegisterNewMeasurementsCallback(&GenHistory::ProcessMeasurements); | ||
| 54 | m_subscriber->Connect("localhost", m_port); | ||
| 55 | |||
| 56 | SubscriptionInfo info; | ||
| 57 | info.FilterExpression = SubscriberInstance::SubscribeAllNoStatsExpression; | ||
| 58 | |||
| 59 | m_subscriber->Subscribe(info); | ||
| 60 | } | ||
| 61 | |||
| 62 | void GenHistory::StopArchive() const | ||
| 63 | { | ||
| 64 | if (m_subscriber->IsConnected()) | ||
| 65 | { | ||
| 66 | if (m_subscriber->IsSubscribed()) | ||
| 67 | m_subscriber->Unsubscribe(); | ||
| 68 | |||
| 69 | m_subscriber->Disconnect(); | ||
| 70 | |||
| 71 | if (m_history) | ||
| 72 | { | ||
| 73 | cout << endl << "Client disconnected, writing history dataset..." << endl; | ||
| 74 | m_history->WriteXml("History.xml"); | ||
| 75 | cout << endl << "Dataset export complete, see \"History.xml\"." << endl; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | void GenHistory::ProcessMeasurements(DataSubscriber* source, const vector<MeasurementPtr>& measurements) | ||
| 81 | { | ||
| 82 | static const GenHistory& instance = *static_cast<const GenHistory*>(source->GetUserData()); | ||
| 83 | static DataTable& history = *instance.m_history->Table("History"); | ||
| 84 | static const int32_t signalIDColumn = history["SignalID"]->Index(); | ||
| 85 | static const int32_t timestampColumn = history["Timestamp"]->Index(); | ||
| 86 | static const int32_t valueColumn = history["Value"]->Index(); | ||
| 87 | |||
| 88 | for (const auto& measurement : measurements) | ||
| 89 | { | ||
| 90 | DataRowPtr row = history.CreateRow(); | ||
| 91 | |||
| 92 | row->SetGuidValue(signalIDColumn, measurement->SignalID); | ||
| 93 | row->SetInt64Value(timestampColumn, measurement->Timestamp); | ||
| 94 | row->SetDoubleValue(valueColumn, measurement->Value); | ||
| 95 | |||
| 96 | history.AddRow(row); | ||
| 97 | } | ||
| 98 | |||
| 99 | if (history.RowCount() >= 400) | ||
| 100 | Thread([]{ instance.StopArchive(); }); | ||
| 101 | } | ||