ExampleDevice.cpp
Go to the documentation of this file.
2 #include <swarmio/data/Helper.h>
3 
4 using namespace swarmio;
5 using namespace swarmio::simulator;
6 
8  : MemberProfile(endpoint)
9 {
10  // Add message counter to telemetry schema
11  data::discovery::Field field;
12  field.set_type(data::discovery::Type::UINT);
13  GetTelemetryService().SetFieldDefinitionForKey("incoming_messages", field, true);
14 
15  // Finish construction
17 }
18 
19 bool ExampleDevice::ReceiveMessage(const Node* sender, const data::Message* message)
20 {
21  // Increment message counter
23 
24  // Update message counter telemetry key
25  data::Variant value;
26  value.set_uint_value(_messageCounter);
27  GetTelemetryService().SetValue("incoming_messages", value);
28 
29  // Leave message unhandled
30  return false;
31 }
32 
33 void ExampleDevice::AddConstantTelemetryValue(const std::string& key, const data::Variant& value, bool includeInStatus)
34 {
36  GetTelemetryService().SetValue(key, value);
37 }
38 
40 {
41  _eventHandlers.push_back(eventHandler);
42  _eventService.RegisterHandler(eventHandler->GetName(), eventHandler);
43 }
44 
46 {
47  _parameters.push_back(parameter);
48  _keyvalueService.RegisterTarget(parameter->GetPath(), parameter);
49 }
50 
52 {
53  for (auto handler : _eventHandlers)
54  {
55  _eventService.UnregisterHandler(handler->GetName());
56  }
57  for (auto parameter : _parameters)
58  {
59  _keyvalueService.UnregisterTarget(parameter->GetPath());
60  }
61 }
void RegisterTarget(const std::string &path, Target *target)
Register a new Target with the specified path.
A special event handler that serves discovery information and ignores the event itself.
const std::string & GetName() const
Get the name of the event.
void UnregisterTarget(const std::string &path)
Unregister a Target.
void SetValue(const std::string &key, const data::Variant &value)
Add or update a value in the local telemetry cache.
void RegisterHandler(const std::string &name, Handler *handler)
Subscribe to events.
void FinishConstruction()
Called when the last constructor has finished its job.
Definition: Mailbox.h:46
std::list< InMemoryParameter * > _parameters
Parameters.
Definition: ExampleDevice.h:36
void SetFieldDefinitionForKey(const std::string &key, const data::discovery::Field &field, bool includeInStatus)
Add field to the schema.
A strongly typed in-memory parameter target that can be made read-only.
const std::string & GetPath() const
Get the resource path of the parameter.
void AddFauxEventHandler(FauxEventHandler *eventHandler)
Add an event handler and register it.
ExampleDevice(Endpoint *endpoint)
Construct a new Example Device object.
void AddInMemoryParameter(InMemoryParameter *parameter)
Add an In-Memory Parameter and register it.
Abstract base class for Endpoint implementations.
Definition: Endpoint.h:25
swarmio::services::telemetry::Service & GetTelemetryService()
Get a reference for the Telemetry service.
Definition: Profile.h:81
static discovery::Field GetFieldDescriptor(const Variant &value, bool includeArraySizes=false)
Get the schema for an existing map.
Definition: Helper.cpp:91
swarmio::services::event::Service _eventService
Event service.
Definition: MemberProfile.h:28
swarmio::services::keyvalue::Service _keyvalueService
Key-Value service.
Definition: MemberProfile.h:22
std::list< FauxEventHandler * > _eventHandlers
Event handlers.
Definition: ExampleDevice.h:30
uint32_t _messageCounter
Number of incoming messages.
Definition: ExampleDevice.h:24
~ExampleDevice() override
Destroy the Example Device object.
Represents a Node the Endpoint knows about and can send messages to.
void UnregisterHandler(const std::string &name)
Unsubscribe from events.
void AddConstantTelemetryValue(const std::string &key, const data::Variant &value, bool includeInStatus=false)
Add a constant telemetry value.
virtual bool ReceiveMessage(const Node *sender, const data::Message *message) override
Delivery point of all messages.


swarmros
Author(s):
autogenerated on Fri Apr 3 2020 03:42:48