-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws.c
189 lines (156 loc) · 4.97 KB
/
ws.c
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Copyright (c) 2009 Sven Bachmann (dev@mcbachmann.de)
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include "ws.h"
/* variables */
struct config_s config;
struct record_s record;
struct version_s version;
/* prototypes */
void ws_print_human(void);
void ws_print_human_sensor(int, int, int);
void ws_print_json(void);
void ws_print_json_sensor(int, int, int, int);
void ws_print_raw(void);
/*
* main
*
* Demonstration of weatherstation polling.
*/
int main(int argc, char **argv)
{
int ret;
/* initialize USB connection */
check(ws_device_open(), goto bye);
/* read stations version */
check(ws_send_repeat(ws_version, &version, "ws_version(version)"), goto bye_close);
/* read configuration */
check(ws_send_repeat(ws_config_read, &config, "ws_config_read(config)"), goto bye_close);
/* read current record */
check(ws_send_repeat(ws_record_current, &record, "ws_record_current(config)"), goto bye_close);
/* present the data */
ws_print_json();
/* all went fine */
ret = 0;
bye_close:
ws_device_close();
bye:
return ret;
}
/*
* ws_print_human
*
* Print data human readable.
*/
void ws_print_human(void)
{
int cnt;
printf("Version: %s\n", VERSION);
printf("Timestamp: %lu\n", ws_timestamp());
printf("Version Station: %i\n", version.nr);
printf("Wind Speed: %.1f km/h\n", ws_conv_wind_speed(&record));
printf("Rain Sensor: %.1f mm\n", ws_conv_rainfall(&config, &record));
printf("Pressure: %.0f hPa\n", ws_conv_pressure(&config, &record));
printf("Forecast: %s\n", ws_conv_forecast(&record));
ws_print_human_sensor(0, record.station.temp, record.station.humidity);
for (cnt = 0; cnt < 9; cnt++) {
/* check if sensor is available */
if ((config.sensor[cnt] & 0xF0) != WS_SENSOR_AVAIL) {
continue;
}
ws_print_human_sensor(cnt + 1, record.sensor[cnt].temp, record.sensor[cnt].humidity);
}
}
/*
* ws_print_human_sensor
*
* Print sensor specific data.
*/
void ws_print_human_sensor(int id, int temp, int hum)
{
printf("Sensor %i\n", id);
printf(" Temperature: %.1f °C\n", ws_conv_temperature(temp));
printf(" Humiditiy: %i %%\n", hum);
}
/*
* ws_print_json
*
* Print data in json format.
*/
void ws_print_json(void)
{
int cnt;
int sensor_avail = 0;
printf("{\n");
printf(" \"version\": \"%s\",\n", VERSION);
printf(" \"timestamp\": \"%lu\",\n", ws_timestamp());
printf(" \"version_station\": \"%i\",\n", version.nr);
printf(" \"wind_speed\": \"%.1f\",\n", ws_conv_wind_speed(&record));
printf(" \"rain_sensor\": \"%.1f\",\n", ws_conv_rainfall(&config, &record));
printf(" \"pressure\": \"%.0f\",\n", ws_conv_pressure(&config, &record));
printf(" \"forecast\": \"%s\",\n", ws_conv_forecast(&record));
printf(" \"sensor\": {\n");
/* check if other sensors are available */
for (cnt = 0; cnt < 9; cnt++) {
if ((config.sensor[cnt] & 0xF0) != WS_SENSOR_AVAIL) {
continue;
}
sensor_avail++;
}
/* print local station data */
ws_print_json_sensor(0, record.station.temp, record.station.humidity, sensor_avail);
/* show available sensors */
for (cnt = 0; cnt < 9; cnt++) {
/* check if sensor is available */
if ((config.sensor[cnt] & 0xF0) != WS_SENSOR_AVAIL) {
continue;
}
ws_print_json_sensor(cnt + 1, record.sensor[cnt].temp, record.sensor[cnt].humidity, --sensor_avail);
}
printf(" }\n");
printf("}\n");
}
/*
* ws_print_json_sensor
*
* Print sensor specific data.
*/
void ws_print_json_sensor(int id, int temp, int hum, int comma)
{
printf(" \"%i\": {\n", id);
printf(" \"temperature\": \"%.1f\",\n", ws_conv_temperature(temp));
printf(" \"humidity\": \"%i\"\n", hum);
printf(" }%s\n", (comma) ? "," : "");
}
/*
* ws_print_raw
*
* Print raw data structures.
*/
void ws_print_raw(void)
{
printf("Version: ");
ws_dump(&version, sizeof(struct version_s));
printf("\n");
printf("Config: ");
ws_dump(&config, sizeof(struct config_s));
printf("\n");
printf("Record: ");
ws_dump(&record, sizeof(struct record_s));
printf("\n");
}