SampleReceiver/libwfr/src/MultiStreamToFile.cpp
Wiesner András a6be70fb7b - MultiStreamOscilloscope first implementation done
- SlopeTrigger -> EdgeTrigger
- working Python example included
- libwfr.h introduced for easy including all libwfr headers
- ChannelBuffer is now template
- Semaphore moved
2022-05-05 22:19:52 +02:00

72 lines
2.2 KiB
C++

//
// Created by epagris on 2022.05.04..
//
#include <stdexcept>
#include <unistd.h>
#include <arpa/inet.h>
#include "MultiStreamToFile.h"
#include "Logger.h"
MultiStreamToFile::MultiStreamToFile(const std::string &targetDir) {
mTargetDir = targetDir;
}
void MultiStreamToFile::createSampleWriters() {
struct stat sb;
if (stat(mTargetDir.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) { // if directory exists then leave it as it is
//unlink(datasetName.c_str()); FIXME
} else if (mkdir(mTargetDir.c_str(), 0777) == -1) { // else: create directory for dataset
throw std::runtime_error("Could not create directory " + mTargetDir + "!");
}
if (chdir(mTargetDir.c_str()) == -1) {
throw std::runtime_error("Could not change to target directory!");
}
std::string nodeDirHint = "";
// create sample writers, ONE-PER-CHANNEL
size_t acc_ch = 0; // accumulative channel index (across all preceding nodes)
for (unsigned int &mClientNode: mNodes) {
for (size_t ch = 0; ch < mAcqFmt.channel_count; ch++) {
// create sample writer
std::string datasetName = std::string("node_") + inet_ntoa({mClientNode}) + "_CH" + std::to_string(acc_ch);
mpSampleWriters.emplace_back(std::make_shared<SampleWriter>(datasetName, mAcqFmt));
// create hint
nodeDirHint += datasetName + "\n";
// increase accumulative channel count
acc_ch++;
}
}
// save hint
std::ofstream hintFile("node_dir_hint.txt", std::ios_base::out);
hintFile << nodeDirHint;
hintFile.close();
if (chdir("..") == -1) {
throw std::runtime_error("Could not change to target directory!");
}
}
void MultiStreamToFile::setup(const std::vector<in_addr_t> &nodes, const AcquisitionFormat &acqFmt) {
MultiStreamProcessor::setup(nodes, acqFmt);
// prepare for receiving data
createSampleWriters();
}
bool MultiStreamToFile::input(size_t ch, const std::shared_ptr<Timestamp> &pTime, const void *pData, size_t size) {
if (!MultiStreamProcessor::input(ch, pTime, pData, size)) {
return false;
}
// store new data
mpSampleWriters[ch]->addSamples((const uint8_t *) pData, pTime);
return true;
}