Rewrite electricity-logger to use an sqlite3 database
This commit is contained in:
25
include/solar/logger/database.hpp
Normal file
25
include/solar/logger/database.hpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <sqlite3.h>
|
||||
#include <string>
|
||||
|
||||
struct Row
|
||||
{
|
||||
time_t epochTime;
|
||||
std::int32_t watt;
|
||||
double kilowattPerHour;
|
||||
};
|
||||
|
||||
class Database {
|
||||
private:
|
||||
sqlite3 * connectionPtr;
|
||||
|
||||
std::string ToSqlInsertStatement(Row const & row) const;
|
||||
std::string ToSqlUpsertStatement(Row const & row) const;
|
||||
|
||||
public:
|
||||
bool Insert(Row & row);
|
||||
|
||||
Database(std::string const & databasePath);
|
||||
~Database();
|
||||
};
|
||||
33
include/solar/logger/zeverdata.hpp
Normal file
33
include/solar/logger/zeverdata.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
#include <cstdio>
|
||||
#include <ctime>
|
||||
#include <curl/curl.h> // tested with libcurl4-openSSH
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
/*
|
||||
This class holds all the data retrieved from the Zeverlution combox /
|
||||
Zeverlution Sxxxx smart inverters / ZeverSolar box.
|
||||
|
||||
For output formatting it uses a timestamp to log the datetime alongside the
|
||||
watts and kilowatts.
|
||||
*/
|
||||
|
||||
struct ZeverData
|
||||
{
|
||||
int number0, number1;
|
||||
std::string registeryID, registeryKey, hardwareVersion, appVersion, wifiVersion;
|
||||
std::string timeValue, dateValue, zeverCloudStatus;
|
||||
int number3;
|
||||
std::string inverterSN;
|
||||
int watt;
|
||||
double kilowattPerHour;
|
||||
std::string OKmsg, ERRORmsg;
|
||||
|
||||
// Parses the data string coming from the zeverlution home.cgi webpage
|
||||
bool ParseString(const std::string & str);
|
||||
|
||||
bool FetchDataFromURL(const std::string & url, const unsigned int timeout);
|
||||
|
||||
ZeverData();
|
||||
};
|
||||
10
include/solar/server/api.hpp
Normal file
10
include/solar/server/api.hpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include <pistache/router.h>
|
||||
|
||||
namespace Api
|
||||
{
|
||||
void GetDay(Pistache::Http::Request const & request, Pistache::Http::ResponseWriter responseWrite);
|
||||
void GetMonth(Pistache::Http::Request const & request, Pistache::Http::ResponseWriter responseWrite);
|
||||
|
||||
void SetupRouting(Pistache::Rest::Router & router);
|
||||
}
|
||||
18
include/solar/server/configuration.hpp
Normal file
18
include/solar/server/configuration.hpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
#include <solar/server/database/database.hpp>
|
||||
#include <string>
|
||||
|
||||
class Configuration {
|
||||
private:
|
||||
Database::Database database;
|
||||
|
||||
Configuration();
|
||||
|
||||
friend int main(int, char **);
|
||||
Configuration & SetupDatabase(std::string const & filePath);
|
||||
|
||||
public:
|
||||
Database::Connection GetDatabaseConnection() const;
|
||||
|
||||
static Configuration & Get();
|
||||
};
|
||||
30
include/solar/server/database/connection.hpp
Normal file
30
include/solar/server/database/connection.hpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
#include <ctime>
|
||||
#include <sqlite3.h>
|
||||
#include <string>
|
||||
#include <util/date.hpp>
|
||||
|
||||
namespace Database
|
||||
{
|
||||
class Connection {
|
||||
private:
|
||||
sqlite3 * const connectionPtr;
|
||||
|
||||
public:
|
||||
Connection(sqlite3 * const databaseConnectionPtr);
|
||||
|
||||
// Date should be in format yyyy-mm-dd
|
||||
// Returns a JSON array
|
||||
std::string GetEntireDay(Util::Date const & date);
|
||||
|
||||
// Dates should be in format yyyy-mm-dd
|
||||
// Returns a JSON array
|
||||
// startDate and endDate are inclusive
|
||||
std::string GetSummarizedPerDayRecords(Util::Date const & startDate, Util::Date const & endDate);
|
||||
|
||||
// Dates should be in format yyyy-mm-dd
|
||||
// Returns a JSON array
|
||||
// startDate and endDate are inclusive
|
||||
std::string GetSummarizedPerMonthRecords(Util::Date const & startDate, Util::Date const & endDate);
|
||||
};
|
||||
}
|
||||
20
include/solar/server/database/database.hpp
Normal file
20
include/solar/server/database/database.hpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
#include <solar/server/database/connection.hpp>
|
||||
#include <sqlite3.h>
|
||||
#include <string>
|
||||
|
||||
namespace Database
|
||||
{
|
||||
class Database {
|
||||
private:
|
||||
sqlite3 * connectionPtr;
|
||||
|
||||
public:
|
||||
bool Connect(std::string const & path);
|
||||
|
||||
Connection GetConnection() const;
|
||||
|
||||
Database();
|
||||
~Database();
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user