Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSDK-4713 RSDK-5626 MovementSensor #171

Merged
merged 23 commits into from
Nov 3, 2023
Merged

RSDK-4713 RSDK-5626 MovementSensor #171

merged 23 commits into from
Nov 3, 2023

Conversation

abe-winter
Copy link
Member

@abe-winter abe-winter commented Nov 1, 2023

What changed

  • implement MovementSensor component (mostly copying from Zack's branch)
  • incidental: fix issue in handler map where only 1st model implementing an API is registered
  • add (privately for now) ResourceRegistration2 a one-liner to implement a resource registration -- this is in movementsensor.cpp for now but could be dropped into registry.hpp if useful in the future

Copy link
Member

@acmorrow acmorrow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love a good boilerplate elimination, and I have lots of thoughts. Feel free to take or leave what you find useful.

One thing I would suggest, is to split this into two reviews. One to just add the movement sensor under the standard registration mechanics, and then a follow-up to refactor registration if that ends up being considered worthwhile.

src/viam/sdk/registry/registry.hpp Outdated Show resolved Hide resolved
typename MyResourceServer,
typename MyProtoService,
typename MyResourceReg>
class ResourceRegistration2 : public ResourceRegistration {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's come up with a better name for this than ...2. If you intend for this to be the canonical way, maybe this should be ResourceRegistration and the existing ResourceRegistration should be ResourceRegistrationBase.

src/viam/sdk/components/movementsensor/client.hpp Outdated Show resolved Hide resolved
if (!sd) {
throw std::runtime_error("Unable to get service descriptor");
}
return std::make_shared<MyResourceReg>(sd);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if you could get rid of the CRTP if this wasn't a static member of the template, but a free function. Something like:

template <typename ClientType, typename ServerType, typename ProtoServiceType>
std::shared_ptr<ResourceRegistration> make_resource_registration() {
    // ...
    return std::make_shared<ResourceRegistration2<ClientType, ServerType, ProtoServiceType>>(sd);
}

Or perhaps it could be a member function of Registry:

class Registry {
public:
...
template<typename T, typename TClient, typename TServer>
void register_resource() { ... };

And then those init blocks would look like:

namespace {
bool init() {
    Registry::register_resource<MovementSensor, MovementSensorClient, MovementSensorServer>();
    return true;
};

// NOLINTNEXTLINE(cert-err58-cpp)
const bool inited = init();
}  // namespace

typename MyResourceServer,
typename MyProtoService,
typename MyResourceReg>
class ResourceRegistration2 : public ResourceRegistration {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest making this class final.

src/viam/sdk/registry/registry.hpp Outdated Show resolved Hide resolved
src/viam/sdk/registry/registry.hpp Outdated Show resolved Hide resolved
/// @brief subclass of ResourceRegistration with templatized defaults
template <typename MyResourceClient,
typename MyResourceServer,
typename MyProtoService,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MyProtoService type parameter could probably go away if all ResourceServer types had a required proto_service_type typedef. They all inherit from that type so it would make sense:

class MLModelServiceServer : public ResourceServer,
                             public ::viam::service::mlmodel::v1::MLModelService::Service {
   public:
       using proto_service_type = ::viam::service::mlmodel::v1::MLModelService::Service;
   ...
};

Then you could just say p->FindServiceByName(MyResourceServer::proto_service_type::service_full_name());, and then this template would have only two types: the Client and the Server (if you get rid of the CRTP).

Of course, that would require attaching that typedef to all server classes.

src/viam/sdk/components/movementsensor/movementsensor.cpp Outdated Show resolved Hide resolved
@acmorrow
Copy link
Member

acmorrow commented Nov 2, 2023

Also, @stuqdog, I know there are some plans afoot to eliminate the static initialization. There have also been some thoughts about making a cleaner separation between the client facing types and the server (e.g. module supporting) types. The current registration model requires that one part of the code (the registration type) know of both the Server and Client types, because there must be one ResourceRegistration subtype that can produce both types. That might be another thing to consider refactoring if/when the auto-registration is addressed.

@abe-winter abe-winter changed the title MovementSensor and boilerplate wrappers RSDK-4713 MovementSensor Nov 2, 2023
@abe-winter abe-winter changed the title RSDK-4713 MovementSensor RSDK-4713 RSDK-5626 MovementSensor Nov 2, 2023
@abe-winter abe-winter marked this pull request as ready for review November 2, 2023 22:36
@abe-winter abe-winter requested a review from a team as a code owner November 2, 2023 22:36
Copy link
Member

@zaporter-work zaporter-work left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Comment on lines +32 to +35
struct compassheading {
/// A number from 0-359 where 0 is North, 90 is East, 180 is South, and 270 is West
double value;
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not hyped that this lives on the movement sensor object, but probably fine

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

want me to change to typedef?

Suggested change
struct compassheading {
/// A number from 0-359 where 0 is North, 90 is East, 180 is South, and 270 is West
double value;
};
/// A number from 0-359 where 0 is North, 90 is East, 180 is South, and 270 is West
typedef double compassheading;

Copy link
Member

@zaporter-work zaporter-work Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong opinion. typedef is probably better but either is fine. Feel free to resolve

Comment on lines 27 to 31
void throw_status(const ::grpc::Status& status) {
if (!status.ok()) {
throw std::runtime_error(status.error_message());
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

@acmorrow acmorrow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with some small changes.

@acmorrow
Copy link
Member

acmorrow commented Nov 3, 2023

I only really looked at the resource registration changes, so my LGTM is limited to that. Overall, I think resource registration needs to be refactored and I expect that the final result will look more like a template member on Registry which does the work that ResourceRegistration2 does now.

Copy link
Member

@stuqdog stuqdog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! A couple small comments/questions but otherwise looks great, thanks for doing this. No need to treat my re-review as blocking.

src/viam/sdk/components/movement_sensor/client.cpp Outdated Show resolved Hide resolved
src/viam/sdk/components/movement_sensor/client.cpp Outdated Show resolved Hide resolved
src/viam/sdk/components/movement_sensor/client.hpp Outdated Show resolved Hide resolved
src/viam/sdk/components/movement_sensor/client.hpp Outdated Show resolved Hide resolved
src/viam/sdk/components/movement_sensor/server.cpp Outdated Show resolved Hide resolved
src/viam/sdk/components/movement_sensor/server.hpp Outdated Show resolved Hide resolved
src/viam/sdk/components/movement_sensor/server.hpp Outdated Show resolved Hide resolved
@benjirewis benjirewis removed their request for review November 3, 2023 15:27
@benjirewis
Copy link
Member

Potentially an unhelpful too-many-cooks situation if I review here, and my review is mostly qs 😆. I'll eventually follow up offline about those qs instead.

@zaporter-work zaporter-work mentioned this pull request Nov 3, 2023
@abe-winter abe-winter merged commit cce39e2 into main Nov 3, 2023
2 checks passed
@abe-winter abe-winter deleted the movement-sensor branch November 3, 2023 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants