Rewrite electricity-logger to use an sqlite3 database
This commit is contained in:
15
include/electricity/logger/database.hpp
Normal file
15
include/electricity/logger/database.hpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
#include <electricity/logger/dsmr.hpp>
|
||||
#include <sqlite3.h>
|
||||
#include <string>
|
||||
|
||||
class Database {
|
||||
private:
|
||||
sqlite3 * connectionPtr;
|
||||
|
||||
public:
|
||||
void Insert(DSMR::Data const & data, time_t const time);
|
||||
|
||||
Database(std::string const & databaseFilePath);
|
||||
~Database();
|
||||
};
|
||||
73
include/electricity/logger/dsmr.hpp
Normal file
73
include/electricity/logger/dsmr.hpp
Normal file
@@ -0,0 +1,73 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
//#include <iostream> // debug
|
||||
|
||||
namespace DSMR
|
||||
{
|
||||
enum class LineTag
|
||||
{
|
||||
Unknown = -1,
|
||||
DSMRversion,
|
||||
DateTimeStamp,
|
||||
SerialNo,
|
||||
TotalPowerConsumedTariff1,
|
||||
TotalPowerConsumedTariff2,
|
||||
TotalReturnedPowerTariff1,
|
||||
TotalReturnedPowerTariff2,
|
||||
CurrentTarif,
|
||||
CurrentPowerConsumption,
|
||||
CurrentPowerReturn,
|
||||
PowerFailureCount,
|
||||
PowerFailureLongCount,
|
||||
PowerFailureEventLog,
|
||||
VoltageL1SagCount,
|
||||
VoltageL2SagCount,
|
||||
VoltageL3SagCount,
|
||||
VoltageL1SwellCount,
|
||||
VoltageL2SwellCount,
|
||||
VoltageL3SwellCount,
|
||||
TextMessageMaxChar,
|
||||
InstantL1VoltageResolution,
|
||||
InstantL2VoltageResolution,
|
||||
InstantL3VoltageResolution,
|
||||
InstantL1CurrentResolution,
|
||||
InstantL2CurrentResolution,
|
||||
InstantL3CurrentResolution,
|
||||
InstantL1ActivePowerResolution,
|
||||
InstantL2ActivePowerResolution,
|
||||
InstantL3ActivePowerResolution,
|
||||
InstantL1ActivePowerResolutionA,
|
||||
InstantL2ActivePowerResolutionA,
|
||||
InstantL3ActivePowerResolutionA,
|
||||
DeviceType,
|
||||
GasDeviceIdentifier,
|
||||
GasTotalConsumptionLog
|
||||
};
|
||||
|
||||
// DSMR output parser for Landis Gyr E350, we only extract things that interest us.
|
||||
class Data {
|
||||
private:
|
||||
static const std::unordered_map<std::string, LineTag> & GetMap();
|
||||
|
||||
static void RemoveUnit(std::string & value);
|
||||
|
||||
// Single argument lines only
|
||||
static std::pair<std::string, std::string> GetKeyValuePair(const std::string & line);
|
||||
|
||||
public:
|
||||
double currentPowerUsageKw = 0.0, currentPowerReturnKw = 0.0;
|
||||
double totalPowerConsumptionDayKwh = 0.0, totalPowerConsumptionNightKwh = 0.0;
|
||||
double totalPowerReturnedDayKwh = 0.0, totalPowerReturnedNightKwh = 0.0;
|
||||
bool usingDayTarif = true;
|
||||
std::string gasTimestamp = "";
|
||||
double gasConsumptionCubicMeters = 0.0; // m^3
|
||||
|
||||
void ParseLine(const std::string & line);
|
||||
void ParseLines(const std::vector<std::string> & lines);
|
||||
|
||||
std::string GetFormattedString(char const separator) const;
|
||||
};
|
||||
}
|
||||
24
include/electricity/logger/serialport.hpp
Normal file
24
include/electricity/logger/serialport.hpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <termios.h>
|
||||
|
||||
class SerialPort {
|
||||
private:
|
||||
const int device;
|
||||
termios configuration, oldConfiguration;
|
||||
|
||||
void SetAttributes(const speed_t baudrate = B115200);
|
||||
|
||||
protected:
|
||||
public:
|
||||
std::string ReadLine();
|
||||
|
||||
SerialPort(const int fd);
|
||||
|
||||
~SerialPort();
|
||||
|
||||
SerialPort(const SerialPort &) = delete;
|
||||
SerialPort(SerialPort &&) = delete;
|
||||
SerialPort & operator=(const SerialPort &) = delete;
|
||||
SerialPort & operator=(SerialPort &&) = delete;
|
||||
};
|
||||
10
include/electricity/server/api.hpp
Normal file
10
include/electricity/server/api.hpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include <pistache/http.h>
|
||||
#include <pistache/router.h>
|
||||
|
||||
namespace Server::Api
|
||||
{
|
||||
void GetDay(Pistache::Http::Request const & request, Pistache::Http::ResponseWriter responseWrite);
|
||||
|
||||
void SetupRouting(Pistache::Rest::Router & router);
|
||||
}
|
||||
33
include/electricity/server/configuration.hpp
Normal file
33
include/electricity/server/configuration.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
#include <chrono>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
namespace Server
|
||||
{
|
||||
class Configuration {
|
||||
private:
|
||||
std::string logDirectory;
|
||||
std::string serverDomain;
|
||||
std::string lastExternalIp;
|
||||
std::chrono::time_point<std::chrono::steady_clock> lastIpCheckTimePoint;
|
||||
std::mutex externalIpRefreshMutex;
|
||||
|
||||
Configuration();
|
||||
Configuration(Configuration & other) = delete;
|
||||
Configuration(Configuration && other) = delete;
|
||||
Configuration & operator=(Configuration & other) = delete;
|
||||
Configuration & operator=(Configuration && other) = delete;
|
||||
|
||||
void RefreshExternalIp();
|
||||
bool ExternalIpRequiresRefresh() const;
|
||||
|
||||
public:
|
||||
void Setup(std::string & electricityLogDirectory, std::string const & serverDomain);
|
||||
|
||||
std::string const & GetLogDirectory() const;
|
||||
std::string const & GetExternalServerIp();
|
||||
|
||||
static Configuration & Get();
|
||||
};
|
||||
}
|
||||
8
include/electricity/server/database.hpp
Normal file
8
include/electricity/server/database.hpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include <string>
|
||||
#include <util/date.hpp>
|
||||
|
||||
namespace Server::Database
|
||||
{
|
||||
std::string GetDetailedJsonOf(Util::Date const & date);
|
||||
std::string GetSummaryJsonOf(Util::Date const & startDate, Util::Date const & stopDate);
|
||||
}
|
||||
Reference in New Issue
Block a user