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
+4 -2
View File
@@ -1,2 +1,4 @@
ListenPort = 15000
LogPath = "/var/log/defrost.log"
ListenPort = 15000
LogPath = "/var/log/defrost.log"
PortAddress = 956
MeasureCommand = "echo 6.5"
+39 -7
View File
@@ -8,19 +8,36 @@
#include <sstream>
#include <unistd.h>
#include <sys/file.h>
#include <sys/io.h>
#include <errno.h>
#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
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(&register_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);
}
+5 -5
View File
@@ -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;
}