SensorLogPlot/main.cpp
2024-06-10 19:58:08 +02:00

73 lines
2.1 KiB
C++

#include <vector>
#include <cmath>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <locale>
#include <iomanip>
#include <time.h>
#include "gnuplot-iostream.h"
std::tm getTm(std::string timeString)
{
std::tm t = {};
strptime(timeString.c_str(), "%b %d %H:%M:%S %Y", &t);
return t;
}
int main (int argc, char* argv[])
{
if(argc != 2)
{
std::cout<<"Plsese supply a log file name\n";
return 1;
}
std::ifstream file(argv[1], std::ifstream::in);
if(!file.is_open())
{
std::cout<<"Invalid file name\n";
return 1;
}
std::vector< std::pair <std::time_t, unsigned> > data;
std::string buffer;
while(file.good())
{
std::getline(file, buffer);
if(buffer.find("SENSOR TYPE: 1 ID: 1") != std::string::npos)
{
std::stringstream ssBuffer(buffer);
std::vector< std::string > tokens;
std::string tmp;
while( getline(ssBuffer, tmp, ' ') )
{
tokens.push_back(tmp);
while(ssBuffer.peek() == ' ')
{
ssBuffer.get();
}
}
std::string dateString;
for(unsigned i = 1; i < tokens.size() && i < 5; ++i) dateString += tokens[i] + ' ';
std::tm t = getTm(dateString);
std::time_t time = mktime(&t);
int temperature = std::stoi(tokens.back());
if( temperature < 500 ) data.push_back(std::pair< std::time_t, unsigned > (time, temperature));
}
}
Gnuplot gp("gnuplot");
gp << "set xrange ["<<std::get<0>(data.front())<<':'<<std::get<0>(data.back())<<']'<<"\nset yrange [0:300]\n";
// Data will be sent via a temporary file. These are erased when you call
// gp.clearTmpfiles() or when gp goes out of scope. If you pass a filename
// (e.g. "gp.file1d(pts, 'mydata.dat')"), then the named file will be created
// and won't be deleted (this is useful when creating a script).
gp << "plot" << gp.file1d(data) << "with lines title 'cubic'"<< std::endl;
std::cin.get();
return 0;
}