Subversion Repositories heating

Rev

Rev 8 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 andreas 1
/*
2
 * Copyright (C) 2015 by Andreas Theofilu. All rights reserved!
3
 *
4
 * All rights reserved. No warranty, explicit or implicit, provided.
5
 *
6
 * NOTICE:  All information contained herein is, and remains
7
 * the property of Andreas Theofilu and his suppliers, if any.
8
 * The intellectual and technical concepts contained
9
 * herein are proprietary to Andreas Theofilu and its suppliers and
10
 * may be covered by European and Foreign Patents, patents in process,
11
 * and are protected by trade secret or copyright law.
12
 * Dissemination of this information or reproduction of this material
13
 * is strictly forbidden unless prior written permission is obtained
14
 * from Andreas Theofilu.
15
 * 
16
 * Author: Andreas Theofilu <andreas@theosys.at>
17
 */
18
#include <iostream>
19
#include <fstream>
20
#include <string>
21
#include <iomanip>
22
#include <cstring>
23
#include <cctype>
24
#include <cstddef>
25
#include <cstdio>
26
#include <cstdlib>
27
#include <ctime>
28
#include <fstream>
29
#include <cerrno>
30
#include <unistd.h>
31
#include <syslog.h>
32
#include <termios.h>
33
#include <sys/stat.h>
34
#include <sys/types.h>
35
#include <fcntl.h>
36
#include "config.h"
37
#include "temperature.h"
38
 
39
using namespace std;
40
 
41
temperature::temperature()
42
{
43
struct termios options;
44
 
45
	initialized = false;
46
	fd_serial = -1;
8 andreas 47
	outPos = 0;
48
	outside = 0.0;
49
	stop = false;
5 andreas 50
 
8 andreas 51
	for (int i = 0; i < MAX_TEMPS; i++)
52
		allTemps[i] = 100.0;
53
 
5 andreas 54
	// Test if serial port is available.
55
	if (access(Configure.serial, R_OK | W_OK))
56
	{
57
		syslog(LOG_DAEMON, "Serial device <%s> is not accessible!", Configure.serial);
58
		return;
59
	}
60
 
61
	// open the serial port for reading and writing
62
	if ((fd_serial = open(Configure.serial, O_RDWR | O_NOCTTY | O_NDELAY)) == -1)
63
	{
64
		syslog(LOG_DAEMON, "Error opening serial device <%s>: %s", Configure.serial, strerror(errno));
65
		return;
66
	}
67
 
68
	tcgetattr(fd_serial, &options);
69
	cfsetispeed(&options, B9600);
70
	cfsetospeed(&options, B9600);
71
	tcsetattr(fd_serial, TCSANOW, &options);
72
	fcntl(fd_serial, F_SETFL, 0);
73
	initialized = true;
74
}
75
 
76
temperature::~temperature()
77
{
78
	if (fd_serial != -1)
79
		close(fd_serial);
80
 
8 andreas 81
	stop = true;
82
	sleep(1);
5 andreas 83
	fd_serial = -1;
84
	initialized = false;
85
}
86
 
8 andreas 87
void temperature::trun()
5 andreas 88
{
8 andreas 89
char buf[512], ch;
90
int r, pos;
5 andreas 91
 
8 andreas 92
	if (!initialized)
93
		return;
5 andreas 94
 
8 andreas 95
	pos = 0;
96
 
97
	while (!stop)
5 andreas 98
	{
8 andreas 99
		if ((r = read(fd_serial, &ch, 1)) <= 0)
100
		{
101
			if (r < 0)
102
				syslog(LOG_DAEMON, "Error reading serial device <%s>: %s", Configure.serial, strerror(errno));
5 andreas 103
 
8 andreas 104
			sleep(1);
105
			continue;
106
		}
5 andreas 107
 
9 andreas 108
		if (pos < (int)(sizeof(buf) - 1))
8 andreas 109
		{
110
			buf[pos] = ch;
111
			buf[pos+1] = 0;
112
			pos++;
5 andreas 113
 
8 andreas 114
			if (ch == 0x0d)
115
			{
9 andreas 116
				debug("Templogger: " + ToString(buf));
117
 
8 andreas 118
				if (strstr(buf, "Templog 2.5") != NULL)
119
				{
120
					ch = '1';
121
					write (fd_serial, &ch, 1);
122
				}
123
				else if (isdigit(buf[0]))
124
				{
125
					string temps = buf;
126
					vector<string> s = split(temps, ' ');
127
 
9 andreas 128
					if (outPos != 0 && outPos <= (int)s.size())
8 andreas 129
						outside = strtod(s.at(outPos - 1).c_str(), NULL);
130
					// save all temperatures
9 andreas 131
					for (int i = 0; i < (int)s.size(); i++)
8 andreas 132
						allTemps[i] = strtod(s.at(i).c_str(), NULL);
133
				}
134
 
135
				pos = 0;
136
			}
137
		}
5 andreas 138
	}
139
 
8 andreas 140
	stop = false;
141
}
142
 
143
double temperature::getTemp (int pos)
144
{
9 andreas 145
	if (pos && pos <= MAX_TEMPS)
8 andreas 146
		return allTemps[pos-1];
147
 
5 andreas 148
	return 100.0;
149
}
150