view wh1080.h @ 2:9da35e705144

Add includes so that it builds without warnings.
author Daniel O'Connor <darius@dons.net.au>
date Tue, 09 Feb 2010 13:48:52 +1030
parents 9dab44dcb331
children b22a888eb975
line wrap: on
line source

/*
 * WH-1080 data input utility.
 *
 * Greg Lehey, 16 November 2009
 *
 * $Id: wh1080.h,v 1.9 2010/02/07 03:41:07 grog Exp $
 */

#include <errno.h>
#include <stdio.h>
#include <math.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <usb.h>
#include <mysql/mysql.h>
#include <sys/mman.h>
#include <pwd.h>

/* Station description */
#include "wh1080_dev.h"

/* Program-dependent stuff. */
#define WH1080_MAX_RETRIES             5
#define WH1080_READ_TIMEOUT            1000
#define WH1080_WRITE_TIMEOUT           1000

#define STAMPSIZE                      32       /* size of time stamp to print out */
#define DATETEXTLENGTH                 48       /* more than maximum size of date text */
#define SECSPERHOUR                    3600     /* seconds per hour */
#define SECSPERDAY                     86400    /* and seconds per day */

/* Conversion factors to archaic units, for Wunderground and friends */
#define	KM_TO_MILES                   0.6213711922
#define MM_TO_IN                      0.039370078
#define C_TO_F(C)                     (C * 1.8 + 32)
#define hPa_TO_inHg                   0.0295299801
/*
 * For some reason, Wunderground doesn't adhere to this constant.  By
 * observation, the constant must be something like this.
 *
 * 29.87 / 1011.4 = 0.029533320
 * 29.88 / 1011.7 = 0.029534446
 * 29.89 / 1012.1 = 0.029532654
 * 29.90 / 1012.4 = 0.029533781
 * Average          0.02953553
 *
 */
#define Wunder_hPa_TO_inHg            0.02953553


/* OK, this one's not so archaic */
#define KELVIN(C)                     (C + 273.15)

struct usb_dev_handle {
  int fd;

  struct usb_bus *bus;
  struct usb_device *device;

  int config;
  int interface;
  int altsetting;

  /* Added by RMT so implementations can store other per-open-device data */
  int *impl_info;
};

/*
 * Local configuration, to be read from configuration file.
 * If this changes, also change the table definition in db/mkdb.
 */
struct config
{
  int version;                                  /* format version, for DB */
#define CONFIG_VERSION 1                        /* current format version */
  float latitude;                               /* In ° */
  float longitude;
  float elevation;                              /* metres */
  float pressure_error;                         /* difference between actual and real pressure, hPa */
  int poll_interval;                            /* time, in seconds, between reading the device */
  char *station_id;                             /* this is the one we use for the database */
  char *address;                                /* Address to display on web pages */
  char *wunderground_station_id;
  char *wunderground_passwd;
  int  wunderground_report_interval;            /* how many seconds between reports to Wunderground */
  char *weatheforyou_station_id;
  char *weatheforyou_passwd;
  int  weatherforyou_report_interval;           /* how many seconds between reports to Weather for you */
  int website_generation_interval;              /* how many seconds between generating PHP header file */
  char *db_host;                                /* database on this host */
  char *db_user;                                /* user ID on host */
  char *db_passwd;                              /* and password */
  char *db;                                     /* database name */
  char *db_table;                               /* table */
  char *php_header;                             /* php header file with weather data */
  char *comparefile;                            /* file to compare local weather stations */
};

extern struct config config;
/* Command line options, defined in db.c */
extern int debug;                               /* -d */
extern int update;                              /* -n turns updating off  */
extern int recover;                             /* -r: recover missed archive data (wh1080 only) */
extern int verbose;                             /* -v */
extern int once;                                /* -1 */

/* MySQL stuff */
extern MYSQL *mysql;
extern MYSQL_RES *mysql_result;
extern MYSQL_ROW mysql_row;
extern char mysql_querytext [1024];             /* build up queries here */


/*
 * Readings in sanitized form.  These are effectively the readings from the
 * station in a format that's easier for us to use.
 */

struct readings
{
  int page;                                     /* page from whence the readings came */
  time_t timestamp;                             /* time of reading */
  int last_save_mins;                           /* last save minutes (?) */
  float inside_temp;                            /* inside temperature */
  int inside_humidity;                          /* humidity in percent */
  float inside_dewpoint;                        /* dewpoint temperature inside */
  float outside_temp;                           /* outside temperature */
  int outside_humidity;                         /* humidity in percent */
  float outside_dewpoint;                       /* dewpoint temperature outside */
  float pressure;                               /* absolute pressure, hPa */
  float pressure_sea_level;                     /* relative sea level pressure, hPa */
  float page1_abs_pressure;                     /* absolute pressure, hPa, from page 1 */
  float page1_rel_pressure;                     /* relative pressure, hPa, from page 1 */
  float wind_speed;                             /* wind speed in km/h */
  float wind_gust;                              /* wind gust speed in km/h */
  float wind_direction;                         /* wind direction, as angle */
#define INVALID_DIRECTION           1024        /* set if we get invalid readings */
  char wind_direction_text [4];                 /* wind direction, in text */
  /*
   * We keep two rain counters.  rain is the current rainfall, incremented on
   * every reading.  This is similar to, but not the same as, what the station
   * does internally.  This field is only changed by the wh1080 program.
   *
   * The other field, 'previous_rain', is set to 'rain' by the report program
   * after reporting the rainfall.  It is only changed by report.  This approach
   * avoids any kind of locking.
   */
  float rain;                                   /* rainfall in mm */
  float previous_rain;                          /* previous rainfall in mm */
};

extern struct readings current_readings;

struct sharefile
{
#define SHARED_MAGIC 0x539
  int magic;                                    /* magic number */
  struct readings current_readings;             /* current readings */
};

extern struct sharefile *sharefile;             /* file to map */
extern int sharefd;                             /* and descriptor */
extern float print_previous_rain;

/* MySQL stuff, in db.c */
extern MYSQL *mysql;
extern MYSQL_RES *mysql_result;
extern MYSQL_ROW mysql_row;
extern char mysql_querytext [1024];             /* build up queries here */
extern char *wind_directions [];                /* in util.c */

/* in db.c */
void db_login ();
int doquery (char *query);
int domyquery (char *query, MYSQL_RES **result);
char *mycopy (char *);
int read_config (int argc, char *argv []);

void print_readings (struct readings *readings);
float dewpoint (float temp, int humidity);
void set_dewpoints (struct readings *readings);
void set_sea_level_pressure (struct readings *readings);
void datetext (time_t, char *, char *);