osd-contiki/examples/osd/arduino-windsonic/resources/res_wind_speed.c
Marcus Priesch 0859e3442c added
2017-08-08 14:36:44 +02:00

84 lines
2.6 KiB
C

/*
** 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_speed
**
** 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_speed,
"title=\"Windspeed\";rt=\"Windspeed\"",
res_get_handler,
NULL,
NULL,
NULL);
extern int ws_speed_hi ;
extern int ws_speed_lo ;
extern char ws_unit [8];
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, "%d.%02d", ws_speed_hi, ws_speed_lo);
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,
"{'speed':%d.%02d,'unit':'%s'}", ws_speed_hi, ws_speed_lo, ws_unit);
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));
}
}