#include #include #include #include #include #include #include #include "utils.h" #ifndef LOGGERS_DELAY #define LOGGERS_DELAY 0 #endif // Global: char *__client; char *__ip; char *__key; int __port; char __logdir[STATIC_LEN]; char __regpower[STATIC_LEN]; int __loginterval; unsigned char __stop=0; void sighandler(int signo){ if (signo == SIGINT){ printf("Stopping...\n"); __stop=1; } } void publish(int queue_id, void* publisher); typedef struct record { time_t secs; long nsecs; uint16_t power; } record; typedef struct queue { int length; long interval; record records[RECORD_MAX]; } queue; queue queues[RECORD_QUEUES]; int main (int argc, char *argv []) { if(argc != 7){ printf("Usage: %s ",argv[0]); exit(1); } //----- Init global variables __client=argv[2]; __loginterval=atoi(argv[3]); __ip=argv[4]; __port=atoi(argv[5]); __key=argv[6]; // __logdir: strcat(__logdir,argv[1]); strcat(__logdir,"/"); strcat(__logdir,__client); // __regpower: strcat(__regpower,INA260_SYSFS); strcat(__regpower,"/"); strcat(__regpower,__client); strcat(__regpower,"/"); strcat(__regpower,INA260_POWER_REGISTER); //----- Sanity checks signal(SIGINT,sighandler); mkdirp(__logdir); if(__loginterval=RECORD_QUEUES ? 0 : (queue_id+1); printf("Queue id %d",queue_id); queues[queue_id].interval=interval; int record=0; while((TIMESTAMP()-interval)<__loginterval){ if(__stop) break; if(record < RECORD_MAX){ // Read power: fgets(buffer,STATIC_LEN,regptr); // Get power measurement timestamp: clock_gettime(CLOCK_REALTIME,&power_ts); queues[queue_id].records[record].secs=power_ts.tv_sec; queues[queue_id].records[record].nsecs=power_ts.tv_nsec; queues[queue_id].records[record].power=atoi(buffer); // Reset power register file: fseek(regptr,0,SEEK_SET); #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); } } queues[queue_id].length=record; publish(queue_id,publisher); } fclose(regptr); zmq_close (publisher); zmq_ctx_destroy (context); return 0; } void publish(int queue_id, void* publisher){ printf("Publishing...\n"); // Build message header: char buffer[ZMQ_MSG_SIZE]; sprintf(buffer,"%s\n%s\n%s\n%ld\n",ZMQ_TOKEN,__key,__client,queues[queue_id].interval); int msglen=strlen(buffer); // Put every lines in the buffer and send it char line[STATIC_LEN]; for(int record=0;record