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

122 lines
3.2 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>
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
2021-02-16 14:41:55 +01:00
#include <sstream>
2021-02-16 15:49:10 +01:00
#include <cstring>
2021-02-16 14:41:55 +01:00
#include <villas/dumper.hpp>
using namespace villas;
using namespace villas::node;
2021-02-16 13:25:37 +01:00
Dumper::Dumper(const std::string &socketNameIn) :
socketName(socketNameIn),
2021-02-10 13:06:19 +01:00
supressRepeatedWarning(true),
2021-02-16 14:15:14 +01:00
warningCounter(0),
logger(logging.get("dumper"))
{
2021-02-16 13:25:37 +01:00
openSocket();
}
Dumper::~Dumper() {
2021-02-10 13:06:19 +01:00
closeSocket();
}
2021-02-16 13:25:37 +01:00
int Dumper::openSocket()
2021-02-10 13:06:19 +01:00
{
2021-02-16 13:25:37 +01:00
socketFd = socket(AF_LOCAL, SOCK_STREAM, 0);
2021-02-10 13:06:19 +01:00
if (socketFd < 0) {
2021-02-16 14:15:14 +01:00
logger->info("Error creating socket {}", socketName);
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, socketName.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()
2021-02-10 13:06:19 +01:00
{
2021-02-16 14:41:55 +01:00
int ret = close(socketFd);
if (!ret)
return ret;
return 0;
}
void Dumper::writeDataBinary(unsigned len, double *yData, double *xData){
if (yData == nullptr)
return;
2021-07-01 17:49:59 +02:00
unsigned dataLen = len * sizeof(yData[0]);
2021-06-29 10:53:05 -04:00
ssize_t bytesWritten = write(socketFd, &dataLen, sizeof(dataLen));
if ((size_t) bytesWritten != sizeof(len)) {
2021-07-01 17:49:59 +02:00
logger->warn("Could not send all content (Len) to socket {}", socketName);
warningCounter++;
}
bytesWritten = write(socketFd, "d000", 4);
2021-06-29 10:53:05 -04:00
if (bytesWritten != 4) {
2021-07-01 17:49:59 +02:00
logger->warn("Could not send all content (Type) to socket {}", socketName);
warningCounter++;
}
2021-06-29 10:53:05 -04:00
bytesWritten = write(socketFd, yData, dataLen );
if (bytesWritten != (ssize_t) dataLen && (!supressRepeatedWarning || warningCounter <1 )) {
2021-07-01 17:49:59 +02:00
logger->warn("Could not send all content (Data) to socket {}", socketName);
warningCounter++;
}
}
void Dumper::writeDataCSV(unsigned len, double *yData, double *xData)
2021-02-10 13:06:19 +01:00
{
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)) {
2021-02-16 14:15:14 +01:00
logger->warn("Could not send all content to socket {}", socketName);
2021-02-10 13:06:19 +01:00
warningCounter++;
}
}
}