Subversion Repositories heating

Rev

Rev 8 | Go to most recent revision | 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;

        // 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);
        // TODO: Detecting the menu of the temperature controller and set it to
        //       meassure mode.

        initialized = true;
}

temperature::~temperature()
{
        if (fd_serial != -1)
                close(fd_serial);

        fd_serial = -1;
        initialized = false;
}

double temperature::getTemp (int pos)
{
char hv0[1024];
int r;

        if (!pos || fd_serial == -1 || !initialized)
                return 100.0;

        if ((r = read(fd_serial, &hv0[0], sizeof(hv0))) <= 0)
        {
                if (r < 0)
                        syslog(LOG_DAEMON, "Error reading serial device <%s>: %s", Configure.serial, strerror(errno));

                return 100.0;
        }

        buffer.append(hv0);

        if (buffer.find('\x0d') != string::npos)
        {
                string temps = buffer.substr(buffer.find('\x0d') - 1);
                buffer.erase(0, buffer.find('\x0d'));
                vector<string> s = split(temps, ' ');
                return strtod(s.at(pos - 1).c_str(), NULL);
        }

        return 100.0;
}