weather.c 3.83 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
        weather.c

        Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2009
        Copyright Stichting C.A. Muller Radioastronomiestation, 2009

        This program is free software: you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation, either version 3 of the License, or
        (at your option) any later version.

        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

        You should have received a copy of the GNU General Public License
        along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */


#include "weather.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <limits.h>
#include "tcp_connect.h"

struct weather {
	double temperature;
	double pressure;
};

#define WEATHER_HTTP_INTERVAL	60
#define WEATHER_HTTP_URL	"http://www.knmi.nl/actueel/index.html"
#define WEATHER_HTTP_HOST	"www.knmi.nl"

#define WEATHER_HTTP_REQUEST	"GET http://www.knmi.nl/actueel/index.html HTTP/1.0\nUser-Agent: CAMRAS-weather/0.1\n\n"

#define WEATHER_HTTP_LOCATION	"Hoogeveen"

static char weather_http_reply[1024*1024];

static void *weather_http(void *arg)
{
	struct weather *weather = arg;
	do {
		int fd;
		
		fd = tcp_connect(WEATHER_HTTP_HOST, 80);
		
		if (fd >= 0) {
			int ret, i;
			int inputsize = 0;
			char *reply = weather_http_reply;
			char *req = WEATHER_HTTP_REQUEST;

			write(fd, req, strlen(req));
			do {
			 	ret = read(fd, &reply[inputsize], 1);
				inputsize++;
			} while (ret == 1);
			
			close(fd);
			reply[inputsize] = 0;
			for (i = 0; i < inputsize; i++) {
				if (!strncmp(&reply[i],
				    WEATHER_HTTP_LOCATION,
				    strlen(WEATHER_HTTP_LOCATION))) {
					char *tok;
					double temp = -270, pres = 0;
					
					tok = strtok(&reply[i], "<>");
					if (tok) tok = strtok(NULL, "</>");
					if (tok) tok = strtok(NULL, "</>");
					if (tok) tok = strtok(NULL, "</>");
					if (tok) tok = strtok(NULL, "</>");
					if (tok) tok = strtok(NULL, "</>");
					if (tok) tok = strtok(NULL, "</>");
					if (tok) tok = strtok(NULL, "</>");
					if (tok) tok = strtok(NULL, "</&>");
					if (tok) {
						temp = atof(tok);
					}
					if (tok) tok = strtok(NULL, ">");
					if (tok) tok = strtok(NULL, ">");
					if (tok) tok = strtok(NULL, ">");
					if (tok) tok = strtok(NULL, ">");
					if (tok) tok = strtok(NULL, ">");
					if (tok) tok = strtok(NULL, ">");
					if (tok) tok = strtok(NULL, ">");
					if (tok) tok = strtok(NULL, ">");
					if (tok) tok = strtok(NULL, ">");
					if (tok) tok = strtok(NULL, ">");
					if (tok) tok = strtok(NULL, ">&;<");
					if (tok) {
						pres = atof(tok);
					}
					if (pres > 900 &&
					    pres < 1100 &&
					    temp > -40 &&
					    temp < 50) {
						weather->temperature = temp;
						weather->pressure = pres;
					}

					break;
				}
			}
		}
	
		sleep(WEATHER_HTTP_INTERVAL);
	} while (1);
	
	return NULL;
}

struct weather *weather_create(char *location)
{
	struct weather *weather;
	pthread_t thread_id;
	pthread_attr_t attr;
	
	if (strcmp(location, "Dwingeloo")) {
		printf("weather: Only the location 'Dwingeloo' is supported\n");
	}
	
	weather = malloc(sizeof(struct weather));
	if (weather) {
		weather->temperature = 11;
		weather->pressure = 1013;
	}
	
	pthread_attr_init(&attr);
139
#ifndef __FreeBSD__
Jeroen Vreeken's avatar
Jeroen Vreeken committed
140
	pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN * 2);
141
#endif
Jeroen Vreeken's avatar
Jeroen Vreeken committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
	
	pthread_create(&thread_id, &attr, weather_http, weather);
	
	return weather;
}

double weather_get_temperature(struct weather *weather)
{
	return weather->temperature;
}

double weather_get_pressure(struct weather *weather)
{
	return weather->pressure;
}