Subversion Repositories heating

Rev

Rev 9 | 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
 
12 andreas 92
	debug ("temperature::trun() reached!");
93
 
8 andreas 94
	if (!initialized)
95
		return;
5 andreas 96
 
8 andreas 97
	pos = 0;
12 andreas 98
	debug ("temperature::trun() is working!");
8 andreas 99
 
100
	while (!stop)
5 andreas 101
	{
8 andreas 102
		if ((r = read(fd_serial, &ch, 1)) <= 0)
103
		{
104
			if (r < 0)
105
				syslog(LOG_DAEMON, "Error reading serial device <%s>: %s", Configure.serial, strerror(errno));
5 andreas 106
 
8 andreas 107
			sleep(1);
108
			continue;
109
		}
5 andreas 110
 
9 andreas 111
		if (pos < (int)(sizeof(buf) - 1))
8 andreas 112
		{
113
			buf[pos] = ch;
114
			buf[pos+1] = 0;
115
			pos++;
5 andreas 116
 
8 andreas 117
			if (ch == 0x0d)
118
			{
9 andreas 119
				debug("Templogger: " + ToString(buf));
120
 
8 andreas 121
				if (strstr(buf, "Templog 2.5") != NULL)
122
				{
123
					ch = '1';
124
					write (fd_serial, &ch, 1);
125
				}
126
				else if (isdigit(buf[0]))
127
				{
128
					string temps = buf;
129
					vector<string> s = split(temps, ' ');
130
 
9 andreas 131
					if (outPos != 0 && outPos <= (int)s.size())
8 andreas 132
						outside = strtod(s.at(outPos - 1).c_str(), NULL);
133
					// save all temperatures
9 andreas 134
					for (int i = 0; i < (int)s.size(); i++)
8 andreas 135
						allTemps[i] = strtod(s.at(i).c_str(), NULL);
136
				}
137
 
138
				pos = 0;
139
			}
140
		}
5 andreas 141
	}
142
 
8 andreas 143
	stop = false;
144
}
145
 
146
double temperature::getTemp (int pos)
147
{
9 andreas 148
	if (pos && pos <= MAX_TEMPS)
8 andreas 149
		return allTemps[pos-1];
150
 
5 andreas 151
	return 100.0;
152
}
153