-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create GridConsumerBase for shared implementation of IGridConsumer; p…
…ersist virtual grid mirror settings in patch
- Loading branch information
Showing
10 changed files
with
203 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#include "GridConsumerBase.hpp" | ||
|
||
GridConsumerBase::GridConsumerBase() | ||
: lastConnectedDeviceId("") | ||
, currentConnectedDeviceId("") | ||
, gridConnection(nullptr) | ||
{ | ||
|
||
} | ||
|
||
void GridConsumerBase::gridConnected(Grid* newConnection) | ||
{ | ||
gridConnection = newConnection; | ||
if (gridConnection) | ||
{ | ||
std::string id = gridConnection->getDevice().id; | ||
lastConnectedDeviceId = id; | ||
currentConnectedDeviceId = id; | ||
connectionOwned = true; | ||
} | ||
} | ||
|
||
void GridConsumerBase::gridDisconnected(bool ownerChanged) | ||
{ | ||
gridConnection = nullptr; | ||
currentConnectedDeviceId = ""; | ||
|
||
if (ownerChanged) | ||
{ | ||
connectionOwned = false; | ||
} | ||
} | ||
|
||
std::string GridConsumerBase::gridGetCurrentDeviceId() | ||
{ | ||
return currentConnectedDeviceId; | ||
} | ||
|
||
std::string GridConsumerBase::gridGetLastDeviceId(bool owned) | ||
{ | ||
if (owned && !connectionOwned) | ||
{ | ||
return ""; | ||
} | ||
|
||
return lastConnectedDeviceId; | ||
} | ||
|
||
Grid* GridConsumerBase::gridGetDevice() | ||
{ | ||
return gridConnection; | ||
} | ||
|
||
void GridConsumerBase::userReacquireGrid() | ||
{ | ||
if (lastConnectedDeviceId != "" && gridConnection == nullptr) | ||
{ | ||
for (Grid* grid : GridConnectionManager::get().getGrids()) | ||
{ | ||
if (gridGetLastDeviceId(false) == grid->getDevice().id) | ||
{ | ||
GridConnectionManager::get().connect(grid, this); | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
|
||
void GridConsumerBase::toggleGridConnection(Grid* grid) | ||
{ | ||
if (gridConnection == grid) | ||
{ | ||
GridConnectionManager::get().disconnect(this, true); | ||
lastConnectedDeviceId = ""; | ||
} | ||
else | ||
{ | ||
GridConnectionManager::get().connect(grid, this); | ||
} | ||
} | ||
|
||
void GridConsumerBase::saveGridConnectionToJson(json_t* rootJ) | ||
{ | ||
std::string deviceId = lastConnectedDeviceId; | ||
if (gridConnection) | ||
{ | ||
deviceId = gridConnection->getDevice().id; | ||
} | ||
json_object_set_new(rootJ, "connectedDeviceId", json_string(deviceId.c_str())); | ||
json_object_set_new(rootJ, "connectionOwned", json_boolean(connectionOwned)); | ||
} | ||
|
||
void GridConsumerBase::loadGridConnectionFromJson(json_t* rootJ) | ||
{ | ||
json_t* id = json_object_get(rootJ, "connectedDeviceId"); | ||
if (id) | ||
{ | ||
lastConnectedDeviceId = json_string_value(id); | ||
} | ||
|
||
json_t* owned = json_object_get(rootJ, "connectionOwned"); | ||
if (owned) | ||
{ | ||
connectionOwned = json_boolean_value(owned); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
|
||
#include "GridConnection.hpp" | ||
#include "rack.hpp" | ||
|
||
struct GridConsumerBase : public IGridConsumer | ||
{ | ||
// IGridConsumer partial implementation; child classes implement gridButtonEvent/encDeltaEvent | ||
void gridConnected(Grid* grid) override; | ||
void gridDisconnected(bool ownerChanged) override; | ||
std::string gridGetCurrentDeviceId() override; | ||
std::string gridGetLastDeviceId(bool owned) override; | ||
Grid* gridGetDevice() override; | ||
|
||
GridConsumerBase(); | ||
virtual ~GridConsumerBase() {}; | ||
|
||
void userReacquireGrid(); | ||
|
||
void saveGridConnectionToJson(json_t* rootJ); | ||
void loadGridConnectionFromJson(json_t* rootJ); | ||
|
||
std::string lastConnectedDeviceId; | ||
std::string currentConnectedDeviceId; | ||
bool connectionOwned; | ||
|
||
protected: | ||
void toggleGridConnection(Grid* grid); | ||
Grid* gridConnection; | ||
}; |
Oops, something went wrong.