Moved measuring into separate thread (thus decreasing response to registers change)
This commit is contained in:
@@ -13,6 +13,9 @@ ModbusServiceWorker.cpp
|
||||
SynchronousSocket.h
|
||||
SynchronousSocket.cpp
|
||||
|
||||
TemperatureThread.h
|
||||
TemperatureThread.cpp
|
||||
|
||||
Thread.h
|
||||
Thread.cpp
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
#include "TemperatureThread.h"
|
||||
#include "libdiff.h"
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
TemperatureThread::TemperatureThread(const string &cmd) : cmd(cmd)
|
||||
{
|
||||
}
|
||||
|
||||
string Read(FILE *f)
|
||||
{
|
||||
char buffer[256];
|
||||
string result = "";
|
||||
while (fgets(buffer, sizeof(buffer), f) != nullptr)
|
||||
{
|
||||
result += buffer;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void TemperatureThread::Run()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
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());
|
||||
LockMutex(register_mutex);
|
||||
registers[REGISTER_CUR_TEMP] = (int)(temperature * 100);
|
||||
registers[REGISTER_SECONDARY_TEMP] = (int)(temperature * 100);
|
||||
ReleaseMutex(register_mutex);
|
||||
sleep(2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef TEMPERATURE_THREAD_H
|
||||
#define TEMPERATURE_THREAD_H
|
||||
|
||||
#include "Thread.h"
|
||||
#include "globals.h"
|
||||
|
||||
class TemperatureThread : public ftc::Thread
|
||||
{
|
||||
string cmd;
|
||||
protected:
|
||||
virtual void Run() override;
|
||||
public:
|
||||
TemperatureThread(const string &cmd);
|
||||
};
|
||||
|
||||
#endif
|
||||
+6
-27
@@ -1,6 +1,7 @@
|
||||
#include "configparser.h"
|
||||
#include "globals.h"
|
||||
#include "ModbusServiceListenThread.h"
|
||||
#include "TemperatureThread.h"
|
||||
#include "libdiff.h"
|
||||
|
||||
#include <climits>
|
||||
@@ -15,31 +16,6 @@
|
||||
|
||||
const int BASE_DEFAULT = 0x3BC;
|
||||
|
||||
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);
|
||||
registers[REGISTER_SECONDARY_TEMP] = (int)(temperature * 100);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
try {
|
||||
@@ -89,12 +65,15 @@ int main(int argc, char *argv[])
|
||||
ModbusServiceListenThread *modbus_service = new ModbusServiceListenThread(&mainlog, atoi(cfg.GetParam("ListenPort").c_str()));
|
||||
modbus_service->Start();
|
||||
|
||||
// run temperature update thread
|
||||
TemperatureThread *temperature_thread = new TemperatureThread(measure_command);
|
||||
temperature_thread->Start();
|
||||
|
||||
int io_value = 0;
|
||||
// loop forever updating the temperature values every second
|
||||
// loop forever tweaking relay state according to control
|
||||
while (true)
|
||||
{
|
||||
LockMutex(register_mutex);
|
||||
temperature_update(measure_command);
|
||||
int mode = registers[REGISTER_MODE];
|
||||
if (mode == 0)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user