mirror of
https://gitlab.com/manzerbredes/ina260-zmq-publisher.git
synced 2025-04-09 22:56:57 +00:00
83 lines
1.9 KiB
C
83 lines
1.9 KiB
C
// 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;
|
|
|
|
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");
|
|
char buffer[255];
|
|
int power;
|
|
time_t interval;
|
|
|
|
while(1){
|
|
interval=INTERVAL(__loginterval);
|
|
*logfilepath='\0';
|
|
sprintf(logfilepath,"%s/%ld",__logdir,interval);
|
|
logptr=fopen(logfilepath,"w");
|
|
fprintf(logptr,"timestamp,power\n");
|
|
// Log current interval
|
|
while((TIMESTAMP()-interval)<__loginterval){
|
|
fgets(buffer,255,regptr);
|
|
power=atoi(buffer);
|
|
fprintf(logptr,"%ld,%d\n",TIMESTAMP(),power);
|
|
fseek(regptr,0,SEEK_SET);
|
|
sleep(1);
|
|
printf("Tick\n");
|
|
}
|
|
fclose(logptr);
|
|
}
|
|
|
|
fclose(regptr);
|
|
return 0;
|
|
}
|