Rev 9 | Blame | Compare with Previous | Last modification | View Log | RSS feed
/*
* Copyright (C) 2015 by Andreas Theofilu. All rights reserved!
*
* All rights reserved. No warranty, explicit or implicit, provided.
*
* NOTICE: All information contained herein is, and remains
* the property of Andreas Theofilu and his suppliers, if any.
* The intellectual and technical concepts contained
* herein are proprietary to Andreas Theofilu and its suppliers and
* may be covered by European and Foreign Patents, patents in process,
* and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Andreas Theofilu.
*
* Author: Andreas Theofilu <andreas@theosys.at>
*/
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstring>
#include <cctype>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <cerrno>
#include <unistd.h>
#include <syslog.h>
#include <termios.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include "config.h"
#include "temperature.h"
using namespace std;
temperature::temperature()
{
struct termios options;
initialized = false;
fd_serial = -1;
outPos = 0;
outside = 0.0;
stop = false;
for (int i = 0; i < MAX_TEMPS; i++)
allTemps[i] = 100.0;
// Test if serial port is available.
if (access(Configure.serial, R_OK | W_OK))
{
syslog(LOG_DAEMON, "Serial device <%s> is not accessible!", Configure.serial);
return;
}
// open the serial port for reading and writing
if ((fd_serial = open(Configure.serial, O_RDWR | O_NOCTTY | O_NDELAY)) == -1)
{
syslog(LOG_DAEMON, "Error opening serial device <%s>: %s", Configure.serial, strerror(errno));
return;
}
tcgetattr(fd_serial, &options);
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
tcsetattr(fd_serial, TCSANOW, &options);
fcntl(fd_serial, F_SETFL, 0);
initialized = true;
}
temperature::~temperature()
{
if (fd_serial != -1)
close(fd_serial);
stop = true;
sleep(1);
fd_serial = -1;
initialized = false;
}
void temperature::trun()
{
char buf[512], ch;
int r, pos;
debug ("temperature::trun() reached!");
if (!initialized)
return;
pos = 0;
debug ("temperature::trun() is working!");
while (!stop)
{
if ((r = read(fd_serial, &ch, 1)) <= 0)
{
if (r < 0)
syslog(LOG_DAEMON, "Error reading serial device <%s>: %s", Configure.serial, strerror(errno));
sleep(1);
continue;
}
if (pos < (int)(sizeof(buf) - 1))
{
buf[pos] = ch;
buf[pos+1] = 0;
pos++;
if (ch == 0x0d)
{
debug("Templogger: " + ToString(buf));
if (strstr(buf, "Templog 2.5") != NULL)
{
ch = '1';
write (fd_serial, &ch, 1);
}
else if (isdigit(buf[0]))
{
string temps = buf;
vector<string> s = split(temps, ' ');
if (outPos != 0 && outPos <= (int)s.size())
outside = strtod(s.at(outPos - 1).c_str(), NULL);
// save all temperatures
for (int i = 0; i < (int)s.size(); i++)
allTemps[i] = strtod(s.at(i).c_str(), NULL);
}
pos = 0;
}
}
}
stop = false;
}
double temperature::getTemp (int pos)
{
if (pos && pos <= MAX_TEMPS)
return allTemps[pos-1];
return 100.0;
}