ina260-zmq-publisher/src/logger.c

172 lines
4.2 KiB
C
Raw Normal View History

2023-07-17 15:14:37 +02:00
#include <stdio.h>
2023-07-14 20:42:12 +02:00
#include <zmq.h>
#include <assert.h>
2023-07-14 22:03:40 +02:00
#include <libgen.h>
#include <unistd.h>
2023-07-14 22:09:39 +02:00
#include <sys/stat.h>
2023-07-16 14:50:53 +02:00
#include <signal.h>
2023-07-17 20:29:37 +02:00
#include <pthread.h>
2023-07-16 17:19:44 +02:00
#include "utils.h"
2023-07-14 22:03:40 +02:00
2023-07-16 17:41:09 +02:00
#ifndef LOGGERS_DELAY
#define LOGGERS_DELAY 0
2023-07-16 10:28:16 +02:00
#endif
2023-07-15 09:25:30 +02:00
// Global:
2023-07-15 10:11:50 +02:00
char *__client;
2023-07-17 14:55:34 +02:00
char *__ip;
2023-07-17 15:10:35 +02:00
char *__key;
2023-07-17 14:55:34 +02:00
int __port;
2023-07-16 10:46:52 +02:00
char __logdir[STATIC_LEN];
char __regpower[STATIC_LEN];
2023-07-15 10:31:36 +02:00
int __loginterval;
2023-07-16 14:50:53 +02:00
unsigned char __stop=0;
void sighandler(int signo){
if (signo == SIGINT){
printf("Stopping...\n");
__stop=1;
}
}
2023-07-15 09:25:30 +02:00
2023-07-18 08:49:46 +02:00
void *publisher(void *zmq_publisher);
2023-07-17 14:50:13 +02:00
typedef struct queue {
2023-07-17 17:58:20 +02:00
int size;
2023-07-17 19:06:16 +02:00
char issending;
2023-07-17 17:58:20 +02:00
char msg[ZMQ_MSG_SIZE];
2023-07-17 14:50:13 +02:00
} queue;
2023-07-17 17:58:20 +02:00
queue queues[MAX_QUEUES];
2023-07-17 14:50:13 +02:00
2023-07-14 20:42:12 +02:00
int main (int argc, char *argv [])
{
2023-07-17 15:10:35 +02:00
if(argc != 7){
printf("Usage: %s <abslogdir> <client> <loginterval> <ip> <port> <key>",argv[0]);
2023-07-14 22:03:40 +02:00
exit(1);
}
2023-07-15 10:11:50 +02:00
//----- Init global variables
__client=argv[2];
2023-07-15 10:31:36 +02:00
__loginterval=atoi(argv[3]);
2023-07-17 14:55:34 +02:00
__ip=argv[4];
__port=atoi(argv[5]);
2023-07-17 15:10:35 +02:00
__key=argv[6];
2023-07-15 10:11:50 +02:00
// __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);
2023-07-15 10:31:36 +02:00
//----- Sanity checks
2023-07-16 14:50:53 +02:00
signal(SIGINT,sighandler);
2023-07-15 10:31:36 +02:00
mkdirp(__logdir);
2023-07-15 10:34:20 +02:00
if(__loginterval<MIN_INTERVAL){
printf("Log interval is too small (min=%ds)\n",MIN_INTERVAL);
exit(2);
}
2023-07-17 17:58:20 +02:00
if(FILE_EXISTS(__regpower)){
2023-07-15 10:31:36 +02:00
printf("Logger cannot access to %s\n",__regpower);
2023-07-15 10:34:20 +02:00
exit(3);
2023-07-15 10:31:36 +02:00
}
2023-07-17 14:55:34 +02:00
//----- Prepare our context and publisher
2023-07-18 08:49:46 +02:00
void *zmq_context = zmq_ctx_new ();
void *zmq_publisher = zmq_socket (zmq_context, ZMQ_PUB);
2023-07-17 16:41:07 +02:00
char bindto[STATIC_LEN];
sprintf(bindto,"tcp://%s:%d",__ip,__port);
2023-07-18 08:49:46 +02:00
int rc = zmq_connect (zmq_publisher, bindto);
2023-07-17 16:41:07 +02:00
if(rc!=0){
printf("Failed to connect to %s\n",bindto);
exit(1);
}
2023-07-17 14:55:34 +02:00
2023-07-15 10:31:36 +02:00
//----- Start logging
2023-07-17 20:29:37 +02:00
pthread_t zmq_thread;
2023-07-15 10:31:36 +02:00
printf("Logger started [client=%s,interval=%ds]\n",__client,__loginterval);
2023-07-15 10:57:40 +02:00
FILE *regptr,*logptr;
2023-07-16 10:46:52 +02:00
char logfilepath[STATIC_LEN]="";
2023-07-17 17:58:20 +02:00
regptr=fopen("/home/loic/out.txt","r");
2023-07-16 10:46:52 +02:00
char buffer[STATIC_LEN];
2023-07-15 11:48:06 +02:00
int power;
time_t interval;
2023-07-16 09:56:30 +02:00
struct timespec power_ts;
2023-07-17 14:50:13 +02:00
int queue_id=0;
2023-07-17 19:06:16 +02:00
// Init queues
for(int i=0;i<MAX_QUEUES;i++){
queues[queue_id].issending=0;
}
2023-07-18 08:49:46 +02:00
pthread_create(&zmq_thread, NULL, publisher, zmq_publisher);
2023-07-17 19:06:16 +02:00
2023-07-16 14:50:53 +02:00
while(!__stop){
2023-07-15 11:48:06 +02:00
interval=INTERVAL(__loginterval);
2023-07-15 11:43:41 +02:00
// Log current interval
2023-07-17 17:58:20 +02:00
queue_id=(queue_id+1)>=MAX_QUEUES ? 0 : (queue_id+1);
2023-07-17 19:06:16 +02:00
// Busy wait:
while(queues[queue_id].issending){};
2023-07-17 18:56:02 +02:00
// Write msg header:
2023-07-17 17:58:20 +02:00
*queues[queue_id].msg='\0';
sprintf(queues[queue_id].msg,"%s\n%s\n%s\n%ld\n",ZMQ_TOKEN,__key,__client,interval);
queues[queue_id].size=strlen(queues[queue_id].msg);
2023-07-17 18:56:02 +02:00
// Monitor:
2023-07-15 11:43:41 +02:00
while((TIMESTAMP()-interval)<__loginterval){
2023-07-17 14:50:13 +02:00
if(__stop)
break;
2023-07-17 17:58:20 +02:00
// Read power:
fgets(buffer,STATIC_LEN,regptr);
// Get power measurement timestamp:
clock_gettime(CLOCK_REALTIME,&power_ts);
2023-07-17 18:56:02 +02:00
// Write measurement into msg buffer:
2023-07-17 18:37:38 +02:00
char line[MAX_RECORD_LEN];
if((queues[queue_id].size+MAX_RECORD_LEN)>ZMQ_MSG_SIZE){
2023-07-17 17:58:20 +02:00
printf("To many measurements to publish. Please increase ZMQ_MSG_SIZE\n");
2023-07-17 14:50:13 +02:00
} else {
2023-07-17 18:37:38 +02:00
sprintf(queues[queue_id].msg+queues[queue_id].size,"%ld,%ld,%d\n",power_ts.tv_sec,power_ts.tv_nsec,atoi(buffer));
queues[queue_id].size+=strlen(queues[queue_id].msg+queues[queue_id].size);
2023-07-17 14:50:13 +02:00
}
2023-07-17 17:58:20 +02:00
// Reset power register file:
fseek(regptr,0,SEEK_SET);
#if LOGGERS_DELAY > 0
usleep(LOGGERS_DELAY*1000);
#endif
2023-07-17 18:22:36 +02:00
//printf("Tick\n"); fflush(stdout);
2023-07-15 11:43:41 +02:00
}
2023-07-17 19:06:16 +02:00
queues[queue_id].issending=1;
2023-07-15 11:43:41 +02:00
}
2023-07-15 10:57:40 +02:00
fclose(regptr);
2023-07-17 20:29:37 +02:00
pthread_join(zmq_thread, NULL);
2023-07-18 08:49:46 +02:00
zmq_close (zmq_publisher);
zmq_ctx_destroy (zmq_context);
2023-07-14 22:03:40 +02:00
return 0;
}
2023-07-17 14:50:13 +02:00
2023-07-18 08:49:46 +02:00
void *publisher(void *zmq_publisher){
2023-07-17 20:29:37 +02:00
int queue_id=0;
while(!__stop){
if(queues[queue_id].issending){
printf("Publishing...");
2023-07-18 08:49:46 +02:00
zmq_send(zmq_publisher,queues[queue_id].msg,queues[queue_id].size,0);
2023-07-17 20:29:37 +02:00
queues[queue_id].issending=0;
printf("done\n");
} else {
#if LOGGERS_DELAY > 0
usleep(LOGGERS_DELAY*1000);
#endif
continue;
}
queue_id++;
if(queue_id>=MAX_QUEUES)
queue_id=0;
}
pthread_exit(EXIT_SUCCESS);
2023-07-17 14:50:13 +02:00
}