-
Notifications
You must be signed in to change notification settings - Fork 0
/
dao.cpp
executable file
·166 lines (139 loc) · 4.21 KB
/
dao.cpp
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
#include <stdlib.h>
#include <time.h>
#include <string>
#include <sstream>
#include <mysql.h>
#include <stdio.h>
#include <confuse.h>
#include "dao.h"
static char* DBHOST = NULL;
static char* DBUSER = NULL;
static char* DBPASSWD = NULL;
static char* DBNAME = NULL;
static long DBPORT = 0;
static cfg_t *cfg;
using namespace std;
void readConfig(){
cfg_opt_t opts[] = {
CFG_SIMPLE_STR("dbhost", &DBHOST),
CFG_SIMPLE_STR("dbuser", &DBUSER),
CFG_SIMPLE_STR("dbpasswd", &DBPASSWD),
CFG_SIMPLE_STR("dbname", &DBNAME),
CFG_SIMPLE_INT("dbport", &DBPORT),
CFG_END()
};
cfg = cfg_init(opts, 0);
cfg_parse(cfg, "/etc/smarttemp.conf");
}
void cleanConfig() {
cfg_free(cfg);
}
void finish_with_error(MYSQL *con){
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
throw "Database error occured";
}
void saveTemperatureToDB(const char* sernum, double temp, int status){
MYSQL *con = mysql_init(NULL);
if (con == NULL){
throw "mysql_init() failed\n";
}
if (mysql_real_connect(con, DBHOST, DBUSER, DBPASSWD, DBNAME, (unsigned int)DBPORT, NULL, 0) == NULL){
finish_with_error(con);
}
char date[20];
time_t rawtime;
struct tm * timeinfo;
time (&rawtime);
timeinfo = localtime (&rawtime);
strftime(date,20,"%F %T",timeinfo);
ostringstream osquery;
osquery << "insert into temperature_results(sernum, temp, readdate, user_id, smartplug_state) values ('" << sernum << "', " <<
temp << ", '" << date << "', 3, " << status << ")";
if (mysql_query(con, osquery.str().c_str())){
finish_with_error(con);
}
mysql_close(con);
}
SmartPlugConfig getCurrentSettings(const char* sernum) {
MYSQL *con = mysql_init(NULL);
if (con == NULL){
throw "mysql_init() failed\n";
}
if (mysql_real_connect(con, DBHOST, DBUSER, DBPASSWD, DBNAME, (unsigned int)DBPORT, NULL, 0) == NULL){
finish_with_error(con);
}
ostringstream osquery;
osquery << "select sernum, min_temp, max_temp, start_date, stop_date from temperature_schedule_view s where s.sernum='" << sernum << "'";
if (mysql_query(con, osquery.str().c_str())){
finish_with_error(con);
}
MYSQL_RES *result = mysql_store_result(con);
if (result == NULL){
finish_with_error(con);
}
SmartPlugConfig spconf;
int num_rows = mysql_num_rows(result);
if(num_rows == 0) {
mysql_free_result(result);
mysql_close(con);
throw "Config database (schedule) is misiing for this time!";
}
MYSQL_ROW row;
while ((row = mysql_fetch_row(result))) {
spconf.minTemp = atof(row[1]);
spconf.maxTemp = atof(row[2]);
spconf.startDate = row[3];
spconf.endDate = row[4];
spconf.serialNo = row[0];
}
mysql_free_result(result);
mysql_close(con);
return spconf;
}
GlobalConfig** getGlobalSettings(int *size){
MYSQL *con = mysql_init(NULL);
if (con == NULL){
throw "mysql_init() failed\n";
}
if (mysql_real_connect(con, DBHOST, DBUSER, DBPASSWD, DBNAME, (unsigned int)DBPORT, NULL, 0) == NULL){
finish_with_error(con);
}
ostringstream osquery;
osquery << "select sernum, url, mac, smart_plug_url, smart_plug_user_pass, \
smart_plug_mac, enabled, smart_plug_type from serial_numbers";
if (mysql_query(con, osquery.str().c_str())){
finish_with_error(con);
}
MYSQL_RES *result = mysql_store_result(con);
if (result == NULL){
finish_with_error(con);
}
int num_rows = mysql_num_rows(result);
if(num_rows == 0) {
mysql_free_result(result);
mysql_close(con);
throw "Config database (schedule) is misiing for this time!";
}
*size = num_rows;
GlobalConfig** globalConfig = new GlobalConfig*[num_rows];
for(int i=0; i < num_rows; i++) {
globalConfig[i] = new GlobalConfig();
}
int i=0;
MYSQL_ROW row;
while ((row = mysql_fetch_row(result))) {
globalConfig[i]->serialNumber = row[0];
globalConfig[i]->url = row[1];
globalConfig[i]->mac = row[2];
globalConfig[i]->smartPlugUrl = row[3];
globalConfig[i]->smartPlugUserPass = row[4];
globalConfig[i]->smartPlugMac = row[5];
globalConfig[i]->enabled = atoi( row[6] );
globalConfig[i]->smartPlugType = row[7][0];
i++;
}
mysql_free_result(result);
mysql_close(con);
return globalConfig;
}