Skip to content

Commit

Permalink
Clean up on how to construct propertry changed event
Browse files Browse the repository at this point in the history
  • Loading branch information
lo-simon committed Oct 19, 2023
1 parent 264cfba commit ce62865
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 33 deletions.
19 changes: 10 additions & 9 deletions Development/nmos/control_protocol_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,21 @@ namespace nmos
{
// update receiver-monitor's connectionStatus propertry

auto active = nmos::fields::master_enable(nmos::fields::endpoint_active(connection_resource.data));
const auto active = nmos::fields::master_enable(nmos::fields::endpoint_active(connection_resource.data));
const web::json::value val = active ? nc_connection_status::connected : nc_connection_status::disconnected;

nc_property_id property_id = nc_receiver_monitor_connection_status_property_id;
web::json::value val = active ? nc_connection_status::connected : nc_connection_status::disconnected;
const nc_property_changed_event_data property_changed_event_data{ property_id, nc_property_change_type::type::value_changed, val };
const auto notification = make_control_protocol_notification(nmos::fields::nc::oid(found->data), nc_object_property_changed_event_id, property_changed_event_data);
const auto notification_event = make_control_protocol_notification(web::json::value_of({ notification }));
// hmm, maybe updating connectionStatusMessage, payloadStatus, and payloadStatusMessage too

const auto propertry_changed_event = make_propertry_changed_event(nmos::fields::nc::oid(found->data),
{
{ nc_receiver_monitor_connection_status_property_id, nc_property_change_type::type::value_changed, val }
});

modify_control_protocol_resource(resources, found->id, [&](nmos::resource& resource)
{
resource.data[nmos::fields::nc::connection_status] = property_changed_event_data.value;
// hmm, maybe updating connectionStatusMessage, payloadStatus, and payloadStatusMessage too
resource.data[nmos::fields::nc::connection_status] = val;

}, notification_event);
}, propertry_changed_event);
}
};
}
Expand Down
29 changes: 7 additions & 22 deletions Development/nmos/control_protocol_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,11 @@ namespace nmos
return make_control_protocol_message_response(handle, { nc_method_status::parameter_error });
}

const nc_property_changed_event_data property_changed_event_data{ parse_nc_property_id(property_id), nc_property_change_type::type::value_changed, val };
const auto notification = make_control_protocol_notification(nmos::fields::nc::oid(resource->data), nc_object_property_changed_event_id, property_changed_event_data);
const auto notification_event = make_control_protocol_notification(web::json::value_of({ notification }));

modify_control_protocol_resource(resources, resource->id, [&](nmos::resource& resource)
{
resource.data[nmos::fields::nc::name(property)] = property_changed_event_data.value;
resource.data[nmos::fields::nc::name(property)] = val;

}, notification_event);
}, make_propertry_changed_event(nmos::fields::nc::oid(resource->data), { { parse_nc_property_id(property_id), nc_property_change_type::type::value_changed, val } }));

return make_control_protocol_message_response(handle, { nc_method_status::ok });
}
Expand Down Expand Up @@ -147,15 +143,11 @@ namespace nmos

if (data.as_array().size() > (size_t)index)
{
const nc_property_changed_event_data property_changed_event_data{ parse_nc_property_id(property_id), nc_property_change_type::type::sequence_item_changed, val, nc_id(index) };
const auto notification = make_control_protocol_notification(nmos::fields::nc::oid(resource->data), nc_object_property_changed_event_id, property_changed_event_data);
const auto notification_event = make_control_protocol_notification(web::json::value_of({ notification }));

modify_control_protocol_resource(resources, resource->id, [&](nmos::resource& resource)
{
resource.data[nmos::fields::nc::name(property)][index] = property_changed_event_data.value;
resource.data[nmos::fields::nc::name(property)][index] = val;

}, notification_event);
}, make_propertry_changed_event(nmos::fields::nc::oid(resource->data), { { parse_nc_property_id(property_id), nc_property_change_type::type::sequence_item_changed, val, nc_id(index) } }));

return make_control_protocol_message_response(handle, { nc_method_status::ok });
}
Expand Down Expand Up @@ -199,17 +191,14 @@ namespace nmos
auto& data = resource->data.at(nmos::fields::nc::name(property));

const nc_id sequence_item_index = data.is_null() ? 0 : nc_id(data.as_array().size());
const nc_property_changed_event_data property_changed_event_data{ parse_nc_property_id(property_id), nc_property_change_type::type::sequence_item_added, val, sequence_item_index };
const auto notification = make_control_protocol_notification(nmos::fields::nc::oid(resource->data), nc_object_property_changed_event_id, property_changed_event_data);
const auto notification_event = make_control_protocol_notification(web::json::value_of({ notification }));

modify_control_protocol_resource(resources, resource->id, [&](nmos::resource& resource)
{
auto& sequence = resource.data[nmos::fields::nc::name(property)];
if (data.is_null()) { sequence = value::array(); }
web::json::push_back(sequence, property_changed_event_data.value);
web::json::push_back(sequence, val);

}, notification_event);
}, make_propertry_changed_event(nmos::fields::nc::oid(resource->data), { { parse_nc_property_id(property_id), nc_property_change_type::type::sequence_item_added, val, sequence_item_index } }));

return make_control_protocol_message_response(handle, { nc_method_status::ok }, sequence_item_index);
}
Expand Down Expand Up @@ -246,16 +235,12 @@ namespace nmos

if (data.as_array().size() > (size_t)index)
{
const nc_property_changed_event_data property_changed_event_data{ parse_nc_property_id(property_id), nc_property_change_type::type::sequence_item_removed, data.as_array().at(index), nc_id(index) };
const auto notification = make_control_protocol_notification(nmos::fields::nc::oid(resource->data), nc_object_property_changed_event_id, property_changed_event_data);
const auto notification_event = make_control_protocol_notification(web::json::value_of({ notification }));

modify_control_protocol_resource(resources, resource->id, [&](nmos::resource& resource)
{
auto& sequence = resource.data[nmos::fields::nc::name(property)].as_array();
sequence.erase(index);

}, notification_event);
}, make_propertry_changed_event(nmos::fields::nc::oid(resource->data), { { parse_nc_property_id(property_id), nc_property_change_type::type::sequence_item_removed, nc_id(index) } }));

return make_control_protocol_message_response(handle, { nc_method_status::ok });
}
Expand Down
18 changes: 17 additions & 1 deletion Development/nmos/control_protocol_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,7 @@ namespace nmos
}

// notification
// See https://specs.amwa.tv/ms-05-01/branches/v1.0.x/docs/Core_Mechanisms.html#notification-messages
// See https://specs.amwa.tv/is-12/branches/v1.0.x/docs/Protocol_messaging.html#notification-message-type
web::json::value make_control_protocol_notification(nc_oid oid, const nc_event_id& event_id, const nc_property_changed_event_data& property_changed_event_data)
{
Expand All @@ -881,7 +882,7 @@ namespace nmos
{ nmos::fields::nc::event_data, details::make_nc_property_changed_event_data(property_changed_event_data) }
});
}
web::json::value make_control_protocol_notification(const web::json::value& notifications)
web::json::value make_control_protocol_notification_message(const web::json::value& notifications)
{
using web::json::value_of;

Expand All @@ -891,6 +892,21 @@ namespace nmos
});
}

// property changed notification event
// See https://specs.amwa.tv/ms-05-01/branches/v1.0.x/docs/Core_Mechanisms.html#the-propertychanged-event
// See https://specs.amwa.tv/ms-05-02/branches/v1.0.x/docs/NcObject.html#propertychanged-event
web::json::value make_propertry_changed_event(nc_oid oid, const std::vector<nc_property_changed_event_data>& property_changed_event_data_list)
{
using web::json::value;

auto notifications = value::array();
for (auto& property_changed_event_data : property_changed_event_data_list)
{
web::json::push_back(notifications, make_control_protocol_notification(oid, nc_object_property_changed_event_id, property_changed_event_data));
}
return make_control_protocol_notification_message(notifications);
}

// error message
// See https://specs.amwa.tv/is-12/branches/v1.0.x/docs/Protocol_messaging.html#error-messages
web::json::value make_control_protocol_error_message(const nc_method_result& method_result, const utility::string_t& error_message)
Expand Down
8 changes: 7 additions & 1 deletion Development/nmos/control_protocol_resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,15 @@ namespace nmos
web::json::value make_control_protocol_subscription_response(const web::json::value& subscriptions);

// notification
// See https://specs.amwa.tv/ms-05-01/branches/v1.0.x/docs/Core_Mechanisms.html#notification-messages
// See https://specs.amwa.tv/is-12/branches/v1.0.x/docs/Protocol_messaging.html#notification-message-type
web::json::value make_control_protocol_notification(nc_oid oid, const nc_event_id& event_id, const nc_property_changed_event_data& property_changed_event_data);
web::json::value make_control_protocol_notification(const web::json::value& notifications);
web::json::value make_control_protocol_notification_message(const web::json::value& notifications);

// property changed notification event
// See https://specs.amwa.tv/ms-05-01/branches/v1.0.x/docs/Core_Mechanisms.html#the-propertychanged-event
// See https://specs.amwa.tv/ms-05-02/branches/v1.0.x/docs/NcObject.html#propertychanged-event
web::json::value make_propertry_changed_event(nc_oid oid, const std::vector<nc_property_changed_event_data>& property_changed_event_data_list);

// error message
// See https://specs.amwa.tv/is-12/branches/v1.0.x/docs/Protocol_messaging.html#error-messages
Expand Down
7 changes: 7 additions & 0 deletions Development/nmos/control_protocol_typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,13 @@ namespace nmos
, sequence_item_index(web::json::value::null())
{}

nc_property_changed_event_data(nc_property_id property_id, nc_property_change_type::type change_type, nc_id sequence_item_index)
: property_id(std::move(property_id))
, change_type(change_type)
, value(web::json::value::null())
, sequence_item_index(sequence_item_index)
{}

auto tied() const -> decltype(std::tie(property_id, change_type, value, sequence_item_index)) { return std::tie(property_id, change_type, value, sequence_item_index); }
friend bool operator==(const nc_property_changed_event_data& lhs, const nc_property_changed_event_data& rhs) { return lhs.tied() == rhs.tied(); }
friend bool operator!=(const nc_property_changed_event_data& lhs, const nc_property_changed_event_data& rhs) { return !(lhs == rhs); }
Expand Down

0 comments on commit ce62865

Please sign in to comment.