1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/lib/dumper.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

116 lines
2.8 KiB
C++
Raw Permalink Normal View History

/* Dump fields and values in a socket to plot them with villasDump.py.
*
* Author: Manuel Pitz <manuel.pitz@eonerc.rwth-aachen.de>
2022-03-15 09:28:57 -04:00
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
2022-07-04 18:20:03 +02:00
* SPDX-License-Identifier: Apache-2.0
*/
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
2021-02-16 15:49:10 +01:00
#include <cstring>
2021-02-16 14:41:55 +01:00
#include <sstream>
#include <villas/dumper.hpp>
using namespace villas;
using namespace villas::node;
Dumper::Dumper()
2021-02-10 13:06:19 +01:00
: active(false), socketFd(0), socketPath(""), supressRepeatedWarning(true),
2021-02-16 14:15:14 +01:00
warningCounter(0), logger(logging.get("dumper")) {}
2021-02-16 14:41:55 +01:00
Dumper::~Dumper() { closeSocket(); }
bool Dumper::isActive() { return active; }
int Dumper::setActive() {
active = true;
return 1;
}
2021-02-16 13:25:37 +01:00
int Dumper::openSocket() {
socketFd = socket(AF_LOCAL, SOCK_STREAM, 0);
2021-02-10 13:06:19 +01:00
if (socketFd < 0) {
logger->info("Error creating socket {}", socketPath);
2021-02-10 13:06:19 +01:00
return -1;
}
2021-02-16 11:43:53 +01:00
sockaddr_un socketaddrUn;
socketaddrUn.sun_family = AF_UNIX;
strcpy(socketaddrUn.sun_path, socketPath.c_str());
2021-02-16 14:41:55 +01:00
int ret =
connect(socketFd, (struct sockaddr *)&socketaddrUn, sizeof(socketaddrUn));
if (!ret)
return ret;
2021-02-16 14:41:55 +01:00
return 0;
}
2021-02-16 14:41:55 +01:00
int Dumper::closeSocket() {
int ret = close(socketFd);
if (!ret)
return ret;
return 0;
}
int Dumper::setPath(const std::string &socketPathIn) {
socketPath = socketPathIn;
return 1;
}
void Dumper::writeDataBinary(unsigned len, double *yData, double *xData) {
if (warningCounter > 10)
return;
if (yData == nullptr)
return;
unsigned dataLen = len * sizeof(double);
2021-06-29 10:53:05 -04:00
ssize_t bytesWritten = write(socketFd, &dataLen, sizeof(dataLen));
if ((size_t)bytesWritten != sizeof(len)) {
logger->warn("Could not send all content (Len) to socket {}", socketPath);
warningCounter++;
}
static const char buf[] = "d000";
bytesWritten = write(socketFd, buf, sizeof(buf));
if (bytesWritten != sizeof(buf)) {
logger->warn("Could not send all content (Type) to socket {}", socketPath);
warningCounter++;
}
2021-06-29 10:53:05 -04:00
bytesWritten = write(socketFd, yData, dataLen);
if (bytesWritten != (ssize_t)dataLen &&
(!supressRepeatedWarning || warningCounter < 1)) {
logger->warn("Could not send all content (Data) to socket {}", socketPath);
warningCounter++;
}
}
void Dumper::writeDataCSV(unsigned len, double *yData, double *xData) {
2021-06-29 10:53:05 -04:00
for (unsigned i = 0; i < len; i++) {
2021-02-16 14:41:55 +01:00
std::stringstream ss;
ss << yData[i];
2021-06-29 10:53:05 -04:00
if (xData != nullptr)
2021-02-16 14:41:55 +01:00
ss << ";" << xData[i];
ss << std::endl;
2021-02-16 14:41:55 +01:00
auto str = ss.str();
auto bytesWritten = write(socketFd, str.c_str(), str.length());
2021-06-29 10:53:05 -04:00
if ((size_t)bytesWritten != str.length() &&
(!supressRepeatedWarning || warningCounter < 1)) {
logger->warn("Could not send all content to socket {}", socketPath);
2021-02-10 13:06:19 +01:00
warningCounter++;
}
}
}