mirror of
https://github.com/emqx/neuron.git
synced 2026-10-17 04:40:42 +08:00
add write function for file plugin
This commit is contained in:
@@ -125,9 +125,10 @@ typedef enum {
|
||||
NEU_ERR_MQTT_SUSPENDED = 4015,
|
||||
NEU_ERR_MQTT_PUBLISH_OVER_LENGTH = 4016,
|
||||
|
||||
NEU_ERR_FILE_TOO_LONG = 4100,
|
||||
NEU_ERR_FILE_OPEN_FAILURE = 4101,
|
||||
NEU_ERR_FILE_READ_FAILURE = 4102,
|
||||
NEU_ERR_FILE_TOO_LONG = 4100,
|
||||
NEU_ERR_FILE_OPEN_FAILURE = 4101,
|
||||
NEU_ERR_FILE_READ_FAILURE = 4102,
|
||||
NEU_ERR_FILE_WRITE_FAILURE = 4103,
|
||||
|
||||
NEU_ERR_PLUGIN_ERROR_START = 10000,
|
||||
NEU_ERR_PLUGIN_ERROR_END = 20000,
|
||||
|
||||
@@ -32,6 +32,8 @@ static int driver_request(neu_plugin_t *plugin, neu_reqresp_head_t *head,
|
||||
|
||||
static int driver_group_timer(neu_plugin_t *plugin, neu_plugin_group_t *group);
|
||||
static int driver_validate_tag(neu_plugin_t *plugin, neu_datatag_t *tag);
|
||||
static int driver_write(neu_plugin_t *plugin, void *req, neu_datatag_t *tag,
|
||||
neu_value_u value);
|
||||
|
||||
static const neu_plugin_intf_funs_t plugin_intf_funs = {
|
||||
.open = driver_open,
|
||||
@@ -45,6 +47,7 @@ static const neu_plugin_intf_funs_t plugin_intf_funs = {
|
||||
|
||||
.driver.validate_tag = driver_validate_tag,
|
||||
.driver.group_timer = driver_group_timer,
|
||||
.driver.write_tag = driver_write,
|
||||
};
|
||||
|
||||
const neu_plugin_module_t neu_plugin_module = {
|
||||
@@ -134,10 +137,6 @@ static int driver_request(neu_plugin_t *plugin, neu_reqresp_head_t *head,
|
||||
|
||||
static int driver_validate_tag(neu_plugin_t *plugin, neu_datatag_t *tag)
|
||||
{
|
||||
if ((tag->attribute & NEU_ATTRIBUTE_WRITE) == NEU_ATTRIBUTE_WRITE) {
|
||||
return NEU_ERR_TAG_ATTRIBUTE_NOT_SUPPORT;
|
||||
}
|
||||
|
||||
plog_debug(plugin, "validate tag success, name:%s, address:%s,type:%d ",
|
||||
tag->name, tag->address, tag->type);
|
||||
|
||||
@@ -148,3 +147,9 @@ static int driver_group_timer(neu_plugin_t *plugin, neu_plugin_group_t *group)
|
||||
{
|
||||
return file_group_timer(plugin, group);
|
||||
}
|
||||
|
||||
static int driver_write(neu_plugin_t *plugin, void *req, neu_datatag_t *tag,
|
||||
neu_value_u value)
|
||||
{
|
||||
return file_write(plugin, req, tag, value);
|
||||
}
|
||||
|
||||
+22
-6
@@ -51,12 +51,6 @@ int file_group_timer(neu_plugin_t *plugin, neu_plugin_group_t *group)
|
||||
char * buf = calloc(1, length);
|
||||
neu_dvalue_t dvalue = { 0 };
|
||||
|
||||
if ((tag->attribute & NEU_ATTRIBUTE_WRITE) == NEU_ATTRIBUTE_WRITE) {
|
||||
dvalue.type = NEU_TYPE_ERROR;
|
||||
dvalue.value.i32 = NEU_ERR_TAG_ATTRIBUTE_NOT_SUPPORT;
|
||||
goto dvalue_result;
|
||||
}
|
||||
|
||||
if (stat(tag->address, &st) != 0) {
|
||||
dvalue.type = NEU_TYPE_ERROR;
|
||||
dvalue.value.i32 = NEU_ERR_FILE_NOT_EXIST;
|
||||
@@ -109,4 +103,26 @@ static void plugin_group_free(neu_plugin_group_t *pgp)
|
||||
free(gd->group);
|
||||
|
||||
free(gd);
|
||||
}
|
||||
|
||||
int file_write(neu_plugin_t *plugin, void *req, neu_datatag_t *tag,
|
||||
neu_value_u value)
|
||||
{
|
||||
FILE *fp = fopen(tag->address, "w");
|
||||
int ret = 0;
|
||||
|
||||
if (0 == fwrite(value.str, sizeof(char), strlen(value.str), fp)) {
|
||||
nlog_error("write file failed:%s", tag->address);
|
||||
ret = NEU_ERR_FILE_WRITE_FAILURE;
|
||||
goto dvalue_result;
|
||||
}
|
||||
|
||||
neu_datatag_string_ltoh(value.str, strlen(value.str));
|
||||
|
||||
dvalue_result:
|
||||
fclose(fp);
|
||||
plugin->common.adapter_callbacks->driver.write_response(
|
||||
plugin->common.adapter, req, ret);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -31,5 +31,7 @@ struct neu_plugin {
|
||||
};
|
||||
|
||||
int file_group_timer(neu_plugin_t *plugin, neu_plugin_group_t *group);
|
||||
int file_write(neu_plugin_t *plugin, void *req, neu_datatag_t *tag,
|
||||
neu_value_u value);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user