Added external measure command

This commit is contained in:
ftc
2020-12-08 20:33:52 +03:00
parent bd5a629fde
commit 68bb96a301
3 changed files with 48 additions and 14 deletions
+3 -1
View File
@@ -1,2 +1,4 @@
ListenPort = 15000 ListenPort = 15000
LogPath = "/var/log/defrost.log" LogPath = "/var/log/defrost.log"
PortAddress = 956
MeasureCommand = "echo 6.5"
+38 -6
View File
@@ -8,19 +8,36 @@
#include <sstream> #include <sstream>
#include <unistd.h> #include <unistd.h>
#include <sys/file.h> #include <sys/file.h>
#include <sys/io.h>
#include <errno.h> #include <errno.h>
#include <cmath> #include <cmath>
#include <stdio.h>
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 // other helper variables
float temperature = 1.5; 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); registers[REGISTER_CUR_TEMP] = (int)(temperature * 100);
float additional_temperature = 2.5; registers[REGISTER_SECONDARY_TEMP] = (int)(temperature * 100);
registers[REGISTER_SECONDARY_TEMP] = (int)(additional_temperature * 100);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@@ -44,6 +61,21 @@ int main(int argc, char *argv[])
mainlog.InitLogging(cfg.GetParam("LogPath"), LIBLOGSINGLEFILE, "defrost", true); mainlog.InitLogging(cfg.GetParam("LogPath"), LIBLOGSINGLEFILE, "defrost", true);
mainlog.LogString("Defrost service started"); 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(&register_mutex, NULL) != 0) if (pthread_mutex_init(&register_mutex, NULL) != 0)
{ {
mainlog.LogString("Unable to create registers_mutex"); mainlog.LogString("Unable to create registers_mutex");
@@ -62,7 +94,7 @@ int main(int argc, char *argv[])
while (true) while (true)
{ {
LockMutex(register_mutex); LockMutex(register_mutex);
temperature_update(); temperature_update(measure_command);
int mode = registers[REGISTER_MODE]; int mode = registers[REGISTER_MODE];
if (mode == 0) if (mode == 0)
{ {
@@ -74,7 +106,7 @@ int main(int argc, char *argv[])
io_value = registers[REGISTER_CUR_STATE]; io_value = registers[REGISTER_CUR_STATE];
} }
registers[REGISTER_CUR_STATE] = io_value; registers[REGISTER_CUR_STATE] = io_value;
//gpio.write(io_value); outb(io_value, port_base);
ReleaseMutex(register_mutex); ReleaseMutex(register_mutex);
sleep(1); sleep(1);
} }
+3 -3
View File
@@ -49,7 +49,7 @@ void LogFile::InitLogging(string logpath, int method, string prefix, bool reopen
file = OpenFileWithRetries(filename.str(), "at"); file = OpenFileWithRetries(filename.str(), "at");
if (file == NULL) if (file == NULL)
{ {
throw (string)("Unable to open file for logging"); throw (string)("Unable to open file \"" + filename.str() + "\"for logging");
} }
} }
else else
@@ -65,7 +65,7 @@ void LogFile::InitLogging(string logpath, int method, string prefix, bool reopen
lastname = filename.str(); lastname = filename.str();
if (file == NULL) if (file == NULL)
{ {
throw (string)("Unable to open file for logging"); throw (string)("Unable to open file \"" + filename.str() + "\"for logging");
} }
} }
} }
@@ -106,7 +106,7 @@ void LogFile::LogString(string s, string pfx)
file = OpenFileWithRetries(filename.str(), "at"); file = OpenFileWithRetries(filename.str(), "at");
if (file == NULL) 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); pthread_mutex_unlock(&filemutex);
return; return;
} }