-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_interface.h
122 lines (87 loc) · 3.88 KB
/
db_interface.h
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
#ifndef __DB_INTERFACE_H__
#define __DB_INTERFACE_H__
/* database interface for smatool
Copyright Tony Brown 2011
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
//definition of struct tm
#include <time.h>
/* The opaque row handle object */
typedef void* row_handle;
/* Configure database parameters. May or may not connect to the database at this time */
void db_init(char *server, char *user, char *password, char *database);
/* Release memory used to store results and close connection */
void db_close();
/* called from --initial to setup database schema. Returns 0 if db setup this call, 1 if db already existed */
int db_install_tables( void );
/*
* returns the integer value of the schema defined in the database
*/
int db_get_schema();
/* Get the sunrise and sunset times for the specified day
* Returns 1 on success, 0 on failure ( no matching row )
*/
int db_fetch_almanac(struct tm *date, char * sunrise, char * sunset );
/* inserts the sunrise/set values for today's date */
int db_update_almanac(struct tm *date, const char * sunrise, const char * sunset );
/*
* get the last recorded interval datetime for the specified date
*/
struct tm db_get_last_recorded_interval_datetime(struct tm *date);
//int is_light( ConfType * conf );
/* Check if all data done and past sunset or before sunrise */
//logic in here is a bit hard to understand...
// if current datetime is before sunrise, exit(0)
// otherwise, if there is a recorded value after sunset today, exit(0)
// else exit(1)
// ...effectively - return 1 if there's data to collect
/* insert or update a single row in the database
Return 1 on success, 0 on failure
TODO: current_power and total_energy should be scaled integer values (decimal(10,3) type )
*/
int db_set_interval_value( struct tm *date, char *inverter, long unsigned int serial, long current_power, long total_energy );
/*
* Get the start of day ETotalEnergy value for the specified day
* Returns 0.0f if there is no data.
*/
long db_get_start_of_day_energy_value( struct tm *day );
/*
* Set the upload date/time to NOW on the intervals between from_datetime and to_datetime inclusive
* Return 1 for success, 0 for failure
*/
int db_set_data_posted(struct tm *from_datetime, struct tm *to_datetime );
/*
* Return an opaque row handle pointer that can be iterated over to get unposted values from the specified datetime
* Column ID 0 = interval datetime
* Column ID 1 = ETotalToday
* Rows are in interval datetime order, ascending
* Call db_row_string_data() and db_row_next() to get values and iterate, respectively.
* Call db_row_handle_free() when done.
* Returns NULL if there are no rows returned
*/
row_handle* db_get_unposted_data( struct tm *from_datetime );
/*
* Get a row's column value as a char* value
* TODO: do we need other data types? ( struct tm, int ? )
*/
char* db_row_string_data( row_handle *row, int column_id );
struct tm db_row_datetime_data( row_handle *row, int column_id );
long db_row_int_data( row_handle *row, int column_id );
/*
* move to next row.
* returns 1 on success, 0 on failure (no more rows)
*/
int db_row_next( row_handle *row );
/*
* frees the row_handle
*/
void db_row_handle_free( row_handle *row );
#endif