osd-contiki/examples/osd/arduino-windsonic/resources/res_power_supply.c

83 lines
2.6 KiB
C
Raw Permalink Normal View History

2017-08-08 14:36:44 +02:00
/*
** 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
** power_supply
**
** Purpose
** provide power_supply resource
**
**
** Revision Dates
** 8-Aug-2017 (MPR) Creation
** <EFBFBD><EFBFBD>revision-date<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
**--
*/
#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_power_supply,
"title=\"Power Supply\";rt=\"Voltage\"",
res_get_handler,
NULL,
NULL,
NULL);
extern int supply_voltage_hi, supply_voltage_lo;
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",
supply_voltage_hi, supply_voltage_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,
"{'power_supply':%d.%02d,'unit':'Volts'}", supply_voltage_hi, supply_voltage_lo);
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));
}
}