Subversion Repositories heizung

Rev

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

Rev Author Line No. Line
5 andreas 1
/*
2
 * (C) Copyright 2011 by Andreas Theofilu <andreas@theosys.at>
3
 * All rights reserved!
4
 */
5
#include <stdio.h>
6
#include <string.h>
7
#include <unistd.h>
8
#include <stdlib.h>
9
#include <math.h>
10
#include <time.h>
11
#include <syslog.h>
12
#include <errno.h>
13
#include <pthread.h>
14
#include <sys/stat.h>
15
#include <sys/types.h>
16
#include <sys/socket.h>
17
#include <fcntl.h>
18
#include <sys/ioctl.h>
19
#include "sensor.h"
20
#include "usb_comm.h"
21
#include "heizung.h"
22
 
23
#define CommandOn       0x0003
24
#define CommandOff      0x0000
25
 
26
typedef struct
27
{
28
        int pressure;
29
        int temperature;
30
        time_t tstamp;
31
} mpoint;
32
 
8 andreas 33
int HeatStatus;
34
 
5 andreas 35
float GetTemp ()
36
{
37
int fd;
38
long anz, offset;
7 andreas 39
double delta_p;
5 andreas 40
struct stat st;
41
mpoint mp;
42
time_t t;
43
 
44
        if ((fd = open (configs.Werte, O_RDONLY)) == -1)
45
        {
46
           syslog(LOG_DAEMON, "Error opening file %s: %s", configs.Werte, strerror(errno));
47
           return 9999.0;
48
        }
49
 
50
        if (fstat (fd, &st) == -1)
51
        {
52
           syslog(LOG_DAEMON, "Error stating file %s: %s", configs.Werte, strerror(errno));
53
           close (fd);
54
           return 9999.0;
55
        }
56
 
57
        if (st.st_size < sizeof (mpoint))
58
        {
59
           syslog(LOG_DAEMON, "No entries in file %s", configs.Werte);
60
           close (fd);
61
           return 9999.0;
62
        }
63
 
64
        anz = st.st_size / sizeof (mpoint);
65
        offset = (anz - 1) * sizeof (mpoint);
66
 
67
        lseek (fd, offset, 0);
68
        read (fd, &mp, sizeof (mpoint));
69
        close (fd);
70
        t = time (NULL);
71
 
72
        /*
73
         * Da unsere Temperaturmessung Teil des Drucksensors ist und der
74
         * Daemon durchaus einmal abstürzen kann, wodurch dann keine
75
         * brauchbare Temperatur mehr existiert, müssen wir hier sicher
76
         * stellen, das sich die Heizung abschaltet und das Programm
77
         * beendet. Das ist der Fall, wenn die letzte Messung mehr als
78
         * 10 Minuten zurück liegt.
79
         */
80
        if (t > (mp.tstamp + 600L))
81
        {
82
           SwitchOff ();
83
           syslog(LOG_DAEMON, "Warning: No temperature was meassured in the last 10 minutes!");
84
           return 9999.0;
85
        }
86
 
87
	// Luftdruck
7 andreas 88
	delta_p = pow((1.0 - (6.5 * 256.0) / 288000.0), 5.255);
89
	ActPressure = (float)(mp.pressure / 10.0) / delta_p;
5 andreas 90
	// Temperatur
8 andreas 91
	ActTemperature = ((float)mp.temperature - 10.0) / 10.0;
92
	return ActTemperature;
5 andreas 93
}
94
 
95
void SwitchOn ()
96
{
97
int set_bits;
98
 
99
        if (serialDev.switch_fd < 0)
100
        {
101
           syslog(LOG_DAEMON, "Es war kein File handle geöffnet! (EIN)");
8 andreas 102
	   HeatStatus = 2;
5 andreas 103
           return;
104
        }
105
 
106
        set_bits = CommandOn;
107
 
108
        if (ioctl(serialDev.switch_fd, TIOCMSET, &set_bits) == -1)
8 andreas 109
	{
5 andreas 110
           syslog(LOG_DAEMON, "Fehler beim Einschalten!");
8 andreas 111
	   HeatStatus = 2;
112
	   return;
113
	}
5 andreas 114
 
8 andreas 115
        syslog(LOG_INFO, "Heizung EIN (%.1f)", ActTemperature);
116
	HeatStatus = 1;
5 andreas 117
}
118
 
119
void SwitchOff ()
120
{
121
int set_bits;
122
 
123
        if (serialDev.switch_fd < 0)
124
        {
125
           syslog(LOG_DAEMON, "Es war kein File handle geöffnet! (AUS)");
8 andreas 126
	   HeatStatus = 2;
5 andreas 127
           return;
128
        }
129
 
130
        set_bits = CommandOff;
131
 
132
        if (ioctl(serialDev.switch_fd, TIOCMSET, &set_bits) == -1)
8 andreas 133
	{
5 andreas 134
           syslog(LOG_DAEMON, "Fehler beim Ausschalten!");
8 andreas 135
	   HeatStatus = 2;
136
	   return;
137
	}
5 andreas 138
 
8 andreas 139
        syslog(LOG_INFO, "Heizung AUS (%.1f)", ActTemperature);
140
	HeatStatus = 0;
5 andreas 141
}