mirror of
https://gitlab.com/manzerbredes/ina260-zmq-publisher.git
synced 2025-04-07 04:16:26 +02:00
Minor changes
This commit is contained in:
parent
44f332f91d
commit
e8417d7397
3 changed files with 54 additions and 23 deletions
|
@ -19,7 +19,7 @@ LOGGERS_DIR=/tmp/ina260_logs/
|
||||||
# LOGGERS_DELAY defines the delay between 2 consecutive
|
# LOGGERS_DELAY defines the delay between 2 consecutive
|
||||||
# ina260 power read performed by the logger
|
# ina260 power read performed by the logger
|
||||||
# Unit is milliseconds
|
# Unit is milliseconds
|
||||||
LOGGERS_DELAY=1
|
LOGGERS_DELAY=1000
|
||||||
# SUBSCRIBER_DIR will contain all the measurments
|
# SUBSCRIBER_DIR will contain all the measurments
|
||||||
# received from the publishers
|
# received from the publishers
|
||||||
SUBSCRIBER_DIR=./data
|
SUBSCRIBER_DIR=./data
|
||||||
|
|
73
src/logger.c
73
src/logger.c
|
@ -24,6 +24,21 @@ void sighandler(int signo){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void publish(int queue_id);
|
||||||
|
|
||||||
|
typedef struct record {
|
||||||
|
time_t secs;
|
||||||
|
long nsecs;
|
||||||
|
uint16_t power;
|
||||||
|
} record;
|
||||||
|
|
||||||
|
typedef struct queue {
|
||||||
|
int length;
|
||||||
|
record records[RECORD_MAX];
|
||||||
|
} queue;
|
||||||
|
|
||||||
|
queue queues[RECORD_QUEUES];
|
||||||
|
|
||||||
int main (int argc, char *argv [])
|
int main (int argc, char *argv [])
|
||||||
{
|
{
|
||||||
if(argc != 4){
|
if(argc != 4){
|
||||||
|
@ -52,7 +67,7 @@ int main (int argc, char *argv [])
|
||||||
printf("Log interval is too small (min=%ds)\n",MIN_INTERVAL);
|
printf("Log interval is too small (min=%ds)\n",MIN_INTERVAL);
|
||||||
exit(2);
|
exit(2);
|
||||||
}
|
}
|
||||||
if(!FILE_EXISTS(__regpower)){
|
if(FILE_EXISTS(__regpower)){
|
||||||
printf("Logger cannot access to %s\n",__regpower);
|
printf("Logger cannot access to %s\n",__regpower);
|
||||||
exit(3);
|
exit(3);
|
||||||
}
|
}
|
||||||
|
@ -62,38 +77,52 @@ int main (int argc, char *argv [])
|
||||||
|
|
||||||
FILE *regptr,*logptr;
|
FILE *regptr,*logptr;
|
||||||
char logfilepath[STATIC_LEN]="";
|
char logfilepath[STATIC_LEN]="";
|
||||||
regptr=fopen(__regpower,"r");
|
regptr=fopen("/home/loic/out.txt","r");
|
||||||
char buffer[STATIC_LEN];
|
char buffer[STATIC_LEN];
|
||||||
int power;
|
int power;
|
||||||
time_t interval;
|
time_t interval;
|
||||||
struct timespec power_ts;
|
struct timespec power_ts;
|
||||||
|
int queue_id=0;
|
||||||
|
|
||||||
while(!__stop){
|
while(!__stop){
|
||||||
interval=INTERVAL(__loginterval);
|
interval=INTERVAL(__loginterval);
|
||||||
*logfilepath='\0'; // Clear previous path
|
|
||||||
sprintf(logfilepath,"%s/%ld",__logdir,interval);
|
|
||||||
logptr=fopen(logfilepath,"w");
|
|
||||||
// Write file header:
|
|
||||||
fprintf(logptr,"timestamp,ns,power\n");
|
|
||||||
// Log current interval
|
// Log current interval
|
||||||
|
queue_id=queue_id>=RECORD_QUEUES ? 0 : queue_id+1;
|
||||||
|
int record=0;
|
||||||
while((TIMESTAMP()-interval)<__loginterval){
|
while((TIMESTAMP()-interval)<__loginterval){
|
||||||
// Read power:
|
if(__stop)
|
||||||
fgets(buffer,STATIC_LEN,regptr);
|
break;
|
||||||
power=atoi(buffer);
|
if(record < RECORD_MAX){
|
||||||
// Get power measurement timestamp:
|
// Read power:
|
||||||
clock_gettime(CLOCK_REALTIME,&power_ts);
|
fgets(buffer,STATIC_LEN,regptr);
|
||||||
// Write measurement in file:
|
// Get power measurement timestamp:
|
||||||
fprintf(logptr,"%d,%ld,%d\n",power_ts.tv_sec,power_ts.tv_nsec,power);
|
clock_gettime(CLOCK_REALTIME,&power_ts);
|
||||||
// Reset power register file:
|
queues[queue_id].records[record].secs=power_ts.tv_sec;
|
||||||
fseek(regptr,0,SEEK_SET);
|
queues[queue_id].records[record].nsecs=power_ts.tv_nsec;
|
||||||
#if LOGGERS_DELAY > 0
|
queues[queue_id].records[record].power=atoi(buffer);
|
||||||
usleep(LOGGERS_DELAY*1000);
|
// Reset power register file:
|
||||||
#endif
|
fseek(regptr,0,SEEK_SET);
|
||||||
//printf("Tick\n"); fflush(stdout);
|
#if LOGGERS_DELAY > 0
|
||||||
|
usleep(LOGGERS_DELAY*1000);
|
||||||
|
#endif
|
||||||
|
printf("Tick\n"); fflush(stdout);
|
||||||
|
record++;
|
||||||
|
} else {
|
||||||
|
printf("Queue overflow, RECORD_MAX must be increase!! n=%d\n",record);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fclose(logptr);
|
queues[queue_id].length=record;
|
||||||
|
publish(queue_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(regptr);
|
fclose(regptr);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void publish(int queue_id){
|
||||||
|
for(int i=0;i<queues[queue_id].length;i++){
|
||||||
|
printf("%d,%ld,%d\n",queues[queue_id].records[i].secs,queues[queue_id].records[i].nsecs,queues[queue_id].records[i].power);
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,6 +10,8 @@
|
||||||
#define INA260_SYSFS "/sys/kernel/ina260"
|
#define INA260_SYSFS "/sys/kernel/ina260"
|
||||||
#define INA260_POWER_REGISTER "registers/power"
|
#define INA260_POWER_REGISTER "registers/power"
|
||||||
#define STATIC_LEN 255
|
#define STATIC_LEN 255
|
||||||
|
#define RECORD_QUEUES 1
|
||||||
|
#define RECORD_MAX 1000
|
||||||
|
|
||||||
#ifndef ZMQ_TOKEN
|
#ifndef ZMQ_TOKEN
|
||||||
#define ZMQ_TOKEN "ina260-zmq-publisher"
|
#define ZMQ_TOKEN "ina260-zmq-publisher"
|
||||||
|
|
Loading…
Add table
Reference in a new issue