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
|
// Weather update client
// Connects SUB socket to tcp://localhost:5556
// Collects weather updates and finds avg temp in zipcode
#include <zmq.h>
#include <assert.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <unistd.h>
#include <sys/stat.h>
#include "utils.h"
#include <time.h>
// Global:
char *__client;
char __logdir[255];
char __regpower[100];
int __loginterval;
void start(char *power_path, char *busid, char *chipaddr);
int main (int argc, char *argv [])
{
if(argc != 4){
printf("Usage: %s <abslogdir> <client> <loginterval>",argv[0]);
exit(1);
}
//----- Init global variables
__client=argv[2];
__loginterval=atoi(argv[3]);
// __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
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);
}
//----- Start logging
printf("Logger started [client=%s,interval=%ds]\n",__client,__loginterval);
FILE *regptr,*logptr;
char logfilepath[255]="";
regptr=fopen("/home/loic/out.txt","r");
*logfilepath='\0';
sprintf(logfilepath,"%s/%ld",__logdir,INTERVAL(TIMESTAMP(),__loginterval));
logptr=fopen(logfilepath,"w");
fprintf(logptr,"timestamp,power\n");
fclose(logptr);
fclose(regptr);
// Extract bus id and ina260 chip address
/*char busid[10];
char chipaddr[10];
char *base=basename(argv[1]);
sscanf(base,"%[^-]-%[^-]",busid,chipaddr);
start("/home/loic/out.txt", busid,chipaddr);*/
// printf("ts=%d, dur=%d, interval=%d\n",15,__logfrequency,INTERVAL(15,__logfrequency));
// printf("ts=%d, dur=%d, interval_last=%d\n",15,__logfrequency,INTERVAL_LAST(15,__logfrequency));
return 0;
}
void start(char *power_path, char *busid, char *chipaddr){
if (!FILE_EXISTS(power_path)){
printf("Could not read %s\n",power_path);
exit(2);
}
char outdir[255];
mkdir(STRINGIFY(LOGGER_DIR),0755);
sprintf(outdir,"%s/%s-%s/",STRINGIFY(LOGGER_DIR), busid,chipaddr);
mkdir(outdir,0755);
char outfile[255];
time_t timestamp;
timestamp = time(NULL);
sprintf(outfile,"%s/%d",outdir,timestamp);
printf("aa %s\n",outfile);
FILE *f;
f=fopen(outfile, "w");
fclose(f);
}
|