#include "configparser.h" #include "globals.h" #include "ModbusServiceListenThread.h" #include "TemperatureThread.h" #include "libdiff.h" #include #include #include #include #include #include #include #include #include const int BASE_DEFAULT = 0x3BC; 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"); 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"); 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(); // run temperature update thread TemperatureThread *temperature_thread = new TemperatureThread(measure_command); temperature_thread->Start(); int io_value = 0; // loop forever tweaking relay state according to control while (true) { LockMutex(register_mutex); 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; outb(io_value, port_base); ReleaseMutex(register_mutex); sleep(1); } } catch (string s) { cerr << "Exception: " << s << "\n"; return EXIT_FAILURE; } return 0; }