AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
Alert.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 Apache License, Version 2.0 (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  * http://aws.amazon.com/apache2.0/
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 ALEXA_CLIENT_SDK_ACSDKALERTS_INCLUDE_ACSDKALERTS_ALERT_H_
17 #define ALEXA_CLIENT_SDK_ACSDKALERTS_INCLUDE_ACSDKALERTS_ALERT_H_
18 
21 
28 
30 
31 #include <map>
32 #include <memory>
33 #include <mutex>
34 #include <string>
35 #include <unordered_map>
36 #include <vector>
37 
38 #include <rapidjson/document.h>
39 
40 namespace alexaClientSDK {
41 namespace acsdkAlerts {
42 
51 class Alert
53  , public std::enable_shared_from_this<Alert> {
54 public:
58  enum class State {
60  UNSET,
62  SET,
64  READY,
66  ACTIVATING,
68  ACTIVE,
70  SNOOZING,
72  SNOOZED,
74  STOPPING,
76  STOPPED,
78  COMPLETED
79  };
80 
84  enum class StopReason {
86  UNSET,
88  AVS_STOP,
90  LOCAL_STOP,
92  SHUTDOWN,
94  LOG_OUT
95  };
96 
100  struct Asset {
104  Asset() = default;
105 
112  Asset(const std::string& id, const std::string& url) : id{id}, url{url} {
113  }
114 
119  };
120 
128  AssetConfiguration() : loopPause{std::chrono::milliseconds{0}} {
129  }
130 
132  std::unordered_map<std::string, Asset> assets;
137  std::vector<std::string> assetPlayOrderItems;
141  std::chrono::milliseconds loopPause;
142  };
143 
149  struct StaticData {
153  StaticData() : dbId{0} {
154  }
157 
159  int dbId;
160  };
161 
166  struct DynamicData {
170  DynamicData() : state{State::SET}, loopCount{0}, hasRenderingFailed{false}, originalTime(""), label("") {
171  }
174 
177 
180 
183 
186 
189 
192  };
193 
197  enum class ParseFromJsonStatus {
199  OK,
201  MISSING_REQUIRED_PROPERTY,
203  INVALID_VALUE
204  };
205 
209  struct ContextInfo {
217  ContextInfo(const std::string& token, const std::string& type, const std::string& scheduledTime_ISO_8601) :
218  token{token},
219  type{type},
220  scheduledTime_ISO_8601{scheduledTime_ISO_8601} {
221  }
222 
229  };
230 
238  validateOriginalTimeString(const std::string& originalTimeStr);
239 
247 
254  static std::string stateToString(Alert::State state);
255 
263 
271 
275  Alert(
276  std::function<std::pair<std::unique_ptr<std::istream>, const avsCommon::utils::MediaType>()>
277  defaultAudioFactory,
278  std::function<std::pair<std::unique_ptr<std::istream>, const avsCommon::utils::MediaType>()> shortAudioFactory,
279  std::shared_ptr<settings::DeviceSettingsManager> settingsManager);
280 
286  virtual std::string getTypeName() const = 0;
287 
293  std::function<std::pair<std::unique_ptr<std::istream>, const avsCommon::utils::MediaType>()>
294  getDefaultAudioFactory() const;
295 
301  std::function<std::pair<std::unique_ptr<std::istream>, const avsCommon::utils::MediaType>()> getShortAudioFactory()
302  const;
303 
310 
320  const std::string& reason = "") const;
321 
323 
331  ParseFromJsonStatus parseFromJson(const rapidjson::Value& payload, std::string* errorMessage);
332 
338  void setRenderer(std::shared_ptr<renderer::RendererInterface> renderer);
339 
347 
351  void activate();
352 
358  void deactivate(StopReason reason);
359 
368  bool updateScheduledTime(const std::string& newScheduledTime);
369 
376  bool snooze(const std::string& updatedScheduledTime);
377 
385 
389  bool setStateActive();
390 
394  void reset();
395 
401  std::string getToken() const;
402 
408  int64_t getScheduledTime_Unix() const;
409 
416 
422  std::chrono::system_clock::time_point getScheduledTime_Utc_TimePoint() const;
423 
430 
437 
444 
450  Alert::State getState() const;
451 
457  StopReason getStopReason() const;
458 
466  void getAlertData(StaticData* staticData, DynamicData* dynamicData) const;
467 
475  bool setAlertData(StaticData* staticData, DynamicData* dynamicData);
476 
482  int getId() const;
483 
491  bool isPastDue(int64_t currentUnixTime, std::chrono::seconds timeLimit);
492 
498  int getLoopCount() const;
499 
505  std::chrono::milliseconds getLoopPause() const;
506 
514 
521 
527  bool setAssetConfiguration(const AssetConfiguration& assetConfiguration);
528 
532  void printDiagnostic();
533 
534 private:
540  std::string getTokenLocked() const;
541 
547  Alert::State getStateLocked() const;
548 
554  int64_t getScheduledTime_UnixLocked() const;
555 
561  std::string getScheduledTime_ISO_8601Locked() const;
562 
568  std::chrono::system_clock::time_point getScheduledTime_Utc_TimePointLocked() const;
569 
576  const;
577 
583  avsCommon::utils::Optional<std::string> getLabelLocked() const;
584 
594  const std::string& reason = "") const;
595 
599  void startRenderer();
600 
604  void startRendererLocked();
605 
609  void onMaxTimerExpiration();
610 
616  bool isAlertPausedLocked() const;
617 
621  void notifyObserver(acsdkAlertsInterfaces::AlertObserverInterface::State state, const std::string& reason = "")
622  const;
623 
625  mutable std::mutex m_mutex;
627  StaticData m_staticData;
629  DynamicData m_dynamicData;
631  StopReason m_stopReason;
633  avsCommon::avs::FocusState m_focusState;
635  avsCommon::avs::MixingBehavior m_mixingBehavior;
637  bool m_hasTimerExpired;
639  avsCommon::utils::timing::Timer m_maxLengthTimer;
643  std::shared_ptr<renderer::RendererInterface> m_renderer;
645  const std::function<std::pair<std::unique_ptr<std::istream>, const avsCommon::utils::MediaType>()>
646  m_defaultAudioFactory;
648  const std::function<std::pair<std::unique_ptr<std::istream>, const avsCommon::utils::MediaType>()>
649  m_shortAudioFactory;
651  std::shared_ptr<settings::DeviceSettingsManager> m_settingsManager;
654  bool m_focusChangedDuringAlertActivation;
656  bool m_startRendererAgainAfterFullStop;
657 };
658 
666  bool operator()(const std::shared_ptr<Alert>& lhs, const std::shared_ptr<Alert>& rhs) const {
667  if (lhs->getScheduledTime_Unix() == rhs->getScheduledTime_Unix()) {
668  return (lhs->getToken() < rhs->getToken());
669  }
670 
671  return (lhs->getScheduledTime_Unix() < rhs->getScheduledTime_Unix());
672  }
673 };
674 
682 inline std::ostream& operator<<(std::ostream& stream, const Alert::State& state) {
683  return stream << Alert::stateToString(state);
684 }
685 
693 inline std::ostream& operator<<(std::ostream& stream, const Alert::StopReason& reason) {
694  return stream << Alert::stopReasonToString(reason);
695 }
696 
704 inline std::ostream& operator<<(std::ostream& stream, const Alert::ParseFromJsonStatus& status) {
705  return stream << Alert::parseFromJsonStatusToString(status);
706 }
707 
708 } // namespace acsdkAlerts
709 } // namespace alexaClientSDK
710 
711 #endif // ALEXA_CLIENT_SDK_ACSDKALERTS_INCLUDE_ACSDKALERTS_ALERT_H_
std::string scheduledTime_ISO_8601
The time, in ISO-8601 format, when this alert should activate.
Definition: Alert.h:228
static std::string stateToString(Alert::State state)
static std::string stopReasonToString(Alert::StopReason stopReason)
MixingBehavior
Definition: MixingBehavior.h:25
std::string originalTime
An attribute representing the local time when the alert was originally set.
Definition: Alert.h:185
std::chrono::milliseconds loopPause
The pause time, in milliseconds, that should be taken between each loop of asset rendering.
Definition: Alert.h:141
avsCommon::utils::Optional< acsdkAlertsInterfaces::AlertObserverInterface::OriginalTime > getOriginalTime() const
std::string label
An attribute representing the content of the alert.
Definition: Alert.h:188
The alert is active, but has been asked to snooze.
std::string id
The id of the asset.
Definition: Alert.h:116
The alert has completed on its own, without user interaction.
bool hasRenderingFailed
A flag to capture if rendering any of asset urls failed.
Definition: Alert.h:182
bool setAssetConfiguration(const AssetConfiguration &assetConfiguration)
avsCommon::utils::Optional< std::string > getLabel() const
std::string token
The AVS token for the alert.
Definition: Alert.h:156
void setRenderer(std::shared_ptr< renderer::RendererInterface > renderer)
StopReason
Definition: Alert.h:84
::std::string string
Definition: gtest-port.h:1097
std::function< std::pair< std::unique_ptr< std::istream >, const avsCommon::utils::MediaType >)> getShortAudioFactory() const
acsdkAlertsInterfaces::AlertObserverInterface::AlertInfo createAlertInfo(acsdkAlertsInterfaces::AlertObserverInterface::State state, const std::string &reason="") const
std::unordered_map< std::string, Asset > assets
A map of the custom assets, mapping from asset.id to the asset itself.
Definition: Alert.h:132
Alert::ContextInfo getContextInfo() const
std::vector< std::string > assetPlayOrderItems
Definition: Alert.h:137
AssetConfiguration assetConfiguration
The assets associated with this alert.
Definition: Alert.h:191
AssetConfiguration getAssetConfiguration() const
void setFocusState(avsCommon::avs::FocusState focusState, avsCommon::avs::MixingBehavior behavior)
ParseFromJsonStatus parseFromJson(const rapidjson::Value &payload, std::string *errorMessage)
MediaType
Definition: MediaType.h:29
FocusState
Definition: FocusState.h:29
The alert is active, but is now stopping due to user interaction.
std::chrono::system_clock::time_point getScheduledTime_Utc_TimePoint() const
The alert is set and is expected to become active at some point in the future.
The renderer is now stopped due to a snooze request.
std::string token
The AVS token identifying this alert.
Definition: Alert.h:224
std::ostream & operator<<(std::ostream &stream, const Alert::State &state)
Definition: Alert.h:682
acsdkAlertsInterfaces::AlertObserverInterface::Type getType() const
static avsCommon::utils::Optional< acsdkAlertsInterfaces::AlertObserverInterface::OriginalTime > validateOriginalTimeString(const std::string &originalTimeStr)
bool snooze(const std::string &updatedScheduledTime)
std::string getBackgroundAssetId() const
bool Value(const T &value, M matcher)
Definition: gmock-matchers.h:4347
std::chrono::milliseconds getLoopPause() const
std::string type
The type of this alert.
Definition: Alert.h:226
void onRendererStateChange(renderer::RendererObserverInterface::State state, const std::string &reason) override
Timer to schedule task for delayed and periodic execution.
Definition: Timer.h:39
std::string backgroundAssetId
The background asset id, if specified by AVS.
Definition: Alert.h:139
virtual std::string getTypeName() const =0
bool operator()(const std::shared_ptr< Alert > &lhs, const std::shared_ptr< Alert > &rhs) const
Definition: Alert.h:666
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
Rendering has been initiated, but is not yet perceivable from a user&#39;s point of view.
bool updateScheduledTime(const std::string &newScheduledTime)
The alert is ready to activate, and is waiting for the channel to be acquired.
std::string getScheduledTime_ISO_8601() const
bool isPastDue(int64_t currentUnixTime, std::chrono::seconds timeLimit)
type
Definition: upload.py:443
std::string url
The url of the asset.
Definition: Alert.h:118
int dbId
The database id for the alert.
Definition: Alert.h:159
ParseFromJsonStatus
Definition: Alert.h:197
bool setAlertData(StaticData *staticData, DynamicData *dynamicData)
Rendering has been initiated, and is perceivable from a user&#39;s point of view.
void getAlertData(StaticData *staticData, DynamicData *dynamicData) const
ContextInfo(const std::string &token, const std::string &type, const std::string &scheduledTime_ISO_8601)
Definition: Alert.h:217
Alert(std::function< std::pair< std::unique_ptr< std::istream >, const avsCommon::utils::MediaType >()> defaultAudioFactory, std::function< std::pair< std::unique_ptr< std::istream >, const avsCommon::utils::MediaType >()> shortAudioFactory, std::shared_ptr< settings::DeviceSettingsManager > settingsManager)
int loopCount
The number of times the sequence of assets should be rendered.
Definition: Alert.h:179
void deactivate(StopReason reason)
The alert has stopped in response to user interaction.
avsCommon::utils::timing::TimePoint timePoint
A TimePoint reflecting the time when the alert should become active.
Definition: Alert.h:176
State state
The state of the alert.
Definition: Alert.h:173
static avsCommon::utils::Optional< std::string > validateLabelString(const std::string &label)
std::function< std::pair< std::unique_ptr< std::istream >, const avsCommon::utils::MediaType >)> getDefaultAudioFactory() const
void setObserver(acsdkAlertsInterfaces::AlertObserverInterface *observer)
static std::string parseFromJsonStatusToString(Alert::ParseFromJsonStatus parseFromJsonStatus)
Asset(const std::string &id, const std::string &url)
Definition: Alert.h:112

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