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

use sensor API #2

Merged
merged 5 commits into from
Dec 15, 2023
Merged
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
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ include(FetchContent)
FetchContent_Declare(
viam-cpp-sdk
GIT_REPOSITORY https://github.com/viamrobotics/viam-cpp-sdk.git
# temporary pin -- use release when possible
GIT_TAG 46ba29fe17a6887fd13deb4ef089a155e9730c55
GIT_TAG 2e5fd316ce43410fb59778220fb53128d613892a
# SOURCE_DIR ${CMAKE_SOURCE_DIR}/../viam-cpp-sdk
CMAKE_ARGS -DVIAMCPPSDK_USE_DYNAMIC_PROTOS=ON
FIND_PACKAGE_ARGS
Expand Down
41 changes: 10 additions & 31 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,46 +1,25 @@
#include <viam/sdk/components/generic/generic.hpp>
#include <viam/sdk/components/sensor/sensor.hpp>
#include <viam/sdk/module/service.hpp>

#include "wifi.hpp"

using namespace viam::sdk;

class MyModule : public Generic {
class WifiSensor : public Sensor {
public:
MyModule(Dependencies deps, ResourceConfig cfg) : Generic(cfg.name()) {}
AttributeMap do_command(AttributeMap command) { return read_wireless(); }
std::vector<GeometryConfig> get_geometries() { return std::vector<GeometryConfig>(); }
WifiSensor(Dependencies deps, ResourceConfig cfg) : Sensor(cfg.name()) {}
AttributeMap do_command(const AttributeMap& command) { return {}; }
std::vector<GeometryConfig> get_geometries(const AttributeMap& extra) { return std::vector<GeometryConfig>(); }
AttributeMap get_readings(const AttributeMap& extra) { return read_wireless(); }
};

int main(int argc, char **argv) {
if (argc < 2) {
throw std::invalid_argument("Pass socket path as first argument");
}
std::string socket_addr = argv[1];

set_logger_severity_from_args(argc, argv);

SignalManager signals;

std::shared_ptr<ModelRegistration> mr = std::make_shared<ModelRegistration>(
ResourceType("MyModule"), Generic::static_api(), Model("viam", "wifi", "cpp"),
[](Dependencies deps, ResourceConfig cfg) { return std::make_shared<MyModule>(deps, cfg); }
Sensor::static_api(), Model("viam", "wifi", "cpp"),
[](Dependencies deps, ResourceConfig cfg) { return std::make_shared<WifiSensor>(deps, cfg); }
);

Registry::register_model(mr);

// The `ModuleService_` must outlive the Server, so the declaration order here matters.
auto my_mod = std::make_shared<ModuleService_>(socket_addr);
auto server = std::make_shared<Server>();

my_mod->add_model_from_registry(server, mr->api(), mr->model());
my_mod->start(server);
std::cout << "Module serving model " << mr->model().to_string() << ", listening on "
<< socket_addr << std::endl;

server->start();
int sig = 0;
auto result = signals.wait(&sig);
server->shutdown();
auto service = std::make_shared<ModuleService>(argc, argv, std::vector<std::shared_ptr<ModelRegistration>>{mr});
service->serve();
return 0;
}
Loading