Files
neuron/plugins/mqtt/command/command.c
T
2022-06-14 11:28:53 +08:00

153 lines
4.3 KiB
C

/**
* NEURON IIoT System for Industry 4.0
* Copyright (C) 2020-2022 EMQ Technologies Co., Ltd All rights reserved.
*
* 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 3 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
#include <stdio.h>
#include <stdlib.h>
#include "ping.h"
#include "read_write.h"
#include "utils/log.h"
#include "command.h"
// Ping topic
static int ping_response(char **output_str, neu_plugin_t *plugin,
neu_json_mqtt_t *mqtt)
{
*output_str = command_ping(plugin, mqtt);
return 0;
}
// Read tags with group config
static int read_response(mqtt_response_t *response, neu_plugin_t *plugin,
neu_json_mqtt_t *mqtt, char *json_str)
{
UNUSED(response);
neu_json_read_req_t *json = NULL;
int rc = neu_json_decode_read_req(json_str, &json);
if (0 == rc) {
command_rw_read_once_request(plugin, mqtt, json);
neu_json_decode_read_req_free(json);
}
return rc;
}
// Write tags with group config
static int write_response(mqtt_response_t *response, neu_plugin_t *plugin,
neu_json_mqtt_t *mqtt, char *json_str)
{
neu_json_write_req_t *req = NULL;
int rc = neu_json_decode_write_req(json_str, &req);
if (0 == rc) {
uint32_t req_id = neu_plugin_get_event_id(plugin);
int ret = command_rw_write_request(plugin, mqtt, req, req_id);
if (0 < req_id && ret == 0) {
response->context_add(plugin, req_id, mqtt, NULL,
response->topic_pair, false);
}
neu_json_decode_write_req_free(req);
}
return rc;
}
void command_response_handle(mqtt_response_t *response)
{
neu_plugin_t *plugin = (neu_plugin_t *) response->plugin;
if (NULL == response->topic_name || NULL == response->payload) {
return;
}
char *json_str = malloc(response->len + 1);
if (NULL == json_str) {
return;
}
memset(json_str, 0x00, response->len + 1);
memcpy(json_str, response->payload, response->len);
neu_json_mqtt_t *mqtt = NULL;
char * ret_str = NULL;
int rc = neu_json_decode_mqtt_req(json_str, &mqtt);
if (0 != rc) {
zlog_error(neuron, "JSON parsing mqtt failed");
return;
}
switch (response->type) {
case TOPIC_TYPE_PING: {
// Ping
rc = ping_response(&ret_str, plugin, mqtt);
break;
}
case TOPIC_TYPE_READ: {
rc = read_response(response, plugin, mqtt, json_str);
break;
}
case TOPIC_TYPE_WRITE: {
rc = write_response(response, plugin, mqtt, json_str);
break;
}
default:
assert(1 == 0);
break;
}
if (0 != rc) {
neu_json_error_resp_t error = { .error = NEU_ERR_BODY_IS_WRONG };
neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt,
neu_json_encode_mqtt_resp, &ret_str);
}
if (NULL != ret_str) {
response->context_add(plugin, 0, mqtt, ret_str, response->topic_pair,
true);
}
if (NULL != json_str) {
free(json_str);
}
neu_json_decode_mqtt_req_free(mqtt);
}
char *command_read_once_response(neu_reqresp_head_t * head,
neu_resp_read_group_t *data, int format)
{
return command_rw_read_once_response(head, data, format);
return NULL;
}
char *command_read_periodic_response(neu_reqresp_trans_data_t *data, int format)
{
return command_rw_read_periodic_response(data, format);
}
// char *command_write_response(neu_json_mqtt_t *parse_header,
// neu_data_val_t * resp_val)
//{
// return command_rw_write_response(parse_header, resp_val);
//}