diff --git a/modb_orno3.c b/modb_orno3.c index 36d9242..920a86c 100644 --- a/modb_orno3.c +++ b/modb_orno3.c @@ -348,17 +348,58 @@ int influx_send_orno_batch(s_voltage *v, int v_ok, s_current *i, int i_ok, char batch[1024]; int offset = 0; - /* Line protocol format: measurement,tag=value field=value ... */ - /* New format groups data by phase with U, I, P, W fields */ - offset += snprintf(batch + offset, sizeof(batch) - offset, - "energydb,device=orno,phase=L1 U=%.2f,I=%.2f,P=%.2f,W=%.2f\n" - "energydb,device=orno,phase=L2 U=%.2f,I=%.2f,P=%.2f,W=%.2f\n" - "energydb,device=orno,phase=L3 U=%.2f,I=%.2f,P=%.2f,W=%.2f\n" - "energydb,device=orno,phase=total P=%.2f,W=%.2f\n", - v->U1, i->I1, p->P1, w->W1, - v->U2, i->I2, p->P2, w->W2, - v->U3, i->I3, p->P3, w->W3, - p->P_Tot, w->W_Tot); + /* New format: group by phase with U, I, P, W fields */ + /* For each phase, build field list based on available data */ + + // Phase L1 + if (v_ok || i_ok || p_ok || w_ok) { + char fields[256] = ""; + int foff = 0; + if (v_ok) foff += snprintf(fields + foff, sizeof(fields) - foff, "U=%.2f,", v->U1); + if (i_ok) foff += snprintf(fields + foff, sizeof(fields) - foff, "I=%.2f,", i->I1); + if (p_ok) foff += snprintf(fields + foff, sizeof(fields) - foff, "P=%.2f,", p->P1); + if (w_ok) foff += snprintf(fields + foff, sizeof(fields) - foff, "W=%.2f,", w->W1); + if (foff > 0) fields[foff - 1] = '\0'; // remove trailing comma + offset += snprintf(batch + offset, sizeof(batch) - offset, + "energydb,device=orno,phase=L1 %s\n", fields); + } + + // Phase L2 + if (v_ok || i_ok || p_ok || w_ok) { + char fields[256] = ""; + int foff = 0; + if (v_ok) foff += snprintf(fields + foff, sizeof(fields) - foff, "U=%.2f,", v->U2); + if (i_ok) foff += snprintf(fields + foff, sizeof(fields) - foff, "I=%.2f,", i->I2); + if (p_ok) foff += snprintf(fields + foff, sizeof(fields) - foff, "P=%.2f,", p->P2); + if (w_ok) foff += snprintf(fields + foff, sizeof(fields) - foff, "W=%.2f,", w->W2); + if (foff > 0) fields[foff - 1] = '\0'; + offset += snprintf(batch + offset, sizeof(batch) - offset, + "energydb,device=orno,phase=L2 %s\n", fields); + } + + // Phase L3 + if (v_ok || i_ok || p_ok || w_ok) { + char fields[256] = ""; + int foff = 0; + if (v_ok) foff += snprintf(fields + foff, sizeof(fields) - foff, "U=%.2f,", v->U3); + if (i_ok) foff += snprintf(fields + foff, sizeof(fields) - foff, "I=%.2f,", i->I3); + if (p_ok) foff += snprintf(fields + foff, sizeof(fields) - foff, "P=%.2f,", p->P3); + if (w_ok) foff += snprintf(fields + foff, sizeof(fields) - foff, "W=%.2f,", w->W3); + if (foff > 0) fields[foff - 1] = '\0'; + offset += snprintf(batch + offset, sizeof(batch) - offset, + "energydb,device=orno,phase=L3 %s\n", fields); + } + + // Phase total + if (p_ok || w_ok) { + char fields[256] = ""; + int foff = 0; + if (p_ok) foff += snprintf(fields + foff, sizeof(fields) - foff, "P=%.2f,", p->P_Tot); + if (w_ok) foff += snprintf(fields + foff, sizeof(fields) - foff, "W=%.2f,", w->W_Tot); + if (foff > 0) fields[foff - 1] = '\0'; + offset += snprintf(batch + offset, sizeof(batch) - offset, + "energydb,device=orno,phase=total %s\n", fields); + } if (f_ok) { offset += snprintf(batch + offset, sizeof(batch) - offset,