/*
This source file is part of Scol
For the latest info, see http://www.scolring.org

Copyright (c) 2010 Stephane Bisaro, aka Iri <iri@irizone.net>

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser 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, or go to
http://www.gnu.org/copyleft/lesser.txt

For others informations, please contact us from http://www.scolring.org/
*/

#ifndef __WEATHER_H__
#define __WEATHER_H__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "../main_science.h"

#include <pthread.h>
#include <unistd.h>

#if defined _WIN32 || defined __WIN32__
#include <winsock2.h>
#elif ((defined linux) || (defined __linux))
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
typedef struct in_addr IN_ADDR;
#else
/* TODO */
#endif





#define URLMAXLEN   128
#define DURLMAXLEN  2*URLMAXLEN
#define MAX_WORD 100
#define DEFAULTINT 0

typedef struct OutputMetar OutputMetar;
struct OutputMetar
{
    char code[6];       /* type du message */
    char oaci[5];       /* code de la station / OACI code */
    char wind_dir[4];   /* direction du vent (en degré) / direction of the wind (in degrees) */
    char wind_speed[3]; /* vitesse du vent / speed of the wind */
    char wind_gust[3];  /* vitesse des rafales de vent / gusting (speed) */
    char wind_unit[4];  /* unité de mesure de la vitesse du vent (noeud, km/h, mps) / wind unit */
    char wind_var_n[4]; /* variabilité dans la direction du vent (en degré) */
    char wind_var_x[4]; /* variabilité dans la direction du vent (en degré) */
    char visibility[5]; /* visibilité dominante / major visibility */
    char weather[50];   /* temps présent (au moment de l'observation) / current weather */
    char nebul_low[7];  /* nébulosité en basse couche / nebulosity (low level) */
    char nebul_med[7];  /* nébulosité à l'étage moyen / nebulosity (medium level) */
    char nebul_high[7]; /* nébulosité à l'étage élevé / nebulosity (high level) */
    char temp[4];       /* température mesurée / temperature */
    char temp_d[4];     /* température du point de rosée / dew point */
    char pressure[5];   /* pression atmosphérique (QNH) / sea level presure */
    char reweather[50]; /* temps récent (durant l'heure précédente) / weather (last hour) */
    char hour[3];       /* heure de l'observation / hour */
    char minute[3];     /* minute de l'observation / minute */
    char day[3];        /* jour de l'observation / day */
};

typedef struct MetarObject MetarObject;
struct MetarObject
{
    char oaci[5];       /* code station */
    char url_prefix[URLMAXLEN];
    char url_sufix[URLMAXLEN];

    char *metar;

    OutputMetar *datas;
};

struct SConnDatas
{
    char *protocol;
    char *domain;
    char *lang;
    char city[64];
    char url[DURLMAXLEN];
    char buffer[512];
    int port;
    #if defined _WIN32 || defined __WIN32__
    char *ahost;
    #else
    IN_ADDR ahost;
    #endif
};
typedef struct SConnDatas *SConnDatas;

/* Objects */
int ObjScienceWeatherMetarType;



/* misc. functions */
void weather_copyValue (char * om, char * word, int len);
void weather_copyNegativeValue (char * om, char * word, int len);
void weather_copyPositiveValue (char * om, char * word, int len);
void weather_addWeather (char* om, const char * word);
int weather_number_int (int i);

/* Sockets */
SConnDatas weather_connection_datas_init (const char *url, const char *station);
void weather_connection_asked (SConnDatas datas);
void weather_connection_end (SOCKET sock);
int weather_connection_init (void);



/* Flags objects */
#define OBJSCIENCE_WEATHER_METAR_SIZE            3
#define OBJSCIENCE_WEATHER_METAR_HANDLE          0
#define OBJSCIENCE_WEATHER_METAR_NAME            1
#define OBJSCIENCE_WEATHER_METAR_KILLED          2


/* Callbacks numbe by type */
#define SCIENCE_METAR_CB    1


/* SCOL Flags api */
#define FROM_NOAA           1

#endif

