/*
** Copyright (C) 2017 Marcus Priesch, All rights reserved
** In Prandnern 31, A--2122 Riedenthal, Austria. office@priesch.co.at
** ****************************************************************************
**
**
** 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., 675 Mass Ave, Cambridge, MA 02139, USA.
** ****************************************************************************
**
**++
** Name
**    wind
**
** Purpose
**    provide wind resource
**
**
** Revision Dates
**     8-Aug-2017 (MPR) Creation
**    ��revision-date�����
**--
*/

#include "contiki.h"

#include <string.h>
#include "rest-engine.h"
#include "Arduino.h"

static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);

RESOURCE(res_wind_status,
         "title=\"Wind status\";rt=\"Wind status\"",
         res_get_handler,
         NULL,
         NULL,
         NULL);

extern char  ws_status [32]    ;

static void
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{

  unsigned int accept = -1;
  REST.get_header_accept(request, &accept);

  if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
    REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
    snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%s", ws_status);

    REST.set_response_payload(response, buffer, strlen((char *)buffer));

  } else if(accept == REST.type.APPLICATION_JSON) {
    REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
    snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'status':'%s'}", ws_status);

    REST.set_response_payload(response, buffer, strlen((char *)buffer));

  } else {
    REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
    const char *msg = "Supporting content-types text/plain application/json";
    REST.set_response_payload(response, msg, strlen(msg));
  }
}