-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathomnikcsv.c
More file actions
73 lines (63 loc) · 1.61 KB
/
omnikcsv.c
File metadata and controls
73 lines (63 loc) · 1.61 KB
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
/*
** omnikcsv.c
**
** Prints the statistics to a CSV file
**
** Returns: None
**
** Author: Beach
** V1.0 may,24 2013
*/
#include <stdio.h>
#include <stdlib.h>
#include <ifaddrs.h>
#include <string.h>
#include <math.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include "omnikstats.h"
void omnikcsv(void) {
int i;
char date[10];
char now[10];
struct stat buffer;
char filename[200];
FILE *pFile;
// check if file exists, otherwise create it
strcpy(filename, getdatetime(now, 2));
strcat(filename, "/");
strcat(filename, getdatetime(now,2));
strcat(filename, "data.csv");
printf("filename = %s\n", filename);
strcpy(stats.filename, filename);
if (stat(filename, &buffer) != 0) {
printf("%s does not exist. Creating file....\n", filename);
if ((i=mkdir(getdatetime(now, 2),S_IRWXU|S_IRWXG|S_IROTH)) != 0) {
printf("Could not create directory %s, %s\n", getdatetime(now,2), strerror(errno));
exit(1);
}
if ((pFile = fopen(filename, "w+")) == NULL) {
printf("Could not create file %s\n", filename);
exit(1);
}
fprintf(pFile, "Date,Time,Powertoday, PowerNow,Temperature,Voltage(DC),Current(DC), Voltage(AC),Current(AC),Frequency,TotalPower,Hours\n");
fclose(pFile);
}
pFile = fopen(filename, "a");
fprintf(pFile, "%s,%s,%.0f,%.1f,%.1f,%.1f,%.1f,%.1f,%.2f,%.2f,%.1f,%.0f,",
getdatetime(date, 0),
getdatetime(now, 1),
stats.PowerToday,
stats.PVPower[0],
stats.temperature,
stats.PVVoltageDC[0],
stats.IVCurrentDC[0],
stats.PVVoltageAC[0],
stats.IVCurrentAC[0],
stats.frequency,
stats.TotalPower,
stats.TotalHours);
fclose(pFile);
}