1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
#include <stdio.h>
#include <zmq.h>
#include <assert.h>
#include <libgen.h>
#include <unistd.h>
#include <sys/stat.h>
#include <signal.h>
#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 <abslogdir> <client> <loginterval> <ip> <port> <key>",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<MIN_INTERVAL){
printf("Log interval is too small (min=%ds)\n",MIN_INTERVAL);
exit(2);
}
if(!FILE_EXISTS(__regpower)){
printf("Logger cannot access to %s\n",__regpower);
exit(3);
}
//----- Prepare our context and publisher
void *context = zmq_ctx_new ();
void *publisher = zmq_socket (context, ZMQ_PUB);
char bindto[STATIC_LEN];
sprintf(bindto,"tcp://%s:%d",__ip,__port);
int rc = zmq_connect (publisher, bindto);
if(rc!=0){
printf("Failed to connect to %s\n",bindto);
exit(1);
}
//----- Start logging
printf("Logger started [client=%s,interval=%ds]\n",__client,__loginterval);
FILE *regptr,*logptr;
char logfilepath[STATIC_LEN]="";
regptr=fopen(__regpower,"r");
char buffer[STATIC_LEN];
int power;
time_t interval;
struct timespec power_ts;
int queue_id=0;
while(!__stop){
interval=INTERVAL(__loginterval);
// Log current interval
queue_id=(queue_id+1)>=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<queues[queue_id].length;record++){
*line='\0';
sprintf(line,"%ld,%ld,%d\n",queues[queue_id].records[record].secs,queues[queue_id].records[record].nsecs,queues[queue_id].records[record].power);
int linelength=strlen(line);
if((linelength+msglen)<ZMQ_MSG_SIZE){
strcat(buffer,line);
msglen+=linelength;
} else {
// It buffer is full, we send the message and create another one
zmq_send (publisher, buffer, msglen, 0);
// Build new message header:
sprintf(buffer,"%s\n%s\n%s\n%ld\n",ZMQ_TOKEN,__key,__client,queues[queue_id].interval);
strcat(buffer,line);
msglen=strlen(buffer);
}
}
printf("msglength=%d\n",msglen);
// Finally send the last message (or the only one)
zmq_send (publisher, buffer, msglen, 0);
}
|