Subversion Repositories heizung

Rev

Rev 8 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 andreas 1
/*
11 andreas 2
 * (C) Copyright 2011 to 2015, 2024 by Andreas Theofilu <andreas@theosys.at>
5 andreas 3
 * All rights reserved!
4
 */
11 andreas 5
//#define SENSOR_ELV
6
#define SENSOR_ETHERNET
7
 
5 andreas 8
#include <stdio.h>
9
#include <string.h>
10
#include <unistd.h>
11
#include <stdlib.h>
12
#include <math.h>
13
#include <time.h>
14
#include <syslog.h>
15
#include <errno.h>
16
#include <pthread.h>
17
#include <sys/stat.h>
18
#include <sys/types.h>
19
#include <sys/socket.h>
11 andreas 20
#include <netinet/in.h>
21
#include <arpa/inet.h>
22
#include <netdb.h>
5 andreas 23
#include <fcntl.h>
24
#include <sys/ioctl.h>
25
#include "sensor.h"
26
#include "usb_comm.h"
27
#include "heizung.h"
28
 
29
#define CommandOn       0x0003
30
#define CommandOff      0x0000
31
 
32
typedef struct
33
{
11 andreas 34
    int pressure;
35
    int temperature;
36
    time_t tstamp;
5 andreas 37
} mpoint;
38
 
8 andreas 39
int HeatStatus;
40
 
11 andreas 41
static pthread_mutex_t fastmutex_ser = PTHREAD_MUTEX_INITIALIZER;
42
static pthread_mutex_t fastmutex_temp = PTHREAD_MUTEX_INITIALIZER;
43
 
44
#ifdef SENSOR_ELV
45
float GetTemp()
5 andreas 46
{
11 andreas 47
    int fd;
48
    long anz, offset;
49
    double delta_p;
50
    struct stat st;
51
    mpoint mp;
52
    time_t t;
5 andreas 53
 
11 andreas 54
    if ((fd = open(configs.Werte, O_RDONLY)) == -1)
55
    {
56
        syslog(LOG_DAEMON, "Error opening file %s: %s", configs.Werte, strerror(errno));
57
        return 9999.0;
58
    }
5 andreas 59
 
11 andreas 60
    if (fstat(fd, &st) == -1)
61
    {
62
        syslog(LOG_DAEMON, "Error stating file %s: %s", configs.Werte, strerror(errno));
63
        close(fd);
64
        return 9999.0;
65
    }
5 andreas 66
 
11 andreas 67
    if (st.st_size < sizeof(mpoint))
68
    {
69
        syslog(LOG_DAEMON, "No entries in file %s", configs.Werte);
70
        close(fd);
71
        return 9999.0;
72
    }
5 andreas 73
 
11 andreas 74
    anz = st.st_size / sizeof(mpoint);
75
    offset = (anz - 1) * sizeof(mpoint);
5 andreas 76
 
11 andreas 77
    lseek(fd, offset, 0);
78
    read(fd, &mp, sizeof(mpoint));
79
    close(fd);
80
    t = time(NULL);
5 andreas 81
 
11 andreas 82
    /*
83
     * Da unsere Temperaturmessung Teil des Drucksensors ist und der
84
     * Daemon durchaus einmal abstürzen kann, wodurch dann keine
85
     * brauchbare Temperatur mehr existiert, müssen wir hier sicher
86
     * stellen, das sich die Heizung abschaltet und das Programm
87
     * beendet. Das ist der Fall, wenn die letzte Messung mehr als
88
     * 10 Minuten zurück liegt.
89
     */
90
    if (t > (mp.tstamp + 600L))
91
    {
92
        SwitchOff();
93
        syslog(LOG_DAEMON, "Warning: No temperature was meassured in the last 10 minutes!");
94
        return 9999.0;
95
    }
5 andreas 96
 
11 andreas 97
    // Luftdruck
98
    delta_p = pow((1.0 - (6.5 * 256.0) / 288000.0), 5.255);
99
    ActPressure = (float)(mp.pressure / 10.0) / delta_p;
100
    // Temperatur
101
    ActTemperature = ((float)mp.temperature - 10.0) / 10.0;
102
    return ActTemperature;
5 andreas 103
}
11 andreas 104
#elif defined SENSOR_ETHERNET
105
float GetTemp(void)
106
{
107
    struct sockaddr_in sa;
108
    int socket_fd;
5 andreas 109
 
11 andreas 110
    sa.sin_family = AF_INET;
111
    sa.sin_port = htons(configs.TempPort);
112
    sa.sin_addr.s_addr = inet_addr(configs.IP);
113
    memset(sa.sin_zero, '\0', sizeof(sa.sin_zero));
114
    pthread_mutex_lock(&fastmutex_temp);
115
 
116
    socket_fd = socket(PF_INET, SOCK_STREAM, 0);
117
 
118
    if (socket_fd == -1)
119
    {
120
        pthread_mutex_unlock(&fastmutex_temp);
121
        syslog(LOG_DAEMON | LOG_ERR, "Could not create socket: %s", strerror(errno));
122
        return 0.0;
123
    }
124
 
125
    if (connect(socket_fd, (struct sockaddr*)&sa, sizeof(sa)) == -1)
126
    {
127
        pthread_mutex_unlock(&fastmutex_temp);
128
        syslog(LOG_DAEMON | LOG_ERR, "Error connecting!: %s", strerror(errno));
129
        return 0.0;
130
    }
131
 
132
    char message[512];
133
    memset(message, '\0', sizeof(message));
134
    int ret = recv(socket_fd, message, sizeof(message), 0);
135
    close(socket_fd);
136
    pthread_mutex_unlock(&fastmutex_temp);
137
 
138
    if (ret > 0)
139
    {
140
        ActTemperature = atof(message);
141
        ActPressure = 0.0;
142
        syslog(LOG_DAEMON | LOG_DEBUG, "Temperature: %0.1f", ActTemperature);
143
        return ActTemperature;
144
    }
145
    else
146
        return 0.0;
147
}
148
#else
149
#WARNING "sensor.c: No valid sensor defined!"
150
#endif
151
 
152
void SwitchOn()
5 andreas 153
{
11 andreas 154
    int set_bits;
5 andreas 155
 
11 andreas 156
    if (serialDev.switch_fd < 0)
157
    {
158
        syslog(LOG_DAEMON | LOG_ERR, "Es war kein File handle geöffnet! (EIN)");
159
        HeatStatus = 2;
160
        return;
161
    }
5 andreas 162
 
11 andreas 163
    set_bits = CommandOn;
164
    pthread_mutex_lock(&fastmutex_ser);
5 andreas 165
 
11 andreas 166
    if (ioctl(serialDev.switch_fd, TIOCMSET, &set_bits) == -1)
167
    {
168
        pthread_mutex_unlock(&fastmutex_ser);
169
        syslog(LOG_DAEMON | LOG_ERR, "Fehler beim Einschalten!");
170
        HeatStatus = 2;
171
        return;
172
    }
5 andreas 173
 
11 andreas 174
    pthread_mutex_unlock(&fastmutex_ser);
175
    syslog(LOG_DAEMON | LOG_INFO, "Heizung EIN (%.1f)", ActTemperature);
176
    HeatStatus = 1;
5 andreas 177
}
178
 
11 andreas 179
void SwitchOff()
5 andreas 180
{
11 andreas 181
    int set_bits;
5 andreas 182
 
11 andreas 183
    if (serialDev.switch_fd < 0)
184
    {
185
        syslog(LOG_DAEMON | LOG_ERR, "Es war kein File handle geöffnet! (AUS)");
186
        HeatStatus = 2;
187
        return;
188
    }
5 andreas 189
 
11 andreas 190
    set_bits = CommandOff;
191
    pthread_mutex_lock(&fastmutex_ser);
5 andreas 192
 
11 andreas 193
    if (ioctl(serialDev.switch_fd, TIOCMSET, &set_bits) == -1)
194
    {
195
        pthread_mutex_unlock(&fastmutex_ser);
196
        syslog(LOG_DAEMON | LOG_ERR, "Fehler beim Ausschalten!");
197
        HeatStatus = 2;
198
        return;
199
    }
5 andreas 200
 
11 andreas 201
    pthread_mutex_unlock(&fastmutex_ser);
202
    syslog(LOG_DAEMON | LOG_INFO, "Heizung AUS (%.1f)", ActTemperature);
203
    HeatStatus = 0;
5 andreas 204
}
11 andreas 205