From 68bb96a301a94ba977d7efd141c7d838ca733038 Mon Sep 17 00:00:00 2001 From: Felix The Cat Date: Tue, 8 Dec 2020 20:33:52 +0300 Subject: [PATCH] Added external measure command --- defrost.cfg.sample | 6 ++++-- defrost.cpp | 46 +++++++++++++++++++++++++++++++++++++++------- liblog.cpp | 10 +++++----- 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/defrost.cfg.sample b/defrost.cfg.sample index 68517d5..4193a0e 100644 --- a/defrost.cfg.sample +++ b/defrost.cfg.sample @@ -1,2 +1,4 @@ -ListenPort = 15000 -LogPath = "/var/log/defrost.log" \ No newline at end of file +ListenPort = 15000 +LogPath = "/var/log/defrost.log" +PortAddress = 956 +MeasureCommand = "echo 6.5" \ No newline at end of file diff --git a/defrost.cpp b/defrost.cpp index 50a2615..f61cd8a 100644 --- a/defrost.cpp +++ b/defrost.cpp @@ -8,19 +8,36 @@ #include #include #include +#include #include #include +#include +const int BASE_DEFAULT = 0x3BC; -void temperature_update() +string Read(FILE *f) +{ + char buffer[256]; + string result = ""; + while (fgets(buffer, sizeof(buffer), f) != nullptr) + { + result += buffer; + } + return result; +} + +void temperature_update(const string &cmd) { // other helper variables float temperature = 1.5; + auto output = popen(cmd.c_str(), "r"); + if (output == nullptr) return; + string value = Read(output); + pclose(output); + temperature = atof(value.c_str()); registers[REGISTER_CUR_TEMP] = (int)(temperature * 100); - float additional_temperature = 2.5; - registers[REGISTER_SECONDARY_TEMP] = (int)(additional_temperature * 100); - + registers[REGISTER_SECONDARY_TEMP] = (int)(temperature * 100); } int main(int argc, char *argv[]) @@ -40,10 +57,25 @@ int main(int argc, char *argv[]) } // load config ConfigParser cfg; - cfg.Load(argv[1] ); + cfg.Load(argv[1]); mainlog.InitLogging(cfg.GetParam("LogPath"), LIBLOGSINGLEFILE, "defrost", true); mainlog.LogString("Defrost service started"); + int port_base = BASE_DEFAULT; + auto port_address = cfg.GetParamSilent("PortAddress"); + if (port_address != "") + { + port_base = atoi(port_address.c_str()); + } + + if (ioperm(port_base, 1, 1)) + { + mainlog.LogString("Access to port denied"); + return EXIT_FAILURE; + } + + string measure_command = cfg.GetParam("MeasureCommand"); + if (pthread_mutex_init(®ister_mutex, NULL) != 0) { mainlog.LogString("Unable to create registers_mutex"); @@ -62,7 +94,7 @@ int main(int argc, char *argv[]) while (true) { LockMutex(register_mutex); - temperature_update(); + temperature_update(measure_command); int mode = registers[REGISTER_MODE]; if (mode == 0) { @@ -74,7 +106,7 @@ int main(int argc, char *argv[]) io_value = registers[REGISTER_CUR_STATE]; } registers[REGISTER_CUR_STATE] = io_value; - //gpio.write(io_value); + outb(io_value, port_base); ReleaseMutex(register_mutex); sleep(1); } diff --git a/liblog.cpp b/liblog.cpp index c32c845..ac6f0ff 100644 --- a/liblog.cpp +++ b/liblog.cpp @@ -49,7 +49,7 @@ void LogFile::InitLogging(string logpath, int method, string prefix, bool reopen file = OpenFileWithRetries(filename.str(), "at"); if (file == NULL) { - throw (string)("Unable to open file for logging"); + throw (string)("Unable to open file \"" + filename.str() + "\"for logging"); } } else @@ -64,8 +64,8 @@ void LogFile::InitLogging(string logpath, int method, string prefix, bool reopen file = OpenFileWithRetries(filename.str(), "at"); lastname = filename.str(); if (file == NULL) - { - throw (string)("Unable to open file for logging"); + { + throw (string)("Unable to open file \"" + filename.str() + "\"for logging"); } } } @@ -105,8 +105,8 @@ void LogFile::LogString(string s, string pfx) filename << logpath << "/" << fileprefix << ".log"; file = OpenFileWithRetries(filename.str(), "at"); if (file == NULL) - { - throw (string)("Unable to open file for logging"); + { + throw (string)("Unable to open file \"" + filename.str() + "\"for logging"); pthread_mutex_unlock(&filemutex); return; }