First usable version

This commit is contained in:
2021-08-26 18:48:27 +02:00
parent 1b64b4de81
commit ca0fdd3428
10 changed files with 172 additions and 1786 deletions

179
main.cpp
View File

@@ -21,19 +21,38 @@
#include <boost/parameter/keyword.hpp>
#include <boost/log/detail/config.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread/thread.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/program_options.hpp>
#include "TimeoutSerial.h"
#include "YmodemFileReceive.h"
#include <exception>
using namespace std;
using namespace boost;
namespace po = boost::program_options;
typedef vector< string > split_vector_type;
enum severity_level
{
normal,
notification,
warning,
error,
critical
};
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)
bool dataHandler(unsigned long blockNo, char* buffer, int size)
{
cout << "Block No. = " << blockNo << endl;
@@ -79,10 +98,11 @@ static void init_log(void)
/* console sink */
auto consoleSink = boost::log::add_console_log(std::clog);
consoleSink->set_formatter(logFmt);
consoleSink->set_filter(severity >= warning);
/* fs sink */
auto fsSink = boost::log::add_file_log(
boost::log::keywords::file_name = "serial-dw_%Y-%m-%d_%H-%M-%S.%N.log",
// boost::log::keywords::file_name = "serial-dw_%Y-%m-%d_%H-%M-%S.%N.log",
boost::log::keywords::file_name = "serial-downloader.log",
boost::log::keywords::rotation_size = 10 * 1024 * 1024,
boost::log::keywords::min_free_space = 30 * 1024 * 1024,
boost::log::keywords::open_mode = std::ios_base::app);
@@ -94,74 +114,132 @@ static void init_log(void)
int main(int argc, char* argv[])
{
string line;
YmodemFileReceive modem;
float temperature = NAN;
float humidity= NAN;
float pressure = NAN;
po::variables_map vm;
std::string port,path,filename;
int speed;
init_log();
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("measure", "get and print values")
("verbose", po::value<string>()->implicit_value("0"), "verbosity level")
("file", po::value<string>(&filename)->default_value("camera.jpg"),"filename of capture filename")
("path", po::value<string>(&path)->default_value("/tmp"),"directory for file of capture")
("capture", "capture photo of camera")
("port", po::value<string>(&port)->default_value("/dev/ttyUSB0"), "port to read from")
("speed", po::value<int>(&speed)->default_value(115200), "speed read from port")
("temperature", "get and print temprature")
("humidity", "get and print humidity")
("pressure", "get and print pressure")
;
try {
BOOST_LOG_TRIVIAL(debug) << "TEST";
TimeoutSerial serial("/dev/ttyUSB0",115200);
serial.setTimeout(posix_time::seconds(10));
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help")) {
cout << desc << "\n";
return 0;
}
} catch(std::exception& e) {
cerr << "error: " << e.what() << "\n";
return 1;
}
catch(...) {
cerr << "Exception of unknown type!\n";
return 1;
}
try {
BOOST_LOG_TRIVIAL(debug) << "Starting Program on " << port ;
TimeoutSerial serial(port,speed);
serial.setTimeout(posix_time::seconds(5));
serial.setDTR(false);
serial.setRTS(false);
boost::this_thread::sleep(posix_time::milliseconds(5000));
line = serial.readStringUntil("\n");
trim(line);
cout << line << endl;
BOOST_LOG_TRIVIAL(debug) << line;
serial.writeString("measure\r\n");
for (;;) {
line = serial.readStringUntil("\n");
trim(line);
if (line == "") continue;
// cout << line << endl;
if (vm.count("measure") || vm.count("temperature") || vm.count("pressure") || vm.count("humidity")) {
serial.writeString("measure\r\n");
split_vector_type SplitVec; // #2: Search for tokens
split( SplitVec, line, is_any_of(" "), token_compress_on ); // SplitVec == { "hello abc","ABC","aBc goodbye" }
for (vector<string>::iterator t=SplitVec.begin(); t!=SplitVec.end(); ++t) {
split_vector_type kv;
split(kv, *t, is_any_of("="), token_compress_on);
// cout << "kv[0]=" << kv[0] << endl;
if (kv[0] == "temperature") temperature = stof(kv[1]);
if (kv[0] == "humidity") humidity = stof(kv[1]);
if (kv[0] == "pressure") pressure = stof(kv[1]);
for (;;) {
line = serial.readStringUntil("\n");
trim(line);
if (line == "") continue;
split_vector_type SplitVec;
split( SplitVec, line, is_any_of(" "), token_compress_on );
for (vector<string>::iterator t=SplitVec.begin(); t!=SplitVec.end(); ++t) {
split_vector_type kv;
split(kv, *t, is_any_of("="), token_compress_on);
if (kv[0] == "temperature") temperature = stof(kv[1]);
if (kv[0] == "humidity") humidity = stof(kv[1]);
if (kv[0] == "pressure") pressure = stof(kv[1]);
}
if (pressure > 0.0) break;
}
if (pressure > 0.0) break;
if (vm.count("measure")) {
cout << "teplota = " << temperature << endl;
cout << "vlhkost = " << humidity << endl;
cout << "tlak = " << pressure << endl;
}
if (vm.count("temperature")) {
cout << temperature << endl;
}
if (vm.count("humidity")) {
cout << humidity << endl;
}
if (vm.count("pressure")) {
cout << pressure << endl;
}
return 0;
}
cout << "teplota = " << temperature << endl;
cout << "vlhkost = " << humidity << endl;
cout << "tlak = " << pressure << endl;
if (vm.count("capture")) {
serial.writeString("capture\r\n");
boost::this_thread::sleep(posix_time::milliseconds(100));
//XModem modem(&serial,dataHandler);
serial.setTimeout(posix_time::milliseconds(500));
serial.writeString("capture\r\n");
boost::this_thread::sleep(posix_time::milliseconds(100));
line = serial.readStringUntil("\r\n");
cout << line << endl;
line = serial.readStringUntil("\r\n");
cout << line << endl;
line = serial.readStringUntil("\r\n");
cout << line << endl;
line = serial.readStringUntil("\r\n");
cout << line << endl;
// line = serial.readStringUntil("\r\n");
// cout << line << endl;
serial.writeString("rb\r\n");
boost::this_thread::sleep(posix_time::milliseconds(100));
serial.readChar(1);
serial.readChar(1);
//modem.receive();
boost::this_thread::sleep(posix_time::milliseconds(100));
serial.writeString("free\r\n");
try {
for (;;) {
line = serial.readStringUntil("\r\n");
cout << line << endl;
}
} catch (...) {
}
serial.writeString("rb\r\n");
boost::this_thread::sleep(posix_time::milliseconds(100));
serial.readChar(1);
serial.readChar(1);
modem.setSerialPort(&serial);
modem.setFilePath(path);
modem.setDefaultFileName(filename);
modem.startReceive();
boost::this_thread::sleep(posix_time::seconds(1));
serial.writeString("free\r\n");
}
serial.close();
@@ -170,5 +248,4 @@ int main(int argc, char* argv[])
cout<<"Error: "<<e.what()<<endl;
return 1;
}
}
}