89 lines
2.3 KiB
C++
89 lines
2.3 KiB
C++
#include "configparser.h"
|
|
#include "globals.h"
|
|
#include "ModbusServiceListenThread.h"
|
|
#include "libdiff.h"
|
|
|
|
#include <climits>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <unistd.h>
|
|
#include <sys/file.h>
|
|
#include <errno.h>
|
|
#include <cmath>
|
|
|
|
|
|
void temperature_update()
|
|
{
|
|
// other helper variables
|
|
float temperature = 1.5;
|
|
|
|
registers[REGISTER_CUR_TEMP] = (int)(temperature * 100);
|
|
float additional_temperature = 2.5;
|
|
registers[REGISTER_SECONDARY_TEMP] = (int)(additional_temperature * 100);
|
|
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
try {
|
|
// ����� lock-file, ����� ���������� ������ ���� ��������� �������
|
|
int lockfile = open("/tmp/defrost.lock", O_RDWR | O_CREAT, 0777);
|
|
if (lockfile == -1)
|
|
{
|
|
cout << "Unable to open file for locking, errno = " << errno << "\n";
|
|
return 1;
|
|
}
|
|
if (flock(lockfile, LOCK_EX | LOCK_NB) == -1)
|
|
{
|
|
cout << "Unable to lock file, errno = " << errno << "\n";
|
|
return 1;
|
|
}
|
|
// load config
|
|
ConfigParser cfg;
|
|
cfg.Load(argv[1] );
|
|
mainlog.InitLogging(cfg.GetParam("LogPath"), LIBLOGSINGLEFILE, "defrost", true);
|
|
mainlog.LogString("Defrost service started");
|
|
|
|
if (pthread_mutex_init(®ister_mutex, NULL) != 0)
|
|
{
|
|
mainlog.LogString("Unable to create registers_mutex");
|
|
return EXIT_FAILURE;
|
|
}
|
|
registers[REGISTER_ON_TEMP] = 100;
|
|
registers[REGISTER_OFF_TEMP] = 500;
|
|
registers[REGISTER_MODE] = 0;
|
|
|
|
// run modbus service listen thread
|
|
ModbusServiceListenThread *modbus_service = new ModbusServiceListenThread(&mainlog, atoi(cfg.GetParam("ListenPort").c_str()));
|
|
modbus_service->Start();
|
|
|
|
int io_value = 0;
|
|
// loop forever updating the temperature values every second
|
|
while (true)
|
|
{
|
|
LockMutex(register_mutex);
|
|
temperature_update();
|
|
int mode = registers[REGISTER_MODE];
|
|
if (mode == 0)
|
|
{
|
|
if (registers[REGISTER_CUR_TEMP] < registers[REGISTER_ON_TEMP]) io_value = 1;
|
|
else if (registers[REGISTER_CUR_TEMP] > registers[REGISTER_OFF_TEMP]) io_value = 0;
|
|
}
|
|
else if (mode == 1)
|
|
{
|
|
io_value = registers[REGISTER_CUR_STATE];
|
|
}
|
|
registers[REGISTER_CUR_STATE] = io_value;
|
|
//gpio.write(io_value);
|
|
ReleaseMutex(register_mutex);
|
|
sleep(1);
|
|
}
|
|
}
|
|
catch (string s)
|
|
{
|
|
cerr << "Exception: " << s << "\n";
|
|
return EXIT_FAILURE;
|
|
}
|
|
return 0;
|
|
}
|