diff --git a/src/smg_server.cpp b/src/smg_server.cpp index d916a98e6dca4548772d720c54525f3990f31275..b6e3cad4cf1d92de83f152782f5608a2ee5323c7 100644 --- a/src/smg_server.cpp +++ b/src/smg_server.cpp @@ -1,194 +1,19 @@ /// @file main.cpp -/// -#include <i2c/Node.hpp> -#include <SmartGridModell.hpp> -#include <spdlog/spdlog.h> - -#include <thread> -#include <cstdlib> -#include <ctime> - - -#include <Socket.hpp> -#include <Protocol.hpp> - -struct CleanPower -{ - double solar = 0.0; - double wind = 0.0; -}; - -struct PowerProduction -{ - double conventional; - CleanPower renewable; - double clean(){return renewable.solar + renewable.wind;} - double sum(){return conventional + clean();} -}; - -struct PowerUsage -{ - double village = 0.0; - double industry = 0.0; - double sum() {return village + industry;} -}; - - -/// @brief Peak Power consumption or production in MW /// -struct MaxPower -{ - static constexpr double village = 600; - static constexpr double industry = 200; - static constexpr double wind = 250; - static constexpr double solar = 110; -}; - -constexpr double village_consumption_at[] { - 98, 95, 93, 94, 95, 101, 115, - 127, 132, 134, 136, 139, 138, - 136, 134, 132, 130, 132, 132, - 131, 125, 119, 114, 105, 98 -}; - -constexpr double sunnshine_percentage[] { - 0, 0, 0, 0, 0, 0, 3, - 12, 30, 52, 73, 88, 97, - 100, 98, 91, 81, 66, 46, - 25, 10, 2, 0, 0, 0 -}; - -constexpr double power_wind[] { - 0, 3, 25, 82, 174, - 321, 532, 815, 1180, - 1612, 1890, 2000, 2100 -}; - -class ModelState -{ -public: - ModelState(SmartGridModell &modell) - : m_modell{modell} - { - std::srand(std::time(nullptr)); - } - - void next_hour() - { - update_time(); - update_sun(); - update_wind(); - update_power_consumption(); - update_power_production(); - update_modell(); - print_states(); - } - - float excess_power() - { - auto sum_prod = m_production.conventional + m_production.renewable.solar + m_production.renewable.wind; - auto sum_used = m_usage.industry + m_usage.village; - return sum_prod - sum_used; - } - -private: - void update_time() - { - if (m_time < 23) - { - ++m_time; - } - else - { - m_time = 0; - } - } - void update_sun() - { - m_sun = sunnshine_percentage[m_time]; - } - void update_wind() - { - double wind_by_sun = m_sun * 7.0 / 100.0; // wind by sun should by 5 max. - auto random_wind = ((std::rand() * 1.0) / RAND_MAX) * 5.0; - m_wind = power_wind[static_cast<int>(wind_by_sun + random_wind)]; - } - void update_power_consumption() - { - m_usage.village = village_consumption_at[m_time] * village_size; - if (m_producing == true) - { - m_usage.industry = MaxPower::industry; - } - else - { - m_usage.industry = 0.0; - } - } - void update_power_production() - { - m_production.renewable.solar = m_sun * solar_size; - m_production.renewable.wind = m_wind * windpark_size; - if (m_usage.sum() > m_production.clean()) - { - m_excess_power = 0.0; - m_production.conventional = m_usage.sum() - m_production.clean(); - } - else - { - m_excess_power = m_production.clean() - m_usage.sum(); - m_production.conventional = 0; - } - } - void update_modell() - { - auto sun = m_sun * 2.5; - auto wind = m_wind / 8.3; - auto renewable = (sun + wind) / 2; - m_modell.set_solar_plant(sun); - m_modell.update_windmill_speed(wind); - m_modell.set_windmill_net(wind); - m_modell.set_renewable_net(renewable); - if (m_production.conventional > 0) - { - m_modell.set_power_plant(200); - m_modell.set_village_color(200, renewable); - } - else - { - m_modell.set_power_plant(0); - m_modell.set_village_color(0, renewable); - } - } - - - static constexpr double solar_size {10}; - static constexpr double windpark_size {1.0}; - static constexpr double village_size {10}; +#include <thread> +#include "spdlog/spdlog.h" - int m_time {0}; - double m_sun {0}; - double m_wind {0}; - double m_excess_power {0.0}; - PowerProduction m_production; - PowerUsage m_usage; - bool m_producing {false}; - SmartGridModell &m_modell; +#include "i2c/Node.hpp" +#include "SmartGridModell.hpp" - void print_states() - { - spdlog::debug("Time <{}> Sun <{}> Wind<{}>", m_time, m_sun, m_wind); - spdlog::debug("Power Conv <{}> Solar <{}> Wind<{}>", m_production.conventional, m_production.renewable.solar, m_production.renewable.wind); - spdlog::debug("Usage Village<{}> Industry<{}>", m_usage.village, m_usage.industry); - spdlog::debug("excess_power<{}>", excess_power()); - } -}; +#include "Socket.hpp" +#include "Protocol.hpp" +#include "ModelState.hpp" void run_sim() { - i2c::Node i2c_device {0x14}; - + i2c::Node i2c_device{0x14}; if (false == i2c_device.open_device("/dev/i2c-1")) { exit(1); @@ -198,64 +23,32 @@ void run_sim() spdlog::debug("Node created"); } - SmartGridModell modell{i2c_device, SmartGridModell::DefaultState::Fancy}; - spdlog::debug("Modell created"); - - modell.put_modell_into_state(SmartGridModell::DefaultState::Off); - + SmartGridModell modell{i2c_device}; ModelState day{modell}; - bool active {true}; + bool active{true}; while (active) { day.next_hour(); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); + std::this_thread::sleep_for(std::chrono::milliseconds(50)); } } - -void test_x64() +void log_args(int argc, char **argv) { - i2c::Node i2c_device {0x14}; - bool done = i2c_device.open_device("/dev/i2c-1"); - if (done == false) + for (int i = 0; i < argc; ++i) { - spdlog::debug("You could try running as root"); - } - - ServerSocket server(8080); - bool finished {false}; - SmartGridModell modell{i2c_device, SmartGridModell::DefaultState::Fancy}; - ModelState day{modell}; - while (!finished) - { - DataSocket accept = server.accept(); - ProtocolSimple accept_simple(accept); - - std::string message; - accept_simple.recv_message(message); - spdlog::info("RECV: {}", message); - - accept_simple.send_message("", "OK"); - - day.next_hour(); + spdlog::debug("[{}] {}", i, argv[i]); } } - -int main(int argc, char** argv) +int main(int argc, char **argv) { spdlog::set_level(spdlog::level::debug); spdlog::info("Starting - Smart Grid Modell"); + log_args(argc, argv); - if (argc > 1) - { - test_x64(); - } - else - { - run_sim(); - } + run_sim(); spdlog::info("End"); return 0;