Skip to content

Commit

Permalink
cid format to tag
Browse files Browse the repository at this point in the history
  • Loading branch information
fengzeroz committed Nov 26, 2024
1 parent 1896b0a commit f9742cb
Show file tree
Hide file tree
Showing 8 changed files with 396 additions and 317 deletions.
1 change: 1 addition & 0 deletions include/neuron/define.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

#define NEU_TAG_META_LENGTH 20
#define NEU_TAG_META_SIZE 32
#define NEU_TAG_FORMAT_LENGTH 16

#define NEU_LOG_LEVEL_DEBUG "debug"
#define NEU_LOG_LEVEL_INFO "info"
Expand Down
5 changes: 5 additions & 0 deletions include/neuron/tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ typedef struct {
char * description;
neu_datatag_addr_option_u option;
uint8_t meta[NEU_TAG_META_LENGTH];
uint8_t format[NEU_TAG_FORMAT_LENGTH];
uint8_t n_format;
} neu_datatag_t;

typedef struct neu_tag_meta {
Expand All @@ -99,6 +101,9 @@ typedef struct neu_tag_meta {

UT_icd *neu_tag_get_icd();

void neu_tag_format_str(const neu_datatag_t *tag, char *buf, int len);
int neu_format_from_str(const char *format_str, uint8_t *formats);

neu_datatag_t *neu_tag_dup(const neu_datatag_t *tag);
void neu_tag_copy(neu_datatag_t *tag, const neu_datatag_t *other);
void neu_tag_fini(neu_datatag_t *tag);
Expand Down
30 changes: 29 additions & 1 deletion include/neuron/utils/cid.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,34 @@ inline static cid_basictype_e decode_basictype(const char *btype)
return ret;
}

inline static const char *fc_to_str(cid_fc_e fc)
{
switch (fc) {
case CO:
return "CO";
case ST:
return "ST";
case MX:
return "MX";
case SP:
return "SP";
case SG:
return "SG";
case SE:
return "SE";
case SV:
return "SV";
case CF:
return "CF";
case DC:
return "DC";
case EX:
return "EX";
default:
return "F_UNKNOWN";
}
}

inline static cid_fc_e decode_fc(const char *fc)
{
cid_fc_e ret = F_UNKNOWN;
Expand Down Expand Up @@ -170,7 +198,7 @@ typedef struct {

cid_fc_e fc;

cid_basictype_e btypes[8]; // for do-[da]
cid_basictype_e btypes[NEU_TAG_FORMAT_LENGTH]; // for do-[da]
int n_btypes;
} cid_fcda_t;

Expand Down
17 changes: 16 additions & 1 deletion persistence/0112_2.11.0_tag.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,26 @@ CREATE TABLE
type INTEGER NOT NULL check (type BETWEEN 0 AND 40),
description TEXT NULL check (length (description) <= 512),
value TEXT,
format TEXT NULL,
UNIQUE (driver_name, group_name, name),
FOREIGN KEY (driver_name, group_name) REFERENCES groups (driver_name, name) ON UPDATE CASCADE ON DELETE CASCADE
);

INSERT INTO tags SELECT * FROM temp_tags;
INSERT INTO tags
SELECT
driver_name,
group_name,
name,
address,
attribute,
precision,
decimal,
bias,
type,
description,
NULL,
""
FROM temp_tags;

DROP TABLE temp_tags;

Expand Down
35 changes: 35 additions & 0 deletions src/base/tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ static void tag_array_copy(void *_dst, const void *_src)
dst->name = strdup(src->name);
dst->description = strdup(src->description);

memcpy(dst->format, src->format, sizeof(src->format));
dst->n_format = src->n_format;
memcpy(dst->meta, src->meta, sizeof(src->meta));
}

Expand All @@ -53,6 +55,39 @@ static void tag_array_free(void *_elt)
free(elt->description);
}

void neu_tag_format_str(const neu_datatag_t *tag, char *buf, int len)
{
int offset = 0;

for (int i = 0; i < tag->n_format; i++) {
if (i == 0) {
offset = snprintf(buf, len, "%d", (int) tag->format[i]);
} else {
offset += snprintf(buf + offset, len - offset, ",%d",
(int) tag->format[i]);
}
}
}

int neu_format_from_str(const char *format_str, uint8_t *formats)
{
int n = 0;

if (format_str == NULL || strlen(format_str) == 0) {
return n;
}

n = sscanf(format_str,
"%hhd,%hhd,%hhd,%hhd,%hhd,%hhd,%hhd,%hhd,%hhd,%hhd,%hhd,%hhd,"
"%hhd,%hhd,%hhd,%hhd",
&formats[0], &formats[1], &formats[2], &formats[3], &formats[4],
&formats[5], &formats[6], &formats[7], &formats[8], &formats[9],
&formats[10], &formats[11], &formats[12], &formats[13],
&formats[14], &formats[15]);

return n;
}

static UT_icd tag_icd = { sizeof(neu_datatag_t), NULL, tag_array_copy,
tag_array_free };

Expand Down
73 changes: 0 additions & 73 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
#include "daemon.h"
#include "version.h"

#include "utils/cid.h"

static bool exit_flag = false;
static neu_manager_t *g_manager = NULL;
zlog_category_t * neuron = NULL;
Expand Down Expand Up @@ -192,77 +190,6 @@ int main(int argc, char *argv[])
neuron = zlog_get_category("neuron");
zlog_level_switch(neuron, default_log_level);

cid_t cid = { 0 };
int ret_c = neu_cid_parse("/tmp/UDT_531A_0011.ICD", &cid);
// int ret_c = neu_cid_parse("/tmp/serverBMS_3.cid", &cid);
printf("neu_cid_parse ret=%d\n", ret_c);
for (int i = 0; i < cid.ied.n_access_points; i++) {
printf("access_points[%d].name=%s\n", i, cid.ied.access_points[i].name);
for (int j = 0; j < cid.ied.access_points[i].n_ldevices; j++) {
printf("-- access_points[%d].ldevices[%d].inst=%s\n", i, j,
cid.ied.access_points[i].ldevices[j].inst);
for (int k = 0; k < cid.ied.access_points[i].ldevices[j].n_lns;
k++) {
printf(
"---- access_points[%d].ldevices[%d].lns[%d].lnprefix=%s\n",
i, j, k,
cid.ied.access_points[i].ldevices[j].lns[k].lnprefix);
for (int l = 0;
l < cid.ied.access_points[i].ldevices[j].lns[k].n_dois;
l++) {
cid_doi_t *doi =
&cid.ied.access_points[i].ldevices[j].lns[k].dois[l];
if (doi->n_ctls > 0) {
for (int a = 0; a < doi->n_ctls; a++) {
printf("write ------ "
"access_points[%d].ldevices[%d].lns[%d]."
"dois[%d]"
".ctls[%d].da_name=%s, fc=%d, btype=%d\n",
i, j, k, l, a, doi->ctls[a].da_name,
doi->ctls[a].fc, doi->ctls[a].btype);
}
}
}
// for (int l = 0;
// l < cid.ied.access_points[i].ldevices[j].lns[k].n_datasets;
// l++) {
// printf("------ "
//"access_points[%d].ldevices[%d].lns[%d].datasets[%d]"
//".name=%s\n",
// i, j, k, l,
// cid.ied.access_points[i]
//.ldevices[j]
//.lns[k]
//.datasets[l]
//.name);
// for (int a = 0; a < cid.ied.access_points[i]
//.ldevices[j]
//.lns[k]
//.datasets[l]
//.n_fcda;
// a++) {
// cid_fcda_t *fcda = &cid.ied.access_points[i]
//.ldevices[j]
//.lns[k]
//.datasets[l]
//.fcdas[a];
// if (fcda->n_btypes == 0) {
// printf(
//"nnn -------- "
//"access_points[%d].ldevices[%d].lns[%d]"
//".datasets[%d].fcdas[%d].name=%s, n type: %d\n",
// i, j, k, l, a, fcda->do_name, fcda->n_btypes);
//}
//}
//}
}
}
}
neu_cid_free(&cid);

zlog_fini();
return 0;

if (neuron_already_running()) {
if (args.stop) {
rv = neuron_stop();
Expand Down
47 changes: 34 additions & 13 deletions src/persist/sqlite.c
Original file line number Diff line number Diff line change
Expand Up @@ -572,15 +572,20 @@ int neu_sqlite_persister_store_tag(neu_persister_t * self,
const char * group_name,
const neu_datatag_t *tag)
{
int rv =
execute_sql(((neu_sqlite_persister_t *) self)->db,
"INSERT INTO tags ("
" driver_name, group_name, name, address, attribute,"
" precision, type, decimal, bias, description, value"
") VALUES (%Q, %Q, %Q, %Q, %i, %i, %i, %lf, %lf, %Q, %Q)",
driver_name, group_name, tag->name, tag->address,
tag->attribute, tag->precision, tag->type, tag->decimal,
tag->bias, tag->description, "");
char format_buf[128] = { 0 };
if (tag->n_format > 0) {
neu_tag_format_str(tag, format_buf, sizeof(format_buf));
}

int rv = execute_sql(
((neu_sqlite_persister_t *) self)->db,
"INSERT INTO tags ("
" driver_name, group_name, name, address, attribute,"
" precision, type, decimal, bias, description, value, format"
") VALUES (%Q, %Q, %Q, %Q, %i, %i, %i, %lf, %lf, %Q, %Q, %Q)",
driver_name, group_name, tag->name, tag->address, tag->attribute,
tag->precision, tag->type, tag->decimal, tag->bias, tag->description,
"", format_buf);

return rv;
}
Expand All @@ -589,7 +594,12 @@ static int put_tags(sqlite3 *db, const char *query, sqlite3_stmt *stmt,
const neu_datatag_t *tags, size_t n)
{
for (size_t i = 0; i < n; ++i) {
const neu_datatag_t *tag = &tags[i];
const neu_datatag_t *tag = &tags[i];
char format_buf[128] = { 0 };

if (tag->n_format > 0) {
neu_tag_format_str(tag, format_buf, sizeof(format_buf));
}

sqlite3_reset(stmt);

Expand Down Expand Up @@ -648,6 +658,12 @@ static int put_tags(sqlite3 *db, const char *query, sqlite3_stmt *stmt,
return -1;
}

if (SQLITE_OK != sqlite3_bind_text(stmt, 12, format_buf, -1, NULL)) {
nlog_error("bind `%s` with format=`%s` fail: %s", query, format_buf,
sqlite3_errmsg(db));
return -1;
}

if (SQLITE_DONE != sqlite3_step(stmt)) {
nlog_error("sqlite3_step fail: %s", sqlite3_errmsg(db));
return -1;
Expand All @@ -668,8 +684,8 @@ int neu_sqlite_persister_store_tags(neu_persister_t * self,
const char * query =
"INSERT INTO tags ("
" driver_name, group_name, name, address, attribute,"
" precision, type, decimal, bias, description, value"
") VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)";
" precision, type, decimal, bias, description, value, format"
") VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12)";

if (SQLITE_OK != sqlite3_exec(persister->db, "BEGIN", NULL, NULL, NULL)) {
nlog_error("begin transaction fail: %s", sqlite3_errmsg(persister->db));
Expand Down Expand Up @@ -719,6 +735,8 @@ static int collect_tag_info(sqlite3_stmt *stmt, UT_array **tags)
{
int step = sqlite3_step(stmt);
while (SQLITE_ROW == step) {
const char *format = (const char *) sqlite3_column_text(stmt, 9);

neu_datatag_t tag = {
.name = (char *) sqlite3_column_text(stmt, 0),
.address = (char *) sqlite3_column_text(stmt, 1),
Expand All @@ -729,6 +747,9 @@ static int collect_tag_info(sqlite3_stmt *stmt, UT_array **tags)
.bias = sqlite3_column_double(stmt, 6),
.description = (char *) sqlite3_column_text(stmt, 7),
};

tag.n_format = neu_format_from_str(format, tag.format);

utarray_push_back(*tags, &tag);

step = sqlite3_step(stmt);
Expand All @@ -749,7 +770,7 @@ int neu_sqlite_persister_load_tags(neu_persister_t *self,

sqlite3_stmt *stmt = NULL;
const char * query = "SELECT name, address, attribute, precision, type, "
"decimal, bias, description, value "
"decimal, bias, description, value, format "
"FROM tags WHERE driver_name=? AND group_name=? "
"ORDER BY rowid ASC";

Expand Down
Loading

0 comments on commit f9742cb

Please sign in to comment.