|
| 1 | +// This file is adapted from examples/modules/simple/main.cpp, but it runs the module |
| 2 | +// service in a thread which is killed after two seconds. This is a workaround to conform with the |
| 3 | +// expected format of a conan test package executable. |
| 4 | +#include <iostream> |
| 5 | +#include <memory> |
| 6 | +#include <sstream> |
| 7 | +#include <thread> |
| 8 | + |
| 9 | +#include <viam/sdk/common/exception.hpp> |
| 10 | +#include <viam/sdk/common/instance.hpp> |
| 11 | +#include <viam/sdk/common/proto_value.hpp> |
| 12 | +#include <viam/sdk/components/sensor.hpp> |
| 13 | +#include <viam/sdk/config/resource.hpp> |
| 14 | +#include <viam/sdk/log/logging.hpp> |
| 15 | +#include <viam/sdk/module/service.hpp> |
| 16 | +#include <viam/sdk/registry/registry.hpp> |
| 17 | +#include <viam/sdk/resource/reconfigurable.hpp> |
| 18 | + |
| 19 | +using namespace viam::sdk; |
| 20 | + |
| 21 | +// Implements a trivial sensor component, constructed with a ResourceConfig that specifies a |
| 22 | +// "multiplier" value which is then returned as the only sensor reading. |
| 23 | +class MySensor : public Sensor, public Reconfigurable { |
| 24 | + public: |
| 25 | + MySensor(const ResourceConfig& cfg) : Sensor(cfg.name()) { |
| 26 | + this->reconfigure({}, cfg); |
| 27 | + } |
| 28 | + |
| 29 | + static std::vector<std::string> validate(const ResourceConfig&); |
| 30 | + |
| 31 | + void reconfigure(const Dependencies&, const ResourceConfig&) override; |
| 32 | + |
| 33 | + ProtoStruct do_command(const ProtoStruct&) override; |
| 34 | + |
| 35 | + std::vector<GeometryConfig> get_geometries(const ProtoStruct&) override { |
| 36 | + throw Exception("method not supported"); |
| 37 | + } |
| 38 | + |
| 39 | + ProtoStruct get_readings(const ProtoStruct&) override; |
| 40 | + |
| 41 | + private: |
| 42 | + double multiplier_{1.0}; |
| 43 | +}; |
| 44 | + |
| 45 | +std::vector<std::string> MySensor::validate(const ResourceConfig& cfg) { |
| 46 | + auto itr = cfg.attributes().find("multiplier"); |
| 47 | + if (itr != cfg.attributes().end()) { |
| 48 | + const double* multiplier = itr->second.get<double>(); |
| 49 | + if (!multiplier) { |
| 50 | + throw Exception("multiplier must be a number value"); |
| 51 | + } |
| 52 | + |
| 53 | + if (*multiplier == 0.0) { |
| 54 | + throw Exception("multiplier cannot be zero"); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return {}; |
| 59 | +} |
| 60 | + |
| 61 | +void MySensor::reconfigure(const Dependencies&, const ResourceConfig& cfg) { |
| 62 | + auto itr = cfg.attributes().find("multiplier"); |
| 63 | + if (itr != cfg.attributes().end()) { |
| 64 | + const double* multiplier = itr->second.get<double>(); |
| 65 | + if (multiplier) { |
| 66 | + multiplier_ = *multiplier; |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +ProtoStruct MySensor::do_command(const ProtoStruct& command) { |
| 72 | + for (const auto& entry : command) { |
| 73 | + // The VIAM_RESOURCE_LOG macro will associate log messages to the current resource and can |
| 74 | + // be filtered upon |
| 75 | + VIAM_RESOURCE_LOG(info) << "Command entry " << entry.first; |
| 76 | + } |
| 77 | + |
| 78 | + return command; |
| 79 | +} |
| 80 | + |
| 81 | +ProtoStruct MySensor::get_readings(const ProtoStruct&) { |
| 82 | + return {{"signal", multiplier_}}; |
| 83 | +} |
| 84 | + |
| 85 | +int main(int argc, char** argv) try { |
| 86 | + // Every Viam C++ SDK program must have one and only one Instance object which is created before |
| 87 | + // any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed. |
| 88 | + Instance inst; |
| 89 | + |
| 90 | + // Write general log statements with the VIAM_SDK_LOG macro |
| 91 | + VIAM_SDK_LOG(info) << "Starting up simple sensor module"; |
| 92 | + |
| 93 | + Model mysensor_model("viam", "sensor", "mysensor"); |
| 94 | + |
| 95 | + std::shared_ptr<ModelRegistration> mr = std::make_shared<ModelRegistration>( |
| 96 | + API::get<Sensor>(), |
| 97 | + mysensor_model, |
| 98 | + [](Dependencies, ResourceConfig cfg) { return std::make_unique<MySensor>(cfg); }, |
| 99 | + &MySensor::validate); |
| 100 | + |
| 101 | + std::vector<std::shared_ptr<ModelRegistration>> mrs = {mr}; |
| 102 | + auto my_mod = std::make_shared<ModuleService>(argc, argv, mrs); |
| 103 | + |
| 104 | + std::thread t{[&my_mod] { my_mod->serve(); }}; |
| 105 | + std::this_thread::sleep_for(std::chrono::seconds{2}); |
| 106 | + |
| 107 | + t.detach(); |
| 108 | + |
| 109 | + return EXIT_SUCCESS; |
| 110 | +} catch (const viam::sdk::Exception& ex) { |
| 111 | + std::cerr << "main failed with exception: " << ex.what() << "\n"; |
| 112 | + return EXIT_FAILURE; |
| 113 | +} |
0 commit comments