Skip to content

Commit

Permalink
Merge branch 'refactor' into 'master'
Browse files Browse the repository at this point in the history
Refactor

See merge request WeatherStationProject/weatherStationPlus!31
  • Loading branch information
monotok committed Oct 20, 2020
2 parents 55d0d64 + 7013063 commit 92c7b0f
Show file tree
Hide file tree
Showing 20 changed files with 992 additions and 479 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ project(weatherStationPlus)
set(CMAKE_CXX_STANDARD 11)

# Complier Flags
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -Wall -Wextra -DELPP_NO_DEFAULT_LOG_FILE -D_DEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "-DCMAKE_BUILD_TYPE=Debug")
set(CMAKE_CXX_FLAGS_RELEASE "-DELPP_NO_DEFAULT_LOG_FILE" -O2)

set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-fsanitize=address -fno-optimize-sibling-calls -fno-omit-frame-pointer -g -O1")
Expand Down
50 changes: 28 additions & 22 deletions include/RetrieveSenData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,54 @@
#include "i2cControl.hpp"
#include "lcdController.h"
#include <pqxx/pqxx>
#include <sstream>

static const int GET_LOCAL_DATA = 0;
static const int GET_REMOTE_DATA = 1;
#define GET_FIRST_SENSOR_READING 0
#define GET_NEXT_SENSOR_READING 1

class RetrieveSenData
{
public:
RetrieveSenData(I2cControl *, LcdController* lcdc, unsigned char);
RetrieveSenData(I2cControl *, LcdController* lcdc, unsigned char, pqxx::connection* conn);
void get_RemoteWeatherSenData(DynamicSensorFactory *);
void get_LocalWeatherData(DynamicSensorFactory *);
RetrieveSenData(I2cControl *, LcdController* lcdc, unsigned char, ConfigParser& wss);
RetrieveSenData(I2cControl *, LcdController* lcdc, unsigned char, pqxx::connection* conn, ConfigParser& wss);
void get_WeatherSenData(DynamicSensorFactory *ptr_dsf);

private:
I2cControl *i2c_controller;
LcdController *lcd_controller;
unsigned char I2C_ADDR;
pqxx::connection* C = nullptr;
int temporaryStructSize = sizeof(weatherSensorUnion.tsd);
ConfigParser* wss = nullptr;
int temporaryStructSize = sizeof(packet);
int get_temporaryStructSize() { return this->temporaryStructSize; }
string get_retrievedSensorID() { string s(weatherSensorUnion.tsd.sensorID); return s; }
bool check_incoming_data();
void store_local_weathersensor_data_in_database(WeatherSensor *ws);
void store_remote_weathersensor_data_in_database(WeatherSensor *ws);
bool process_ReceivedSensor(DynamicSensorFactory *ptr_dsf);
void prepare_insert_statements(pqxx::connection &c);

FRIEND_TEST(RetrieveSensorData, Get_specified_data_from_atmega_over_i2c);
FRIEND_TEST(RetrieveSensorData, check_incoming_data_valid);
FRIEND_TEST(RetrieveSensorData, check_incoming_data_invalid);
FRIEND_TEST(RetrieveSensorData, store_incoming_data_in_database);
FRIEND_TEST(RetrieveSensorData, store_battery_correctly_for_non_here_sensors);
FRIEND_TEST(RetrieveSensorData, get_overall_sensor_id);

typedef struct tempSensorData
struct SensorData
{
int16_t temperature;
char sensorID[10];
uint16_t perBatt;
};

union convertSensorClassChar {
tempSensorData tsd;
char packet[sizeof(tsd)];
};
union convertSensorClassChar weatherSensorUnion;
char sensorID[4];
char sensorType[4];
float reading;
char unit[4];
} tempSensor = {};
char packet[sizeof(tempSensor)] = {};

//Need to declare this after the struct otherwise they would be in different scopes and therefore not the same type
bool check_incoming_data(SensorData& sensor);

void get_retrievedGroupSensorID(char* sensorId) {
strtok(sensorId, ".");
}
string get_retrievedSensorType(SensorData& sensor) { return sensor.sensorType; }


};

#endif //ifndef RETRIEVE_SEN_DATA_H
5 changes: 5 additions & 0 deletions include/configParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ class ConfigParser
void getGPIODetails(const libconfig::Setting& root);
void getLogDetails(const libconfig::Setting& root);
void getI2cDetails(const libconfig::Setting& root);
void getPositionInformation(const libconfig::Setting& root);
Position& validatePosition(const string& position);

public:
ConfigParser(Settings& wsettings, const char* settingsFileLocation = "conf/settings.conf");
void ParseConfiguration();
string getSensorsName(string sensorID);
string getSensorReadingName(const string &sensorId, const string &readingId);
Position& getSensorReadingPosition(const string& sensorId, const string& readingId);
Position& matchNamePositionToValuePosition(Position& position);

};

Expand Down
14 changes: 13 additions & 1 deletion include/data/Abc_Sensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

//This is an abstract class

#include "../settings.hpp"
#include <string>

using namespace std;
Expand All @@ -13,6 +14,8 @@ class Sensor
string sensorID;
string sensorName;
string sensorType;
Position posName = {1, 0};
Position posTitle = {1, 9};

public:
Sensor(string sensorID, string sensorName, string sensorType)
Expand All @@ -22,7 +25,16 @@ class Sensor
void set_sensorID(string sid) { this->sensorID = sid; }
void set_sensorName(string sname) { this->sensorName = sname; }
void set_sensorType(string sensorType) { this->sensorType = sensorType; }
string get_sensorID() { return this->sensorID; }
Position& get_Position_SensorName() { return posName; };
Position& get_Position_Title() { return posTitle; };

string get_sensorID() {
string::size_type pos = this->sensorID.find(".");
if (pos != string::npos) {
return this->sensorID.substr(0, pos);
}
return this->sensorID;
}
string get_sensorName(){
if(this->sensorName.compare("NotSet") == 0) {
return this->sensorID;
Expand Down
56 changes: 38 additions & 18 deletions include/data/WeatherSensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,57 @@

#include "Abc_Sensor.hpp"
#include "../Utilities.hpp"
#include "../easylogging++.hpp"
#include "../configParser.hpp"
#include <string>
#include "vector"
#include <pqxx/pqxx>

using namespace std;

class WeatherSensor : public Sensor
{
private:
float temperature = 0.0;
float humidity = 0.0;
char tempUnit = 'c';

public:

struct Data {
string readingId = {};
float reading = {};
string type = {};
string unit = {};
string name = {};
Position posName = {0, 0};
Position posVal = {0, 0};
Data(string readingId) {
this->readingId = readingId;
}
};

WeatherSensor(string sensorID, string sensorName, string sensorType) : Sensor(sensorID, sensorName, sensorType) {};
~WeatherSensor(){};

//Sensor Values
string get_temperature();
string get_humidity();
Data *getReading_ptr(string readingID);
WeatherSensor::Data *createNewSensorReading_obj(string readingID);
vector<Data *> getAvailableReadings();

//Database
void store_weathersensor_data_in_database(Data *reading, pqxx::connection &C);
void store_weathersensormetadata_data_in_database(Data *sensor, pqxx::connection &C);

float get_temperature_float() { return this->temperature/100; }
float get_humidity_float() { return this->humidity/100; }

//Sensor Values
void set_humidity(float humidity) { this->humidity = humidity; }
void set_temperature(float temperature) { this->temperature = temperature; }

//Sensor Units
void set_tempUnit_to_C();
void set_tempUnit_to_F();
void switch_tempUnit();
char getTempUnit();
string get_Reading(Data* reading);
string get_Reading(string reading);

//Sensor Values
Data* set_reading(string readingId, string type, float reading, string unit, pqxx::connection* C, ConfigParser* wss);

private:

// We only support 6 sensor readings per sensor as this fits on a 20x4 display
vector<Data *> readings_vector = {};
vector<Data *>::iterator readingsIterator;
void addNewReadingArray(Data *newReading);
void setLcdReadingPosition(Data& data, string sensorId, string readingId, ConfigParser* wss);
};

#endif // TEMP_SENSOR_H
32 changes: 22 additions & 10 deletions include/lcdController.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "../include/i2cControl.hpp"
#include "../include/lcdDriver.hpp"
#include "../include/Utilities.hpp"
#include "../include/settings.hpp"

#define FIXED 0
#define VAR 1
Expand All @@ -26,33 +27,44 @@ using namespace std;
class LcdController
{
public:
void createWeatherPage(WeatherSensor* ws);
void drawPage(string SensorName, LcdDriver &lcd);
void updatePageValues(WeatherSensor* ws, LcdDriver &lcd);
string getNextPage(string CurrentPage);
LcdController(LcdDriver& lcd): lcd(lcd) {};
void createWeatherPage(WeatherSensor* ws, WeatherSensor::Data* reading);
void drawPage_Locking();
void drawPage_NonLocking();
void drawPage();
void updatePageValues(WeatherSensor *ws);
void getNextPage();
void createDateTimePage();
void drawDateTimePage(LcdDriver &lcd);
void updateDateTimePage(LcdDriver &lcd);
void drawDateTimePage();
void updateDateTimePage();
void clearDisplay();
void setCurrentPage(string pageName);
string getCurrentPage();

private:
LcdDriver lcd;

struct Pageitem {
string id;
int row_start;
int col_start;
Position pos;
int type;
string value;
};

string currentPage = {};

FRIEND_TEST(LcdController, create_new_weather_page_struct_independant);
FRIEND_TEST(LcdController, create_new_datetime_page_struct_independant);
FRIEND_TEST(LcdController, update_only_changed_values_datetime);
FRIEND_TEST(LcdController, update_values_only_on_existing_page);
FRIEND_TEST(LcdController, check_for_existing_weather_sensor);

bool existingWeatherPage(string SensorName);
void drawElementToLCD(LcdDriver &lcd);
bool existingWeatherPageReading(string SensorName, string reading);
void drawElementToLCD();
void checkValuesFitLcd();
void checkValuesFitLcd(float newValue, LcdDriver &lcd);
void checkValuesFitLcd(string newValue);
void createNewReading(WeatherSensor* ws, WeatherSensor::Data* reading);

map<string, vector<Pageitem>> pages_map;
map<string, vector<Pageitem>>::iterator pm_iter;
Expand Down
2 changes: 1 addition & 1 deletion include/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
#include <thread>

Settings weatherStationSettings {};
string currentPage = "Here";
ConfigParser wss(weatherStationSettings);

#endif //WEATHERSTATIONPLUS_MAIN_H
32 changes: 32 additions & 0 deletions include/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define WEATHERSTATIONPLUS_SETTINGS_HPP

#include <string>
#include <tuple>

using namespace std;

Expand Down Expand Up @@ -35,12 +36,43 @@ struct I2c {
int lcd;
};

struct Position {
int row_start = 0;
int col_start = 0;
Position() = default;
Position(int row, int col): row_start(row), col_start(col) {};

//So it can be used in a map
bool operator<(const Position& pos) const
{
return std::tie(row_start, col_start) < std::tie(pos.row_start, pos.col_start);
}

//So it can be used in a test to compare positions
bool operator==(const Position& pos) const
{
return std::tie(row_start, col_start) == std::tie(pos.row_start, pos.col_start);
}
};

struct Settings {
float version;
struct Database db;
struct Gpio gpio;
struct Logging logg;
struct I2c i2c;
Position topleft_Name;
Position topleft_Val;
Position topright_Name;
Position topright_Val;
Position middleleft_Name;
Position middleleft_Val;
Position middleright_Name;
Position middleright_Val;
Position bottomleft_Name;
Position bottomleft_Val;
Position bottomright_Name;
Position bottomright_Val;
};

#endif //WEATHERSTATIONPLUS_SETTINGS_HPP
Loading

0 comments on commit 92c7b0f

Please sign in to comment.