Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cmake_minimum_required(VERSION 3.18)
set(PROJECT_NAME "UniversalDevice")

# name of a project
project(${PROJECT_NAME} VERSION 2.0.0)
project(${PROJECT_NAME} VERSION 2.0.1)

# create file with version defines
configure_file(Version.hpp.in Version.hpp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ class EventWidgetHelper final {
static void Hide() {}

template<typename T>
static void Hide(T t) {
static void Hide(T& t) {
t->hide();
}

template<typename T, typename... Ts>
static void Hide(T t, Ts... args) {
static void Hide(T& t, Ts... args) {
Hide(t);
Hide(args...);
}
Expand All @@ -20,12 +20,12 @@ class EventWidgetHelper final {
static void Show() {}

template<typename T>
static void Show(T t) {
static void Show(T& t) {
t->show();
}

template<typename T, typename... Ts>
static void Show(T t, Ts... args) {
static void Show(T& t, Ts&... args) {
Show(t);
Show(args...);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,31 @@
#include <memory>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#include <Wt/WCheckBox.h>
#include <Wt/WContainerWidget.h>
#include <Wt/WGlobal.h>
#include <Wt/WGroupBox.h>
#include <Wt/WHBoxLayout.h>
#include <Wt/WPushButton.h>
#include <Wt/WSelectionBox.h>
#include <Wt/WText.h>
#include <Wt/WVBoxLayout.h>
#include <Wt/WWidget.h>
#include <fmt/format.h>
#include <nlohmann/json_fwd.hpp>

#include "ApplicationSettings.hpp"
#include "BaseStackWidget.hpp"
#include "Command.hpp"
#include "Constants.hpp"
#include "Defines.hpp"
#include "Device.hpp"
#include "Enums.hpp"
#include "Event.hpp"
#include "EventReceiverWidget.hpp"
#include "EventUtils.hpp"
#include "FrontendDefines.hpp"
#include "IStackHolder.hpp"
Expand Down Expand Up @@ -94,6 +101,11 @@ ScenariosWidget::ScenariosWidget(IStackHolder* stackHolder, const ApplicationSet
_activateEventsGroup = groupsLayout->addWidget(std::make_unique<WGroupBox>("Активировать:"), 1);

_deactivateEventsGroup = groupsLayout->addWidget(std::make_unique<WGroupBox>("Деактивировать:"), 1);

_commandsCanvas = selectionLayout->addWidget(std::make_unique<WContainerWidget>(), 0, AlignmentFlag::Bottom);
_commandsCanvas->setOverflow(Overflow::Auto, Orientation::Vertical);
_commandsCanvas->setOverflow(Overflow::Hidden, Orientation::Horizontal);
_commandsLayout = _commandsCanvas->setLayout(std::make_unique<WGridLayout>());
}

void ScenariosWidget::Initialize(const std::string& data) { Refresh(); }
Expand All @@ -110,6 +122,7 @@ void ScenariosWidget::Clear() {
_deactivateEventsGroup->removeWidget(deactivateCheckBox);
_activatedEvents.clear();
_deactivatedEvents.clear();
CleanupCommandsCanvas();
}

void ScenariosWidget::Refresh() {
Expand Down Expand Up @@ -164,8 +177,10 @@ void ScenariosWidget::AddScenario() {

void ScenariosWidget::OnSelection(int index) {
const int selectedIndex = _scenariosList->currentIndex();
if (selectedIndex == -1)
if (selectedIndex == -1) {
CleanupCommandsCanvas();
return;
}

const Scenario& scenario = _scenarios.at(selectedIndex);

Expand All @@ -181,6 +196,7 @@ void ScenariosWidget::OnSelection(int index) {
}
SetSelectedEventIndexes(_activatedEvents, activatedIndexes);
SetSelectedEventIndexes(_deactivatedEvents, deactivatedIndexes);
FillCommandsCanvas(scenario._commands);
}

void ScenariosWidget::UpdateScenario() {
Expand Down Expand Up @@ -230,7 +246,7 @@ void ScenariosWidget::DeleteScenario() {
Refresh();
}

std::set<Uuid> ScenariosWidget::GetSelectedEventIndexes(const std::vector<Wt::WCheckBox*>& container) const {
std::set<Uuid> ScenariosWidget::GetSelectedEventIndexes(const std::vector<WCheckBox*>& container) const {
std::set<Uuid> result;
for (std::size_t index = 0; index < container.size(); ++index) {
if (container.at(index)->isChecked())
Expand All @@ -239,12 +255,57 @@ std::set<Uuid> ScenariosWidget::GetSelectedEventIndexes(const std::vector<Wt::WC
return result;
}

void ScenariosWidget::SetSelectedEventIndexes(const std::vector<Wt::WCheckBox*>& container, const std::set<std::size_t>& indexes) {
void ScenariosWidget::SetSelectedEventIndexes(const std::vector<WCheckBox*>& container, const std::set<std::size_t>& indexes) {
for (std::size_t index = 0; index < container.size(); ++index) {
container.at(index)->setChecked(indexes.contains(index));
}
}

void ScenariosWidget::CleanupCommandsCanvas() {
for (auto iter = _commandsWidgets.rbegin(); iter != _commandsWidgets.rend(); ++iter) {
_commandsLayout->removeWidget(*iter);
}

_commandsWidgets.clear();

_commandsLayout->removeWidget(_commandEditor);
_commandsLayout->removeWidget(_addCommandButton);

_commandEditor = nullptr;
_addCommandButton = nullptr;
}

Devices ScenariosWidget::GetDevices() {
auto resultJson = RequestHelper::DoGetRequest({ BACKEND_IP, _settings._servicePort, API_CLIENT_DEVICES }, Constants::LoginService);
Devices devices = resultJson.is_null() ? Devices{} : resultJson.get<Devices>();
// right now not all devices can receive events
auto newEnd = std::remove_if(devices.begin(), devices.end(), [](const Device& device) {
return device._type == DeviceType::Undefined || device._type == DeviceType::UniversalDevice;
});
devices.erase(newEnd, devices.end());
return devices;
}

void ScenariosWidget::FillCommandsCanvas(const std::unordered_map<Uuid, Command>& commands) {
const Devices devices = GetDevices();

_commandEditor = _commandsLayout->addWidget(std::make_unique<EventReceiverWidget>(), 0, 0, 1, 2);
_commandEditor->SetDevices(devices);
_addCommandButton = _commandsLayout->addWidget(std::make_unique<WPushButton>("Добавить"), 0, 2, AlignmentFlag::Right);
WidgetHelper::SetUsualButtonSize(_addCommandButton);
for (const auto& [deviceId, command] : commands) {
EventReceiverWidget* eventReceiverWidget = _commandsLayout->addWidget(std::make_unique<EventReceiverWidget>(), 0, 0, 1, 2);
eventReceiverWidget->SetDevices(devices);
_commandsWidgets.push_back(eventReceiverWidget);

WPushButton* deleteButton = _commandsLayout->addWidget(std::make_unique<WPushButton>("Добавить"), 0, 2, AlignmentFlag::Right);
WidgetHelper::SetUsualButtonSize(deleteButton);
_commandsWidgets.push_back(deleteButton);

(void)deviceId;
}
}

bool ScenariosWidget::IsUiValid() const {
const auto selectedActivated = GetSelectedEventIndexes(_activatedEvents);
const auto selectedDeactivated = GetSelectedEventIndexes(_deactivatedEvents);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@
#include <cstddef>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#include <Wt/WCheckBox.h>
#include <Wt/WContainerWidget.h>
#include <Wt/WGlobal.h>
#include <Wt/WGridLayout.h>
#include <Wt/WGroupBox.h>
#include <Wt/WHBoxLayout.h>
#include <Wt/WLineEdit.h>
#include <Wt/WPushButton.h>
#include <Wt/WSelectionBox.h>
#include <Wt/WVBoxLayout.h>

#include "ApplicationSettings.hpp"
#include "BaseStackWidget.hpp"
#include "Command.hpp"
#include "Device.hpp"
#include "Event.hpp"
#include "EventReceiverWidget.hpp"
#include "IStackHolder.hpp"
#include "Scenario.hpp"
#include "Uuid.hpp"
Expand Down Expand Up @@ -44,6 +52,12 @@ class ScenariosWidget final : public Wt::WContainerWidget, public BaseStackWidge

void SetSelectedEventIndexes(const std::vector<Wt::WCheckBox*>& container, const std::set<std::size_t>& indexes);

void CleanupCommandsCanvas();

Devices GetDevices();

void FillCommandsCanvas(const std::unordered_map<Uuid, Command>& commands);

bool IsUiValid() const;

private:
Expand All @@ -52,6 +66,11 @@ class ScenariosWidget final : public Wt::WContainerWidget, public BaseStackWidge
Wt::WLineEdit* _nameEditor;
Wt::WGroupBox* _activateEventsGroup;
Wt::WGroupBox* _deactivateEventsGroup;
WContainerWidget* _commandsCanvas;
Wt::WGridLayout* _commandsLayout;
EventReceiverWidget* _commandEditor;
Wt::WPushButton* _addCommandButton;
std::vector<Wt::WWidget*> _commandsWidgets;
std::vector<Wt::WCheckBox*> _activatedEvents;
std::vector<Wt::WCheckBox*> _deactivatedEvents;
std::vector<Scenario> _scenarios;
Expand Down
7 changes: 5 additions & 2 deletions src/UniversalDeviceLib/events/Scenario.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>

#include "Command.hpp"
#include "Uuid.hpp"

struct Scenario {
Uuid _id;
std::string _name;
std::set<Uuid> _activateEvent;
std::set<Uuid> _deactivateEvent;
std::unordered_map<Uuid, Command> _commands;

bool operator==(const Scenario& other) const {
return std::tie(_id, _name, _activateEvent, _deactivateEvent) ==
std::tie(other._id, other._name, other._activateEvent, other._deactivateEvent);
return std::tie(_id, _name, _activateEvent, _deactivateEvent, _commands) ==
std::tie(other._id, other._name, other._activateEvent, other._deactivateEvent, other._commands);
}
};
4 changes: 4 additions & 0 deletions src/UniversalDeviceLib/marshaling/Marshaling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <set>
#include <string>
#include <string_view>
#include <unordered_map>
#include <variant>

#include <boost/hof.hpp>
Expand Down Expand Up @@ -617,6 +618,7 @@ void to_json(nlohmann::json& json, const Scenario& scenario) {
{ "name", scenario._name },
{ "activate", scenario._activateEvent },
{ "deactivate", scenario._deactivateEvent },
{ "commands", scenario._commands },
};
}

Expand All @@ -625,6 +627,8 @@ void from_json(const nlohmann::json& json, Scenario& scenario) {
scenario._name = json.at("name").get<std::string>();
scenario._activateEvent = json.at("activate").get<std::set<Uuid>>();
scenario._deactivateEvent = json.at("deactivate").get<std::set<Uuid>>();
scenario._commands =
json.count("commands") == 0 ? std::unordered_map<Uuid, Command>{} : json.at("commands").get<std::unordered_map<Uuid, Command>>();
}

void to_json(nlohmann::json& json, const ThermometerLedBrightness& thermometerLedBrightness) {
Expand Down
5 changes: 5 additions & 0 deletions src/UniversalDeviceLib/network/RequestHelper.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
#include "RequestHelper.hpp"

#include <functional>
#include <ostream>
#include <sstream>
#include <string>
#include <string_view>
#include <utility>

#include <fmt/format.h>
#include <ixwebsocket/IXHttp.h>
#include <ixwebsocket/IXHttpClient.h>
#include <ixwebsocket/IXSocketTLSOptions.h>
#include <nlohmann/json_fwd.hpp>

#include "AccountManager.hpp"
#include "Base64Helper.hpp"
#include "Logger.hpp"
#include "RequestAddress.hpp"

nlohmann::json RequestHelper::DoGetRequest(const RequestAddress& requestAddress, const std::string_view login) {
try {
Expand Down
10 changes: 8 additions & 2 deletions src/UniversalDeviceServiceLib/controllers/DevicesController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@
#include "IQueryExecutor.hpp"
#include "Logger.hpp"
#include "Marshaling.hpp"
#include "ScenariosController.hpp"
#include "SettingsController.hpp"
#include "TimeHelper.hpp"

DevicesController::DevicesController(IQueryExecutor* queryExecutor, SettingsController& settingsController, CommandsController& commandsController) :
DevicesController::DevicesController(IQueryExecutor* queryExecutor,
SettingsController& settingsController,
CommandsController& commandsController,
ScenariosController& scenariosController) :
Controller(queryExecutor),
_settingsController(settingsController),
_commandsController(commandsController) //
_commandsController(commandsController),
_scenariosController(scenariosController) //
{
FillCache();
}
Expand Down Expand Up @@ -139,6 +144,7 @@ bool DevicesController::Remove(const Uuid& id) {

_settingsController.Remove(id);
_commandsController.Remove(id);
_scenariosController.CleanupCommands(id);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
#include "Controller.hpp"
#include "Device.hpp"
#include "IQueryExecutor.hpp"
#include "ScenariosController.hpp"
#include "SettingsController.hpp"
#include "Uuid.hpp"

class DevicesController final : public Controller {
public:
DevicesController(IQueryExecutor* queryExecutor, SettingsController& settingsController, CommandsController& commandsController);
DevicesController(IQueryExecutor* queryExecutor,
SettingsController& settingsController,
CommandsController& commandsController,
ScenariosController& scenariosController);

~DevicesController() = default;

Expand All @@ -39,4 +43,5 @@ class DevicesController final : public Controller {
mutable Cache<Uuid, Device> _cache;
SettingsController& _settingsController;
CommandsController& _commandsController;
ScenariosController& _scenariosController;
};
Loading
Loading