AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
BaseAPLCapabilityAgent.h
Go to the documentation of this file.
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Amazon Software License (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * https://aws.amazon.com/asl/
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #ifndef ACSDK_APLCAPABILITYCOMMON_BASEAPLCAPABILITYAGENT_H_
17 #define ACSDK_APLCAPABILITYCOMMON_BASEAPLCAPABILITYAGENT_H_
18 
19 #include <chrono>
20 #include <map>
21 #include <memory>
22 #include <queue>
23 #include <string>
24 #include <unordered_set>
25 
39 
45 
46 namespace alexaClientSDK {
47 namespace aplCapabilityCommon {
59  , public alexaClientSDK::notifier::Notifier<aplCapabilityCommonInterfaces::APLCapabilityAgentObserverInterface>
60  , public std::enable_shared_from_this<BaseAPLCapabilityAgent> {
61 public:
74  const std::string& avsNamespace,
75  std::shared_ptr<alexaClientSDK::avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface> exceptionSender,
76  std::shared_ptr<alexaClientSDK::avsCommon::utils::metrics::MetricRecorderInterface> metricRecorder,
77  std::shared_ptr<alexaClientSDK::avsCommon::sdkInterfaces::MessageSenderInterface> messageSender,
78  std::shared_ptr<alexaClientSDK::avsCommon::sdkInterfaces::ContextManagerInterface> contextManager,
79  const std::string& APLMaxVersion,
80  std::shared_ptr<alexaClientSDK::aplCapabilityCommonInterfaces::VisualStateProviderInterface>
81  visualStateProvider = nullptr);
82 
86  virtual ~BaseAPLCapabilityAgent() = default;
87 
91  void handleDirectiveImmediately(std::shared_ptr<alexaClientSDK::avsCommon::avs::AVSDirective> directive) override;
92  void preHandleDirective(std::shared_ptr<DirectiveInfo> info) override;
93  void handleDirective(std::shared_ptr<DirectiveInfo> info) override;
94  void cancelDirective(std::shared_ptr<DirectiveInfo> info) override;
97 
100  std::unordered_set<std::shared_ptr<alexaClientSDK::avsCommon::avs::CapabilityConfiguration>>
101  getCapabilityConfigurations() override;
103 
106  void onContextAvailable(const std::string& jsonContext) override;
109 
112  void provideState(
113  const alexaClientSDK::avsCommon::avs::NamespaceAndName& stateProviderName,
114  unsigned int stateRequestToken) override;
116 
117  // @name RequiresShutdown Functions
119  void doShutdown() override;
121 
129  const bool markAsFailed = true) override;
139  const bool result,
140  const std::string& error) override;
144  const std::string& error) override;
145  void recordRenderComplete(const std::chrono::steady_clock::time_point& timestamp) override;
146  void proactiveStateReport() override;
148 
152  virtual bool initialize();
153 
155 
161  void setExecutor(const std::shared_ptr<alexaClientSDK::avsCommon::utils::threading::Executor>& executor);
162 
164 protected:
166  enum class DirectiveType {
169 
172 
175 
178 
181 
183  UNKNOWN
184  };
185 
187  enum class MetricEvent {
190  };
191 
193  enum class MetricActivity {
195  ACTIVITY_RENDER_DOCUMENT,
196 
198  ACTIVITY_RENDER_DOCUMENT_FAIL,
199  };
200 
209  };
210 
212 
216 
220  virtual std::unordered_set<std::shared_ptr<alexaClientSDK::avsCommon::avs::CapabilityConfiguration>>
221  getAPLCapabilityConfigurations(const std::string& APLMaxVersion) = 0;
222 
226  virtual DirectiveType getDirectiveType(std::shared_ptr<DirectiveInfo> info) = 0;
227 
231  virtual const std::string& getConfigurationRootKey() = 0;
232 
236  virtual const std::string& getMetricDataPointName(MetricEvent event) = 0;
237 
241  virtual const std::string& getMetricActivityName(MetricActivity activity) = 0;
242 
249 
253  virtual const bool shouldPackPresentationSessionToAvsEvents() = 0;
254 
260  void handleUnknownDirective(std::shared_ptr<DirectiveInfo> info);
261 
267  void setHandlingCompleted(std::shared_ptr<DirectiveInfo> info);
268 
273  std::shared_ptr<alexaClientSDK::avsCommon::utils::threading::Executor> getExecutor();
274 
275 private:
281  void removeDirective(std::shared_ptr<DirectiveInfo> info);
282 
288  void handleRenderDocumentDirective(std::shared_ptr<DirectiveInfo> info);
289 
295  void handleShowDocumentDirective(std::shared_ptr<DirectiveInfo> info);
296 
302  void handleExecuteCommandDirective(std::shared_ptr<DirectiveInfo> info);
303 
310  void handleDynamicListDataDirective(std::shared_ptr<DirectiveInfo> info, const std::string& sourceType);
311 
318  void executeExecuteCommand(std::shared_ptr<DirectiveInfo> info);
319 
327  void executeDataSourceUpdate(std::shared_ptr<DirectiveInfo> info, const std::string& sourceType);
328 
335  void executeRenderDocument(
336  const std::shared_ptr<alexaClientSDK::avsCommon::avs::CapabilityAgent::DirectiveInfo> info);
337 
346  void executeClearExecuteCommands(
347  const std::string& reason,
349  const bool markAsFailed = true);
350 
357  void executeSendEvent(const std::string& avsNamespace, const std::string& name, const std::string& payload);
358 
363  void executeProvideState(unsigned int stateRequestToken);
364 
368  void executeProactiveStateReport();
369 
379  void executeOnVisualContextAvailable(
382 
387  void addPresentationSessionPayload(rapidjson::Document* document);
388 
393  std::string getPresentationSessionPayload();
394 
401  std::shared_ptr<alexaClientSDK::avsCommon::avs::CapabilityAgent::DirectiveInfo> m_lastDisplayedDirective;
404 
406  std::pair<std::string, std::shared_ptr<alexaClientSDK::avsCommon::avs::CapabilityAgent::DirectiveInfo>>
407  m_lastExecuteCommandTokenAndDirective;
409 
411  std::shared_ptr<alexaClientSDK::avsCommon::sdkInterfaces::MessageSenderInterface> m_messageSender;
412 
415 
417  std::shared_ptr<alexaClientSDK::avsCommon::sdkInterfaces::ContextManagerInterface> m_contextManager;
418 
420  std::shared_ptr<alexaClientSDK::aplCapabilityCommonInterfaces::VisualStateProviderInterface> m_visualStateProvider;
421 
423  std::queue<std::tuple<std::string, std::string, std::string>> m_events;
424 
426  const std::string m_APLVersion;
427 
433  void startMetricsEvent(MetricEvent metricEvent);
434 
442  void endMetricsEvent(
443  MetricEvent metricEvent,
444  MetricActivity activity,
445  const std::chrono::steady_clock::time_point& timestamp);
446 
452  void resetMetricsEvent(MetricEvent metricEvent);
453 
455  std::shared_ptr<alexaClientSDK::avsCommon::utils::metrics::MetricRecorderInterface> m_metricRecorder;
456 
458  std::mutex m_MetricsRecorderMutex;
459 
461  std::map<MetricEvent, std::chrono::steady_clock::time_point> m_currentActiveTimePoints;
462 
464  std::string m_lastReportedState;
465 
467  std::chrono::time_point<std::chrono::steady_clock> m_lastReportTime;
468 
470  std::chrono::milliseconds m_minStateReportInterval{};
471 
473  std::chrono::milliseconds m_stateReportCheckInterval{};
474 
476  bool m_stateReportPending;
477 
480 
482  bool m_documentRendered;
483 
486 
488  std::chrono::steady_clock::time_point m_renderReceivedTime;
489 
491  const std::string m_avsNamespace;
492 
494  const alexaClientSDK::avsCommon::avs::NamespaceAndName m_visualContextHeader;
495 
497  std::shared_ptr<alexaClientSDK::avsCommon::utils::threading::Executor> m_executor;
498 };
499 
500 } // namespace aplCapabilityCommon
501 } // namespace alexaClientSDK
502 
503 #endif // ACSDK_APLCAPABILITYCOMMON_BASEAPLCAPABILITYAGENT_H_
Definition: CapabilityAgent.h:47
DirectiveType
Concrete Implementation Facing Interfaces.
Definition: BaseAPLCapabilityAgent.h:166
void processRenderDocumentResult(const aplCapabilityCommonInterfaces::PresentationToken &token, const bool result, const std::string &error) override
Definition: NamespaceAndName.h:32
::std::string string
Definition: gtest-port.h:1097
void preHandleDirective(std::shared_ptr< DirectiveInfo > info) override
ContextRequestError
Definition: ContextRequesterInterface.h:40
unsigned int ContextRequestToken
Alias for the token used to identify a context request.
Definition: ContextRequestToken.h:23
void sendRuntimeErrorEvent(const aplCapabilityCommonInterfaces::aplEventPayload::RuntimeError &errors) override
virtual alexaClientSDK::avsCommon::avs::DirectiveHandlerConfiguration getAPLDirectiveConfiguration() const =0
Template Methods to be implemented in Concrete Implementations.
void processExecuteCommandsResult(const aplCapabilityCommonInterfaces::PresentationToken &token, aplCapabilityCommonInterfaces::APLCommandExecutionEvent event, const std::string &error) override
void setHandlingCompleted(std::shared_ptr< DirectiveInfo > info)
void handleDirective(std::shared_ptr< DirectiveInfo > info) override
void handleUnknownDirective(std::shared_ptr< DirectiveInfo > info)
static std::shared_ptr< avsCommon::utils::metrics::MetricRecorderInterface > metricRecorder
Metric recorder shared ptr.
Definition: BaseAPLCapabilityAgentTest.cpp:261
virtual DirectiveType getDirectiveType(std::shared_ptr< DirectiveInfo > info)=0
virtual std::unordered_set< std::shared_ptr< alexaClientSDK::avsCommon::avs::CapabilityConfiguration > > getAPLCapabilityConfigurations(const std::string &APLMaxVersion)=0
Generic implementation of NotifierInterface.
Definition: Notifier.h:38
std::string PresentationToken
Alias for the token used by APL directives.
Definition: PresentationToken.h:22
void provideState(const alexaClientSDK::avsCommon::avs::NamespaceAndName &stateProviderName, unsigned int stateRequestToken) override
void onActiveDocumentChanged(const aplCapabilityCommonInterfaces::PresentationToken &token, const alexaClientSDK::aplCapabilityCommonInterfaces::PresentationSession &session) override
void clearExecuteCommands(const aplCapabilityCommonInterfaces::PresentationToken &token=std::string(), const bool markAsFailed=true) override
void onContextFailure(const alexaClientSDK::avsCommon::sdkInterfaces::ContextRequestError error) override
std::unordered_map< directiveRoutingRule::DirectiveRoutingRule, BlockingPolicy > DirectiveHandlerConfiguration
Definition: DirectiveHandlerConfiguration.h:32
void sendDataSourceFetchRequestEvent(const aplCapabilityCommonInterfaces::aplEventPayload::DataSourceFetch &fetchPayload) override
Timer to schedule task for delayed and periodic execution.
Definition: Timer.h:39
MetricActivity
Enumeration of timer metric activity names that could be emitted.
Definition: BaseAPLCapabilityAgent.h:193
std::shared_ptr< alexaClientSDK::avsCommon::utils::threading::Executor > getExecutor()
std::string skillId
SkillId field name.
Definition: BaseAPLCapabilityAgent.h:206
void onContextAvailable(const std::string &jsonContext) override
Directive contains one or multiple APL commands to be executed.
void onVisualContextAvailable(avsCommon::sdkInterfaces::ContextRequestToken requestToken, const aplCapabilityCommonInterfaces::aplEventPayload::VisualContext &context) override
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
void cancelDirective(std::shared_ptr< DirectiveInfo > info) override
void recordRenderComplete(const std::chrono::steady_clock::time_point &timestamp) override
void sendUserEvent(const aplCapabilityCommonInterfaces::aplEventPayload::UserEvent &eventPayload) override
alexaClientSDK::avsCommon::avs::DirectiveHandlerConfiguration getConfiguration() const override
std::unordered_set< std::shared_ptr< alexaClientSDK::avsCommon::avs::CapabilityConfiguration > > getCapabilityConfigurations() override
virtual const std::string & getMetricActivityName(MetricActivity activity)=0
Directive indicates that a previously received document should be now displayed.
virtual PresentationSessionFieldNames getPresentationSessionFieldNames()=0
void setExecutor(const std::shared_ptr< alexaClientSDK::avsCommon::utils::threading::Executor > &executor)
Tests Facing interfaces.
BaseAPLCapabilityAgent(const std::string &avsNamespace, std::shared_ptr< alexaClientSDK::avsCommon::sdkInterfaces::ExceptionEncounteredSenderInterface > exceptionSender, std::shared_ptr< alexaClientSDK::avsCommon::utils::metrics::MetricRecorderInterface > metricRecorder, std::shared_ptr< alexaClientSDK::avsCommon::sdkInterfaces::MessageSenderInterface > messageSender, std::shared_ptr< alexaClientSDK::avsCommon::sdkInterfaces::ContextManagerInterface > contextManager, const std::string &APLMaxVersion, std::shared_ptr< alexaClientSDK::aplCapabilityCommonInterfaces::VisualStateProviderInterface > visualStateProvider=nullptr)
std::string presentationSessionId
Presentation session id field name.
Definition: BaseAPLCapabilityAgent.h:208
APLCommandExecutionEvent
Enumeration of APL Command Execution Events that can be reported to the APL capability agent...
Definition: APLCommandExecutionEvent.h:27
virtual const std::string & getMetricDataPointName(MetricEvent event)=0
void handleDirectiveImmediately(std::shared_ptr< alexaClientSDK::avsCommon::avs::AVSDirective > directive) override
MetricEvent
Enumeration of timer metrics events that could be emitted.
Definition: BaseAPLCapabilityAgent.h:187

AlexaClientSDK 3.0.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0