Skip to content

Commit

Permalink
bitfocus: implement serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Jan 4, 2025
1 parent 464ffa8 commit 8d3dc6e
Show file tree
Hide file tree
Showing 18 changed files with 382 additions and 155 deletions.
17 changes: 10 additions & 7 deletions src/plugins/score-lib-device/Device/Protocol/DeviceInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ void OwningDeviceInterface::releaseDevice()
{
DeviceInterface::disconnect();
deviceChanged(m_dev.get(), nullptr);
m_dev.release();
// FIXME instead it should just be shared_ptrs in player m_dev.release();
}

void OwningDeviceInterface::disconnect()
Expand Down Expand Up @@ -1040,14 +1040,18 @@ void DeviceInterface::addressRemoved(const ossia::net::parameter_base& addr)
void releaseDevice(
ossia::net::network_context& ctx, std::unique_ptr<ossia::net::device_base> dd)
{
if(dd)
releaseDevice(ctx, std::shared_ptr(std::move(dd)));
}

void releaseDevice(
ossia::net::network_context& ctx, std::shared_ptr<ossia::net::device_base> shared_dd)
{
if(shared_dd)
{
auto shared_dd = std::shared_ptr(std::move(dd));
auto strand = boost::asio::make_strand(ctx.context);
boost::asio::dispatch(strand, [dev = shared_dd] { dev->get_protocol().stop(); });

std::future<void> wait1
= boost::asio::dispatch(strand, boost::asio::use_future);
std::future<void> wait1 = boost::asio::dispatch(strand, boost::asio::use_future);
if(auto res = wait1.wait_for(std::chrono::seconds(1));
res != std::future_status::ready)
{
Expand All @@ -1056,8 +1060,7 @@ void releaseDevice(

boost::asio::dispatch(strand, [d = std::move(shared_dd)]() mutable { d.reset(); });

std::future<void> wait2
= boost::asio::dispatch(strand, boost::asio::use_future);
std::future<void> wait2 = boost::asio::dispatch(strand, boost::asio::use_future);
if(auto res = wait2.wait_for(std::chrono::seconds(1));
res != std::future_status::ready)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class SCORE_LIB_DEVICE_EXPORT OwningDeviceInterface : public DeviceInterface

ossia::net::device_base* getDevice() const final override { return m_dev.get(); }

std::unique_ptr<ossia::net::device_base> m_dev;
std::shared_ptr<ossia::net::device_base> m_dev;
bool m_owned{true};
};

Expand All @@ -216,4 +216,7 @@ findNodeFromPath(const QStringList& path, ossia::net::device_base& dev);
SCORE_LIB_DEVICE_EXPORT
void releaseDevice(
ossia::net::network_context& ctx, std::unique_ptr<ossia::net::device_base> dev);
SCORE_LIB_DEVICE_EXPORT
void releaseDevice(
ossia::net::network_context& ctx, std::shared_ptr<ossia::net::device_base> dev);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class SCORE_LIB_DEVICE_EXPORT DeviceEnumerator : public QObject
E_SIGNAL(SCORE_LIB_DEVICE_EXPORT, deviceAdded, n, s)
void deviceRemoved(const QString& s)
E_SIGNAL(SCORE_LIB_DEVICE_EXPORT, deviceRemoved, s)

void sort() E_SIGNAL(SCORE_LIB_DEVICE_EXPORT, sort)
};

using DeviceEnumerators = std::vector<std::pair<QString, Device::DeviceEnumerator*>>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ void DeviceEditDialog::selectedProtocolChanged()

connect(e.get(), &Device::DeviceEnumerator::deviceAdded, this, addItem);
connect(e.get(), &Device::DeviceEnumerator::deviceRemoved, this, rmItem);
connect(e.get(), &Device::DeviceEnumerator::sort, this, [cat] {
cat->sortChildren(0, Qt::SortOrder::AscendingOrder);
});
e->enumerate(addItem);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace bitfocus
module_handler::~module_handler() { }

module_handler::module_handler(
QString path, QString apiversion, module_configuration conf)
: module_handler_base{path}
QString path, QString entrypoint, QString apiversion, module_configuration conf)
: module_handler_base{path, entrypoint}
{
for(QChar& c : apiversion)
if(!c.isDigit() && c != '.')
Expand All @@ -26,6 +26,7 @@ module_handler::module_handler(
= QVersionNumber::fromString(apiversion) >= QVersionNumber(1, 2);

this->m_model.config = std::move(conf);

// Init an udp socket for sending osc
boost::system::error_code ec;
m_socket.open(boost::asio::ip::udp::v4(), ec);
Expand All @@ -49,6 +50,12 @@ module_handler::module_handler(
// <- setVariableDefinitions
// <- etc.
// <- init response

// -> updateFeedback
// <- response

// -> requestConfigFields
// <- response
}

void module_handler::do_write(QString str)
Expand All @@ -62,6 +69,14 @@ QString module_handler::jsonToString(QJsonObject obj)
return QJsonDocument{obj}.toJson(QJsonDocument::Compact);
}

void module_handler::afterRegistration(std::function<void()> f)
{
if(m_registered)
f();
else
m_afterRegistrationQueue.push_back(std::move(f));
}

void module_handler::processMessage(std::string_view v)
{
auto doc = QJsonDocument::fromJson(QByteArray::fromRawData(v.data(), v.size()));
Expand All @@ -75,20 +90,19 @@ void module_handler::processMessage(std::string_view v)

auto payload_json = QJsonDocument::fromJson(pay.toString().toUtf8());

auto pretty = [&] {
// qDebug() << payload_json.toJson().toStdString().data();
};
// auto pretty
// = [&] { qDebug() << " <- " << payload_json.toJson().toStdString().data(); };

// qDebug() << id.toString() << name.toString() << direction.toString();
//qDebug() << payload_json.toJson().toStdString().data();
if(direction == "call")
{
if(name == "register")
{
// First message
on_register(id);

QMetaObject::invokeMethod(this, [this] { init_msg_id = init(); });
QMetaObject::invokeMethod(this, [this] {
m_init_msg_id = init("label_" + QString::number(std::abs(rand() % 100)));
});
}
else if(name == "upgradedItems")
QMetaObject::invokeMethod(this, [this, id] { send_success(id); });
Expand Down Expand Up @@ -136,10 +150,10 @@ void module_handler::processMessage(std::string_view v)
}
else if(direction == "response")
{
if(id == init_msg_id)
if(id == m_init_msg_id)
{
// Query config field
req_cfg_id = this->requestConfigFields();
m_req_cfg_id = this->requestConfigFields();
// Update:
//{
// "hasHttpHandler": false,
Expand All @@ -152,16 +166,20 @@ void module_handler::processMessage(std::string_view v)
// }
//}
}
else if(id == req_cfg_id)
else if(id == m_req_cfg_id)
{
on_response_configFields(payload_json["fields"].toArray());
for(auto fun : m_afterRegistrationQueue)
fun();
m_afterRegistrationQueue.clear();
m_registered = true;
}
}
}

int module_handler::writeRequest(QString name, QString p)
{
int id = cbid++;
int id = m_cbid++;
QJsonObject obj;
obj["direction"] = "call";
obj["name"] = name;
Expand All @@ -186,6 +204,21 @@ void module_handler::writeReply(QJsonValue id, QJsonObject p)
{
return writeReply(id, QJsonDocument(p).toJson(QJsonDocument::Compact));
}
void module_handler::writeReply(QJsonValue id, QString p, bool success)
{
QJsonObject obj;
obj["direction"] = "response";
obj["payload"] = p;
obj["success"] = success;
obj["callbackId"] = id;

do_write(jsonToString(obj));
}

void module_handler::writeReply(QJsonValue id, QJsonObject p, bool success)
{
return writeReply(id, QJsonDocument(p).toJson(QJsonDocument::Compact), success);
}

void module_handler::on_register(QJsonValue id)
{
Expand All @@ -206,9 +239,7 @@ void module_handler::on_setActionDefinitions(QJsonArray actions)
def.hasLearn = obj["hasLearn"].toBool();
def.name = obj["name"].toString();
for(auto opt : obj["options"].toArray())
{
def.options.push_back(parseConfigField(opt.toObject()));
}

m_model.actions.emplace(obj["id"].toString(), std::move(def));
}
Expand All @@ -235,7 +266,7 @@ void module_handler::on_setFeedbackDefinitions(QJsonArray fbs)
def.name = obj["name"].toString();
def.type = obj["type"].toString();
for(auto opt : obj["options"].toArray())
def.options.push_back(opt.toObject().toVariantMap());
def.options.push_back(parseConfigField(opt.toObject()));

m_model.feedbacks.emplace(obj["id"].toString(), std::move(def));
}
Expand Down Expand Up @@ -298,8 +329,6 @@ module_data::config_field module_handler::parseConfigField(QJsonObject f)

void module_handler::on_response_configFields(QJsonArray fields)
{
qDebug() << Q_FUNC_INFO << fields.size();

m_model.config_fields.clear();
for(auto obj : fields)
{
Expand All @@ -312,7 +341,8 @@ void module_handler::on_response_configFields(QJsonArray fields)
void module_handler::on_send_osc(QJsonObject obj)
{
const std::string host = obj["host"].toString().toStdString();
const int port = obj["port"].toInt();
const auto pp = obj["port"];
const int port = pp.isDouble() ? pp.toInt() : pp.toString().toInt();
const std::string path = obj["path"].toString().toStdString();
const auto args = obj["args"].toArray();

Expand Down Expand Up @@ -380,10 +410,10 @@ void module_handler::on_send_osc(QJsonObject obj)
}
}

int module_handler::init()
int module_handler::init(QString label)
{
QJsonObject obj;
obj["label"] = "OSCPoint";
obj["label"] = label;
obj["isFirstInit"] = true;
QJsonObject config;
for(auto& [k, v] : this->m_model.config)
Expand Down Expand Up @@ -412,9 +442,9 @@ void module_handler::on_saveConfig(QJsonObject obj) { }
void module_handler::on_parseVariablesInString(QJsonValue id, QJsonObject obj)
{
QJsonObject p;
p["text"] = ""; // FIXME
p["text"] = obj["text"].toString(); // FIXME
p["variableIds"] = QJsonArray{};
writeReply(id, p);
writeReply(id, p, true);
}
void module_handler::on_updateFeedbackValues(QJsonObject obj) { }
void module_handler::on_recordAction(QJsonObject obj) { }
Expand Down Expand Up @@ -447,13 +477,31 @@ void module_handler::updateConfigAndLabel(QString label, module_configuration co

int module_handler::requestConfigFields()
{
qDebug() << Q_FUNC_INFO;
return writeRequest("getConfigFields", jsonToString(QJsonObject{}));
}

void module_handler::updateFeedbacks()
{
qDebug() << "TODO" << Q_FUNC_INFO;
QJsonObject feedbacks;
for(const auto& [k, v] : this->m_model.feedbacks)
{
const module_data::feedback_definition& fb = v;
QJsonObject fb_options;
// QJsonObject::fromVariantMap(fb.second.options);

feedbacks[k] = QJsonObject{
{"id", k},
{"controlId", 0},
{"feedbackId", fb.type},
{"options", fb_options},
{"isInverted", fb.isInverted},
{"upgradeIndex", QJsonValue(QJsonValue::Null)},
{"disabled", fb.disabled},
{"image", QJsonArray{1, 1}},
};
}

writeRequest("updateFeedbacks", jsonToString(QJsonObject{{"feedbacks", feedbacks}}));
}

void module_handler::feedbackLearnValues()
Expand Down
Loading

0 comments on commit 8d3dc6e

Please sign in to comment.