Files
domo-iot/src/devices/outlet_dio.c
NADAL Jean-Baptiste a0a3c90e3c Add datamodel
2019-12-24 16:59:55 +01:00

141 lines
3.7 KiB
C

/*!
* outlet_dio.c
*
* Copyright (c) 2015-2019, NADAL Jean-Baptiste. All rights reserved.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 24/12/2019
*
*/
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
/*-------------------------------- INCLUDES ---------------------------------*/
#include <strings.h>
#include <string.h>
#include <json.h>
#include "macro.h"
#include "domo.h"
#include "outlet_dio.h"
/*----------------------------- PUBLIC FUNCTIONS ----------------------------*/
/*--------------------------------------------------------------------------*/
outlet_dio_t *outlet_dio_new(void)
{
outlet_dio_t *outlet = NEW_OBJECT(outlet_dio_t);
if (outlet == NULL)
{
return NULL;
}
bzero((void *)outlet, sizeof(outlet_dio_t));
outlet->device = device_new();
return outlet;
}
/*--------------------------------------------------------------------------*/
void outlet_dio_free(outlet_dio_t *outlet)
{
if (outlet == NULL)
return;
if (outlet->device != NULL)
device_free(outlet->device);
if (outlet->zone != NULL)
free(outlet->zone);
free(outlet);
}
/*--------------------------------------------------------------------------*/
int outlet_dio_from_json(outlet_dio_t *outlet, struct json_object *node)
{
int result;
struct json_object *value_node;
result = device_from_json(outlet->device, node);
if (result < 0)
return result;
// zone
if (json_object_object_get_ex(node, k_entry_zone, &value_node))
{
outlet->zone = strdup(json_object_get_string(value_node));
}
// sender
if (json_object_object_get_ex(node, k_entry_sender, &value_node))
{
outlet->sender_id = json_object_get_int(value_node);
}
// switch
if (json_object_object_get_ex(node, k_entry_switch, &value_node))
{
outlet->switch_id = json_object_get_int(value_node);
}
// Sanity checks.
// if ((the_switch == -1) || (the_sender == -1))
// return -1;
return 0;
}
/*--------------------------------------------------------------------------*/
struct json_object *outlet_dio_to_json_object(outlet_dio_t *outlet)
{
struct json_object *root_node;
root_node = json_object_new_object();
outlet_dio_to_json(outlet, root_node);
return root_node;
}
/*--------------------------------------------------------------------------*/
int outlet_dio_to_json(outlet_dio_t *outlet, struct json_object *node)
{
// Get Devices fields
device_to_json(outlet->device, node);
// zone
if (outlet->zone != NULL)
json_object_object_add(node, k_entry_zone, json_object_new_string(outlet->zone));
// sender
json_object_object_add(node, k_entry_sender, json_object_new_int(outlet->sender_id));
// switch
json_object_object_add(node, k_entry_switch, json_object_new_int(outlet->switch_id));
return 0;
}