Skip to content

Instantly share code, notes, and snippets.

@bobvanluijt
Last active February 3, 2016 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobvanluijt/73e5e8b6cd95e8314ed0 to your computer and use it in GitHub Desktop.
Save bobvanluijt/73e5e8b6cd95e8314ed0 to your computer and use it in GitHub Desktop.
Example chunk C++ for sending an action via Weave
[...]
/**
* Google Weave example by @bobvanluijt
* This example shows the private function 'OnBobCommand' which is proccesed when the deamon receives a request
*/
private:
void OnBobsCommand(const std::weak_ptr<weave::Command>& command) {
/**
* Chunk: processing the received command
*/
auto cmd = command.lock();
if (!cmd)
return;
LOG(INFO) << "received command: " << cmd->GetName(); // output the received command
/**
* Chunk: processing the 'name' param
*/
const auto& params = cmd->GetParameters(); // get the params
std::string name; // get the string 'name'
/**
* Chunk: handling error if param is not found
*/
if (!params.GetString("name", &name)) { // 'no name' is set, abort
weave::ErrorPtr error;
weave::Error::AddTo(&error, FROM_HERE, "invalid_parameter_value", "Name is missing");
cmd->Abort(error.get(), nullptr);
return;
}
/**
* Chunk: processing device action
*/
base::DictionaryValue result;
result.SetString("reply", "Hello " + name); // output 'Hello Bob'
cmd->Complete(result, nullptr);
/**
* Chunk: process logging
*/
LOG(INFO) << cmd->GetName() << " command finished: " << result; // create log
}
/**
* Chunk: process device state
*/
weave::Device* device_{nullptr};
weave::provider::TaskRunner* task_runner_{nullptr};
base::WeakPtrFactory<SampleHandler> weak_ptr_factory_{this};
};
[...]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment